Commit fa096ef8 authored by Viral Solani's avatar Viral Solani

Test Cases for Reset Password

parent 0534f76d
......@@ -17,6 +17,25 @@ function generateUuid()
return uuid::uuid4();
}
if (! function_exists('homeRoute')) {
/**
* Return the route to the "home" page depending on authentication/authorization status.
*
* @return string
*/
function homeRoute()
{
if (access()->allow('view-backend')) {
return 'admin.dashboard';
} elseif (auth()->check()) {
return 'frontend.user.dashboard';
}
return 'frontend.index';
}
}
/*
* Global helpers file with misc functions.
*/
......
......@@ -50,9 +50,20 @@ class ResetPasswordController extends Controller
*/
public function showResetForm($token = null)
{
return view('frontend.auth.passwords.reset')
->withToken($token)
->withEmail($this->user->getEmailForPasswordToken($token));
if (! $token) {
return redirect()->route('frontend.auth.password.email');
}
$user = $this->user->findByPasswordResetToken($token);
if ($user && app()->make('auth.password.broker')->tokenExists($user, $token)) {
return view('frontend.auth.passwords.reset')
->withToken($token)
->withEmail($user->email);
}
return redirect()->route('frontend.auth.password.email')
->withFlashDanger(trans('exceptions.frontend.auth.password.reset_problem'));
}
/**
......@@ -80,4 +91,15 @@ class ResetPasswordController extends Controller
'password.regex' => 'Password must contain at least 1 uppercase letter and 1 number.',
];
}
/**
* Get the response for a successful password reset.
*
* @param string $response
* @return \Illuminate\Http\RedirectResponse
*/
protected function sendResetResponse($response)
{
return redirect()->route(homeRoute())->withFlashSuccess(trans($response));
}
}
......@@ -323,4 +323,20 @@ class UserRepository extends BaseRepository
return $token;
}
/**
* @param $token
*
* @return mixed
*/
public function findByPasswordResetToken($token)
{
foreach (DB::table(config('auth.passwords.users.table'))->get() as $row) {
if (password_verify($token, $row->token)) {
return $this->findByEmail($row->email);
}
}
return false;
}
}
<?php
namespace Tests\Feature;
namespace Tests\Feature\Auth;
use App\Events\Frontend\Auth\UserLoggedIn;
use Illuminate\Support\Facades\Auth;
......
<?php
namespace Tests\Feature;
namespace Tests\Feature\Auth;
use App\Events\Frontend\Auth\UserRegistered;
use App\Models\Access\User\User;
......@@ -47,7 +47,7 @@ class RegistrationTest extends BrowserKitTestCase
*/
/** @test */
public function test_registration_form()
public function user_can_register()
{
// Make sure our events are fired
Event::fake();
......
<?php
namespace Tests\Feature\Auth;
use Tests\BrowserKitTestCase;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Event;
use App\Events\Frontend\Auth\UserLoggedIn;
use Illuminate\Support\Facades\Notification;
use App\Notifications\Frontend\Auth\UserNeedsPasswordReset;
class ResetPasswordTest extends BrowserKitTestCase
{
/** @test */
public function forgot_password_page_loads_properly()
{
$this->visit('/password/reset')
->see('Email')
->see('Reset Password');
}
/** @test **/
public function forgot_password_fails_when_a_required_field_is_not_filled_in()
{
$this->visit('/password/reset')
->type('', 'email')
->press('Send Password Reset Link')
->seePageIs('/password/reset')
->see('The email field is required.');
}
/** @test **/
public function users_can_request_a_password_reset_link()
{
Notification::fake();
$this->visit('password/reset')
->type($this->user->email, 'email')
->press('Send Password Reset Link')
->seePageIs('password/reset')
->see('We have e-mailed your password reset link!')
->seeInDatabase('password_resets', ['email' => $this->user->email]);
Notification::assertSentTo(
[$this->user],
UserNeedsPasswordReset::class
);
}
/** @test **/
public function reset_password_fails_when_a_required_field_is_not_filled_in()
{
$token = $this->app->make('auth.password.broker')->createToken($this->user);
$this->visit('password/reset/'.$token)
->see($this->user->email)
->type('', 'password')
->type('', 'password_confirmation')
->press('Reset Password')
->see('The password field is required.');
}
/** @test **/
public function users_can_reset_password()
{
$token = $this->app->make('auth.password.broker')->createToken($this->user);
$this->visit('password/reset/'.$token)
->see($this->user->email)
->type('12345678', 'password')
->type('12345678', 'password_confirmation')
->press('Reset Password')
->see($this->user->first_name);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment