├── LICENSE ├── LoginTest.php ├── README.md ├── RegisterTest.php └── ResetsPasswordTest.php /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Dwight Watson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LoginTest.php: -------------------------------------------------------------------------------- 1 | get('/login'); 23 | 24 | $response->assertStatus(200); 25 | } 26 | 27 | /** 28 | * A valid user can be logged in. 29 | * 30 | * @return void 31 | */ 32 | public function testLoginAValidUser() 33 | { 34 | $user = factory(User::class)->create(); 35 | 36 | $response = $this->post('/login', [ 37 | 'email' => $user->email, 38 | 'password' => 'password' 39 | ]); 40 | 41 | $response->assertStatus(302); 42 | 43 | $this->assertAuthenticatedAs($user); 44 | } 45 | 46 | /** 47 | * An invalid user cannot be logged in. 48 | * 49 | * @return void 50 | */ 51 | public function testDoesNotLoginAnInvalidUser() 52 | { 53 | $user = factory(User::class)->create(); 54 | 55 | $response = $this->post('/login', [ 56 | 'email' => $user->email, 57 | 'password' => 'invalid' 58 | ]); 59 | 60 | $response->assertSessionHasErrors(); 61 | 62 | $this->assertGuest(); 63 | } 64 | 65 | /** 66 | * A logged in user can be logged out. 67 | * 68 | * @return void 69 | */ 70 | public function testLogoutAnAuthenticatedUser() 71 | { 72 | $user = factory(User::class)->create(); 73 | 74 | $response = $this->actingAs($user)->post('/logout'); 75 | 76 | $response->assertStatus(302); 77 | 78 | $this->assertGuest(); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # laravel-auth-tests 2 | 3 | These are some boilerplate PHPUnit tests that will provide coverage for Laravel's auth scaffold. Drop them into a fresh (or existing) Laravel app that uses the scaffold to ensure your users can register, login and reset their passwords. 4 | 5 | You may need to adjust them if you've customised your app's auth flow at all, but that's probably a good thing. 6 | 7 | For more information, here's [a relevant blog post](https://www.neontsunami.com/posts/laravel-auth-scaffold-tests). 8 | -------------------------------------------------------------------------------- /RegisterTest.php: -------------------------------------------------------------------------------- 1 | get('/register'); 23 | 24 | $response->assertStatus(200); 25 | } 26 | 27 | /** 28 | * A valid user can be registered. 29 | * 30 | * @return void 31 | */ 32 | public function testRegistersAValidUser() 33 | { 34 | $user = factory(User::class)->make(); 35 | 36 | $response = $this->post('register', [ 37 | 'name' => $user->name, 38 | 'email' => $user->email, 39 | 'password' => 'password', 40 | 'password_confirmation' => 'password' 41 | ]); 42 | 43 | $response->assertStatus(302); 44 | 45 | $this->assertAuthenticated(); 46 | } 47 | 48 | /** 49 | * An invalid user is not registered. 50 | * 51 | * @return void 52 | */ 53 | public function testDoesNotRegisterAnInvalidUser() 54 | { 55 | $user = factory(User::class)->make(); 56 | 57 | $response = $this->post('register', [ 58 | 'name' => $user->name, 59 | 'email' => $user->email, 60 | 'password' => 'password', 61 | 'password_confirmation' => 'invalid' 62 | ]); 63 | 64 | $response->assertSessionHasErrors(); 65 | 66 | $this->assertGuest(); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /ResetsPasswordTest.php: -------------------------------------------------------------------------------- 1 | get('password/reset'); 26 | 27 | $response->assertStatus(200); 28 | } 29 | 30 | /** 31 | * Sends the password reset email when the user exists. 32 | * 33 | * @return void 34 | */ 35 | public function testSendsPasswordResetEmail() 36 | { 37 | $user = factory(User::class)->create(); 38 | 39 | $this->expectsNotification($user, ResetPassword::class); 40 | 41 | $response = $this->post('password/email', ['email' => $user->email]); 42 | 43 | $response->assertStatus(302); 44 | } 45 | 46 | /** 47 | * Does not send a password reset email when the user does not exist. 48 | * 49 | * @return void 50 | */ 51 | public function testDoesNotSendPasswordResetEmail() 52 | { 53 | $this->doesntExpectJobs(ResetPassword::class); 54 | 55 | $this->post('password/email', ['email' => 'invalid@email.com']); 56 | } 57 | 58 | /** 59 | * Displays the form to reset a password. 60 | * 61 | * @return void 62 | */ 63 | public function testDisplaysPasswordResetForm() 64 | { 65 | $response = $this->get('/password/reset/token'); 66 | 67 | $response->assertStatus(200); 68 | } 69 | 70 | /** 71 | * Allows a user to reset their password. 72 | * 73 | * @return void 74 | */ 75 | public function testChangesAUsersPassword() 76 | { 77 | $user = factory(User::class)->create(); 78 | 79 | $token = Password::createToken($user); 80 | 81 | $response = $this->post('/password/reset', [ 82 | 'token' => $token, 83 | 'email' => $user->email, 84 | 'password' => 'password', 85 | 'password_confirmation' => 'password' 86 | ]); 87 | 88 | $this->assertTrue(Hash::check('password', $user->fresh()->password)); 89 | } 90 | } 91 | --------------------------------------------------------------------------------