game-village-politics-backend/tests/Feature/AuthTest.php

29 lines
1.2 KiB
PHP

<?php
use App\Models\User;
it('registers a player and returns a bearer token', function () {
$response = $this->postJson('/api/auth/register', [
'name' => 'Ada', 'email' => 'ada@example.test', 'password' => 'secret-pass',
]);
$response->assertOk()->assertJsonPath('user.name', 'Ada')->assertJsonStructure(['token', 'user' => ['id', 'name', 'email']]);
expect(User::where('email', 'ada@example.test')->first()->tokens)->toHaveCount(1);
});
it('refuses a second account with the same email', function () {
User::factory()->create(['email' => 'ada@example.test']);
$this->postJson('/api/auth/register', ['name' => 'Ada', 'email' => 'ada@example.test', 'password' => 'secret-pass'])
->assertStatus(422)->assertJsonPath('code', 'email_taken');
});
it('logs in with the right password only', function () {
User::factory()->create(['email' => 'ada@example.test', 'password' => 'secret-pass']);
$this->postJson('/api/auth/login', ['email' => 'ada@example.test', 'password' => 'wrong-pass'])
->assertStatus(422)->assertJsonPath('code', 'invalid_credentials');
$this->postJson('/api/auth/login', ['email' => 'ada@example.test', 'password' => 'secret-pass'])
->assertOk()->assertJsonStructure(['token']);
});