Skip to content
Permalink
Browse files

Add phpunit feature test for auth module

  • Loading branch information...
dignityinside committed Jun 17, 2019
1 parent 137ef86 commit 13ec1100d980f9b5f451537f24f9d97f57fd6e5e
@@ -17,6 +17,7 @@
},
"require-dev": {
"beyondcode/laravel-dump-server": "^1.0",
"dczajkowski/auth-tests": "^5.8",
"filp/whoops": "^2.0",
"fzaninotto/faker": "^1.4",
"mockery/mockery": "^1.0",
@@ -62,6 +63,7 @@
"@php artisan key:generate --ansi"
],
"cs-check": "phpcs",
"cs-fix": "phpcbf"
"cs-fix": "phpcbf",
"phpunit": "./vendor/bin/phpunit"
}
}

Some generated files are not rendered by default. Learn more.

@@ -29,5 +29,7 @@
<server name="MAIL_DRIVER" value="array"/>
<server name="QUEUE_CONNECTION" value="sync"/>
<server name="SESSION_DRIVER" value="array"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
</php>
</phpunit>
@@ -0,0 +1,101 @@
<?php
namespace Tests\Feature\Auth;
use App\User;
use Tests\TestCase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Notification;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Foundation\Testing\RefreshDatabase;
class ForgotPasswordTest extends TestCase
{
use RefreshDatabase;
protected function passwordRequestRoute()
{
return route('password.request');
}
protected function passwordEmailGetRoute()
{
return route('password.email');
}
protected function passwordEmailPostRoute()
{
return route('password.email');
}
protected function guestMiddlewareRoute()
{
return route('home');
}
public function testUserCanViewAnEmailPasswordForm()
{
$response = $this->get($this->passwordRequestRoute());
$response->assertSuccessful();
$response->assertViewIs('auth.passwords.email');
}
public function testUserCannotViewAnEmailPasswordFormWhenAuthenticated()
{
$user = factory(User::class)->make();
$response = $this->actingAs($user)->get($this->passwordRequestRoute());
$response->assertRedirect($this->guestMiddlewareRoute());
}
public function testUserReceivesAnEmailWithAPasswordResetLink()
{
Notification::fake();
$user = factory(User::class)->create([
'email' => 'john@example.com',
]);
$response = $this->post($this->passwordEmailPostRoute(), [
'email' => 'john@example.com',
]);
$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;
});
}
public function testUserDoesNotReceiveEmailWhenNotRegistered()
{
Notification::fake();
$response = $this->from($this->passwordEmailGetRoute())->post($this->passwordEmailPostRoute(), [
'email' => 'nobody@example.com',
]);
$response->assertRedirect($this->passwordEmailGetRoute());
$response->assertSessionHasErrors('email');
Notification::assertNotSentTo(factory(User::class)->make(['email' => 'nobody@example.com']), ResetPassword::class);
}
public function testEmailIsRequired()
{
$response = $this->from($this->passwordEmailGetRoute())->post($this->passwordEmailPostRoute(), []);
$response->assertRedirect($this->passwordEmailGetRoute());
$response->assertSessionHasErrors('email');
}
public function testEmailIsAValidEmail()
{
$response = $this->from($this->passwordEmailGetRoute())->post($this->passwordEmailPostRoute(), [
'email' => 'invalid-email',
]);
$response->assertRedirect($this->passwordEmailGetRoute());
$response->assertSessionHasErrors('email');
}
}
@@ -0,0 +1,180 @@
<?php
namespace Tests\Feature\Auth;
use App\User;
use Tests\TestCase;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Foundation\Testing\RefreshDatabase;
class LoginTest extends TestCase
{
use RefreshDatabase;
protected function successfulLoginRoute()
{
return route('home');
}
protected function loginGetRoute()
{
return route('login');
}
protected function loginPostRoute()
{
return route('login');
}
protected function logoutRoute()
{
return route('logout');
}
protected function successfulLogoutRoute()
{
return '/';
}
protected function guestMiddlewareRoute()
{
return route('home');
}
public function testUserCanViewALoginForm()
{
$response = $this->get($this->loginGetRoute());
$response->assertSuccessful();
$response->assertViewIs('auth.login');
}
public function testUserCannotViewALoginFormWhenAuthenticated()
{
$user = factory(User::class)->make();
$response = $this->actingAs($user)->get($this->loginGetRoute());
$response->assertRedirect($this->guestMiddlewareRoute());
}
public function testUserCanLoginWithCorrectCredentials()
{
$user = factory(User::class)->create([
'password' => Hash::make($password = 'i-love-laravel'),
]);
$response = $this->post($this->loginPostRoute(), [
'email' => $user->email,
'password' => $password,
]);
$response->assertRedirect($this->successfulLoginRoute());
$this->assertAuthenticatedAs($user);
}
public function testRememberMeFunctionality()
{
$user = factory(User::class)->create([
'id' => random_int(1, 100),
'password' => Hash::make($password = 'i-love-laravel'),
]);
$response = $this->post($this->loginPostRoute(), [
'email' => $user->email,
'password' => $password,
'remember' => 'on',
]);
$user = $user->fresh();
$response->assertRedirect($this->successfulLoginRoute());
$response->assertCookie(Auth::guard()->getRecallerName(), vsprintf('%s|%s|%s', [
$user->id,
$user->getRememberToken(),
$user->password,
]));
$this->assertAuthenticatedAs($user);
}
public function testUserCannotLoginWithIncorrectPassword()
{
$user = factory(User::class)->create([
'password' => Hash::make('i-love-laravel'),
]);
$response = $this->from($this->loginGetRoute())->post($this->loginPostRoute(), [
'email' => $user->email,
'password' => 'invalid-password',
]);
$response->assertRedirect($this->loginGetRoute());
$response->assertSessionHasErrors('email');
$this->assertTrue(session()->hasOldInput('email'));
$this->assertFalse(session()->hasOldInput('password'));
$this->assertGuest();
}
public function testUserCannotLoginWithEmailThatDoesNotExist()
{
$response = $this->from($this->loginGetRoute())->post($this->loginPostRoute(), [
'email' => 'nobody@example.com',
'password' => 'invalid-password',
]);
$response->assertRedirect($this->loginGetRoute());
$response->assertSessionHasErrors('email');
$this->assertTrue(session()->hasOldInput('email'));
$this->assertFalse(session()->hasOldInput('password'));
$this->assertGuest();
}
public function testUserCanLogout()
{
$this->be(factory(User::class)->create());
$response = $this->post($this->logoutRoute());
$response->assertRedirect($this->successfulLogoutRoute());
$this->assertGuest();
}
public function testUserCannotLogoutWhenNotAuthenticated()
{
$response = $this->post($this->logoutRoute());
$response->assertRedirect($this->successfulLogoutRoute());
$this->assertGuest();
}
public function testUserCannotMakeMoreThanFiveAttemptsInOneMinute()
{
$user = factory(User::class)->create([
'password' => Hash::make($password = 'i-love-laravel'),
]);
foreach (range(0, 5) as $_) {
$response = $this->from($this->loginGetRoute())->post($this->loginPostRoute(), [
'email' => $user->email,
'password' => 'invalid-password',
]);
}
$response->assertRedirect($this->loginGetRoute());
$response->assertSessionHasErrors('email');
$this->assertContains(
'Too many login attempts.',
collect($response
->baseResponse
->getSession()
->get('errors')
->getBag('default')
->get('email')
)->first()
);
$this->assertTrue(session()->hasOldInput('email'));
$this->assertFalse(session()->hasOldInput('password'));
$this->assertGuest();
}
}

0 comments on commit 13ec110

Please sign in to comment.
You can’t perform that action at this time.