Files
portal/app/tests/Unit/External/YandexCloudBalanceProviderTest.php
T

42 lines
1.6 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
use App\Services\External\YandexCloudBalanceProvider;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;
uses(TestCase::class); // нужен booted-app: config()/app()/Http::fake()
it('читает баланс YC: OAuth→IAM→billing', function () {
config()->set('services.yandex_cloud.oauth_token', 'oauth-x');
config()->set('services.yandex_cloud.billing_account_id', 'dn-test');
config()->set('services.yandex_cloud.daily_spend_rub', 600);
Http::fake([
'iam.api.cloud.yandex.net/*' => Http::response(['iamToken' => 'iam-x'], 200),
'billing.api.cloud.yandex.net/*' => Http::response(['balance' => '-540.48', 'currency' => 'RUB'], 200),
]);
$r = app(YandexCloudBalanceProvider::class)->fetch();
expect($r->ok)->toBeTrue();
expect($r->balance)->toBe(-540.48);
expect($r->currency)->toBe('RUB');
expect($r->dailySpend)->toBe(600.0);
});
it('нет токена → fail без сетевого вызова', function () {
config()->set('services.yandex_cloud.oauth_token', null);
Http::fake();
$r = app(YandexCloudBalanceProvider::class)->fetch();
expect($r->ok)->toBeFalse();
Http::assertNothingSent();
});
it('IAM не отдал токен → fail', function () {
config()->set('services.yandex_cloud.oauth_token', 'oauth-x');
config()->set('services.yandex_cloud.billing_account_id', 'dn-test');
Http::fake(['iam.api.cloud.yandex.net/*' => Http::response('nope', 401)]);
$r = app(YandexCloudBalanceProvider::class)->fetch();
expect($r->ok)->toBeFalse();
expect($r->error)->toContain('IAM');
});