LoginPageTest.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. use Illuminate\Support\Facades\Hash;
  3. use Webkul\Faker\Helpers\Customer as CustomerFaker;
  4. use function Pest\Laravel\get;
  5. use function Pest\Laravel\post;
  6. use function Pest\Laravel\postJson;
  7. it('returns a successful response', function () {
  8. // Act and Assert.
  9. get(route('shop.customer.session.index'))
  10. ->assertOk()
  11. ->assertSeeText(trans('shop::app.customers.login-form.page-title'));
  12. });
  13. it('should fails validation errors when email and password not provided when login', function () {
  14. // Act and Assert.
  15. postJson(route('shop.customer.session.create'))
  16. ->assertJsonValidationErrorFor('email')
  17. ->assertJsonValidationErrorFor('password')
  18. ->assertUnprocessable();
  19. });
  20. it('should fails validation errors when email is not valid', function () {
  21. // Act and Assert.
  22. postJson(route('shop.customer.session.create'), [
  23. 'email' => fake()->word(),
  24. 'password' => 'shop123',
  25. ])
  26. ->assertJsonValidationErrorFor('email')
  27. ->assertUnprocessable();
  28. });
  29. it('should fails validation errors when password length not valid', function () {
  30. // Act and Assert.
  31. postJson(route('shop.customer.session.create'), [
  32. 'email' => fake()->email(),
  33. 'password' => 'shop',
  34. ])
  35. ->assertJsonValidationErrorFor('password')
  36. ->assertUnprocessable();
  37. });
  38. it('successfully logins a customer', function () {
  39. // Arrange.
  40. $customer = (new CustomerFaker)->factory()->create([
  41. 'password' => Hash::make($password = 'admin123'),
  42. ]);
  43. // Act and Assert.
  44. post(route('shop.customer.session.create'), [
  45. 'email' => $customer->email,
  46. 'password' => $password,
  47. ])
  48. ->assertRedirectToRoute('shop.home.index')
  49. ->assertSessionMissing('error')
  50. ->assertSessionMissing('warning')
  51. ->assertSessionMissing('info');
  52. });
  53. it('fails to log in a customer if the email is invalid', function () {
  54. // Arrange.
  55. (new CustomerFaker)->factory()->create([
  56. 'password' => Hash::make($password = 'admin123'),
  57. ]);
  58. // Act and Assert.
  59. post(route('shop.customer.session.create'), [
  60. 'email' => 'wrong@email.com',
  61. 'password' => $password,
  62. ])
  63. ->assertRedirectToRoute('shop.home.index')
  64. ->assertSessionHas('error');
  65. });
  66. it('fails to log in a customer if the password is invalid', function () {
  67. // Arrange.
  68. $customer = (new CustomerFaker)->factory()->create();
  69. // Act and Assert.
  70. post(route('shop.customer.session.create'), [
  71. 'email' => $customer->email,
  72. 'password' => 'WRONG_PASSWORD',
  73. ])
  74. ->assertRedirectToRoute('shop.home.index')
  75. ->assertSessionHas('error');
  76. });