Files
app-agent/tests/Feature/MobileAppApiTest.php
T
2026-08-24 19:44:24 +03:30

83 lines
3.2 KiB
PHP

<?php
use App\Models\BazaarApiRequest;
use App\Models\BazaarPurchase;
use App\Models\BazaarRelease;
use App\Models\BazaarReview;
use App\Models\BazaarSubscription;
use App\Models\MobileApp;
use App\Models\User;
it('returns only public configuration for the selected app', function () {
$app = MobileApp::factory()->create([
'package_name' => 'ir.example.coffee',
'configuration' => ['api_base' => 'https://example.test'],
'feature_flags' => ['new_home' => true],
'bazaar_login_client_secret' => 'must-not-leak',
]);
$this->withHeaders(appApiHeaders($app))
->getJson('/api/v1/apps/ir.example.coffee')
->assertSuccessful()
->assertJsonPath('data.package_name', 'ir.example.coffee')
->assertJsonPath('data.configuration.api_base', 'https://example.test')
->assertJsonPath('data.feature_flags.new_home', true)
->assertJsonMissing(['bazaar_login_client_secret' => 'must-not-leak']);
});
it('rejects invalid app keys and disabled apps', function () {
$app = MobileApp::factory()->create(['package_name' => 'ir.example.disabled', 'is_active' => false]);
$this->withHeaders(['X-App-Key' => $app->client_key])
->getJson('/api/v1/apps/ir.example.disabled')
->assertForbidden()
->assertJsonPath('error.code', 'app_disabled');
$activeApp = MobileApp::factory()->create(['package_name' => 'ir.example.active']);
$this->withHeaders(['X-App-Key' => 'wrong'])
->getJson('/api/v1/apps/ir.example.active')
->assertUnauthorized()
->assertJsonPath('error.code', 'invalid_app_key');
});
it('allows panel users to access only attached app tenants', function () {
$user = User::factory()->create();
$allowedApp = MobileApp::factory()->create();
$otherApp = MobileApp::factory()->create();
$user->mobileApps()->attach($allowedApp, ['role' => 'owner']);
expect($user->canAccessTenant($allowedApp))->toBeTrue()
->and($user->canAccessTenant($otherApp))->toBeFalse();
});
it('renders the tenant dashboard for an attached panel user', function () {
$user = User::factory()->create();
$app = MobileApp::factory()->create(['package_name' => 'ir.example.panel']);
$user->mobileApps()->attach($app, ['role' => 'owner']);
$this->actingAs($user)
->get('/admin/apps/ir.example.panel')
->assertSuccessful();
$this->get('/admin/apps/ir.example.panel/profile')
->assertSuccessful();
});
it('renders every tenant-scoped Bazaar monitoring resource', function () {
$user = User::factory()->create();
$app = MobileApp::factory()->create(['package_name' => 'ir.example.monitored']);
$user->mobileApps()->attach($app, ['role' => 'owner']);
BazaarPurchase::factory()->for($app)->create();
BazaarSubscription::factory()->for($app)->create();
BazaarRelease::factory()->for($app)->create();
BazaarReview::factory()->for($app)->create();
BazaarApiRequest::factory()->for($app)->create();
$this->actingAs($user);
foreach (['bazaar-purchases', 'bazaar-subscriptions', 'bazaar-releases', 'bazaar-reviews', 'bazaar-api-requests'] as $resource) {
$this->get("/admin/apps/ir.example.monitored/{$resource}")->assertSuccessful();
}
});