hoàn thành chức năng các test case chính
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Browser;
|
||||
|
||||
use App\Models\Role;
|
||||
use App\Models\User;
|
||||
use App\Models\UserRole;
|
||||
use Laravel\Dusk\Browser;
|
||||
use Tests\DuskTestCase;
|
||||
|
||||
class AdminLoginTest extends DuskTestCase
|
||||
{
|
||||
private User $adminUser;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
// Tạo admin test user với đầy đủ roles
|
||||
$this->adminUser = User::create([
|
||||
'name' => 'DUSK ADMIN TEST',
|
||||
'email' => 'dusk_admin_test@test.local',
|
||||
'phone' => '09' . rand(10000000, 99999999),
|
||||
'password' => bcrypt('dusk_admin_pwd_2026'),
|
||||
'status' => 1,
|
||||
'address' => 'Test',
|
||||
'known_sis'=> 0,
|
||||
'job' => 'Admin',
|
||||
'spined' => 0,
|
||||
]);
|
||||
// Gán tất cả roles
|
||||
$roles = Role::where('role_status', 1)->get();
|
||||
foreach ($roles as $role) {
|
||||
UserRole::create(['user_id' => $this->adminUser->id, 'role_id' => $role->id]);
|
||||
}
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
UserRole::where('user_id', $this->adminUser->id)->delete();
|
||||
$this->adminUser->delete();
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Trang admin hiển thị form đăng nhập.
|
||||
*/
|
||||
public function test_admin_login_page_shows_form(): void
|
||||
{
|
||||
$this->browse(function (Browser $browser) {
|
||||
$browser->visit('/admin')
|
||||
->assertPresent('input[name="email"]')
|
||||
->assertPresent('input[name="password"]')
|
||||
->assertPresent('input[type="submit"]');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Đăng nhập admin với email/password sai phải hiện lỗi.
|
||||
*/
|
||||
public function test_admin_login_with_wrong_credentials_shows_error(): void
|
||||
{
|
||||
$this->browse(function (Browser $browser) {
|
||||
$browser->visit('/admin')
|
||||
->type('email', 'wrong@email.com')
|
||||
->type('password', 'wrongpassword')
|
||||
->press('Login')
|
||||
->pause(300)
|
||||
->assertPresent('.alert-danger, .error, [class*="error"]');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Đăng nhập admin với email rỗng phải hiện lỗi validation (bypass HTML5 required).
|
||||
*/
|
||||
public function test_admin_login_with_empty_fields_shows_validation(): void
|
||||
{
|
||||
$this->browse(function (Browser $browser) {
|
||||
$browser->visit('/admin');
|
||||
// HTML5 `required` prevents native submit, strip it to get server-side validation
|
||||
$browser->script("
|
||||
document.querySelectorAll('input[required]').forEach(function(el) {
|
||||
el.removeAttribute('required');
|
||||
});
|
||||
");
|
||||
$browser->press('Login')
|
||||
->pause(500)
|
||||
->assertPresent('.text-danger, .alert-danger');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Đăng nhập admin thành công và redirect vào trang tài khoản.
|
||||
*/
|
||||
public function test_admin_login_successful(): void
|
||||
{
|
||||
$this->browse(function (Browser $browser) {
|
||||
$browser->visit('/admin/logout')
|
||||
->visit('/admin')
|
||||
->type('email', 'dusk_admin_test@test.local')
|
||||
->type('password', 'dusk_admin_pwd_2026')
|
||||
->press('Login')
|
||||
->waitForLocation('/admin/account', 10)
|
||||
->assertPathIs('/admin/account');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Khi đã đăng nhập admin, truy cập /admin vẫn hiển thị form login (không có redirect).
|
||||
*/
|
||||
public function test_authenticated_admin_visiting_login_sees_form(): void
|
||||
{
|
||||
$this->browse(function (Browser $browser) {
|
||||
$browser->loginAs($this->adminUser)
|
||||
->visit('/admin')
|
||||
->assertPathIs('/admin')
|
||||
->assertPresent('input[name="email"]');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Truy cập trang admin khi chưa đăng nhập bị redirect về /admin login.
|
||||
*/
|
||||
public function test_unauthenticated_access_to_admin_account_redirects(): void
|
||||
{
|
||||
$this->browse(function (Browser $browser) {
|
||||
$browser->visit('/admin/logout')
|
||||
->visit('/admin/account')
|
||||
->assertPathIs('/admin');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Đăng xuất admin thành công qua route /logout (route /admin/logout bị middleware chặn
|
||||
* vì không có role admin/logout trong DB). Dùng real login thay vì loginAs()
|
||||
* để tránh Dusk tự re-auth sau khi logout.
|
||||
*/
|
||||
public function test_admin_logout(): void
|
||||
{
|
||||
$this->browse(function (Browser $browser) {
|
||||
// Đăng nhập thực qua form (không dùng loginAs để tránh Dusk re-auth)
|
||||
$browser->visit('/admin/logout') // clear any previous session
|
||||
->visit('/admin')
|
||||
->type('email', $this->adminUser->email)
|
||||
->type('password', 'dusk_admin_pwd_2026')
|
||||
->press('Login')
|
||||
->waitForLocation('/admin/account', 10)
|
||||
// Thực hiện logout qua frontend /logout
|
||||
->visit('/logout')
|
||||
->pause(300)
|
||||
// Truy cập trang cần auth -> phải bị redirect về /admin
|
||||
->visit('/admin/account')
|
||||
->assertPathIs('/admin');
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user