Files
KanbanGit/tests/Feature/Git/GiteaProviderTest.php
T

121 lines
4.1 KiB
PHP

<?php
namespace Tests\Feature\Git;
use App\Actions\Git\Data\GitAuthenticatedUser;
use App\Actions\Git\Data\GitBranch;
use App\Actions\Git\Data\GitRepository;
use App\Actions\Git\Gitea\GiteaProviderClient;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;
class GiteaProviderTest extends TestCase
{
use RefreshDatabase;
/**
* Ensure the provider authenticates against the Gitea user endpoint.
*/
public function test_provider_can_fetch_the_authenticated_user(): void
{
Http::preventStrayRequests();
Http::fake([
'https://git.example.test/api/v1/user' => Http::response([
'id' => 101,
'login' => 'nico',
'full_name' => 'Nico Example',
]),
]);
$authenticatedUser = app(GiteaProviderClient::class)->getAuthenticatedUser(
'https://git.example.test',
'super-secret-token',
);
$this->assertInstanceOf(GitAuthenticatedUser::class, $authenticatedUser);
$this->assertSame('101', $authenticatedUser->providerUserId);
$this->assertSame('nico', $authenticatedUser->login);
$this->assertSame('Nico Example', $authenticatedUser->name);
Http::assertSent(function ($request): bool {
return $request->url() === 'https://git.example.test/api/v1/user'
&& $request->hasHeader('Authorization', 'token super-secret-token');
});
}
/**
* Ensure the provider lists repositories from the authenticated Gitea account.
*/
public function test_provider_can_list_repositories(): void
{
Http::preventStrayRequests();
Http::fake([
'https://git.example.test/api/v1/user/repos*' => Http::response([
[
'id' => 501,
'name' => 'kanbangit',
'full_name' => 'nico/kanbangit',
'html_url' => 'https://git.example.test/nico/kanbangit',
'owner' => [
'login' => 'nico',
],
'private' => false,
],
]),
]);
$repositories = app(GiteaProviderClient::class)->listRepositories(
'https://git.example.test/api/v1',
'repo-token',
);
$this->assertCount(1, $repositories);
$this->assertContainsOnlyInstancesOf(GitRepository::class, $repositories);
$this->assertSame('501', $repositories[0]->providerRepositoryId);
$this->assertSame('nico/kanbangit', $repositories[0]->fullName);
$this->assertFalse($repositories[0]->isPrivate);
Http::assertSent(function ($request): bool {
return str_starts_with($request->url(), 'https://git.example.test/api/v1/user/repos')
&& $request->hasHeader('Authorization', 'token repo-token');
});
}
/**
* Ensure the provider lists branches for a specific repository.
*/
public function test_provider_can_list_repository_branches(): void
{
Http::preventStrayRequests();
Http::fake([
'https://git.example.test/api/v1/repos/core-team/kanbangit/branches*' => Http::response([
[
'name' => 'CORE-1-login-form',
],
[
'name' => 'main',
],
]),
]);
$branches = app(GiteaProviderClient::class)->listBranches(
'https://git.example.test',
'branch-token',
'core-team',
'kanbangit',
);
$this->assertCount(2, $branches);
$this->assertContainsOnlyInstancesOf(GitBranch::class, $branches);
$this->assertSame('CORE-1-login-form', $branches[0]->name);
Http::assertSent(function ($request): bool {
return str_starts_with(
$request->url(),
'https://git.example.test/api/v1/repos/core-team/kanbangit/branches',
) && $request->hasHeader('Authorization', 'token branch-token');
});
}
}