Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Laravel Make Auth Tests
<?php
namespace Dappa\AuthTests;
use Illuminate\Support\ServiceProvider;
/**
* Auth test service provider
*/
class AuthTestsServiceProvider extends ServiceProvider
{
/**
* Bootstrap application services
*/
public function boot()
{
$this->publishes([
__DIR__ . '/Stubs/tests/Feature/Auth/ForgotPasswordTest.php' => base_path('tests/Feature/Auth/ForgotPasswordTest.php'),
__DIR__ . '/Stubs/tests/Feature/Auth/LoginTest.php' => base_path('tests/Feature/Auth/LoginTest.php'),
__DIR__ . '/Stubs/tests/Feature/Auth/RegisterTest.php' => base_path('tests/Feature/Auth/RegisterTest.php'),
__DIR__ . '/Stubs/tests/Feature/Auth/ResetPasswordTest.php' => base_path('tests/Feature/Auth/ResetPasswordTest.php'),
]);
}
}
<?php
namespace Tests\Feature\Auth;
use App\Models\User;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Notification;
use Tests\TestCase;
/**
* Class ForgotPasswordTest
* @package Tests\Feature\Auth
*
* @group auth
*/
class ForgotPasswordTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function it_shows_password_form()
{
// Given
// When
$response = $this->get(
route('password.request')
);
// Then
$response->assertSuccessful();
$response->assertViewIs('auth.passwords.email');
}
/** @test */
public function it_will_send_an_email_to_user_with_reset_password_link()
{
// Given
Notification::fake();
$user = factory(User::class)->create();
// When
$response = $this->post(
route('password.email'),
[
'email' => $user->email
]
);
// Then
$this->assertNotNull($token = DB::table('password_resets')->first());
Notification::assertSentTo($user, ResetPassword::class, function ($notification, $channels) use ($token) {
return Hash::check($notification->token, $token->token) === true;
});
}
/** @test */
public function it_does_not_send_email_if_not_registered()
{
// Given
Notification::fake();
$user = factory(User::class)->make();
// When
$response = $this->from(route('password.email'))
->post(
route('password.email'),
[
'email' => $user->email
]
);
// Then
$response->assertRedirect(route('password.email'));
$response->assertSessionHasErrors('email');
Notification::assertNotSentTo($user, ResetPassword::class);
}
/** @test */
public function it_requires_email_on_post_form()
{
// Given
// When
$response = $this->from(route('password.email'))
->post(
route('password.email'),
[]
);
// Then
$response->assertRedirect(route('password.email'));
$response->assertSessionHasErrors('email');
}
}
<?php
namespace Tests\Feature\Auth;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
/**
* Class LoginTest
* @package Tests\Feature\Auth
*
* @group auth
*/
class LoginTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function it_display_login_form()
{
// Given
// When
$response = $this->get(route('login'));
// Then
$response->assertSuccessful();
}
/** @test */
public function it_logs_user_in_with_correct_credentials()
{
// Given
$user = factory(User::class)->create([
'password' => bcrypt($password = 'random-password'),
]);
// When
$response = $this->post(route('login'), [
'email' => $user->email,
'password' => $password,
]);
// Then
$this->assertAuthenticatedAs($user);
}
/** @test */
public function it_will_not_login_user_with_wrong_password()
{
// Given
$user = factory(User::class)->create([
'password' => bcrypt($password = 'random-password'),
]);
// When
$response = $this->from(route('login'))
->post(route('login'), [
'email' => $user->email,
'password' => 'wrong-password',
]);
// Then
$response->assertRedirect(route('login'));
$response->assertSessionHasErrors('email');
$this->assertGuest();
}
/** @test */
public function it_can_not_login_if_user_doesnt_exist()
{
// Given
// When
$response = $this->from(route('login'))
->post(route('login'), [
'email' => 'doesnt-exist-email',
'password' => 'wrong-password',
]);
// Then
$response->assertRedirect(route('login'));
$response->assertSessionHasErrors('email');
$this->assertGuest();
}
/** @test */
public function it_allows_user_to_logout()
{
// Given
$user = factory(User::class)->create();
$this->be($user);
// When
$this->post(route('logout'));
// Then
$this->assertGuest();
}
}
<?php
namespace Tests\Feature\Auth;
use App\Models\User;
use Illuminate\Auth\Events\Registered;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Hash;
use Tests\TestCase;
/**
* Class RegisterTest
* @package Tests\Feature\Auth
*
* @group auth
*/
class RegisterTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function it_can_register_a_user()
{
// Given
Event::fake();
// When
$response = $this->post(route('register'), [
'name' => 'John Smith',
'email' => 'john.smith@email.com',
'password' => 'password',
'password_confirmation' => 'password'
]);
// Then
$users = User::all();
$user = $users->first();
$this->assertCount(1, $users);
$this->assertAuthenticatedAs($user);
$this->assertEquals('John Smith', $user->name);
$this->assertEquals('john.smith@email.com', $user->email);
$this->assertTrue(Hash::check('password', $user->password));
Event::assertDispatched(Registered::class, function ($e) use ($user) {
return $e->user->id === $user->id;
});
}
/** @test */
public function it_validates_a_user_without_name()
{
// Given
Event::fake();
// When
$response = $this->post(route('register'), [
'name' => '',
'email' => 'john.smith@email.com',
'password' => 'password',
'password_confirmation' => 'password'
]);
// Then
$users = User::all();
$this->assertCount(0, $users);
$this->assertGuest();
$response->assertSessionHasErrors('name');
Event::assertNotDispatched(Registered::class);
}
/** @test */
public function it_validates_a_user_without_email()
{
// Given
Event::fake();
// When
$response = $this->post(route('register'), [
'name' => 'John Smith',
'email' => '',
'password' => 'password',
'password_confirmation' => 'password'
]);
// Then
$users = User::all();
$this->assertCount(0, $users);
$this->assertGuest();
$response->assertSessionHasErrors('email');
Event::assertNotDispatched(Registered::class);
}
/** @test */
public function it_validates_a_user_without_password()
{
// Given
Event::fake();
// When
$response = $this->post(route('register'), [
'name' => 'John Smith',
'email' => 'john.smith@email.com',
'password' => '',
'password_confirmation' => 'password'
]);
// Then
$users = User::all();
$this->assertCount(0, $users);
$this->assertGuest();
$response->assertSessionHasErrors('password');
Event::assertNotDispatched(Registered::class);
}
/** @test */
public function it_validates_a_user_without_password_confirmation()
{
// Given
Event::fake();
// When
$response = $this->post(route('register'), [
'name' => 'John Smith',
'email' => 'john.smith@email.com',
'password' => 'password',
'password_confirmation' => ''
]);
// Then
$users = User::all();
$this->assertCount(0, $users);
$this->assertGuest();
$response->assertSessionHasErrors('password');
Event::assertNotDispatched(Registered::class);
}
/** @test */
public function it_validates_a_user_without_matching_password()
{
// Given
Event::fake();
// When
$response = $this->post(route('register'), [
'name' => 'John Smith',
'email' => 'john.smith@email.com',
'password' => 'password',
'password_confirmation' => 'doesntmatch'
]);
// Then
$users = User::all();
$this->assertCount(0, $users);
$this->assertGuest();
$response->assertSessionHasErrors('password');
Event::assertNotDispatched(Registered::class);
}
/** @test */
public function it_validates_email_if_already_exists()
{
// Given
Event::fake();
$user = factory(User::class)->create([
'name' => 'John Smith',
'email' => 'john.smith@email.com',
'password' => bcrypt('password')
]);
// When
$response = $this->post(route('register'), [
'name' => 'John Smith',
'email' => 'john.smith@email.com',
'password' => 'password',
'password_confirmation' => 'password'
]);
// Then
$users = User::all();
$this->assertCount(1, $users);
$this->assertGuest();
Event::assertNotDispatched(Registered::class);
}
}
<?php
namespace Tests\Feature\Auth;
use App\Models\User;
use Illuminate\Auth\Events\PasswordReset;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Password;
use Tests\TestCase;
/**
* Class ResetPasswordTest
* @package Tests\Feature\Auth
*
* @group auth
*/
class ResetPasswordTest extends TestCase
{
use RefreshDatabase;
/**
* @param $user
* @return mixed
*/
private function getValidToken($user)
{
return Password::broker()->createToken($user);
}
/** @test */
public function it_shows_password_reset_page()
{
// Given
$user = factory(User::class)->create();
$token = $this->getValidToken($user);
// When
$response = $this->get(route('password.reset', $token));
// Then
$response->assertSuccessful();
$response->assertViewHas('token', $token);
}
/** @test */
public function it_reset_password_with_valid_token()
{
// Given
Event::fake();
$user = factory(User::class)->create();
// When
$response = $this->post('/password/reset', [
'token' => $this->getValidToken($user),
'email' => $user->email,
'password' => 'new-password',
'password_confirmation' => 'new-password',
]);
// Then
$this->assertEquals($user->email, $user->fresh()->email);
$this->assertTrue(Hash::check('new-password', $user->fresh()->password));
$this->assertAuthenticatedAs($user);
Event::assertDispatched(PasswordReset::class, function ($e) use ($user) {
return $e->user->id === $user->id;
});
}
/** @test */
public function it_doesnt_reset_password_with_invalid_token()
{
// Given
Event::fake();
$user = factory(User::class)->create([
'password' => bcrypt('password')
]);
$token = $this->getValidToken($user);
// When
$response = $this->from(route('password.reset', $token))->post('/password/reset', [
'token' => str_random(24),
'email' => $user->email,
'password' => 'new-password',
'password_confirmation' => 'new-password',
]);
// Then
$this->assertEquals($user->email, $user->fresh()->email);
$this->assertTrue(Hash::check('password', $user->fresh()->password));
$this->assertGuest();
}
/** @test */
public function it_doesnt_update_with_empty_password()
{
// Given
Event::fake();
$user = factory(User::class)->create([
'password' => bcrypt('password')
]);
$token = $this->getValidToken($user);
// When
$response = $this->from(route('password.reset', $token))->post('/password/reset', [
'token' => str_random(24),
'email' => $user->email,
'password' => '',
'password_confirmation' => '',
]);
// Then
$response->assertSessionHasErrors('password');
$this->assertTrue(Hash::check('password', $user->fresh()->password));
$this->assertGuest();
}
/** @test */
public function it_doesnt_update_password_with_blank_email()
{
// Given
Event::fake();
$user = factory(User::class)->create([
'password' => bcrypt('password')
]);
$token = $this->getValidToken($user);
// When
$response = $this->from(route('password.reset', $token))->post('/password/reset', [
'token' => str_random(24),
'email' => '',
'password' => 'new-password',
'password_confirmation' => 'new-password',
]);
// Then
$response->assertSessionHasErrors('email');
$this->assertTrue(Hash::check('password', $user->fresh()->password));
$this->assertGuest();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment