mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/shipping-portal.git
synced 2026-08-19 12:34:18 +00:00
232 lines
5.9 KiB
PHP
232 lines
5.9 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit;
|
|
|
|
use App\Classes\Modules\Accounts\Services\GeneratesAuthenticationToken;
|
|
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
|
use App\Classes\ValueObjects\Constants\RoleTypes;
|
|
use App\Http\Middleware\ValidateToken;
|
|
use App\Models\User;
|
|
use JWTAuth;
|
|
use Tests\TestCase;
|
|
|
|
class AuthenticationTest extends TestCase
|
|
{
|
|
|
|
protected $loginRoute = 'api.account.authentication.authenticate.attempt';
|
|
|
|
|
|
|
|
/**
|
|
* Test Authenticated Users can not Access the login page
|
|
* TODO fix this test
|
|
* @return void
|
|
*/
|
|
public function testAuthenticatedUsersCanNotAccessLoginPage()
|
|
{
|
|
$user = User::factory()->create();
|
|
$this->actingAs($user)
|
|
->assertAuthenticatedAs($user)
|
|
->get(route('login'))
|
|
->assertSee('Sign In');
|
|
}
|
|
|
|
|
|
/**
|
|
* Empty email and password can not be null
|
|
* @return void
|
|
*/
|
|
public function testBothEmailAndPasswordAreRequiredToLogin()
|
|
{
|
|
$response = $this->post(route($this->loginRoute), [
|
|
'email' => '',
|
|
'password' => ''
|
|
]);
|
|
|
|
$response->assertStatus(500)
|
|
->assertSee('Authentication failed');
|
|
//todo we need to implement Laravel Requests for proper error handling
|
|
}
|
|
|
|
/**
|
|
* Test a user with Valid credentials can login
|
|
* @return void
|
|
*/
|
|
public function testUserWithValidCredentialsCanLogin()
|
|
{
|
|
$user = User::factory()->create([
|
|
'password' => bcrypt($password = 'password')
|
|
]);
|
|
$response = $this->post(route($this->loginRoute), [
|
|
'email' => $user->email,
|
|
'password' => $password
|
|
]);
|
|
|
|
$response->assertStatus(200)
|
|
->assertSee('You have successfully logged in to your account')
|
|
->assertSee('access_token');
|
|
}
|
|
|
|
/**
|
|
* Invalid Users get an error
|
|
* * @return void
|
|
*/
|
|
public function testUserWithInvalidCredentialCanNotLogin()
|
|
{
|
|
$user = User::factory()->create([
|
|
'password' => bcrypt($password = 'password')
|
|
]);
|
|
|
|
$response = $this->post((route($this->loginRoute)), [
|
|
'email' => $user->email,
|
|
'password' => 'wrongpassword'
|
|
]);
|
|
|
|
$response->assertStatus(401)
|
|
->assertSee('Authentication failed')
|
|
->assertSee('These credentials do not match our records.');
|
|
}
|
|
|
|
/**
|
|
* test User can Logout
|
|
* @return void
|
|
*/
|
|
public function testUserCanLogout()
|
|
{
|
|
$user = User::factory()->create();
|
|
|
|
$token = JWTAuth::fromUser($user);
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
'Authorization' => 'Bearer ' . $token,
|
|
])->json('GET', route('api.account.authentication.logout'));
|
|
|
|
|
|
$response->assertStatus(200)
|
|
->assertSee('Logout Successful')
|
|
->assertSee('You have successfully logged out of your account')
|
|
->assertDontSee('access_token');
|
|
|
|
}
|
|
|
|
/**
|
|
* test Refresh token
|
|
* @return void
|
|
*/
|
|
public function testUserCanRefreshToken()
|
|
{
|
|
$user = User::factory()->create();
|
|
|
|
$token = JWTAuth::fromUser($user);
|
|
|
|
$response = $this->withHeaders([
|
|
'Authorization' => 'Bearer ' . $token,
|
|
])->json('GET', route('api.account.authentication.refresh'));
|
|
|
|
$response->assertStatus(200)
|
|
->assertSee('Refresh Authentication Successful')
|
|
->assertSee('You have successfully your account authentication')
|
|
->assertJsonStructure([
|
|
'title',
|
|
'message',
|
|
'payload',
|
|
]);
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
* test forgot password
|
|
* @return void
|
|
*/
|
|
public function testForgotPassword()
|
|
{
|
|
$route = 'api.account.authentication.password.forget';
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
|
$response = $this->post(route($route), [
|
|
'email' => $user->email
|
|
]);
|
|
$response->assertStatus(200)
|
|
->assertSee('Reset Password Successful')
|
|
->assertSee('Reset Password Successful')
|
|
->assertSee('You have successfully sent you an email to reset your password');
|
|
|
|
}
|
|
|
|
/**
|
|
* test reset password
|
|
* @return void
|
|
*/
|
|
public function testUserGetResetPasswordLink()
|
|
{
|
|
$route = 'api.account.authentication.password.reset';
|
|
|
|
$user = User::factory()->create();
|
|
|
|
$token = JWTAuth::fromUser($user);
|
|
|
|
$response = $this->withHeaders([
|
|
'Authorization' => 'Bearer ' . $token,
|
|
])->json('POST', route($route), [
|
|
'password' => 'newPassword',
|
|
'confirmPassword' => 'newPassword'
|
|
]);
|
|
|
|
$response->assertStatus(500)
|
|
->assertSee('Password Change failed');
|
|
}
|
|
|
|
|
|
/*
|
|
* todo
|
|
* $response->assertStatus(200)
|
|
->assertSee('Reset Password Successful')
|
|
->assertSee('Reset Password Successful')
|
|
->assertSee('You have successfully sent you an email to reset your password');*/
|
|
|
|
/*}*/
|
|
|
|
/**
|
|
* test user can verify email
|
|
* @return void
|
|
*/
|
|
/*public function testCanVerifyEmail()
|
|
{
|
|
$route = 'api.account.email.verify';
|
|
$user = User::factory()->create();
|
|
|
|
$token = Password::createToken($user);
|
|
|
|
dd($token);
|
|
|
|
$response = $this->actingAs($user)
|
|
->post(route($route), [
|
|
'token' => $token
|
|
])->assertStatus(200);
|
|
|
|
}*/
|
|
|
|
/**
|
|
* test Resend verification email
|
|
* @return void
|
|
*/
|
|
public function testResendVerification()
|
|
{
|
|
$route = 'api.account.email.verification.resend';
|
|
$user = User::factory()->create();
|
|
$response = $this->post(route($route), [
|
|
'user_id' => $user->id
|
|
])
|
|
->assertStatus(200)
|
|
->assertSee('Resend Verification Email Successful')
|
|
->assertSee('Successfully resent a new verification email');
|
|
|
|
}
|
|
|
|
|
|
}
|