mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-13 02:32:15 +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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user