Added client and organization endpoints

This commit is contained in:
Constantin Graf
2024-03-01 16:34:26 +01:00
parent a86e72f655
commit e8912650c0
18 changed files with 679 additions and 17 deletions

View File

@@ -0,0 +1,218 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Endpoint\Api\V1;
use App\Models\Client;
use App\Models\Organization;
use Illuminate\Testing\Fluent\AssertableJson;
use Laravel\Passport\Passport;
class ClientEndpointTest extends ApiEndpointTestAbstract
{
public function test_index_endpoint_fails_if_user_has_no_permission_to_view_clients(): void
{
// Arrange
$data = $this->createUserWithPermission([
]);
$clients = Client::factory()->forOrganization($data->organization)->createMany(4);
Passport::actingAs($data->user);
// Act
$response = $this->getJson(route('api.v1.clients.index', [$data->organization->getKey()]));
// Assert
$response->assertStatus(403);
}
public function test_index_endpoint_returns_list_of_all_clients_of_organization_ordered_by_created_at_desc_per_default(): void
{
// Arrange
$data = $this->createUserWithPermission([
'clients:view',
]);
$clients = Client::factory()->forOrganization($data->organization)->createMany(4);
Passport::actingAs($data->user);
// Act
$response = $this->getJson(route('api.v1.clients.index', [$data->organization->getKey()]));
// Assert
$response->assertStatus(200);
$response->assertJsonCount(4, 'data');
$response->assertJson(fn (AssertableJson $json) => $json
->has('data')
->count('data', 4)
->where('data.0.id', $clients->sortByDesc('created_at')->get(0)->getKey())
->where('data.1.id', $clients->sortByDesc('created_at')->get(1)->getKey())
->where('data.2.id', $clients->sortByDesc('created_at')->get(2)->getKey())
->where('data.3.id', $clients->sortByDesc('created_at')->get(3)->getKey())
);
}
public function test_store_endpoint_fails_if_user_has_no_permission_to_create_clients(): void
{
// Arrange
$data = $this->createUserWithPermission([
]);
Passport::actingAs($data->user);
// Act
$response = $this->postJson(route('api.v1.clients.store', [$data->organization->getKey()]), [
'name' => 'Test Client',
]);
// Assert
$response->assertStatus(403);
}
public function test_store_endpoint_creates_new_client(): void
{
// Arrange
$data = $this->createUserWithPermission([
'clients:create',
]);
$clientFake = Client::factory()->make();
Passport::actingAs($data->user);
// Act
$response = $this->postJson(route('api.v1.clients.store', [$data->organization->getKey()]), [
'name' => $clientFake->name,
]);
// Assert
$response->assertStatus(201);
$response->assertJson(fn (AssertableJson $json) => $json
->has('data')
->where('data.name', $clientFake->name)
);
}
public function test_update_endpoint_fails_if_user_has_no_permission_to_update_clients(): void
{
// Arrange
$data = $this->createUserWithPermission([
]);
$client = Client::factory()->forOrganization($data->organization)->create();
$clientFake = Client::factory()->make();
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.clients.update', [$data->organization->getKey(), $client->getKey()]), [
'name' => $clientFake->name,
]);
// Assert
$response->assertStatus(403);
}
public function test_update_endpoint_fails_if_user_is_not_part_of_client_organization(): void
{
// Arrange
$data = $this->createUserWithPermission([
'clients:update',
]);
$otherOrganization = Organization::factory()->create();
$client = Client::factory()->forOrganization($otherOrganization)->create();
$clientFake = Client::factory()->make();
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.clients.update', [$data->organization->getKey(), $client->getKey()]), [
'name' => $clientFake->name,
]);
// Assert
$response->assertStatus(403);
$this->assertDatabaseHas(Client::class, [
'id' => $client->getKey(),
'name' => $client->name,
'organization_id' => $otherOrganization->getKey(),
]);
}
public function test_update_endpoint_updates_client(): void
{
// Arrange
$data = $this->createUserWithPermission([
'clients:update',
]);
$client = Client::factory()->forOrganization($data->organization)->create();
$clientFake = Client::factory()->make();
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.clients.update', [$data->organization->getKey(), $client->getKey()]), [
'name' => $clientFake->name,
]);
// Assert
$response->assertStatus(200);
$response->assertJson(fn (AssertableJson $json) => $json
->has('data')
->where('data.name', $clientFake->name)
);
$this->assertDatabaseHas(Client::class, [
'name' => $clientFake->name,
'organization_id' => $data->organization->getKey(),
]);
}
public function test_destroy_endpoint_fails_if_user_has_no_permission_to_delete_clients(): void
{
// Arrange
$data = $this->createUserWithPermission([
]);
$client = Client::factory()->forOrganization($data->organization)->create();
Passport::actingAs($data->user);
// Act
$response = $this->deleteJson(route('api.v1.clients.destroy', [$data->organization->getKey(), $client->getKey()]));
// Assert
$response->assertStatus(403);
}
public function test_destroy_endpoint_fails_if_user_is_not_part_of_client_organization(): void
{
// Arrange
$data = $this->createUserWithPermission([
'clients:delete',
]);
$otherOrganization = Organization::factory()->create();
$client = Client::factory()->forOrganization($otherOrganization)->create();
Passport::actingAs($data->user);
// Act
$response = $this->deleteJson(route('api.v1.clients.destroy', [$data->organization->getKey(), $client->getKey()]));
// Assert
$response->assertStatus(403);
$this->assertDatabaseHas(Client::class, [
'id' => $client->getKey(),
'name' => $client->name,
'organization_id' => $otherOrganization->getKey(),
]);
}
public function test_destroy_endpoint_deletes_client(): void
{
// Arrange
$data = $this->createUserWithPermission([
'clients:delete',
]);
$client = Client::factory()->forOrganization($data->organization)->create();
Passport::actingAs($data->user);
// Act
$response = $this->deleteJson(route('api.v1.clients.destroy', [$data->organization->getKey(), $client->getKey()]));
// Assert
$response->assertStatus(204);
$response->assertNoContent();
$this->assertDatabaseMissing(Client::class, [
'id' => $client->getKey(),
]);
}
}

View File

@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Endpoint\Api\V1;
use App\Models\Organization;
use Laravel\Passport\Passport;
class OrganizationEndpointTest extends ApiEndpointTestAbstract
{
public function test_show_endpoint_fails_if_user_has_no_permission_to_view_organizations(): void
{
// Arrange
$data = $this->createUserWithPermission([
]);
Passport::actingAs($data->user);
// Act
$response = $this->getJson(route('api.v1.organizations.show', [$data->organization->getKey()]));
// Assert
$response->assertStatus(403);
}
public function test_show_endpoint_returns_organization(): void
{
// Arrange
$data = $this->createUserWithPermission([
'organizations:view',
]);
Passport::actingAs($data->user);
// Act
$response = $this->getJson(route('api.v1.organizations.show', [$data->organization->getKey()]));
// Assert
$response->assertStatus(200);
$response->assertJsonPath('data.id', $data->organization->getKey());
}
public function test_update_endpoint_fails_if_user_has_no_permission_to_update_organizations(): void
{
// Arrange
$data = $this->createUserWithPermission([
]);
$organizationFake = Organization::factory()->make();
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.organizations.update', [$data->organization->getKey()]), [
'name' => $organizationFake->name,
]);
// Assert
$response->assertStatus(403);
}
public function test_update_endpoint_updates_project(): void
{
// Arrange
$data = $this->createUserWithPermission([
'organizations:update',
]);
$organizationFake = Organization::factory()->make();
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.organizations.update', [$data->organization->getKey()]), [
'name' => $organizationFake->name,
]);
// Assert
$response->assertStatus(200);
$this->assertDatabaseHas(Organization::class, [
'name' => $organizationFake->name,
]);
}
}

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Tests\Unit\Endpoint\Api\V1;
use App\Models\Client;
use App\Models\Organization;
use App\Models\Project;
use Laravel\Passport\Passport;
@@ -16,6 +17,7 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
$data = $this->createUserWithPermission([
]);
$projects = Project::factory()->forOrganization($data->organization)->createMany(4);
$projectsWithClients = Project::factory()->forOrganization($data->organization)->withClient()->createMany(4);
Passport::actingAs($data->user);
// Act
@@ -133,6 +135,33 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
]);
}
public function test_store_endpoint_creates_new_project_with_client(): void
{
// Arrange
$data = $this->createUserWithPermission([
'projects:create',
]);
$client = Client::factory()->forOrganization($data->organization)->create();
$project = Project::factory()->forOrganization($data->organization)->make();
Passport::actingAs($data->user);
// Act
$response = $this->postJson(route('api.v1.projects.store', [$data->organization->getKey()]), [
'name' => $project->name,
'color' => $project->color,
'client_id' => $client->getKey(),
]);
// Assert
$response->assertStatus(201);
$this->assertDatabaseHas(Project::class, [
'name' => $project->name,
'color' => $project->color,
'organization_id' => $project->organization_id,
'client_id' => $client->getKey(),
]);
}
public function test_update_endpoint_fails_if_user_is_not_part_of_project_organization(): void
{
// Arrange

View File

@@ -6,6 +6,7 @@ namespace Tests\Unit\Model;
use App\Models\Client;
use App\Models\Organization;
use App\Models\Project;
class ClientModelTest extends ModelTestAbstract
{
@@ -23,4 +24,22 @@ class ClientModelTest extends ModelTestAbstract
$this->assertNotNull($organizationRel);
$this->assertTrue($organizationRel->is($organization));
}
public function test_it_has_many_projects(): void
{
// Arrange
$client = Client::factory()->create();
$otherClient = Client::factory()->create();
$projects = Project::factory()->forClient($client)->createMany(4);
$projectsOtherClient = Project::factory()->forClient($otherClient)->createMany(4);
// Act
$client->refresh();
$projectsRel = $client->projects;
// Assert
$this->assertNotNull($projectsRel);
$this->assertCount(4, $projectsRel);
$this->assertTrue($projectsRel->first()->is($projects->first()));
}
}