Implement Gitea user connection foundation
This commit is contained in:
@@ -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');
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Models;
|
||||
|
||||
use App\Actions\Git\GitProviderKey;
|
||||
use App\Models\UserGitConnection;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Tests\TestCase;
|
||||
|
||||
class UserGitConnectionTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* Ensure the user git connection factory creates a valid encrypted Gitea connection.
|
||||
*/
|
||||
public function test_factory_creates_a_gitea_connection_with_an_encrypted_token(): void
|
||||
{
|
||||
$gitConnection = UserGitConnection::factory()->create([
|
||||
'api_token' => 'plain-test-token',
|
||||
]);
|
||||
|
||||
$storedToken = DB::table('user_git_connections')
|
||||
->where('id', $gitConnection->id)
|
||||
->value('api_token');
|
||||
|
||||
$this->assertNotSame('plain-test-token', $storedToken);
|
||||
$this->assertSame('plain-test-token', $gitConnection->fresh()->api_token);
|
||||
$this->assertSame(GitProviderKey::Gitea, $gitConnection->fresh()->provider);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the model belongs to a user for per-account credential scoping.
|
||||
*/
|
||||
public function test_git_connections_belong_to_a_user(): void
|
||||
{
|
||||
$gitConnection = UserGitConnection::factory()->create();
|
||||
|
||||
$this->assertTrue($gitConnection->user()->exists());
|
||||
$this->assertTrue($gitConnection->user->is($gitConnection->fresh()->user));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Settings;
|
||||
|
||||
use App\Actions\Git\GitProviderKey;
|
||||
use App\Models\User;
|
||||
use App\Models\UserGitConnection;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Inertia\Testing\AssertableInertia as Assert;
|
||||
use Tests\TestCase;
|
||||
|
||||
class GitConnectionTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* Ensure the Git connections settings page is displayed to authenticated users.
|
||||
*/
|
||||
public function test_git_connections_page_is_displayed(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
UserGitConnection::factory()->for($user)->create([
|
||||
'account_login' => 'nico',
|
||||
'base_url' => 'https://git.example.test',
|
||||
]);
|
||||
|
||||
$this->actingAs($user)
|
||||
->get(route('git-connections.edit'))
|
||||
->assertOk()
|
||||
->assertInertia(fn (Assert $page) => $page
|
||||
->component('settings/GitConnections')
|
||||
->where('gitConnections.0.account_login', 'nico')
|
||||
->where('gitConnections.0.base_url', 'https://git.example.test')
|
||||
->where('gitConnections.0.provider', GitProviderKey::Gitea->value),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure a user can add a verified Gitea connection from settings.
|
||||
*/
|
||||
public function test_users_can_store_verified_gitea_connections(): void
|
||||
{
|
||||
Http::preventStrayRequests();
|
||||
Http::fake([
|
||||
'https://git.example.test/api/v1/user' => Http::response([
|
||||
'id' => 101,
|
||||
'login' => 'nico',
|
||||
'full_name' => 'Nico Example',
|
||||
]),
|
||||
]);
|
||||
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->actingAs($user)
|
||||
->post(route('git-connections.store'), [
|
||||
'provider' => 'gitea',
|
||||
'base_url' => 'https://git.example.test/api/v1',
|
||||
'api_token' => 'super-secret-token',
|
||||
])
|
||||
->assertRedirect(route('git-connections.edit'))
|
||||
->assertSessionHas('status', 'Git connection was added.');
|
||||
|
||||
$this->assertDatabaseHas('user_git_connections', [
|
||||
'user_id' => $user->id,
|
||||
'provider' => GitProviderKey::Gitea->value,
|
||||
'base_url' => 'https://git.example.test',
|
||||
'provider_user_id' => '101',
|
||||
'account_login' => 'nico',
|
||||
'account_name' => 'Nico Example',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure invalid provider credentials are rejected with a validation error.
|
||||
*/
|
||||
public function test_invalid_provider_credentials_are_rejected(): void
|
||||
{
|
||||
Http::preventStrayRequests();
|
||||
Http::fake([
|
||||
'https://git.example.test/api/v1/user' => Http::response([
|
||||
'message' => 'Unauthorized',
|
||||
], 401),
|
||||
]);
|
||||
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->actingAs($user)
|
||||
->from(route('git-connections.edit'))
|
||||
->post(route('git-connections.store'), [
|
||||
'provider' => 'gitea',
|
||||
'base_url' => 'https://git.example.test',
|
||||
'api_token' => 'wrong-token',
|
||||
])
|
||||
->assertRedirect(route('git-connections.edit'))
|
||||
->assertSessionHasErrors('api_token');
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure a user can update only their own Git connection.
|
||||
*/
|
||||
public function test_users_can_update_their_own_git_connections(): void
|
||||
{
|
||||
Http::preventStrayRequests();
|
||||
Http::fake([
|
||||
'https://git.internal.test/api/v1/user' => Http::response([
|
||||
'id' => 202,
|
||||
'login' => 'updated-user',
|
||||
'full_name' => 'Updated User',
|
||||
]),
|
||||
]);
|
||||
|
||||
$user = User::factory()->create();
|
||||
$gitConnection = UserGitConnection::factory()->for($user)->create([
|
||||
'account_login' => 'old-user',
|
||||
'account_name' => 'Old User',
|
||||
'base_url' => 'https://git.example.test',
|
||||
'provider' => GitProviderKey::Gitea,
|
||||
'provider_user_id' => '100',
|
||||
]);
|
||||
|
||||
$this->actingAs($user)
|
||||
->patch(route('git-connections.update', $gitConnection), [
|
||||
'provider' => 'gitea',
|
||||
'base_url' => 'https://git.internal.test',
|
||||
'api_token' => 'fresh-token',
|
||||
])
|
||||
->assertRedirect(route('git-connections.edit'))
|
||||
->assertSessionHas('status', 'Git connection was updated.');
|
||||
|
||||
$this->assertDatabaseHas('user_git_connections', [
|
||||
'id' => $gitConnection->id,
|
||||
'base_url' => 'https://git.internal.test',
|
||||
'provider_user_id' => '202',
|
||||
'account_login' => 'updated-user',
|
||||
'account_name' => 'Updated User',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure users cannot update another user's Git connection.
|
||||
*/
|
||||
public function test_users_cannot_update_foreign_git_connections(): void
|
||||
{
|
||||
Http::preventStrayRequests();
|
||||
Http::fake();
|
||||
|
||||
$owner = User::factory()->create();
|
||||
$otherUser = User::factory()->create();
|
||||
$gitConnection = UserGitConnection::factory()->for($owner)->create();
|
||||
|
||||
$this->actingAs($otherUser)
|
||||
->patch(route('git-connections.update', $gitConnection), [
|
||||
'provider' => 'gitea',
|
||||
'base_url' => 'https://git.example.test',
|
||||
'api_token' => 'fresh-token',
|
||||
])
|
||||
->assertForbidden();
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure users can remove their own Git connections.
|
||||
*/
|
||||
public function test_users_can_delete_their_own_git_connections(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$gitConnection = UserGitConnection::factory()->for($user)->create([
|
||||
'account_login' => 'nico',
|
||||
'base_url' => 'https://git.example.test',
|
||||
]);
|
||||
|
||||
$this->actingAs($user)
|
||||
->delete(route('git-connections.destroy', $gitConnection))
|
||||
->assertRedirect(route('git-connections.edit'))
|
||||
->assertSessionHas(
|
||||
'status',
|
||||
'nico @ https://git.example.test was removed.',
|
||||
);
|
||||
|
||||
$this->assertDatabaseMissing('user_git_connections', [
|
||||
'id' => $gitConnection->id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user