Files
2026-10-09 08:21:06 +02:00

215 lines
6.3 KiB
PHP

<?php
use App\Events\User\Approved;
use App\Mail\UserRegistered;
use App\Models\User;
use App\Support\Enum\UserStatus;
use Facades\Tests\Setup\UserFactory;
use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Notification;
use Inertia\Testing\AssertableInertia as Assert;
use Tests\UpdatesSettings;
uses(RefreshDatabase::class);
uses(UpdatesSettings::class);
beforeEach(function () {
$this->artisan('db:seed');
});
test('registration form is displayed when registration is enabled', function () {
$this->setSettings(['reg_enabled' => true]);
$this->get('register')
->assertOk()
->assertInertia(fn (Assert $page) => $page->component('auth/Register'));
});
test('registration form exposes the captcha sitekey only when captcha is enabled', function () {
$this->setSettings(['reg_enabled' => true, 'registration.captcha.enabled' => false]);
$this->get('register')
->assertInertia(fn (Assert $page) => $page->where('captchaSitekey', null));
$this->setSettings(['registration.captcha.enabled' => true]);
config(['captcha.sitekey' => 'test-sitekey']);
$this->get('register')
->assertInertia(fn (Assert $page) => $page->where('captchaSitekey', 'test-sitekey'));
});
test('when registration is disabled a visitor cannot see the registration form', function () {
$this->setSettings(['reg_enabled' => false]);
$this->get('register')->assertStatus(404);
$this->get('login')->assertDontSee('You don\'t have an account?');
});
test('registration with approval enabled', function () {
$this->setSettings([
'reg_enabled' => true,
'approval.enabled' => true,
'reg_email_confirmation' => true,
'registration.captcha.enabled' => false,
'tos' => true,
]);
Notification::fake();
Event::fake([
Approved::class,
]);
$data = getRegistrationFormStubData();
$this->post('/register', $data)->assertRedirect('/login');
$expected = Arr::except($data, ['password', 'password_confirmation', 'tos']);
$expected += ['status' => UserStatus::UNCONFIRMED];
$this->assertDatabaseHas('users', $expected);
$user = User::where('email', $data['email'])->first();
$user->email_verified_at = now();
$user->status = UserStatus::WAITING_APPROVAL;
$user->save();
$this->actingAsAdmin()
->from("/users/{$user->id}/edit")
->put("/users/{$user->id}/update/approve")
->assertRedirect("users/{$user->id}/edit");
$expected = array_merge($expected, ['status' => UserStatus::ACTIVE]);
$this->assertDatabaseHas('users', $expected);
Event::assertDispatched(Approved::class);
Notification::assertSentTo($user, VerifyEmail::class);
});
test('registration with email confirmation', function () {
$this->setSettings([
'reg_enabled' => true,
'approval.enabled' => false,
'reg_email_confirmation' => true,
'registration.captcha.enabled' => false,
'tos' => true,
]);
Notification::fake();
$data = getRegistrationFormStubData();
$this->post('/register', $data)->assertRedirect('/');
$expected = Arr::except($data, ['password', 'password_confirmation', 'tos']);
$expected += ['status' => UserStatus::UNCONFIRMED];
$this->assertDatabaseHas('users', $expected);
$user = User::where('email', $data['email'])->first();
Notification::assertSentTo($user, VerifyEmail::class);
});
test('registration without email confirmation', function () {
$this->setSettings([
'reg_enabled' => true,
'reg_email_confirmation' => false,
'notifications_signup_email' => false,
'registration.captcha.enabled' => false,
'tos' => true,
]);
Notification::fake();
$data = getRegistrationFormStubData();
$this->post('/register', $data)->assertRedirect('/');
$expected = Arr::except($data, ['password', 'password_confirmation', 'tos']);
$expected += ['status' => UserStatus::ACTIVE];
$this->assertDatabaseHas('users', $expected);
Notification::assertNotSentTo(
User::where('email', $data['email'])->first(),
VerifyEmail::class
);
});
test('email notification is being sent when new user registers', function () {
$this->setSettings([
'app_name' => 'foo',
'reg_enabled' => true,
'reg_email_confirmation' => false,
'notifications_signup_email' => true,
'registration.captcha.enabled' => false,
'tos' => true,
]);
Mail::fake();
$admin = UserFactory::admin()->email('john.doe@test.com')->create();
$user1 = UserFactory::user()->email('jane.doe@test.com')->create();
$user2 = UserFactory::user()->email('josh.doe@test.com')->create();
$this->post('/register', getRegistrationFormStubData());
Mail::assertQueued(UserRegistered::class, 2);
});
test('redirect to custom page after login', function () {
UserFactory::withCredentials('foo', 'bar')->create();
$this->post('/login', [
'username' => 'foo',
'password' => 'bar',
'to' => 'http://www.google.com',
])->assertRedirect('http://www.google.com');
});
test('custom redirect page is available after failed login attempt', function () {
$to = 'http://www.google.com';
$this->post('/login', [
'username' => 'foo',
'password' => 'bar',
'to' => 'http://www.google.com',
])->assertRedirect('login?to='.$to);
});
test('access to auth pages is not allowed for authenticated users', function () {
$this->setSettings([
'reg_enabled' => true,
'2fa.enabled' => true,
'forgot_password' => true,
]);
$user = UserFactory::withCredentials('foo', 'bar')->create();
$this->be($user);
$forbiddenGetRoutes = [
'login', 'register', 'password/reset', 'password/reset/123',
'auth/facebook/login', 'auth/facebook/callback',
];
foreach ($forbiddenGetRoutes as $route) {
$this->get($route)->assertRedirect('/');
}
$this->get('auth/two-factor-authentication')->assertRedirect('/login');
});
function getRegistrationFormStubData()
{
return [
'email' => 'test@test.com',
'username' => 'johndoe',
'password' => '123123123',
'password_confirmation' => '123123123',
'tos' => 1,
];
}