trypost/tests/Feature/GitHubAuthToggleTest.php
Paulo Castellano 74bd88d3d6 feat(auth): self-hosted registration gate + admin seeder (closes #46)
Self-hosted installs (SELF_HOSTED=true, the default) now close /register
to the public. Workspace invites still work — the AcceptInvite page
links into /register with ?invite={id}, the middleware persists that
into the session, and POST /register passes through.

- EnsureRegistrationEnabled middleware gates GET/POST /register.
  Accepts ?invite=… (URL) or pending_invite_id (session) as the pass.
- RegisteredUserController::store clears the marker after signup.
- AcceptInvite.vue passes invite.id in the register link's query string.
- Login.vue hides the "Sign up" link when self_hosted.
- UserSeeder bootstraps a single admin (admin@trypost.it / password).
  Idempotent; not wired into DatabaseSeeder — operator runs
  `php artisan db:seed --class=UserSeeder` per the install docs.
- Tests cover both flag values for every changed surface.

Docs PR: see trypost-docs self-hosting/installation.mdx step 3.
2026-05-19 11:45:16 -03:00

69 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
beforeEach(fn () => config()->set('trypost.self_hosted', false));
test('login page shares github auth enabled prop as false when disabled', function () {
config(['trypost.github_auth_enabled' => false]);
$response = $this->get(route('login'));
$response->assertOk();
$page = $response->original->getData()['page'];
expect($page['props']['githubAuthEnabled'])->toBeFalse();
});
test('login page shares github auth enabled prop as true when enabled', function () {
config(['trypost.github_auth_enabled' => true]);
$response = $this->get(route('login'));
$response->assertOk();
$page = $response->original->getData()['page'];
expect($page['props']['githubAuthEnabled'])->toBeTrue();
});
test('register page shares github auth enabled prop', function () {
config(['trypost.github_auth_enabled' => true]);
$response = $this->get(route('register'));
$response->assertOk();
$page = $response->original->getData()['page'];
expect($page['props']['githubAuthEnabled'])->toBeTrue();
});
test('github auth redirect route exists', function () {
config(['services.github.client_id' => 'test-id']);
config(['services.github.client_secret' => 'test-secret']);
config(['services.github.redirect' => 'https://app.trypost.test/auth/github/callback']);
$response = $this->get(route('auth.github.redirect'));
// Should redirect to GitHub OAuth, not 404
$response->assertRedirect();
});
test('github auth callback route exists', function () {
$response = $this->get(route('auth.github.callback'));
// Should redirect to login on failure (no OAuth code), not 404
$response->assertRedirect(route('login'));
});
test('register page still shares github auth enabled prop when self_hosted (via pending invite)', function () {
config()->set('trypost.self_hosted', true);
config()->set('trypost.github_auth_enabled', true);
$response = $this
->withSession(['pending_invite_id' => 'invite-abc'])
->get(route('register'));
$response->assertOk();
$page = $response->original->getData()['page'];
expect($page['props']['githubAuthEnabled'])->toBeTrue();
});