125 lines
3.4 KiB
PHP
125 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class AdminLoginTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
/**
|
|
* Test admin login page can be rendered.
|
|
*/
|
|
public function test_admin_can_view_login_page()
|
|
{
|
|
$response = $this->get('/admin/login');
|
|
$response->assertStatus(200);
|
|
}
|
|
|
|
/**
|
|
* Test admin login validation fails if email is invalid.
|
|
*/
|
|
public function test_admin_login_fails_if_email_is_invalid()
|
|
{
|
|
$response = $this->post('/admin/login', [
|
|
'email' => 'invalid-email',
|
|
'password' => 'password',
|
|
]);
|
|
|
|
$response->assertSessionHasErrors(['email']);
|
|
$this->assertEquals(
|
|
'Please enter a valid email address.',
|
|
session('errors')->get('email')[0]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Test admin login validation fails if email is not registered.
|
|
*/
|
|
public function test_admin_login_fails_if_email_is_not_registered()
|
|
{
|
|
$response = $this->post('/admin/login', [
|
|
'email' => 'unregistered@example.com',
|
|
'password' => 'password',
|
|
]);
|
|
|
|
$response->assertSessionHasErrors(['email']);
|
|
$this->assertEquals(
|
|
'This email is not registered.',
|
|
session('errors')->get('email')[0]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Test admin login validation fails if email is registered but not as an admin.
|
|
*/
|
|
public function test_admin_login_fails_if_user_is_not_an_admin()
|
|
{
|
|
User::create([
|
|
'name' => 'Regular User',
|
|
'email' => 'user@example.com',
|
|
'password' => bcrypt('password'),
|
|
'role' => 'employer', // Not admin
|
|
]);
|
|
|
|
$response = $this->post('/admin/login', [
|
|
'email' => 'user@example.com',
|
|
'password' => 'password',
|
|
]);
|
|
|
|
$response->assertSessionHasErrors(['email']);
|
|
$this->assertEquals(
|
|
'This email is not registered as an admin.',
|
|
session('errors')->get('email')[0]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Test admin login validation fails if password is wrong.
|
|
*/
|
|
public function test_admin_login_fails_if_password_is_incorrect()
|
|
{
|
|
User::create([
|
|
'name' => 'Admin User',
|
|
'email' => 'admin_test@example.com',
|
|
'password' => bcrypt('correct_password'),
|
|
'role' => 'admin',
|
|
]);
|
|
|
|
$response = $this->post('/admin/login', [
|
|
'email' => 'admin_test@example.com',
|
|
'password' => 'wrong_password',
|
|
]);
|
|
|
|
$response->assertSessionHasErrors(['password']);
|
|
$this->assertEquals(
|
|
'The password you entered is incorrect.',
|
|
session('errors')->get('password')[0]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Test admin login succeeds with correct credentials.
|
|
*/
|
|
public function test_admin_login_succeeds_with_correct_credentials()
|
|
{
|
|
$admin = User::create([
|
|
'name' => 'Admin User',
|
|
'email' => 'admin_test@example.com',
|
|
'password' => bcrypt('correct_password'),
|
|
'role' => 'admin',
|
|
]);
|
|
|
|
$response = $this->post('/admin/login', [
|
|
'email' => 'admin_test@example.com',
|
|
'password' => 'correct_password',
|
|
]);
|
|
|
|
$response->assertRedirect('/admin/dashboard');
|
|
$this->assertAuthenticatedAs($admin);
|
|
}
|
|
}
|