#591 - profile, users and projects

This commit is contained in:
Saeed Vaziry
2025-05-18 18:25:27 +02:00
parent edd4ba1bc2
commit 8b4d156afa
67 changed files with 1467 additions and 760 deletions

View File

@ -2,12 +2,9 @@
namespace Tests\Feature;
use App\Web\Pages\Settings\Profile\Index;
use App\Web\Pages\Settings\Profile\Widgets\ProfileInformation;
use App\Web\Pages\Settings\Profile\Widgets\UpdatePassword;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash;
use Livewire\Livewire;
use Inertia\Testing\AssertableInertia as Assert;
use Tests\TestCase;
class ProfileTest extends TestCase
@ -19,44 +16,38 @@ public function test_profile_page_is_displayed(): void
$this->actingAs($this->user);
$this
->get(Index::getUrl())
->get(route('profile'))
->assertSuccessful()
->assertSee('Profile Information')
->assertSee('Update Password')
->assertSee('Browser Sessions')
->assertSee('Two Factor Authentication');
->assertInertia(fn (Assert $page) => $page->component('profile/index'));
}
public function test_profile_information_can_be_updated(): void
{
$this->actingAs($this->user);
Livewire::test(ProfileInformation::class)
->fill([
'name' => 'Test',
'email' => 'test@example.com',
'timezone' => 'Europe/Berlin',
])
->call('submit');
$this->patch(route('profile.update'), [
'name' => 'Test',
'email' => 'test@example.com',
])
->assertRedirect(route('profile'));
$this->user->refresh();
$this->assertSame('Test', $this->user->name);
$this->assertSame('test@example.com', $this->user->email);
$this->assertSame('Europe/Berlin', $this->user->timezone);
}
public function test_password_can_be_updated(): void
{
$this->actingAs($this->user);
Livewire::test(UpdatePassword::class)
->fill([
'current_password' => 'password',
'password' => 'new-password',
'password_confirmation' => 'new-password',
])
->call('submit');
$this->put(route('profile.password'), [
'current_password' => 'password',
'password' => 'new-password',
'password_confirmation' => 'new-password',
])
->assertRedirect(route('profile'))
->assertSessionDoesntHaveErrors();
$this->assertTrue(Hash::check('new-password', $this->user->refresh()->password));
}
@ -64,14 +55,11 @@ public function test_password_can_be_updated(): void
public function test_correct_password_must_be_provided_to_update_password(): void
{
$this->actingAs($this->user);
Livewire::test(UpdatePassword::class)
->fill([
'current_password' => 'wrong-password',
'password' => 'new-password',
'password_confirmation' => 'new-password',
])
->call('submit')
->assertHasErrors('current_password');
$this->put(route('profile.password'), [
'current_password' => 'wrong-password',
'password' => 'new-password',
'password_confirmation' => 'new-password',
])
->assertSessionHasErrors('current_password');
}
}

View File

@ -3,11 +3,8 @@
namespace Tests\Feature;
use App\Models\Project;
use App\Web\Pages\Settings\Projects\Index;
use App\Web\Pages\Settings\Projects\Settings;
use App\Web\Pages\Settings\Projects\Widgets\UpdateProject;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Inertia\Testing\AssertableInertia;
use Tests\TestCase;
class ProjectsTest extends TestCase
@ -18,15 +15,17 @@ public function test_create_project(): void
{
$this->actingAs($this->user);
Livewire::test(Index::class)
->callAction('create', [
'name' => 'test',
])
->assertSuccessful();
$this->post(route('projects.store'), [
'name' => 'create-project-test',
])
->assertSessionDoesntHaveErrors()
->assertRedirect(route('projects'));
$this->assertDatabaseHas('projects', [
'name' => 'test',
'name' => 'create-project-test',
]);
$this->assertEquals($this->user->refresh()->current_project_id, Project::query()->where('name', 'create-project-test')->first()->id);
}
public function test_see_projects_list(): void
@ -37,9 +36,10 @@ public function test_see_projects_list(): void
$this->user->projects()->attach($project);
$this->get(Index::getUrl())
$this->get(route('projects'))
->assertSuccessful()
->assertSee($project->name);
->assertInertia(fn (AssertableInertia $page) => $page->component('projects/index'));
}
public function test_delete_project(): void
@ -50,11 +50,11 @@ public function test_delete_project(): void
$this->user->projects()->attach($project);
Livewire::test(Settings::class, [
'project' => $project,
$this->delete(route('projects.destroy', $project), [
'name' => $project->name,
])
->callAction('delete')
->assertSuccessful();
->assertSessionDoesntHaveErrors()
->assertRedirect(route('projects'));
$this->assertDatabaseMissing('projects', [
'id' => $project->id,
@ -69,14 +69,11 @@ public function test_edit_project(): void
$this->user->projects()->attach($project);
Livewire::test(UpdateProject::class, [
'project' => $project,
$this->patch(route('projects.update', $project), [
'name' => 'new-name',
])
->fill([
'name' => 'new-name',
])
->call('submit')
->assertSuccessful();
->assertSessionDoesntHaveErrors()
->assertRedirect(route('projects'));
$this->assertDatabaseHas('projects', [
'id' => $project->id,
@ -88,11 +85,12 @@ public function test_cannot_delete_last_project(): void
{
$this->actingAs($this->user);
Livewire::test(Settings::class, [
'project' => $this->user->currentProject,
$this->delete(route('projects.destroy', $this->user->currentProject->id), [
'name' => $this->user->currentProject->name,
])
->callAction('delete')
->assertNotified('Cannot delete the last project.');
->assertSessionHasErrors([
'name' => 'Cannot delete the last project.',
]);
$this->assertDatabaseHas('projects', [
'id' => $this->user->currentProject->id,

View File

@ -5,10 +5,8 @@
use App\Enums\UserRole;
use App\Models\Project;
use App\Models\User;
use App\Web\Pages\Settings\Users\Index;
use App\Web\Pages\Settings\Users\Widgets\UsersList;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Inertia\Testing\AssertableInertia as Assert;
use Tests\TestCase;
class UserTest extends TestCase
@ -19,13 +17,14 @@ public function test_create_user(): void
{
$this->actingAs($this->user);
Livewire::test(Index::class)
->callAction('create', [
'name' => 'new user',
'email' => 'newuser@example.com',
'password' => 'password',
'role' => UserRole::USER,
]);
$this->post(route('users.store'), [
'name' => 'new user',
'email' => 'newuser@example.com',
'password' => 'password',
'role' => UserRole::USER,
])
->assertSessionDoesntHaveErrors()
->assertRedirect(route('users'));
$this->assertDatabaseHas('users', [
'name' => 'new user',
@ -38,13 +37,11 @@ public function test_see_users_list(): void
{
$this->actingAs($this->user);
$user = User::factory()->create();
User::factory()->create();
$this->get(Index::getUrl())
->assertSuccessful();
Livewire::test(UsersList::class)
->assertCanSeeTableRecords([$user]);
$this->get(route('users'))
->assertSuccessful()
->assertInertia(fn (Assert $page) => $page->component('users/index'));
}
public function test_must_be_admin_to_see_users_list(): void
@ -54,7 +51,7 @@ public function test_must_be_admin_to_see_users_list(): void
$this->actingAs($this->user);
$this->get(Index::getUrl())
$this->get(route('users'))
->assertForbidden();
}
@ -64,8 +61,9 @@ public function test_delete_user(): void
$user = User::factory()->create();
Livewire::test(UsersList::class)
->callTableAction('delete', $user);
$this->delete(route('users.destroy', $user))
->assertSessionDoesntHaveErrors()
->assertRedirect(route('users'));
$this->assertDatabaseMissing('users', [
'id' => $user->id,
@ -76,8 +74,8 @@ public function test_cannot_delete_yourself(): void
{
$this->actingAs($this->user);
Livewire::test(UsersList::class)
->assertTableActionHidden('delete', $this->user);
$this->delete(route('users.destroy', $this->user))
->assertForbidden();
}
public function test_edit_user_info(): void
@ -86,36 +84,33 @@ public function test_edit_user_info(): void
$user = User::factory()->create();
Livewire::test(UsersList::class)
->callTableAction('edit', $user, [
'name' => 'new-name',
'email' => 'newemail@example.com',
'timezone' => 'Europe/London',
'role' => UserRole::ADMIN,
])
->assertSuccessful();
$this->patch(route('users.update', $user), [
'name' => 'new-name',
'email' => 'newemail@example.com',
'role' => UserRole::ADMIN,
])
->assertRedirect(route('users'));
$this->assertDatabaseHas('users', [
'id' => $user->id,
'name' => 'new-name',
'email' => 'newemail@example.com',
'timezone' => 'Europe/London',
'role' => UserRole::ADMIN,
]);
}
public function test_edit_user_projects(): void
public function test_add_user_to_project(): void
{
$this->actingAs($this->user);
$user = User::factory()->create();
$project = Project::factory()->create();
Livewire::test(UsersList::class)
->callTableAction('update-projects', $user, [
'projects' => [$project->id],
])
->assertSuccessful();
$this->post(route('users.projects.store', $user), [
'project' => $project->id,
])
->assertSessionDoesntHaveErrors()
->assertRedirect(route('users'));
$this->assertDatabaseHas('user_project', [
'user_id' => $user->id,
@ -123,32 +118,22 @@ public function test_edit_user_projects(): void
]);
}
public function test_edit_user_projects_with_current_project(): void
public function test_remove_user_from_project(): void
{
$this->actingAs($this->user);
/** @var User $user */
$user = User::factory()->create();
$user->current_project_id = null;
$user->save();
/** @var Project $project */
$project = Project::factory()->create();
Livewire::test(UsersList::class)
->callTableAction('update-projects', $user, [
'projects' => [$project->id],
])
->assertSuccessful();
$user->projects()->attach($project);
$this->assertDatabaseHas('user_project', [
$this->delete(route('users.projects.destroy', [$user, $project]))
->assertSessionDoesntHaveErrors()
->assertRedirect(route('users'));
$this->assertDatabaseMissing('user_project', [
'user_id' => $user->id,
'project_id' => $project->id,
]);
$this->assertDatabaseHas('users', [
'id' => $user->id,
'current_project_id' => $project->id,
]);
}
}