CaiWuXiTong/tests/Feature/Auth/UsersTest.php
renshen 566a3ef4db 登录后固定进入首页,不再回跳上次打开的业务页面
- 账号密码登录与钉钉免登都忽略 url.intended,直接进 landing_page(首页/财务驾驶舱)
- 补充登录落地测试,修正首页改版后仍按旧行为断言的测试
2026-10-19 14:26:10 +08:00

278 lines
7.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Tests\Feature\Auth;
use App\Jobs\Auth\CreateUser;
use App\Notifications\Auth\Invitation;
use Illuminate\Support\Facades\Notification;
use Tests\Feature\FeatureTestCase;
class UsersTest extends FeatureTestCase
{
public function testItShouldSeeUserListPage()
{
$this->loginAs()
->get(route('users.index'))
->assertOk()
->assertSeeText(trans_choice('general.users', 2));
}
public function testItShouldSeePendingUserListPage()
{
$request = $this->getRequest();
$user = $this->dispatch(new CreateUser($request));
$this->loginAs()
->get(route('users.index'))
->assertOk()
->assertSeeTextInOrder([
$user->name,
trans('documents.statuses.pending')
])
->assertSee(route('users.invite', $user->id));
}
public function testItShouldSeeUserCreatePage()
{
$this->loginAs()
->get(route('users.create'))
->assertOk()
->assertSeeText(trans('general.title.invite', ['type' => trans_choice('general.users', 1)]));
}
public function testItShouldCreateUser()
{
Notification::fake();
$request = $this->getRequest();
$response = $this->loginAs()
->post(route('users.store'), $request)
->assertOk()
->assertJson([
'success' => true,
'error' => false,
'message' => '',
'redirect' => route('users.show', user_model_class()::max('id')),
])
->json();
$user = user_model_class()::findOrFail($response['data']['id']);
$this->assertFlashLevel('success');
$this->assertModelExists($user);
$this->assertModelExists($user->invitation);
Notification::assertSentTo([$user], Invitation::class);
}
public function testItShouldSeeUserUpdatePage()
{
$request = $this->getRequest();
$user = $this->dispatch(new CreateUser($request));
$this->loginAs()
->get(route('users.edit', $user->id))
->assertOk()
->assertSee($user->email);
}
public function testItShouldUpdateUser()
{
$request = $this->getRequest();
$user = $this->dispatch(new CreateUser($request));
$request['email'] = $this->faker->freeEmail;
$this->loginAs()
->patch(route('users.update', $user->id), $request)
->assertOk()
->assertSee($request['email']);
$this->assertFlashLevel('success');
$this->assertDatabaseHas('users', $this->getAssertRequest($request));
}
public function testItShouldDeleteUser()
{
$request = $this->getRequest();
$user = $this->dispatch(new CreateUser($request));
$this->loginAs()
->delete(route('users.destroy', $user->id))
->assertOk();
$this->assertFlashLevel('success');
$this->assertSoftDeleted('users', $this->getAssertRequest($request));
$this->assertSoftDeleted('user_invitations', ['user_id' => $user->id]);
}
public function testItShouldSeeLoginPage()
{
$this->get(route('login'))
->assertOk()
->assertSeeText(trans('auth.login_to'));
}
public function testItShouldLoginUser()
{
$request = $this->getRequest();
$user = $this->dispatch(new CreateUser($request));
$this->withHeaders(['X-Requested-With' => 'XMLHttpRequest', 'Accept' => 'application/json'])
->post(route('login'), ['email' => $user->email, 'password' => $user->password])
->assertOk();
$this->isAuthenticated($user->user);
}
public function testItShouldRedirectNativeLogin()
{
$request = $this->getRequest();
$user = $this->dispatch(new CreateUser($request));
$this->post(route('login'), ['email' => $user->email, 'password' => $user->password])
->assertRedirect();
$this->isAuthenticated($user->user);
}
public function testItShouldLandOnTheHomePageAfterLoginInsteadOfTheLastOpenedBusinessPage()
{
// 未登录访问业务页面时,框架会记录 intended;登录后不应该再回跳到该页面。
$this->withExceptionHandling()->get(route('business.pages.show', [
'company_id' => $this->company->id,
'section' => 'courier-payables',
'page' => 'freight-reconciliation',
]));
$response = $this->post(route('login'), [
'email' => $this->user->email,
'password' => '123456',
]);
$response->assertRedirect();
$this->assertTrue(auth()->check());
$this->assertStringNotContainsString('/workspace/', (string) $response->headers->get('Location'));
}
public function testItShouldNotLoginUser()
{
$request = $this->getRequest();
$user = $this->dispatch(new CreateUser($request));
$this->withHeaders(['X-Requested-With' => 'XMLHttpRequest', 'Accept' => 'application/json'])
->post(route('login'), ['email' => $user->email, 'password' => $this->faker->password()])
->assertOk();
$this->assertGuest();
}
public function testItShouldLogoutUser()
{
$request = $this->getRequest();
$user = $this->dispatch(new CreateUser($request));
$this->loginAs()
->get(route('logout', $user->id))
->assertStatus(302)
->assertRedirect(route('login'));
$this->assertGuest();
}
public function testItShouldSeeRegisterPage()
{
$request = $this->getRequest();
$user = $this->dispatch(new CreateUser($request));
$this->get(route('register', ['token' => $user->invitation->token]))
->assertOk();
$this->assertGuest();
}
public function testItShouldNotSeeRegisterPage()
{
$this->withExceptionHandling()
->get(route('register', ['token' => $this->faker->uuid]))
->assertForbidden();
$this->assertGuest();
}
public function testItShouldRegisterUser()
{
$request = $this->getRequest();
$user = $this->dispatch(new CreateUser($request));
$password = $this->faker->password;
$data = [
'token' => $user->invitation->token,
'password' => $password,
'password_confirmation' => $password,
];
$this->post(route('register.store'), $data)
->assertOk()
->assertJson([
'redirect' => url('/'),
]);
$this->assertFlashLevel('success');
$this->assertSoftDeleted('user_invitations', ['user_id' => $user->id]);
$this->isAuthenticated($user->user);
}
public function testItShouldNotRegisterUser()
{
$password = $this->faker->password;
$data = [
'token' => $this->faker->uuid,
'password' => $password,
'password_confirmation' => $password,
];
$this->withExceptionHandling()
->post(route('register.store'), $data)
->assertForbidden();
$this->assertGuest();
}
public function getRequest()
{
return user_model_class()::factory()->enabled()->raw();
}
public function getAssertRequest($request)
{
unset($request['password']);
unset($request['password_confirmation']);
unset($request['remember_token']);
unset($request['roles']);
unset($request['companies']);
return $request;
}
}