akaunting/tests/Feature/Auth/UsersTest.php

246 lines
6.2 KiB
PHP
Raw Normal View History

2018-10-19 19:05:11 +03:00
<?php
namespace Tests\Feature\Auth;
2019-11-17 15:06:00 +03:00
use App\Jobs\Auth\CreateUser;
2019-12-22 19:27:10 +03:00
use App\Models\Auth\User;
2022-06-29 22:40:14 +03:00
use App\Notifications\Auth\Invitation;
use Illuminate\Support\Facades\Notification;
2018-10-19 19:05:11 +03:00
use Tests\Feature\FeatureTestCase;
class UsersTest extends FeatureTestCase
{
public function testItShouldSeeUserListPage()
{
$this->loginAs()
->get(route('users.index'))
2022-06-01 10:15:55 +03:00
->assertOk()
2018-10-19 19:05:11 +03:00
->assertSeeText(trans_choice('general.users', 2));
}
2022-06-29 22:40:14 +03:00
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));
}
2018-10-19 19:05:11 +03:00
public function testItShouldSeeUserCreatePage()
{
$this->loginAs()
->get(route('users.create'))
2022-06-01 10:15:55 +03:00
->assertOk()
->assertSeeText(trans('general.title.invite', ['type' => trans_choice('general.users', 1)]));
2018-10-19 19:05:11 +03:00
}
public function testItShouldCreateUser()
{
2022-06-29 22:40:14 +03:00
Notification::fake();
2020-06-21 01:34:44 +03:00
$request = $this->getRequest();
2020-06-21 01:27:20 +03:00
2022-06-29 22:40:14 +03:00
$response = $this->loginAs()
2020-06-21 01:27:20 +03:00
->post(route('users.store'), $request)
2022-06-29 22:40:14 +03:00
->assertOk()
->assertJson([
'success' => true,
'error' => false,
'message' => '',
'redirect' => route('users.index'),
])
->json();
$user = User::findOrFail($response['data']['id']);
2018-10-19 19:05:11 +03:00
$this->assertFlashLevel('success');
2020-06-21 01:27:20 +03:00
2022-06-29 22:40:14 +03:00
$this->assertModelExists($user);
$this->assertModelExists($user->invitation);
Notification::assertSentTo([$user], Invitation::class);
2018-10-19 19:05:11 +03:00
}
public function testItShouldSeeUserUpdatePage()
{
2020-06-21 01:34:44 +03:00
$request = $this->getRequest();
2020-06-21 01:27:20 +03:00
$user = $this->dispatch(new CreateUser($request));
2018-10-19 19:05:11 +03:00
$this->loginAs()
2020-01-13 10:55:19 +03:00
->get(route('users.edit', $user->id))
2022-06-01 10:15:55 +03:00
->assertOk()
2020-03-27 01:36:32 +03:00
->assertSee($user->email);
2018-10-19 19:05:11 +03:00
}
public function testItShouldUpdateUser()
{
2020-01-07 01:28:05 +03:00
$request = $this->getRequest();
2018-10-19 19:05:11 +03:00
2019-11-17 15:06:00 +03:00
$user = $this->dispatch(new CreateUser($request));
2018-10-19 19:05:11 +03:00
2022-06-01 10:15:55 +03:00
$request['email'] = $this->faker->freeEmail;
2018-10-19 19:05:11 +03:00
$this->loginAs()
->patch(route('users.update', $user->id), $request)
2022-06-01 10:15:55 +03:00
->assertOk()
2022-06-29 22:40:14 +03:00
->assertSee($request['email']);
2018-10-19 19:05:11 +03:00
$this->assertFlashLevel('success');
2020-06-21 01:27:20 +03:00
2020-06-21 01:34:44 +03:00
$this->assertDatabaseHas('users', $this->getAssertRequest($request));
2018-10-19 19:05:11 +03:00
}
public function testItShouldDeleteUser()
{
2020-06-21 01:34:44 +03:00
$request = $this->getRequest();
2020-06-21 01:27:20 +03:00
$user = $this->dispatch(new CreateUser($request));
2018-10-19 19:05:11 +03:00
$this->loginAs()
->delete(route('users.destroy', $user->id))
2022-06-01 10:15:55 +03:00
->assertOk();
2018-10-19 19:05:11 +03:00
$this->assertFlashLevel('success');
2020-06-21 01:27:20 +03:00
2020-06-21 01:34:44 +03:00
$this->assertSoftDeleted('users', $this->getAssertRequest($request));
2022-06-29 22:40:14 +03:00
$this->assertSoftDeleted('user_invitations', ['user_id' => $user->id]);
2018-10-19 19:05:11 +03:00
}
2019-11-17 15:06:00 +03:00
public function testItShouldSeeLoginPage()
{
$this->get(route('login'))
2022-06-01 10:15:55 +03:00
->assertOk()
2019-11-17 15:06:00 +03:00
->assertSeeText(trans('auth.login_to'));
}
public function testItShouldLoginUser()
{
2020-06-21 01:34:44 +03:00
$request = $this->getRequest();
2020-06-21 01:27:20 +03:00
$user = $this->dispatch(new CreateUser($request));
2019-11-17 15:06:00 +03:00
$this->post(route('login'), ['email' => $user->email, 'password' => $user->password])
2022-06-01 10:15:55 +03:00
->assertOk();
2019-11-17 15:06:00 +03:00
$this->isAuthenticated($user->user);
}
public function testItShouldNotLoginUser()
{
2020-06-21 01:34:44 +03:00
$request = $this->getRequest();
2020-06-21 01:27:20 +03:00
$user = $this->dispatch(new CreateUser($request));
2019-11-17 15:06:00 +03:00
2019-12-22 19:15:58 +03:00
$this->post(route('login'), ['email' => $user->email, 'password' => $this->faker->password()])
2022-06-01 10:15:55 +03:00
->assertOk();
2019-11-17 15:06:00 +03:00
$this->assertGuest();
}
public function testItShouldLogoutUser()
{
2020-06-21 01:34:44 +03:00
$request = $this->getRequest();
2020-06-21 01:27:20 +03:00
$user = $this->dispatch(new CreateUser($request));
2019-11-17 15:06:00 +03:00
$this->loginAs()
->get(route('logout', $user->id))
->assertStatus(302)
->assertRedirect(route('login'));
$this->assertGuest();
}
2020-01-07 01:28:05 +03:00
2022-06-29 22:40:14 +03:00
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();
}
2020-01-07 01:28:05 +03:00
public function getRequest()
{
2020-10-14 17:07:59 +03:00
return User::factory()->enabled()->raw();
2020-01-07 01:28:05 +03:00
}
2020-06-21 01:27:20 +03:00
public function getAssertRequest($request)
{
unset($request['password']);
unset($request['password_confirmation']);
unset($request['remember_token']);
unset($request['roles']);
unset($request['companies']);
return $request;
}
2019-12-22 19:27:10 +03:00
}