app->instance(AlimtalkTemplateService::class, $mock); return $mock; } /** * 지정 권한을 가진 admin 사용자를 생성한다. * * @param array $permissionIdentifiers */ private function adminWith(array $permissionIdentifiers): User { $user = User::factory()->create(); $adminRole = Role::firstOrCreate( ['identifier' => 'admin'], ['name' => json_encode(['ko' => '관리자', 'en' => 'Admin']), 'type' => 'admin'] ); $testRole = Role::create([ 'identifier' => 'bizppurio_tpl_test_'.uniqid(), 'name' => json_encode(['ko' => '테스트', 'en' => 'Test']), 'type' => 'admin', ]); $permissionIds = []; foreach ($permissionIdentifiers as $identifier) { $permissionIds[] = Permission::firstOrCreate( ['identifier' => $identifier], ['name' => json_encode(['ko' => $identifier, 'en' => $identifier]), 'type' => 'admin'] )->id; } $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(); } /** * @param array $permissions * @return array */ private function authHeaders(array $permissions): array { $user = $this->adminWith($permissions); $token = $user->createToken('test-token')->plainTextToken; return [ 'Authorization' => 'Bearer '.$token, 'Accept' => 'application/json', ]; } /** * view 권한으로 카테고리 전체를 조회한다 (data.categories 봉투). */ public function test_view권한으로_카테고리를_조회한다(): void { $this->mockService()->shouldReceive('categories')->once()->andReturn([ ['code' => '001001', 'name' => '회원가입', 'groupName' => '회원'], ]); $response = $this->withHeaders($this->authHeaders(['sirsoft-message_bizppurio.messaging.view'])) ->getJson(self::BASE.'/categories'); $response->assertStatus(200); $response->assertJsonPath('data.categories.0.code', '001001'); $response->assertJsonPath('data.categories.0.groupName', '회원'); } /** * view 권한으로 발신프로필 목록을 조회한다 (data.profiles 봉투). */ public function test_view권한으로_발신프로필을_조회한다(): void { $this->mockService()->shouldReceive('senderProfiles')->once()->andReturn([ ['senderKey' => 'SK_40', 'name' => '테스트채널', 'status' => 'A'], ]); $response = $this->withHeaders($this->authHeaders(['sirsoft-message_bizppurio.messaging.view'])) ->getJson(self::BASE.'/profiles'); $response->assertStatus(200); $response->assertJsonPath('data.profiles.0.senderKey', 'SK_40'); $response->assertJsonPath('data.profiles.0.name', '테스트채널'); } /** * 비인증 요청은 401 이다. */ public function test_비인증은_401(): void { $this->getJson(self::BASE.'/categories')->assertStatus(401); $this->getJson(self::BASE.'/profiles')->assertStatus(401); } /** * view 권한이 없으면 두 라우트 모두 403 이다. */ public function test_view권한_없으면_403(): void { $headers = $this->authHeaders(['sirsoft-message_bizppurio.messaging.other']); $this->withHeaders($headers)->getJson(self::BASE.'/categories')->assertStatus(403); $this->withHeaders($headers)->getJson(self::BASE.'/profiles')->assertStatus(403); } /** * kapi 실패는 카카오 사유(message)·결과코드가 담긴 422 로 전파된다 (GuardsKakaoRequests 규약). */ public function test_kapi_실패는_422로_전파된다(): void { $this->mockService()->shouldReceive('categories')->once() ->andThrow(new BizppurioApiException('접근할 수 없는 IP 입니다.', resultCode: '403')); $response = $this->withHeaders($this->authHeaders(['sirsoft-message_bizppurio.messaging.view'])) ->getJson(self::BASE.'/categories'); $response->assertStatus(422); $response->assertJsonPath('errors.bizppurio_message', '접근할 수 없는 IP 입니다.'); $response->assertJsonPath('errors.result_code', '403'); } /** * 발신프로필 조회의 kapi 실패도 동일한 422 규약으로 전파된다. */ public function test_발신프로필_kapi_실패도_422로_전파된다(): void { $this->mockService()->shouldReceive('senderProfiles')->once() ->andThrow(new BizppurioApiException('발신프로필을 찾을 수 없습니다.', resultCode: '7204')); $response = $this->withHeaders($this->authHeaders(['sirsoft-message_bizppurio.messaging.view'])) ->getJson(self::BASE.'/profiles'); $response->assertStatus(422); $response->assertJsonPath('errors.bizppurio_message', '발신프로필을 찾을 수 없습니다.'); $response->assertJsonPath('errors.result_code', '7204'); } protected function tearDown(): void { Mockery::close(); parent::tearDown(); } }