79 lines
2.0 KiB
PHP
79 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use Tests\TestCase;
|
|
|
|
class AgentLoginTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
#[Test]
|
|
public function ajax_login_succeeds_and_sets_session(): void
|
|
{
|
|
User::query()->create([
|
|
'username' => 'okagent',
|
|
'password' => 'secret12',
|
|
'status' => 1,
|
|
]);
|
|
|
|
$this->post('/user/login', [
|
|
'username' => 'okagent',
|
|
'password' => 'secret12',
|
|
])->assertOk()
|
|
->assertJson([
|
|
'code' => 0,
|
|
'msg' => '登录成功',
|
|
'data' => route('user.home'),
|
|
]);
|
|
|
|
$this->assertAuthenticated('agent');
|
|
}
|
|
|
|
#[Test]
|
|
public function ajax_login_rejects_bad_password(): void
|
|
{
|
|
User::query()->create([
|
|
'username' => 'okagent',
|
|
'password' => 'secret12',
|
|
'status' => 1,
|
|
]);
|
|
|
|
$this->post('/user/login', [
|
|
'username' => 'okagent',
|
|
'password' => 'wrongpass',
|
|
])->assertOk()
|
|
->assertJson(['code' => 1, 'msg' => '用户名或密码错误']);
|
|
|
|
$this->assertGuest('agent');
|
|
}
|
|
|
|
#[Test]
|
|
public function login_is_rate_limited_after_failures(): void
|
|
{
|
|
User::query()->create([
|
|
'username' => 'okagent',
|
|
'password' => 'secret12',
|
|
'status' => 1,
|
|
]);
|
|
|
|
for ($i = 0; $i < 5; $i++) {
|
|
$this->post('/user/login', [
|
|
'username' => 'okagent',
|
|
'password' => 'bad-password',
|
|
])->assertOk()->assertJsonPath('code', 1);
|
|
}
|
|
|
|
$response = $this->post('/user/login', [
|
|
'username' => 'okagent',
|
|
'password' => 'secret12',
|
|
])->assertOk()
|
|
->assertJsonPath('code', 1);
|
|
|
|
$this->assertStringContainsString('登陆失败次数过多', (string) $response->json('msg'));
|
|
}
|
|
}
|