84 lines
2.6 KiB
PHP
84 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Auth;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class RegistrationTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_first_user_bootstraps_as_admin_and_is_logged_in(): void
|
|
{
|
|
$response = $this->postJson('/web-api/register', [
|
|
'name' => 'Admin One',
|
|
'email' => 'admin@example.com',
|
|
'password' => 'password123',
|
|
'password_confirmation' => 'password123',
|
|
]);
|
|
|
|
$response->assertOk()->assertJson(['bootstrap' => true]);
|
|
|
|
$user = User::where('email', 'admin@example.com')->firstOrFail();
|
|
|
|
$this->assertTrue($user->hasRole('admin'));
|
|
$this->assertNotNull($user->approved_at);
|
|
$this->assertAuthenticatedAs($user);
|
|
}
|
|
|
|
public function test_second_user_registration_is_pending_and_not_logged_in(): void
|
|
{
|
|
$this->postJson('/web-api/register', [
|
|
'name' => 'Admin One',
|
|
'email' => 'admin@example.com',
|
|
'password' => 'password123',
|
|
'password_confirmation' => 'password123',
|
|
])->assertOk();
|
|
|
|
$this->postJson('/web-api/logout');
|
|
|
|
$response = $this->postJson('/web-api/register', [
|
|
'name' => 'Regular User',
|
|
'email' => 'user@example.com',
|
|
'password' => 'password123',
|
|
'password_confirmation' => 'password123',
|
|
]);
|
|
|
|
$response->assertCreated()->assertJson(['pending' => true]);
|
|
|
|
$user = User::where('email', 'user@example.com')->firstOrFail();
|
|
|
|
$this->assertTrue($user->hasRole('user'));
|
|
$this->assertNull($user->approved_at);
|
|
$this->assertGuest();
|
|
}
|
|
|
|
public function test_registration_requires_matching_password_confirmation(): void
|
|
{
|
|
$response = $this->postJson('/web-api/register', [
|
|
'name' => 'Someone',
|
|
'email' => 'someone@example.com',
|
|
'password' => 'password123',
|
|
'password_confirmation' => 'different',
|
|
]);
|
|
|
|
$response->assertUnprocessable()->assertJsonValidationErrors('password');
|
|
}
|
|
|
|
public function test_registration_requires_unique_email(): void
|
|
{
|
|
User::factory()->create(['email' => 'taken@example.com']);
|
|
|
|
$response = $this->postJson('/web-api/register', [
|
|
'name' => 'Someone',
|
|
'email' => 'taken@example.com',
|
|
'password' => 'password123',
|
|
'password_confirmation' => 'password123',
|
|
]);
|
|
|
|
$response->assertUnprocessable()->assertJsonValidationErrors('email');
|
|
}
|
|
}
|