106 lines
3.0 KiB
PHP
106 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use Tests\TestCase;
|
|
|
|
class AgentPasswordTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private function agent(string $password = 'secret12'): User
|
|
{
|
|
return User::query()->create([
|
|
'username' => 'okagent',
|
|
'password' => $password,
|
|
'status' => 1,
|
|
]);
|
|
}
|
|
|
|
#[Test]
|
|
public function guest_cannot_open_password_page(): void
|
|
{
|
|
$this->get(route('user.password.index'))
|
|
->assertRedirect(route('user.login'));
|
|
}
|
|
|
|
#[Test]
|
|
public function agent_can_open_password_page(): void
|
|
{
|
|
$this->actingAs($this->agent(), 'agent')
|
|
->get(route('user.password.index'))
|
|
->assertOk()
|
|
->assertSee('修改密码')
|
|
->assertSee('okagent');
|
|
}
|
|
|
|
#[Test]
|
|
public function agent_can_change_password(): void
|
|
{
|
|
$agent = $this->agent();
|
|
|
|
$this->actingAs($agent, 'agent')
|
|
->putJson(route('user.password.update'), [
|
|
'current_password' => 'secret12',
|
|
'password' => 'newpass12',
|
|
'password_confirmation' => 'newpass12',
|
|
])
|
|
->assertOk()
|
|
->assertJson(['code' => 0]);
|
|
|
|
$agent->refresh();
|
|
$this->assertTrue(Hash::check('newpass12', $agent->password));
|
|
$this->assertFalse(Hash::check('secret12', $agent->password));
|
|
$this->assertAuthenticated('agent');
|
|
}
|
|
|
|
#[Test]
|
|
public function wrong_current_password_is_rejected(): void
|
|
{
|
|
$agent = $this->agent();
|
|
|
|
$this->actingAs($agent, 'agent')
|
|
->putJson(route('user.password.update'), [
|
|
'current_password' => 'wrong-old',
|
|
'password' => 'newpass12',
|
|
'password_confirmation' => 'newpass12',
|
|
])
|
|
->assertOk()
|
|
->assertJson(['code' => 1, 'msg' => '当前密码不正确']);
|
|
|
|
$agent->refresh();
|
|
$this->assertTrue(Hash::check('secret12', $agent->password));
|
|
}
|
|
|
|
#[Test]
|
|
public function confirmation_mismatch_is_rejected(): void
|
|
{
|
|
$this->actingAs($this->agent(), 'agent')
|
|
->putJson(route('user.password.update'), [
|
|
'current_password' => 'secret12',
|
|
'password' => 'newpass12',
|
|
'password_confirmation' => 'mismatch',
|
|
])
|
|
->assertStatus(422);
|
|
}
|
|
|
|
#[Test]
|
|
public function same_password_is_rejected(): void
|
|
{
|
|
$agent = $this->agent();
|
|
|
|
$this->actingAs($agent, 'agent')
|
|
->putJson(route('user.password.update'), [
|
|
'current_password' => 'secret12',
|
|
'password' => 'secret12',
|
|
'password_confirmation' => 'secret12',
|
|
])
|
|
->assertOk()
|
|
->assertJson(['code' => 1, 'msg' => '新密码不能与当前密码相同']);
|
|
}
|
|
}
|