mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-14 11:12:16 +01:00
Make name fields in projects, tasks, clients and tags unique; fixes ST-265
This commit is contained in:
committed by
Gregor Vostrak
parent
313cee2db0
commit
512089ccbd
@@ -18,8 +18,7 @@ class ClientEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_index_endpoint_fails_if_user_has_no_permission_to_view_clients(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
$clients = Client::factory()->forOrganization($data->organization)->createMany(4);
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
@@ -61,8 +60,7 @@ class ClientEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_store_endpoint_fails_if_user_has_no_permission_to_create_clients(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
@@ -74,6 +72,48 @@ class ClientEndpointTest extends ApiEndpointTestAbstract
|
||||
$response->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_store_endpoint_fails_if_client_with_same_name_already_exists(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
'clients:create',
|
||||
]);
|
||||
$client = Client::factory()->forOrganization($data->organization)->create();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
$response = $this->postJson(route('api.v1.clients.store', [$data->organization->getKey()]), [
|
||||
'name' => $client->name,
|
||||
]);
|
||||
|
||||
// Assert
|
||||
$response->assertStatus(422);
|
||||
$response->assertJsonValidationErrors([
|
||||
'name' => 'A client with the same name already exists in the organization.',
|
||||
]);
|
||||
$this->assertDatabaseCount(Client::class, 1);
|
||||
}
|
||||
|
||||
public function test_store_endpoint_fails_if_client_with_same_name_exists_in_different_organization(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
'clients:create',
|
||||
]);
|
||||
$otherOrganization = Organization::factory()->create();
|
||||
$client = Client::factory()->forOrganization($otherOrganization)->create();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
$response = $this->postJson(route('api.v1.clients.store', [$data->organization->getKey()]), [
|
||||
'name' => $client->name,
|
||||
]);
|
||||
|
||||
// Assert
|
||||
$response->assertStatus(201);
|
||||
$this->assertDatabaseCount(Client::class, 2);
|
||||
}
|
||||
|
||||
public function test_store_endpoint_creates_new_client(): void
|
||||
{
|
||||
// Arrange
|
||||
@@ -99,8 +139,7 @@ class ClientEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_update_endpoint_fails_if_user_has_no_permission_to_update_clients(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
$client = Client::factory()->forOrganization($data->organization)->create();
|
||||
$clientFake = Client::factory()->make();
|
||||
Passport::actingAs($data->user);
|
||||
@@ -139,6 +178,58 @@ class ClientEndpointTest extends ApiEndpointTestAbstract
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_update_endpoint_fails_if_client_if_client_with_same_name_already_exists(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
'clients:update',
|
||||
]);
|
||||
$client = Client::factory()->forOrganization($data->organization)->create();
|
||||
$clientFake = Client::factory()->forOrganization($data->organization)->create();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
$response = $this->putJson(route('api.v1.clients.update', [$data->organization->getKey(), $client->getKey()]), [
|
||||
'name' => $clientFake->name,
|
||||
]);
|
||||
|
||||
// Assert
|
||||
$response->assertStatus(422);
|
||||
$response->assertJsonValidationErrors([
|
||||
'name' => 'A client with the same name already exists in the organization.',
|
||||
]);
|
||||
$this->assertDatabaseHas(Client::class, [
|
||||
'id' => $client->getKey(),
|
||||
'name' => $client->name,
|
||||
'organization_id' => $data->organization->getKey(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_update_endpoint_updates_client_name_even_if_client_with_same_name_exists_in_different_organization(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
'clients:update',
|
||||
]);
|
||||
$client = Client::factory()->forOrganization($data->organization)->create();
|
||||
$otherOrganization = Organization::factory()->create();
|
||||
$clientSameName = Client::factory()->forOrganization($otherOrganization)->create();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
$response = $this->putJson(route('api.v1.clients.update', [$data->organization->getKey(), $client->getKey()]), [
|
||||
'name' => $clientSameName->name,
|
||||
]);
|
||||
|
||||
// Assert
|
||||
$response->assertStatus(200);
|
||||
$this->assertDatabaseHas(Client::class, [
|
||||
'id' => $client->getKey(),
|
||||
'name' => $clientSameName->name,
|
||||
'organization_id' => $data->organization->getKey(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_update_endpoint_updates_client(): void
|
||||
{
|
||||
// Arrange
|
||||
@@ -169,8 +260,7 @@ class ClientEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_destroy_endpoint_fails_if_user_has_no_permission_to_delete_clients(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
$client = Client::factory()->forOrganization($data->organization)->create();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
|
||||
@@ -19,8 +19,7 @@ class ImportEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_index_fails_if_user_does_not_have_permission()
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
@@ -62,8 +61,7 @@ class ImportEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_import_fails_if_user_does_not_have_permission()
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
|
||||
@@ -18,8 +18,7 @@ class InvitationEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_index_fails_if_user_has_no_permission_to_view_invitations(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
@@ -133,8 +132,7 @@ class InvitationEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_resend_fails_if_user_has_no_permission_to_resend_the_invitation(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
Passport::actingAs($data->user);
|
||||
$invitation = OrganizationInvitation::factory()->forOrganization($data->organization)->create();
|
||||
|
||||
@@ -192,8 +190,7 @@ class InvitationEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_delete_fails_if_user_has_no_permission_to_remove_invitations(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
Passport::actingAs($data->user);
|
||||
$invitation = OrganizationInvitation::factory()->forOrganization($data->organization)->create();
|
||||
|
||||
|
||||
@@ -23,8 +23,7 @@ class MemberEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_index_fails_if_user_has_no_permission_to_view_members(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
@@ -52,8 +51,7 @@ class MemberEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_update_member_fails_if_user_has_no_permission_to_update_members(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
@@ -219,8 +217,7 @@ class MemberEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_destroy_member_fails_if_user_has_no_permission_to_delete_members(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
@@ -327,8 +324,7 @@ class MemberEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_invite_placeholder_fails_if_user_does_not_have_permission(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
$user = User::factory()->create([
|
||||
'is_placeholder' => true,
|
||||
]);
|
||||
|
||||
@@ -17,8 +17,7 @@ class OrganizationEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_show_endpoint_fails_if_user_has_no_permission_to_view_organizations(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
|
||||
@@ -22,8 +22,7 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_index_endpoint_fails_if_user_has_no_permission_to_view_projects(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
$projects = Project::factory()->forOrganization($data->organization)->createMany(4);
|
||||
$projectsWithClients = Project::factory()->forOrganization($data->organization)->withClient()->createMany(4);
|
||||
Passport::actingAs($data->user);
|
||||
@@ -92,8 +91,7 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_show_endpoint_fails_if_user_has_no_permission_to_view_projects(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
$project = Project::factory()->forOrganization($data->organization)->create();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
@@ -124,8 +122,7 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_store_endpoint_fails_if_user_has_no_permission_to_create_projects(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
$projectFake = Project::factory()->forOrganization($data->organization)->make();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
@@ -166,6 +163,64 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_store_endpoint_fails_if_name_is_already_used_in_organization(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
'projects:create',
|
||||
]);
|
||||
$name = 'Project Name';
|
||||
$project = Project::factory()->forOrganization($data->organization)->create([
|
||||
'name' => $name,
|
||||
]);
|
||||
$projectFake = Project::factory()->forOrganization($data->organization)->make();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
$response = $this->postJson(route('api.v1.projects.store', [$data->organization->getKey()]), [
|
||||
'name' => $name,
|
||||
'color' => $projectFake->color,
|
||||
'is_billable' => $projectFake->is_billable,
|
||||
]);
|
||||
|
||||
// Assert
|
||||
$response->assertStatus(422);
|
||||
$response->assertJsonValidationErrors([
|
||||
'name' => 'A project with the same name already exists in the organization.',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_store_endpoint_creates_project_if_name_is_used_in_other_organization(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
'projects:create',
|
||||
]);
|
||||
$name = 'Project Name';
|
||||
$otherOrganization = Organization::factory()->create();
|
||||
$project = Project::factory()->forOrganization($otherOrganization)->create([
|
||||
'name' => $name,
|
||||
]);
|
||||
$projectFake = Project::factory()->forOrganization($data->organization)->make();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
$response = $this->postJson(route('api.v1.projects.store', [$data->organization->getKey()]), [
|
||||
'name' => $name,
|
||||
'color' => $projectFake->color,
|
||||
'is_billable' => $projectFake->is_billable,
|
||||
]);
|
||||
|
||||
// Assert
|
||||
$response->assertStatus(201);
|
||||
$this->assertDatabaseHas(Project::class, [
|
||||
'name' => $name,
|
||||
'color' => $projectFake->color,
|
||||
'organization_id' => $data->organization->getKey(),
|
||||
'is_billable' => $projectFake->is_billable,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_store_endpoint_creates_new_project_with_client(): void
|
||||
{
|
||||
// Arrange
|
||||
@@ -267,6 +322,68 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
|
||||
$response->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_update_endpoint_fails_if_name_is_already_used_in_organization(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
'projects:update',
|
||||
]);
|
||||
$name = 'Project Name';
|
||||
$projectWithTheName = Project::factory()->forOrganization($data->organization)->create([
|
||||
'name' => $name,
|
||||
]);
|
||||
$project = Project::factory()->forOrganization($data->organization)->create();
|
||||
$projectFake = Project::factory()->forOrganization($data->organization)->create();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
$response = $this->putJson(route('api.v1.projects.update', [$data->organization->getKey(), $project->getKey()]), [
|
||||
'name' => $name,
|
||||
'color' => $projectFake->color,
|
||||
'is_billable' => $projectFake->is_billable,
|
||||
]);
|
||||
|
||||
// Assert
|
||||
$response->assertStatus(422);
|
||||
$response->assertJsonValidationErrors([
|
||||
'name' => 'A project with the same name already exists in the organization.',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_update_endpoint_updates_project_if_name_is_used_in_other_organization(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
'projects:update',
|
||||
]);
|
||||
$name = 'Project Name';
|
||||
$otherOrganization = Organization::factory()->create();
|
||||
$otherProject = Project::factory()->forOrganization($otherOrganization)->create([
|
||||
'name' => $name,
|
||||
]);
|
||||
$project = Project::factory()->forOrganization($data->organization)->create([
|
||||
'name' => $name,
|
||||
]);
|
||||
$projectFake = Project::factory()->forOrganization($data->organization)->make();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
$response = $this->putJson(route('api.v1.projects.update', [$data->organization->getKey(), $project->getKey()]), [
|
||||
'name' => $name,
|
||||
'color' => $projectFake->color,
|
||||
'is_billable' => $projectFake->is_billable,
|
||||
]);
|
||||
|
||||
// Assert
|
||||
$response->assertStatus(200);
|
||||
$this->assertDatabaseHas(Project::class, [
|
||||
'name' => $name,
|
||||
'color' => $projectFake->color,
|
||||
'organization_id' => $data->organization->getKey(),
|
||||
'is_billable' => $projectFake->is_billable,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_update_endpoint_updates_project(): void
|
||||
{
|
||||
// Arrange
|
||||
@@ -377,8 +494,7 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_destroy_endpoint_fails_if_user_has_no_permission_to_delete_projects(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
$project = Project::factory()->forOrganization($data->organization)->create();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
|
||||
@@ -20,8 +20,7 @@ class ProjectMemberEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_index_endpoint_fails_if_user_has_no_permission_to_view_project_members(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
$project = Project::factory()->forOrganization($data->organization)->create();
|
||||
$projectMembers = ProjectMember::factory()->forProject($project)->createMany(4);
|
||||
Passport::actingAs($data->user);
|
||||
@@ -83,8 +82,7 @@ class ProjectMemberEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_store_endpoint_fails_if_user_has_no_permission_to_add_members_to_project(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
$project = Project::factory()->forOrganization($data->organization)->create();
|
||||
$projectMemberFake = ProjectMember::factory()->make();
|
||||
$user = User::factory()->create();
|
||||
@@ -268,8 +266,7 @@ class ProjectMemberEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_update_endpoint_fails_if_user_has_no_permission_to_update_projects(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
$project = Project::factory()->forOrganization($data->organization)->create();
|
||||
$projectMember = ProjectMember::factory()->forProject($project)->create();
|
||||
$projectMemberFake = ProjectMember::factory()->make();
|
||||
@@ -367,8 +364,7 @@ class ProjectMemberEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_destroy_endpoint_fails_if_user_has_no_permission_to_delete_project_members(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
$project = Project::factory()->forOrganization($data->organization)->create();
|
||||
$projectMember = ProjectMember::factory()->forProject($project)->create();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
@@ -18,8 +18,7 @@ class TagEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_index_endpoint_fails_if_user_has_no_permission_to_view_tags(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
$tags = Tag::factory()->forOrganization($data->organization)->createMany(4);
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
@@ -61,8 +60,7 @@ class TagEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_store_endpoint_fails_if_user_has_no_permission_to_create_tags(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
@@ -74,6 +72,55 @@ class TagEndpointTest extends ApiEndpointTestAbstract
|
||||
$response->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_store_endpoint_fails_if_name_is_already_taken(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
'tags:create',
|
||||
]);
|
||||
$name = 'Test Tag';
|
||||
$tagWithName = Tag::factory()->forOrganization($data->organization)->create([
|
||||
'name' => $name,
|
||||
]);
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
$response = $this->postJson(route('api.v1.tags.store', [$data->organization->getKey()]), [
|
||||
'name' => $tagWithName->name,
|
||||
]);
|
||||
|
||||
// Assert
|
||||
$response->assertStatus(422);
|
||||
$response->assertJsonValidationErrors([
|
||||
'name' => 'A tag with the same name already exists in the organization.',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_store_endpoint_creates_tag_if_tag_name_is_only_used_in_other_organizations(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
'tags:create',
|
||||
]);
|
||||
$name = 'Test Tag';
|
||||
$tagWithName = Tag::factory()->create([
|
||||
'name' => $name,
|
||||
]);
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
$response = $this->postJson(route('api.v1.tags.store', [$data->organization->getKey()]), [
|
||||
'name' => $tagWithName->name,
|
||||
]);
|
||||
|
||||
// Assert
|
||||
$response->assertStatus(201);
|
||||
$response->assertJson(fn (AssertableJson $json) => $json
|
||||
->has('data')
|
||||
->where('data.name', $tagWithName->name)
|
||||
);
|
||||
}
|
||||
|
||||
public function test_store_endpoint_creates_new_tag(): void
|
||||
{
|
||||
// Arrange
|
||||
@@ -99,8 +146,7 @@ class TagEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_update_endpoint_fails_if_user_has_no_permission_to_update_tags(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
$tag = Tag::factory()->forOrganization($data->organization)->create();
|
||||
$tagFake = Tag::factory()->make();
|
||||
Passport::actingAs($data->user);
|
||||
@@ -139,6 +185,57 @@ class TagEndpointTest extends ApiEndpointTestAbstract
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_update_endpoint_fails_if_name_is_already_taken(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
'tags:update',
|
||||
]);
|
||||
$tag = Tag::factory()->forOrganization($data->organization)->create();
|
||||
$tagWithName = Tag::factory()->forOrganization($data->organization)->create();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
$response = $this->putJson(route('api.v1.tags.update', [$data->organization->getKey(), $tag->getKey()]), [
|
||||
'name' => $tagWithName->name,
|
||||
]);
|
||||
|
||||
// Assert
|
||||
$response->assertStatus(422);
|
||||
$response->assertJsonValidationErrors([
|
||||
'name' => 'A tag with the same name already exists in the organization.',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_update_endpoint_updates_tag_if_tag_name_is_only_used_in_other_organizations(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
'tags:update',
|
||||
]);
|
||||
$tag = Tag::factory()->forOrganization($data->organization)->create();
|
||||
$tagWithName = Tag::factory()->create([
|
||||
'name' => 'Test Tag',
|
||||
]);
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
$response = $this->putJson(route('api.v1.tags.update', [$data->organization->getKey(), $tag->getKey()]), [
|
||||
'name' => $tagWithName->name,
|
||||
]);
|
||||
|
||||
// Assert
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(fn (AssertableJson $json) => $json
|
||||
->has('data')
|
||||
->where('data.name', $tagWithName->name)
|
||||
);
|
||||
$this->assertDatabaseHas(Tag::class, [
|
||||
'name' => $tagWithName->name,
|
||||
'organization_id' => $data->organization->getKey(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_update_endpoint_updates_tag(): void
|
||||
{
|
||||
// Arrange
|
||||
@@ -169,8 +266,7 @@ class TagEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_destroy_endpoint_fails_if_user_has_no_permission_to_delete_tags(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
$tag = Tag::factory()->forOrganization($data->organization)->create();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
|
||||
@@ -33,8 +33,7 @@ class TaskEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_index_endpoint_fails_if_user_has_no_permission_to_view_tasks(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
Task::factory()->forOrganization($data->organization)->createMany(4);
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
@@ -48,8 +47,7 @@ class TaskEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_index_endpoint_validation_fails_if_project_id_is_not_pat(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
Task::factory()->forOrganization($data->organization)->createMany(4);
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
@@ -199,8 +197,7 @@ class TaskEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_store_endpoint_fails_if_user_has_no_permission_to_create_tasks()
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
$project = Project::factory()->forOrganization($data->organization)->create();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
@@ -217,6 +214,59 @@ class TaskEndpointTest extends ApiEndpointTestAbstract
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_store_endpoint_fails_if_task_with_same_name_already_exists_in_same_project(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
'tasks:create',
|
||||
]);
|
||||
$project = Project::factory()->forOrganization($data->organization)->create();
|
||||
$task = Task::factory()->forOrganization($data->organization)->forProject($project)->create([
|
||||
'name' => 'Task 1',
|
||||
]);
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
$response = $this->postJson(route('api.v1.tasks.store', [$data->organization->getKey()]), [
|
||||
'name' => $task->name,
|
||||
'project_id' => $project->getKey(),
|
||||
]);
|
||||
|
||||
// Assert
|
||||
$response->assertStatus(422);
|
||||
$response->assertJsonValidationErrors([
|
||||
'name' => 'A task with the same name already exists in the project.',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_store_endpoint_creates_new_task_even_if_task_with_same_name_already_exists_in_other_project(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
'tasks:create',
|
||||
]);
|
||||
$project = Project::factory()->forOrganization($data->organization)->create();
|
||||
$otherProject = Project::factory()->forOrganization($data->organization)->create();
|
||||
$task = Task::factory()->forOrganization($data->organization)->forProject($otherProject)->create([
|
||||
'name' => 'Task 1',
|
||||
]);
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
$response = $this->postJson(route('api.v1.tasks.store', [$data->organization->getKey()]), [
|
||||
'name' => $task->name,
|
||||
'project_id' => $project->getKey(),
|
||||
]);
|
||||
|
||||
// Assert
|
||||
$response->assertStatus(201);
|
||||
$this->assertDatabaseHas(Task::class, [
|
||||
'name' => $task->name,
|
||||
'project_id' => $project->getKey(),
|
||||
'organization_id' => $data->organization->getKey(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_store_endpoint_creates_new_task_if_user_has_permission_to_create_tasks()
|
||||
{
|
||||
// Arrange
|
||||
@@ -244,8 +294,7 @@ class TaskEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_update_endpoint_fails_if_user_has_no_permission(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
$task = Task::factory()->forOrganization($data->organization)->create();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
@@ -266,6 +315,64 @@ class TaskEndpointTest extends ApiEndpointTestAbstract
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_update_endpoint_fails_if_task_with_same_name_already_exists_in_same_project(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
'tasks:update',
|
||||
]);
|
||||
$project = Project::factory()->forOrganization($data->organization)->create();
|
||||
$name = 'Task 1';
|
||||
$task = Task::factory()->forProject($project)->forOrganization($data->organization)->create([
|
||||
'name' => $name,
|
||||
]);
|
||||
$otherTask = Task::factory()->forProject($project)->forOrganization($data->organization)->create([
|
||||
'name' => $name,
|
||||
]);
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
$response = $this->putJson(route('api.v1.tasks.update', [$data->organization->getKey(), $task->getKey()]), [
|
||||
'name' => $name,
|
||||
]);
|
||||
|
||||
// Assert
|
||||
$response->assertStatus(422);
|
||||
$response->assertJsonValidationErrors([
|
||||
'name' => 'A task with the same name already exists in the project.',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_update_endpoint_updates_task_if_task_with_same_name_already_exists_in_other_project(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
'tasks:update',
|
||||
]);
|
||||
$project = Project::factory()->forOrganization($data->organization)->create();
|
||||
$otherProject = Project::factory()->forOrganization($data->organization)->create();
|
||||
$name = 'Task 1';
|
||||
$task = Task::factory()->forProject($project)->forOrganization($data->organization)->create([
|
||||
'name' => $name,
|
||||
]);
|
||||
$otherTask = Task::factory()->forProject($otherProject)->forOrganization($data->organization)->create([
|
||||
'name' => $name,
|
||||
]);
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
$response = $this->putJson(route('api.v1.tasks.update', [$data->organization->getKey(), $task->getKey()]), [
|
||||
'name' => $name,
|
||||
]);
|
||||
|
||||
// Assert
|
||||
$response->assertStatus(200);
|
||||
$this->assertDatabaseHas(Task::class, [
|
||||
'id' => $task->getKey(),
|
||||
'name' => $name,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_update_endpoint_updates_task_if_user_has_permission(): void
|
||||
{
|
||||
// Arrange
|
||||
@@ -331,8 +438,7 @@ class TaskEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_delete_endpoint_fails_if_user_has_no_permission_to_delete_tasks(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
$task = Task::factory()->forOrganization($data->organization)->create();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
|
||||
@@ -28,8 +28,7 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_index_endpoint_fails_if_user_has_no_permission_to_view_time_entries(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
@@ -457,8 +456,7 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_aggregate_endpoint_fails_if_user_has_no_permission_to_view_time_entries(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
@@ -774,8 +772,7 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_store_endpoint_fails_if_user_has_no_permission_to_create_time_entries(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
$timeEntryFake = TimeEntry::factory()->forOrganization($data->organization)->withTags($data->organization)->make();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
@@ -1026,8 +1023,7 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_update_endpoint_fails_if_user_has_no_permission_to_update_own_time_entries(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
$timeEntry = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->create();
|
||||
$timeEntryFake = TimeEntry::factory()->forOrganization($data->organization)->make();
|
||||
Passport::actingAs($data->user);
|
||||
@@ -1365,8 +1361,7 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_destroy_endpoint_fails_if_user_has_no_permission_to_delete_own_time_entries(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
$timeEntry = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->create();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
|
||||
@@ -18,8 +18,7 @@ class UserTimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_my_active_endpoint_returns_unauthorized_if_user_is_not_logged_in(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
|
||||
// Act
|
||||
$response = $this->getJson(route('api.v1.users.time-entries.my-active'));
|
||||
@@ -31,8 +30,7 @@ class UserTimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_my_active_endpoint_returns_current_time_entry_of_logged_in_user(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
$activeTimeEntry = TimeEntry::factory()->forMember($data->member)->active()->create();
|
||||
$inactiveTimeEntry = TimeEntry::factory()->forMember($data->member)->create();
|
||||
Passport::actingAs($data->user);
|
||||
@@ -48,8 +46,7 @@ class UserTimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_my_active_endpoint_logs_a_warning_if_user_has_multiple_active_time_entries_and_return_the_latest_one(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
$activeTimeEntry1 = TimeEntry::factory()->forMember($data->member)->active()->start(Carbon::now()->subDay())->create();
|
||||
$activeTimeEntry2 = TimeEntry::factory()->forMember($data->member)->active()->start(Carbon::now())->create();
|
||||
Passport::actingAs($data->user);
|
||||
@@ -69,8 +66,7 @@ class UserTimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_my_active_endpoint_returns_not_found_if_user_has_no_active_time_entry(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
]);
|
||||
$data = $this->createUserWithPermission();
|
||||
$inactiveTimeEntry = TimeEntry::factory()->forMember($data->member)->create();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user