app->instance(AlimtalkTemplateService::class, $mock); return $mock; } /** * 지정 template_code 목록을 승인(ACT) 상태로 반환하는 mock 을 등록한다. * * @param array $codes 승인 처리할 template_code 목록 */ private function stubApprovedTemplates(array $codes): void { $templates = array_map( fn (string $code) => ['templateCode' => $code, 'templateName' => $code, 'serviceStatus' => 'ACT'], $codes, ); $this->mockTemplateService() ->shouldReceive('list') ->andReturn(['templates' => $templates, 'pagination' => []]); } /** * 지정 권한 식별자들을 가진 admin 사용자를 만듭니다. * * @param array $permissionIds 부여할 권한 식별자 */ private function adminWith(array $permissionIds): User { $user = User::factory()->create(); $adminRole = Role::firstOrCreate( ['identifier' => 'admin'], ['name' => json_encode(['ko' => '관리자', 'en' => 'Admin']), 'type' => 'admin'] ); $permIds = []; foreach ($permissionIds as $identifier) { $permission = Permission::firstOrCreate( ['identifier' => $identifier], ['name' => json_encode(['ko' => $identifier, 'en' => $identifier]), 'type' => 'admin'] ); $permIds[] = $permission->id; } $testRole = Role::create([ 'identifier' => 'bizppurio_binding_test_'.uniqid(), 'name' => json_encode(['ko' => '테스트', 'en' => 'Test']), 'type' => 'admin', ]); $testRole->permissions()->sync($permIds); $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 authHeaders(User $user): array { return [ 'Authorization' => 'Bearer '.$user->createToken('test')->plainTextToken, 'Accept' => 'application/json', ]; } public function test_인증_없이_목록_조회는_401이다(): void { $this->getJson(self::BASE)->assertStatus(401); } public function test_view_권한으로_연동_맵을_조회한다(): void { BizppurioNotificationBinding::create([ 'notification_type' => 'welcome', 'channel' => 'alimtalk', 'template_code' => 'TW_1236', 'template_name' => '가입환영', 'fallback_sms_enabled' => true, 'is_active' => true, ]); $admin = $this->adminWith(['sirsoft-message_bizppurio.messaging.view']); $response = $this->withHeaders($this->authHeaders($admin))->getJson(self::BASE); $response->assertStatus(200); $response->assertJsonPath('data.bindings.welcome.template_code', 'TW_1236'); $response->assertJsonPath('data.bindings.welcome.fallback_sms_enabled', true); } public function test_연동이_없으면_빈_맵을_반환한다(): void { $admin = $this->adminWith(['sirsoft-message_bizppurio.messaging.view']); $response = $this->withHeaders($this->authHeaders($admin))->getJson(self::BASE); $response->assertStatus(200); $response->assertJsonPath('data.bindings', []); } public function test_권한_없는_사용자는_승인템플릿_조회_403이다(): void { $admin = $this->adminWith([]); // messaging.view 없음 $response = $this->withHeaders($this->authHeaders($admin)) ->getJson(self::BASE.'/approved-templates'); $response->assertStatus(403); } public function test_view_권한만으로는_저장할_수_없다(): void { $admin = $this->adminWith(['sirsoft-message_bizppurio.messaging.view']); $response = $this->withHeaders($this->authHeaders($admin))->postJson(self::BASE, [ 'notification_type' => 'welcome', 'template_code' => 'TW_1236', 'template_name' => '가입환영', ]); $response->assertStatus(403); } public function test_manage_권한으로_연동을_즉시_저장한다(): void { $this->stubApprovedTemplates(['TW_1236']); $admin = $this->adminWith([ 'sirsoft-message_bizppurio.messaging.view', 'sirsoft-message_bizppurio.messaging.manage', ]); $response = $this->withHeaders($this->authHeaders($admin))->postJson(self::BASE, [ 'notification_type' => 'welcome', 'template_code' => 'TW_1236', 'template_name' => '가입환영', 'fallback_sms_enabled' => true, ]); $response->assertStatus(200); $this->assertDatabaseHas('bizppurio_notification_bindings', [ 'notification_type' => 'welcome', 'channel' => 'alimtalk', 'template_code' => 'TW_1236', 'fallback_sms_enabled' => true, ]); } public function test_미승인_템플릿_코드로_저장하면_422이고_저장되지_않는다(): void { // 회귀: 화면 드롭다운은 승인 템플릿만 보여주지만, 그 필터를 우회해 API 를 직접 // 호출하면 미승인 template_code 도 저장되던 결함. bind() 서버측 재검증으로 차단. $this->stubApprovedTemplates(['TW_OTHER']); $admin = $this->adminWith([ 'sirsoft-message_bizppurio.messaging.view', 'sirsoft-message_bizppurio.messaging.manage', ]); $response = $this->withHeaders($this->authHeaders($admin))->postJson(self::BASE, [ 'notification_type' => 'welcome', 'template_code' => 'TW_UNAPPROVED', 'template_name' => '미승인', ]); $response->assertStatus(422); $this->assertDatabaseMissing('bizppurio_notification_bindings', [ 'notification_type' => 'welcome', 'template_code' => 'TW_UNAPPROVED', ]); } public function test_빈_코드로_저장하면_연동을_해제한다(): void { BizppurioNotificationBinding::create([ 'notification_type' => 'welcome', 'channel' => 'alimtalk', 'template_code' => 'TW_1236', 'template_name' => '가입환영', 'fallback_sms_enabled' => false, 'is_active' => true, ]); $admin = $this->adminWith([ 'sirsoft-message_bizppurio.messaging.view', 'sirsoft-message_bizppurio.messaging.manage', ]); // 해제는 카카오 승인 조회를 거치지 않으므로 mock 불필요. $response = $this->withHeaders($this->authHeaders($admin))->postJson(self::BASE, [ 'notification_type' => 'welcome', 'template_code' => '', ]); $response->assertStatus(200); $this->assertDatabaseMissing('bizppurio_notification_bindings', [ 'notification_type' => 'welcome', 'channel' => 'alimtalk', ]); } public function test_notification_type_누락_시_422다(): void { $admin = $this->adminWith([ 'sirsoft-message_bizppurio.messaging.view', 'sirsoft-message_bizppurio.messaging.manage', ]); // FormRequest 검증(notification_type required)이 서비스 호출보다 먼저 실패하므로 mock 불필요. $response = $this->withHeaders($this->authHeaders($admin))->postJson(self::BASE, [ 'template_code' => 'TW_1236', ]); $response->assertStatus(422); } }