255 lines
9.0 KiB
PHP
255 lines
9.0 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Git;
|
|
|
|
use App\Actions\Git\Data\GitAuthenticatedUser;
|
|
use App\Actions\Git\Data\GitBranch;
|
|
use App\Actions\Git\Data\GitPullRequest;
|
|
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 can verify repository read access with minimal payload.
|
|
*/
|
|
public function test_provider_can_verify_repository_read_access(): void
|
|
{
|
|
Http::preventStrayRequests();
|
|
Http::fake([
|
|
'https://git.example.test/api/v1/user/repos*' => Http::response([]),
|
|
]);
|
|
|
|
app(GiteaProviderClient::class)->verifyRepositoryAccess(
|
|
'https://git.example.test',
|
|
'repo-token',
|
|
);
|
|
|
|
Http::assertSent(function ($request): bool {
|
|
return $request->url() === 'https://git.example.test/api/v1/user/repos?limit=1&page=1'
|
|
&& $request->hasHeader('Authorization', 'token repo-token');
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 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',
|
|
'default_branch' => 'main',
|
|
'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->assertSame('main', $repositories[0]->defaultBranch);
|
|
$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 can fetch a single repository with its default branch.
|
|
*/
|
|
public function test_provider_can_fetch_a_single_repository(): void
|
|
{
|
|
Http::preventStrayRequests();
|
|
Http::fake([
|
|
'https://git.example.test/api/v1/repos/core-team/kanbangit' => Http::response([
|
|
'id' => 501,
|
|
'name' => 'kanbangit',
|
|
'full_name' => 'core-team/kanbangit',
|
|
'html_url' => 'https://git.example.test/core-team/kanbangit',
|
|
'default_branch' => 'main',
|
|
'owner' => [
|
|
'login' => 'core-team',
|
|
],
|
|
'private' => true,
|
|
]),
|
|
]);
|
|
|
|
$repository = app(GiteaProviderClient::class)->getRepository(
|
|
'https://git.example.test',
|
|
'repo-token',
|
|
'core-team',
|
|
'kanbangit',
|
|
);
|
|
|
|
$this->assertInstanceOf(GitRepository::class, $repository);
|
|
$this->assertSame('501', $repository->providerRepositoryId);
|
|
$this->assertSame('core-team', $repository->owner);
|
|
$this->assertSame('kanbangit', $repository->name);
|
|
$this->assertSame('main', $repository->defaultBranch);
|
|
$this->assertTrue($repository->isPrivate);
|
|
}
|
|
|
|
/**
|
|
* 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');
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Ensure the provider can create branches from a base reference.
|
|
*/
|
|
public function test_provider_can_create_a_repository_branch(): void
|
|
{
|
|
Http::preventStrayRequests();
|
|
Http::fake([
|
|
'https://git.example.test/api/v1/repos/core-team/kanbangit/branches' => Http::response([
|
|
'name' => 'core-1-login-form',
|
|
], 201),
|
|
]);
|
|
|
|
$branch = app(GiteaProviderClient::class)->createBranch(
|
|
'https://git.example.test',
|
|
'branch-token',
|
|
'core-team',
|
|
'kanbangit',
|
|
'core-1-login-form',
|
|
'main',
|
|
);
|
|
|
|
$this->assertInstanceOf(GitBranch::class, $branch);
|
|
$this->assertSame('core-1-login-form', $branch->name);
|
|
|
|
Http::assertSent(function ($request): bool {
|
|
return $request->url() === 'https://git.example.test/api/v1/repos/core-team/kanbangit/branches'
|
|
&& $request['new_branch_name'] === 'core-1-login-form'
|
|
&& $request['old_ref_name'] === 'main'
|
|
&& $request->hasHeader('Authorization', 'token branch-token');
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Ensure the provider lists pull requests for a specific repository.
|
|
*/
|
|
public function test_provider_can_list_repository_pull_requests(): void
|
|
{
|
|
Http::preventStrayRequests();
|
|
Http::fake([
|
|
'https://git.example.test/api/v1/repos/core-team/kanbangit/pulls*' => Http::response([
|
|
[
|
|
'number' => 42,
|
|
'title' => 'Add login form',
|
|
'state' => 'open',
|
|
'merged' => false,
|
|
'html_url' => 'https://git.example.test/core-team/kanbangit/pulls/42',
|
|
'head' => [
|
|
'ref' => 'CORE-1-login-form',
|
|
],
|
|
],
|
|
]),
|
|
]);
|
|
|
|
$pullRequests = app(GiteaProviderClient::class)->listPullRequests(
|
|
'https://git.example.test',
|
|
'pull-token',
|
|
'core-team',
|
|
'kanbangit',
|
|
);
|
|
|
|
$this->assertCount(1, $pullRequests);
|
|
$this->assertContainsOnlyInstancesOf(GitPullRequest::class, $pullRequests);
|
|
$this->assertSame('42', $pullRequests[0]->number);
|
|
$this->assertSame('Add login form', $pullRequests[0]->title);
|
|
$this->assertSame('open', $pullRequests[0]->status);
|
|
$this->assertSame('CORE-1-login-form', $pullRequests[0]->sourceBranch);
|
|
|
|
Http::assertSent(function ($request): bool {
|
|
return str_starts_with(
|
|
$request->url(),
|
|
'https://git.example.test/api/v1/repos/core-team/kanbangit/pulls',
|
|
) && $request->hasHeader('Authorization', 'token pull-token');
|
|
});
|
|
}
|
|
}
|