trypost/tests/Feature/Auth/RegistrationTest.php
Paulo Castellano e4bc6f1d5c test(auth): cover remaining surfaces of the registration gate
- POST /register with ?invite query and no prior session passes the gate.
- UserSeeder seeds the admin + workspace on an empty DB and skips when a
  user already exists.
- Login page exposes selfHosted prop in both modes (proves the flag the
  frontend uses to hide the "Sign up" link reaches the client).
2026-05-19 12:03:41 -03:00

199 lines
6.4 KiB
PHP

<?php
declare(strict_types=1);
use App\Models\User;
beforeEach(fn () => config()->set('trypost.self_hosted', false));
test('registration screen can be rendered', function () {
$response = $this->get(route('register'));
$response->assertOk();
});
test('new users can register', function () {
$response = $this->post(route('register.store'), [
'name' => 'Test User',
'email' => 'test@example.com',
'password' => 'Password123!',
]);
$response->assertSessionHasNoErrors();
$this->assertAuthenticated();
$response->assertRedirect(route('register.success', absolute: false));
});
test('new users do not get a default workspace on registration', function () {
$this->post(route('register.store'), [
'name' => 'Test User',
'email' => 'test@example.com',
'password' => 'Password123!',
]);
$user = User::where('email', 'test@example.com')->first();
expect($user)->not->toBeNull();
expect($user->account_id)->not->toBeNull();
expect($user->workspaces()->count())->toBe(0);
expect($user->current_workspace_id)->toBeNull();
});
test('new users can register with a timezone preference', function () {
$this->post(route('register.store'), [
'name' => 'Test User',
'email' => 'test@example.com',
'password' => 'Password123!',
'timezone' => 'America/Sao_Paulo',
]);
$user = User::where('email', 'test@example.com')->first();
expect($user)->not->toBeNull();
expect($user->workspaces()->count())->toBe(0);
});
test('new users do not have verified email by default', function () {
$this->post(route('register.store'), [
'name' => 'Test User',
'email' => 'test@example.com',
'password' => 'Password123!',
]);
$user = User::where('email', 'test@example.com')->first();
expect($user->email_verified_at)->toBeNull();
});
test('new users can register with deprecated timezone Asia/Calcutta', function () {
$response = $this->post(route('register.store'), [
'name' => 'Test User',
'email' => 'test@example.com',
'password' => 'Password123!',
'timezone' => 'Asia/Calcutta',
]);
$response->assertSessionHasNoErrors();
expect(User::where('email', 'test@example.com')->exists())->toBeTrue();
});
test('new users can register with deprecated timezone US/Eastern', function () {
$response = $this->post(route('register.store'), [
'name' => 'Test User',
'email' => 'test@example.com',
'password' => 'Password123!',
'timezone' => 'US/Eastern',
]);
$response->assertSessionHasNoErrors();
expect(User::where('email', 'test@example.com')->exists())->toBeTrue();
});
test('new users cannot register with invalid timezone', function () {
$response = $this->post(route('register.store'), [
'name' => 'Test User',
'email' => 'test@example.com',
'password' => 'Password123!',
'timezone' => 'Invalid/Timezone',
]);
$response->assertSessionHasErrors('timezone');
expect(User::where('email', 'test@example.com')->exists())->toBeFalse();
});
test('new users registering via invite have verified email automatically', function () {
$this->post(route('register.store'), [
'name' => 'Test User',
'email' => 'test@example.com',
'password' => 'Password123!',
'redirect' => '/invites/some-invite-id',
]);
$user = User::where('email', 'test@example.com')->first();
expect($user->email_verified_at)->not->toBeNull();
});
test('register page returns 404 when self_hosted and no pending invite in session', function () {
config()->set('trypost.self_hosted', true);
$response = $this->get(route('register'));
$response->assertNotFound();
});
test('register POST returns 404 when self_hosted and no pending invite in session', function () {
config()->set('trypost.self_hosted', true);
$response = $this->post(route('register.store'), [
'name' => 'Test User',
'email' => 'test@example.com',
'password' => 'Password123!',
]);
$response->assertNotFound();
expect(User::where('email', 'test@example.com')->exists())->toBeFalse();
});
test('register page renders when self_hosted but session has pending invite', function () {
config()->set('trypost.self_hosted', true);
$response = $this
->withSession(['pending_invite_id' => 'invite-abc'])
->get(route('register'));
$response->assertOk();
});
test('register page renders when self_hosted with invite query param and persists it to session', function () {
config()->set('trypost.self_hosted', true);
$response = $this->get(route('register', ['invite' => 'invite-xyz']));
$response->assertOk();
$response->assertSessionHas('pending_invite_id', 'invite-xyz');
});
test('signup clears pending_invite_id from session', function () {
config()->set('trypost.self_hosted', true);
$this->withSession(['pending_invite_id' => 'invite-abc'])
->post(route('register.store'), [
'name' => 'Invitee',
'email' => 'invitee@example.com',
'password' => 'Password123!',
]);
expect(session('pending_invite_id'))->toBeNull();
expect(User::where('email', 'invitee@example.com')->exists())->toBeTrue();
});
test('register POST passes when self_hosted with invite query param even without prior session', function () {
config()->set('trypost.self_hosted', true);
$response = $this->post(route('register.store', ['invite' => 'invite-xyz']), [
'name' => 'Invitee',
'email' => 'invitee@example.com',
'password' => 'Password123!',
]);
$response->assertSessionHasNoErrors();
expect(User::where('email', 'invitee@example.com')->exists())->toBeTrue();
});
test('register works normally when not self_hosted even with pending invite in session', function () {
config()->set('trypost.self_hosted', false);
$response = $this
->withSession(['pending_invite_id' => 'invite-abc'])
->post(route('register.store'), [
'name' => 'Invitee',
'email' => 'invitee@example.com',
'password' => 'Password123!',
]);
$response->assertSessionHasNoErrors();
expect(User::where('email', 'invitee@example.com')->exists())->toBeTrue();
// Cleared regardless of mode, since signup consumes the marker.
expect(session('pending_invite_id'))->toBeNull();
});