Implement Gitea user connection foundation

This commit is contained in:
2026-03-26 08:56:52 +01:00
parent ec5b531e5e
commit 5f52d050ee
23 changed files with 1206 additions and 1 deletions
+83
View File
@@ -0,0 +1,83 @@
<?php
namespace Tests\Feature\Git;
use App\Actions\Git\Data\GitAuthenticatedUser;
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');
});
}
}