postJson('/api/auth/register', [ 'name' => 'Ada', 'email' => 'Ada@Example.test', 'password' => 'secret-pass', 'terms' => true, ]); $response->assertOk()->assertJsonPath('user.name', 'Ada')->assertJsonStructure(['token', 'user' => ['id', 'name', 'email']]); $user = User::where('email', 'ada@example.test')->first(); expect($user->tokens)->toHaveCount(1)->and($user->terms_accepted_at)->not->toBeNull(); }); 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', 'terms' => true]) ->assertStatus(422)->assertJsonPath('code', 'email_taken')->assertJsonPath('field', 'email'); }); 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']); }); it('checks every field and names the one that is wrong', function () { User::factory()->create(['name' => 'Taken One']); $try = fn (array $over) => $this->postJson('/api/auth/register', [...['name' => 'Ada', 'email' => 'ada@example.test', 'password' => 'secret-pass', 'terms' => true], ...$over]); $try(['name' => 'A'])->assertStatus(422)->assertJsonPath('code', 'name_invalid')->assertJsonPath('field', 'name'); $try(['name' => '