Files
Gnuboard7/plugins/_bundled/sirsoft-message_bizppurio/tests/Unit/Services/BizppurioTokenServiceTest.php
T
HeuJung 6a8a537f10 feat(message_bizppurio): 임시 삭제된 비즈뿌리오 플러그인 복구 및 현행 규정 정합화
32afe9530 에서 게시판 반응 기능과 함께 임시 제거됐던 비즈뿌리오 메시징
플러그인 본체(137 파일)와 일본어 번들 언어팩(9 파일)을 삭제 직전 상태
(32afe9530~1)로 복구한다. 템플릿 신청·승인 개편의 기반 작업.

복구 범위
- plugins/_bundled/sirsoft-message_bizppurio 전체 + 번들 ja 언어팩
- 공유 파일의 비즈뿌리오 참조 복원: build-language-pack-ja.cjs 팩 정의,
 api-doc-unfilled-baseline.json(32건), docs/backend/api/README.md 표 행,
 audit coverage 노트·vite-sourcemap-env-gate 룰 주석, via 테스트 주석,
 ·AGENTS.md 확장 API 표(자동 재생성), 라우팅 패리티 스냅샷(+1)

삭제 이후 강화된 규정 2건 정합화
- TokenCheckController: 예외 원문을 메시지 키 자리에 전달하던 422 응답을
 키(token_check.failed) + errors.bizppurio_message 페이로드로 분리
 (GenericCatchStatusCodeContractTest 계약). 관리자 토스트는 errors 페이로드로
 상세 사유를 계속 표시하도록 레이아웃 동기 수정, lang ko/en/ja 키 추가
- AlimtalkTemplateController::index: base Request 주입 금지 룰에 따라
 AlimtalkTemplateListRequest FormRequest 신설 (형태 검증만 — kapi 위임 유지)

미복원(의도)
- 게시판·이커머스 CHANGELOG 의 알림톡 연결 문구 2줄은 연결 방식이 로
 재설계되므로 되살리지 않고, 완료 시점에 새 동작 기준으로 차기 버전에 기재

검증: TokenCheck 7 + AlimtalkController 10 + GenericCatch 계약 3 (PHPUnit),
플러그인 레이아웃 Vitest 137건, BindingShape 라우팅 패리티 8건 green.
audit 는 복구 전부터 baseline 처리된 API 문서 미채움 32건만 잔존.
2026-08-22 22:39:36 +09:00

254 lines
8.6 KiB
PHP

<?php
namespace Plugins\Sirsoft\MessageBizppurio\Tests\Unit\Services;
use App\Contracts\Extension\CacheInterface;
use App\Services\PluginSettingsService;
use Illuminate\Support\Facades\Http;
use Mockery;
use Plugins\Sirsoft\MessageBizppurio\Exceptions\BizppurioApiException;
use Plugins\Sirsoft\MessageBizppurio\Services\BizppurioTokenService;
use Plugins\Sirsoft\MessageBizppurio\Tests\PluginTestCase;
/**
* BizppurioTokenService — 토큰 발급·캐시 재사용·재발급 검증.
*/
class BizppurioTokenServiceTest extends PluginTestCase
{
private const IDENTIFIER = 'sirsoft-message_bizppurio';
/**
* 메모리 배열로 remember/forget 을 재현하는 CacheInterface mock.
*
* @param array<string, mixed> $store 참조로 전달되는 캐시 저장소
*/
private function makeCache(array &$store): CacheInterface
{
$mock = Mockery::mock(CacheInterface::class);
$mock->shouldReceive('remember')->andReturnUsing(
function (string $key, callable $callback) use (&$store) {
if (! array_key_exists($key, $store)) {
$store[$key] = $callback();
}
return $store[$key];
}
);
$mock->shouldReceive('forget')->andReturnUsing(
function (string $key) use (&$store) {
unset($store[$key]);
return true;
}
);
$mock->shouldReceive('put')->andReturnUsing(
function (string $key, $value) use (&$store) {
$store[$key] = $value;
return true;
}
);
return $mock;
}
/**
* bizppurio_id/password/is_test_mode 를 반환하는 설정 mock.
*
* @param array<string, mixed> $settings
*/
private function makeSettings(array $settings): PluginSettingsService
{
$mock = Mockery::mock(PluginSettingsService::class);
$mock->shouldReceive('get')->with(self::IDENTIFIER)->andReturn($settings);
return $mock;
}
public function test_토큰_발급_후_캐시에서_재사용된다(): void
{
Http::fake([
'*/v1/token' => Http::response(['accesstoken' => 'TOKEN_A', 'type' => 'Bearer'], 200),
]);
$store = [];
$service = new BizppurioTokenService(
$this->makeCache($store),
$this->makeSettings(['bizppurio_id' => 'acct', 'password' => 'pw', 'is_test_mode' => true]),
);
$this->assertSame('TOKEN_A', $service->getToken());
// 두 번째 호출은 캐시에서 → HTTP 1회만
$this->assertSame('TOKEN_A', $service->getToken());
Http::assertSentCount(1);
}
public function test_refresh는_forget_후_재발급한다(): void
{
$tokens = ['TOKEN_A', 'TOKEN_B'];
Http::fake([
'*/v1/token' => Http::sequence()
->push(['accesstoken' => 'TOKEN_A', 'type' => 'Bearer'], 200)
->push(['accesstoken' => 'TOKEN_B', 'type' => 'Bearer'], 200),
]);
$store = [];
$service = new BizppurioTokenService(
$this->makeCache($store),
$this->makeSettings(['bizppurio_id' => 'acct', 'password' => 'pw', 'is_test_mode' => true]),
);
$this->assertSame('TOKEN_A', $service->getToken());
// refresh → 캐시 무효화 후 새 토큰
$this->assertSame('TOKEN_B', $service->refreshToken());
Http::assertSentCount(2);
}
public function test_검수_모드는_dev_도메인을_호출한다(): void
{
Http::fake([
'dev-api.bizppurio.com/*' => Http::response(['accesstoken' => 'T', 'type' => 'Bearer'], 200),
]);
$store = [];
$service = new BizppurioTokenService(
$this->makeCache($store),
$this->makeSettings(['bizppurio_id' => 'acct', 'password' => 'pw', 'is_test_mode' => true]),
);
$service->getToken();
Http::assertSent(fn ($request) => str_contains($request->url(), 'dev-api.bizppurio.com'));
}
public function test_운영_모드는_live_도메인을_호출한다(): void
{
Http::fake([
'api.bizppurio.com/*' => Http::response(['accesstoken' => 'T', 'type' => 'Bearer'], 200),
]);
$store = [];
$service = new BizppurioTokenService(
$this->makeCache($store),
$this->makeSettings(['bizppurio_id' => 'acct', 'password' => 'pw', 'is_test_mode' => false]),
);
$service->getToken();
Http::assertSent(fn ($request) => str_contains($request->url(), 'api.bizppurio.com')
&& ! str_contains($request->url(), 'dev-api.bizppurio.com'));
}
public function test_자격증명_미설정시_예외(): void
{
$store = [];
$service = new BizppurioTokenService(
$this->makeCache($store),
$this->makeSettings(['bizppurio_id' => '', 'password' => '']),
);
$this->expectException(BizppurioApiException::class);
$service->getToken();
}
public function test_http_실패시_예외(): void
{
Http::fake(['*/v1/token' => Http::response([], 500)]);
$store = [];
$service = new BizppurioTokenService(
$this->makeCache($store),
$this->makeSettings(['bizppurio_id' => 'acct', 'password' => 'pw', 'is_test_mode' => true]),
);
$this->expectException(BizppurioApiException::class);
$service->getToken();
}
public function test_verifyCredentials는_캐시를_거치지_않고_재발급한다(): void
{
Http::fake([
'*/v1/token' => Http::response(['accesstoken' => 'FRESH_TOKEN', 'type' => 'Bearer'], 200),
]);
$store = ['bizppurio:token' => 'STALE_TOKEN'];
$service = new BizppurioTokenService(
$this->makeCache($store),
$this->makeSettings(['bizppurio_id' => 'acct', 'password' => 'pw', 'is_test_mode' => true]),
);
$this->assertSame('FRESH_TOKEN', $service->verifyCredentials());
Http::assertSentCount(1);
$this->assertSame('FRESH_TOKEN', $store['bizppurio:token']);
}
public function test_verifyCredentials_실패시_예외를_전파하고_캐시를_건드리지_않는다(): void
{
Http::fake(['*/v1/token' => Http::response([], 500)]);
$store = ['bizppurio:token' => 'EXISTING_TOKEN'];
$service = new BizppurioTokenService(
$this->makeCache($store),
$this->makeSettings(['bizppurio_id' => 'acct', 'password' => 'pw', 'is_test_mode' => true]),
);
try {
$service->verifyCredentials();
$this->fail('예외가 발생해야 합니다.');
} catch (BizppurioApiException $e) {
// 실패 시 캐시는 그대로 보존 — 발송이 기존 토큰(만료 전까지)을 계속 사용
$this->assertSame('EXISTING_TOKEN', $store['bizppurio:token']);
}
}
public function test_토큰_발급_실패시_비즈뿌리오_사유가_메시지에_포함된다(): void
{
Http::fake([
'*/v1/token' => Http::response(['code' => '3007', 'description' => 'invalid password'], 401),
]);
$store = [];
$service = new BizppurioTokenService(
$this->makeCache($store),
$this->makeSettings(['bizppurio_id' => 'acct', 'password' => 'pw', 'is_test_mode' => true]),
);
try {
$service->getToken();
$this->fail('예외가 발생해야 합니다.');
} catch (BizppurioApiException $e) {
$this->assertStringContainsString('invalid password', $e->getMessage());
$this->assertSame('3007', $e->getResultCode());
}
}
public function test_토큰_발급_실패시_사유_없으면_고정_메시지로_폴백한다(): void
{
Http::fake(['*/v1/token' => Http::response([], 500)]);
$store = [];
$service = new BizppurioTokenService(
$this->makeCache($store),
$this->makeSettings(['bizppurio_id' => 'acct', 'password' => 'pw', 'is_test_mode' => true]),
);
try {
$service->getToken();
$this->fail('예외가 발생해야 합니다.');
} catch (BizppurioApiException $e) {
$this->assertSame(
__('sirsoft-message_bizppurio::messages.error.token_issue_failed'),
$e->getMessage()
);
$this->assertNull($e->getResultCode());
}
}
protected function tearDown(): void
{
Mockery::close();
parent::tearDown();
}
}