AuthenticationTest.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. use App\Models\User;
  3. test('login screen can be rendered', function () {
  4. $response = $this->get(route('login'));
  5. $response->assertStatus(200);
  6. });
  7. test('users can authenticate using the login screen', function () {
  8. $user = User::factory()->create();
  9. $response = $this->post(route('login.store'), [
  10. 'email' => $user->email,
  11. 'password' => 'password',
  12. ]);
  13. $this->assertAuthenticated();
  14. $response->assertRedirect(route('dashboard', absolute: false));
  15. });
  16. test('users can not authenticate with invalid password', function () {
  17. $user = User::factory()->create();
  18. $this->post(route('login.store'), [
  19. 'email' => $user->email,
  20. 'password' => 'wrong-password',
  21. ]);
  22. $this->assertGuest();
  23. });
  24. test('users can logout', function () {
  25. $user = User::factory()->create();
  26. $response = $this->actingAs($user)->post(route('logout'));
  27. $this->assertGuest();
  28. $response->assertRedirect(route('home'));
  29. });
  30. test('users are rate limited', function () {
  31. $user = User::factory()->create();
  32. for ($i = 0; $i < 5; $i++) {
  33. $this->post(route('login.store'), [
  34. 'email' => $user->email,
  35. 'password' => 'wrong-password',
  36. ])
  37. ->assertStatus(302)
  38. ->assertSessionHasErrors([
  39. 'email' => 'These credentials do not match our records.',
  40. ]);
  41. }
  42. $response = $this->post(route('login.store'), [
  43. 'email' => $user->email,
  44. 'password' => 'wrong-password',
  45. ]);
  46. $response->assertSessionHasErrors('email');
  47. $errors = session('errors');
  48. $this->assertStringContainsString('Too many login attempts', $errors->first('email'));
  49. });