Files
shipping-portal/tests/Unit/AuthenticationTest.php
T
2023-01-19 09:39:15 +03:00

162 lines
3.9 KiB
PHP

<?php
namespace Tests\Unit;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\RoleTypes;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Tests\TestCase;
use Tymon\JWTAuth\JWTAuth;
class AuthenticationTest extends TestCase
{
protected $loginRoute = 'api.account.authentication.authenticate.attempt';
/**
* A basic feature test example.
*
* @return void
*/
public function test_example()
{
$response = $this->get('/');
$response->assertStatus(200);
}
/**
* Test Guests can access the login page.
*
* @return void
*/
public function testGuestCanAccessLoginPage()
{
$response = $this->get('');
$response->assertStatus(200);
$response->assertSee('Sign In');
$response->assertSee('To keep using the service please sign in with your personal info');
}
/**
* 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');
}
/**
* Assert Logged-in Users can Access the Dashboard
* @return void
*/
public function testAuthenticatedUserCanAccessDashboard()
{
$user = User::factory()->create([
'type' => RoleTypes::ADMIN,
'status' => ApprovalStatus::APPROVED
]
);
$this->actingAs($user)
->assertAuthenticatedAs($user)
->get('/dashboard')
->assertOk();
}
/**
* 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([
'password'=> bcrypt('password')
]
);
$user = User::factory()->create();
$response = $this->actingAs($user, 'api')->get(route('api.account.authentication.logout'));
$response->assertStatus(401)
->assertDontSee('access_token');
//todo assert guest
//todo assert mission token
}
public function testUserCanRefreshToken()
{
$user = User::factory()->create();
}
}