trypost/tests/Feature/Auth/RegistrationTest.php

151 lines
4.8 KiB
PHP
Raw Normal View History

2026-01-15 01:13:44 +00:00
<?php
declare(strict_types=1);
2026-01-15 01:13:44 +00:00
use App\Models\User;
beforeEach(fn () => config()->set('trypost.self_hosted', false));
2026-01-15 01:13:44 +00:00
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();
2026-03-31 00:32:43 +00:00
$response->assertRedirect(route('register.success', absolute: false));
2026-01-15 01:13:44 +00:00
});
test('new users get a default workspace on registration', function () {
2026-01-15 01:13:44 +00:00
$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(1);
expect($user->workspaces()->first()->name)->toBe("Test User's Workspace");
expect($user->current_workspace_id)->toBe($user->workspaces()->first()->id);
2026-01-21 21:55:33 +00:00
});
2026-01-20 19:53:54 +00:00
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 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();
});