50 lines
2.0 KiB
PHP
50 lines
2.0 KiB
PHP
<?php
|
|
|
|
use App\Models\MobileApp;
|
|
use Illuminate\Http\Client\Request;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
beforeEach(function () {
|
|
Http::preventStrayRequests();
|
|
});
|
|
|
|
it('builds a state-bound Bazaar authorization URL', function () {
|
|
$app = MobileApp::factory()->create([
|
|
'package_name' => 'ir.example.login',
|
|
'bazaar_login_client_id' => 'client-id',
|
|
'bazaar_login_client_secret' => 'client-secret',
|
|
'bazaar_login_redirect_url' => 'https://api.example.test/callback',
|
|
]);
|
|
|
|
$this->withHeaders(appApiHeaders($app))
|
|
->getJson('/api/v1/apps/ir.example.login/bazaar/login/authorization-url?state=csrf-state')
|
|
->assertSuccessful()
|
|
->assertJsonPath('data.state', 'csrf-state')
|
|
->assertJsonPath('data.authorization_url', fn (string $url): bool => str_contains($url, 'client_id=client-id') && str_contains($url, 'state=csrf-state'));
|
|
});
|
|
|
|
it('exchanges Bazaar authorization codes without logging secrets', function () {
|
|
$app = MobileApp::factory()->create([
|
|
'package_name' => 'ir.example.oauth',
|
|
'bazaar_login_client_id' => 'client-id',
|
|
'bazaar_login_client_secret' => 'client-secret',
|
|
'bazaar_login_redirect_url' => 'https://api.example.test/callback',
|
|
]);
|
|
Http::fake([
|
|
'account.cafebazaar.ir/api/v0/tokens' => Http::response([
|
|
'access_token' => 'access-token',
|
|
'token_type' => 'Bearer',
|
|
'expires_in' => 3600,
|
|
'refresh_token' => 'refresh-token',
|
|
]),
|
|
]);
|
|
|
|
$this->withHeaders(appApiHeaders($app))
|
|
->postJson('/api/v1/apps/ir.example.oauth/bazaar/login/exchange', ['code' => 'authorization-code'])
|
|
->assertSuccessful()
|
|
->assertJsonPath('data.access_token', 'access-token');
|
|
|
|
Http::assertSent(fn (Request $request): bool => $request['client_id'] === 'client-id' && $request['client_secret'] === 'client-secret');
|
|
expect($app->bazaarApiRequests()->firstOrFail()->response_body['access_token'])->toBe('[REDACTED]');
|
|
});
|