142 lines
4.9 KiB
PHP
142 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Api\Admin;
|
|
|
|
use App\Enums\ExtensionOwnerType;
|
|
use App\Enums\PermissionType;
|
|
use App\Models\Permission;
|
|
use App\Models\Role;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
/**
|
|
* GET /api/admin/modules/{moduleName}/install-preview Feature 테스트.
|
|
*
|
|
* 인스톨 모달 cascade 프리뷰 응답의 권한/응답 shape 골든 패스를 검증합니다.
|
|
*/
|
|
class ModuleInstallPreviewTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private User $admin;
|
|
|
|
private string $token;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$this->admin = $this->createAdminUser();
|
|
$this->token = $this->admin->createToken('test-token')->plainTextToken;
|
|
}
|
|
|
|
private function createAdminUser(array $permissions = ['core.modules.read', 'core.modules.install']): User
|
|
{
|
|
$user = User::factory()->create();
|
|
|
|
$permissionIds = [];
|
|
foreach ($permissions as $permIdentifier) {
|
|
$permission = Permission::firstOrCreate(
|
|
['identifier' => $permIdentifier],
|
|
[
|
|
'name' => json_encode(['ko' => $permIdentifier, 'en' => $permIdentifier]),
|
|
'description' => json_encode(['ko' => $permIdentifier, 'en' => $permIdentifier]),
|
|
'extension_type' => ExtensionOwnerType::Core,
|
|
'extension_identifier' => 'core',
|
|
'type' => PermissionType::Admin,
|
|
]
|
|
);
|
|
$permissionIds[] = $permission->id;
|
|
}
|
|
|
|
$testRole = Role::create([
|
|
'identifier' => 'admin_test_'.uniqid(),
|
|
'name' => json_encode(['ko' => '테스트', 'en' => 'Test']),
|
|
'description' => json_encode(['ko' => '', 'en' => '']),
|
|
'is_active' => true,
|
|
]);
|
|
|
|
$adminRole = Role::firstOrCreate(
|
|
['identifier' => 'admin'],
|
|
[
|
|
'name' => json_encode(['ko' => '관리자', 'en' => 'Administrator']),
|
|
'description' => json_encode(['ko' => '관리자', 'en' => 'Admin']),
|
|
'extension_type' => ExtensionOwnerType::Core,
|
|
'extension_identifier' => 'core',
|
|
'is_active' => true,
|
|
]
|
|
);
|
|
|
|
$testRole->permissions()->sync($permissionIds);
|
|
$user->roles()->attach($adminRole->id, ['assigned_at' => now(), 'assigned_by' => null]);
|
|
$user->roles()->attach($testRole->id, ['assigned_at' => now(), 'assigned_by' => null]);
|
|
|
|
return $user->fresh();
|
|
}
|
|
|
|
private function authRequest(): static
|
|
{
|
|
return $this->withHeaders([
|
|
'Authorization' => 'Bearer '.$this->token,
|
|
'Accept' => 'application/json',
|
|
]);
|
|
}
|
|
|
|
public function test_returns_401_without_authentication(): void
|
|
{
|
|
$response = $this->getJson('/api/admin/modules/sirsoft-board/install-preview');
|
|
|
|
$response->assertStatus(401);
|
|
}
|
|
|
|
public function test_returns_403_without_install_permission(): void
|
|
{
|
|
// 새 사용자 — install 권한 없음
|
|
$user = $this->createAdminUser(['core.modules.read']);
|
|
$token = $user->createToken('no-install')->plainTextToken;
|
|
|
|
$response = $this->withHeaders([
|
|
'Authorization' => 'Bearer '.$token,
|
|
'Accept' => 'application/json',
|
|
])->getJson('/api/admin/modules/sirsoft-board/install-preview');
|
|
|
|
// permission 미들웨어가 401/403 — 정확한 코드는 환경별 다를 수 있어 4xx 검증
|
|
$this->assertGreaterThanOrEqual(401, $response->status());
|
|
$this->assertLessThan(500, $response->status());
|
|
}
|
|
|
|
public function test_returns_404_for_unknown_module(): void
|
|
{
|
|
$response = $this->authRequest()->getJson(
|
|
'/api/admin/modules/__nonexistent_module__/install-preview'
|
|
);
|
|
|
|
// ExtensionInstallPreviewBuilder 가 not_found 예외 → 컨트롤러 catch 후 500 (현 시점)
|
|
// 추후 별도 404 매핑 추가 시 본 테스트 갱신
|
|
$this->assertContains($response->status(), [404, 500]);
|
|
}
|
|
|
|
public function test_response_shape_for_known_module(): void
|
|
{
|
|
// 번들 sirsoft-board 모듈에 대한 프리뷰 호출
|
|
$response = $this->authRequest()->getJson('/api/admin/modules/sirsoft-board/install-preview');
|
|
|
|
if ($response->status() !== 200) {
|
|
// sirsoft-board 가 환경에 없을 수 있음 — skip
|
|
$this->markTestSkipped('sirsoft-board module not available in test environment');
|
|
}
|
|
|
|
$response->assertJsonStructure([
|
|
'data' => [
|
|
'target' => ['identifier', 'name', 'version'],
|
|
'dependencies',
|
|
'language_packs',
|
|
],
|
|
]);
|
|
|
|
$data = $response->json('data');
|
|
$this->assertIsArray($data['dependencies']);
|
|
$this->assertIsArray($data['language_packs']);
|
|
}
|
|
}
|