Added shareable reports

This commit is contained in:
Constantin Graf
2024-08-13 16:21:59 +02:00
committed by Constantin Graf
parent 0ee0175f04
commit c03aad1abd
30 changed files with 1912 additions and 141 deletions

View File

@@ -0,0 +1,123 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Console\Commands\Report;
use App\Console\Commands\Report\ReportSetExpiredToPrivateCommand;
use App\Models\Report;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use Tests\TestCaseWithDatabase;
#[CoversClass(ReportSetExpiredToPrivateCommand::class)]
#[UsesClass(ReportSetExpiredToPrivateCommand::class)]
class ReportSetExpiredToPrivateCommandTest extends TestCaseWithDatabase
{
public function test_command_sets_expired_reports_to_private(): void
{
// Arrange
$reportPrivateExpired = Report::factory()->private()->create([
'public_until' => now()->subDay(),
]);
$reportPublicExpired = Report::factory()->public()->create([
'public_until' => now()->subDay(),
]);
$reportPrivateNoExpiration = Report::factory()->private()->create([
'public_until' => null,
]);
$reportPublicNoExpiration = Report::factory()->public()->create([
'public_until' => null,
]);
$reportPrivateNotExpired = Report::factory()->private()->create([
'public_until' => now()->addDay(),
]);
$reportPublicNotExpired = Report::factory()->public()->create([
'public_until' => now()->addDay(),
]);
// Act
$exitCode = $this->withoutMockingConsoleOutput()->artisan('report:set-expired-to-private');
// Assert
$this->assertSame(Command::SUCCESS, $exitCode);
$output = Artisan::output();
$this->assertStringContainsString('Makes public reports private if the public_until date has passed...', $output);
$this->assertStringContainsString('Make report "'.$reportPrivateExpired->name.'" ('.$reportPrivateExpired->getKey().') private, expired: '.$reportPrivateExpired->public_until->toIso8601ZuluString().' ('.$reportPrivateExpired->public_until->diffForHumans().')', $output);
$this->assertStringContainsString('Make report "'.$reportPublicExpired->name.'" ('.$reportPublicExpired->getKey().') private, expired: '.$reportPublicExpired->public_until->toIso8601ZuluString().' ('.$reportPublicExpired->public_until->diffForHumans().')', $output);
$this->assertStringContainsString('Finished setting 2 expired reports to private...', $output);
$reportPrivateExpired->refresh();
$reportPublicExpired->refresh();
$reportPrivateNoExpiration->refresh();
$reportPublicNoExpiration->refresh();
$reportPrivateNotExpired->refresh();
$reportPublicNotExpired->refresh();
$this->assertFalse($reportPrivateExpired->is_public);
$this->assertNull($reportPrivateExpired->share_secret);
$this->assertFalse($reportPublicExpired->is_public);
$this->assertNull($reportPublicExpired->share_secret);
$this->assertFalse($reportPrivateNoExpiration->is_public);
$this->assertNull($reportPrivateNoExpiration->share_secret);
$this->assertTrue($reportPublicNoExpiration->is_public);
$this->assertNotNull($reportPublicNoExpiration->share_secret);
$this->assertFalse($reportPrivateNotExpired->is_public);
$this->assertNull($reportPrivateNotExpired->share_secret);
$this->assertTrue($reportPublicNotExpired->is_public);
$this->assertNotNull($reportPublicNotExpired->share_secret);
}
public function test_command_sets_expired_reports_to_private_in_dry_run_mode(): void
{
// Arrange
$reportPrivateExpired = Report::factory()->private()->create([
'public_until' => now()->subDay(),
]);
$reportPublicExpired = Report::factory()->public()->create([
'public_until' => now()->subDay(),
]);
$reportPrivateNoExpiration = Report::factory()->private()->create([
'public_until' => null,
]);
$reportPublicNoExpiration = Report::factory()->public()->create([
'public_until' => null,
]);
$reportPrivateNotExpired = Report::factory()->private()->create([
'public_until' => now()->addDay(),
]);
$reportPublicNotExpired = Report::factory()->public()->create([
'public_until' => now()->addDay(),
]);
// Act
$exitCode = $this->withoutMockingConsoleOutput()->artisan('report:set-expired-to-private', ['--dry-run' => true]);
// Assert
$this->assertSame(Command::SUCCESS, $exitCode);
$output = Artisan::output();
$this->assertStringContainsString('Makes public reports private if the public_until date has passed...', $output);
$this->assertStringContainsString('Running in dry-run mode. Nothing will be saved to the database.', $output);
$this->assertStringContainsString('Make report "'.$reportPrivateExpired->name.'" ('.$reportPrivateExpired->getKey().') private, expired: '.$reportPrivateExpired->public_until->toIso8601ZuluString().' ('.$reportPrivateExpired->public_until->diffForHumans().')', $output);
$this->assertStringContainsString('Make report "'.$reportPublicExpired->name.'" ('.$reportPublicExpired->getKey().') private, expired: '.$reportPublicExpired->public_until->toIso8601ZuluString().' ('.$reportPublicExpired->public_until->diffForHumans().')', $output);
$this->assertStringContainsString('Finished setting 2 expired reports to private...', $output);
$reportPrivateExpired->refresh();
$reportPublicExpired->refresh();
$reportPrivateNoExpiration->refresh();
$reportPublicNoExpiration->refresh();
$reportPrivateNotExpired->refresh();
$reportPublicNotExpired->refresh();
$this->assertFalse($reportPrivateExpired->is_public);
$this->assertNull($reportPrivateExpired->share_secret);
$this->assertTrue($reportPublicExpired->is_public);
$this->assertNotNull($reportPublicExpired->share_secret);
$this->assertFalse($reportPrivateNoExpiration->is_public);
$this->assertNull($reportPrivateNoExpiration->share_secret);
$this->assertTrue($reportPublicNoExpiration->is_public);
$this->assertNotNull($reportPublicNoExpiration->share_secret);
$this->assertFalse($reportPrivateNotExpired->is_public);
$this->assertNull($reportPrivateNotExpired->share_secret);
$this->assertTrue($reportPublicNotExpired->is_public);
$this->assertNotNull($reportPublicNotExpired->share_secret);
}
}

View File

@@ -59,4 +59,17 @@ class SelfHostGenerateKeysCommandTest extends TestCase
$this->assertStringContainsString("PASSPORT_PRIVATE_KEY: |\n -----BEGIN PRIVATE KEY-----", $output);
$this->assertStringContainsString("PASSPORT_PUBLIC_KEY: |\n -----BEGIN PUBLIC KEY-----", $output);
}
public function test_generates_app_fail_if_attribute_format_is_invalid(): void
{
// Arrange
// Act
$exitCode = $this->withoutMockingConsoleOutput()->artisan('self-host:generate-keys --format=invalid');
// Assert
$this->assertSame(Command::FAILURE, $exitCode);
$output = Artisan::output();
$this->assertSame("Invalid format\n", $output);
}
}

View File

@@ -200,7 +200,7 @@ class OrganizationEndpointTest extends ApiEndpointTestAbstract
Passport::actingAs($data->user);
// Act
$response = $this->withoutExceptionHandling()->putJson(route('api.v1.organizations.update', [$data->organization->getKey()]), [
$response = $this->putJson(route('api.v1.organizations.update', [$data->organization->getKey()]), [
'name' => $organizationFake->name,
'billable_rate' => $organizationFake->billable_rate,
]);

View File

@@ -23,7 +23,7 @@ class ProjectMemberEndpointTest extends ApiEndpointTestAbstract
// Arrange
$data = $this->createUserWithPermission();
$project = Project::factory()->forOrganization($data->organization)->create();
$projectMembers = ProjectMember::factory()->forProject($project)->createMany(4);
ProjectMember::factory()->forProject($project)->createMany(4);
Passport::actingAs($data->user);
// Act
@@ -46,7 +46,7 @@ class ProjectMemberEndpointTest extends ApiEndpointTestAbstract
'project-members:view',
]);
$project = Project::factory()->forOrganization($otherData->organization)->create();
$projectMembers = ProjectMember::factory()->forProject($project)->createMany(4);
ProjectMember::factory()->forProject($project)->createMany(4);
Passport::actingAs($data->user);
// Act
@@ -66,7 +66,7 @@ class ProjectMemberEndpointTest extends ApiEndpointTestAbstract
'project-members:view',
]);
$project = Project::factory()->forOrganization($data->organization)->create();
$projectMembers = ProjectMember::factory()->forProject($project)->createMany(4);
ProjectMember::factory()->forProject($project)->createMany(4);
Passport::actingAs($data->user);
// Act

View File

@@ -0,0 +1,113 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Endpoint\Api\V1\Public;
use App\Models\Report;
use Tests\Unit\Endpoint\Api\V1\ApiEndpointTestAbstract;
class PublicReportEndpointTest extends ApiEndpointTestAbstract
{
public function test_show_fails_with_not_found_if_secret_is_incorrect(): void
{
// Arrange
Report::factory()->public()->create();
// Act
$response = $this->getJson(route('api.v1.public.reports.show'), [
'X-Api-Key' => 'incorrect-secret',
]);
// Assert
$response->assertNotFound();
}
public function test_show_fails_with_not_found_if_no_secret_is_provided(): void
{
// Arrange
Report::factory()->public()->create();
// Act
$response = $this->getJson(route('api.v1.public.reports.show'));
// Assert
$response->assertNotFound();
}
public function test_show_fails_with_not_found_if_report_is_not_public(): void
{
// Arrange
$report = Report::factory()->private()->create();
// Act
$response = $this->getJson(route('api.v1.public.reports.show'), [
'X-Api-Key' => $report->share_secret,
]);
// Assert
$response->assertNotFound();
}
public function test_show_fails_with_not_found_if_report_is_expired(): void
{
// Arrange
$report = Report::factory()->public()->create([
'public_until' => now()->subDay(),
]);
// Act
$response = $this->getJson(route('api.v1.public.reports.show'), [
'X-Api-Key' => $report->share_secret,
]);
// Assert
$response->assertNotFound();
}
public function test_show_returns_detailed_information_about_the_report(): void
{
// Arrange
$report = Report::factory()->public()->create([
'public_until' => null,
]);
// Act
$response = $this->getJson(route('api.v1.public.reports.show'), [
'X-Api-Key' => $report->share_secret,
]);
// Assert
$response->assertOk();
$response->assertJsonFragment([
'id' => $report->id,
'name' => $report->name,
'description' => $report->description,
'is_public' => $report->is_public,
'public_until' => $report->public_until?->toIso8601ZuluString(),
]);
}
public function test_show_returns_detailed_information_about_the_report_with_not_expired_expiration_date(): void
{
// Arrange
$report = Report::factory()->public()->create([
'public_until' => now()->addDay(),
]);
// Act
$response = $this->getJson(route('api.v1.public.reports.show'), [
'X-Api-Key' => $report->share_secret,
]);
// Assert
$response->assertOk();
$response->assertJsonFragment([
'id' => $report->id,
'name' => $report->name,
'description' => $report->description,
'is_public' => $report->is_public,
'public_until' => $report->public_until?->toIso8601ZuluString(),
]);
}
}

View File

@@ -0,0 +1,490 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Endpoint\Api\V1;
use App\Enums\TimeEntryAggregationType;
use App\Http\Controllers\Api\V1\ReportController;
use App\Models\Report;
use Illuminate\Support\Carbon;
use Illuminate\Testing\Fluent\AssertableJson;
use Laravel\Passport\Passport;
use PHPUnit\Framework\Attributes\UsesClass;
#[UsesClass(ReportController::class)]
class ReportEndpointTest extends ApiEndpointTestAbstract
{
public function test_index_endpoint_fails_if_user_does_not_have_permission_to_view_reports(): void
{
// Arrange
$data = $this->createUserWithPermission();
Report::factory()->forOrganization($data->organization)->createMany(4);
Passport::actingAs($data->user);
// Act
$response = $this->getJson(route('api.v1.reports.index', ['organization' => $data->organization->getKey()]));
// Assert
$response->assertForbidden();
}
public function test_index_endpoint_returns_list_of_all_reports_of_organization_ordered_by_created_at_desc_per_default(): void
{
// Arrange
$data = $this->createUserWithPermission([
'reports:view',
]);
Report::factory()->forOrganization($data->organization)->randomCreatedAt()->createMany(4);
Passport::actingAs($data->user);
// Act
$response = $this->getJson(route('api.v1.reports.index', [$data->organization->getKey()]));
// Assert
$response->assertStatus(200);
$response->assertJsonCount(4, 'data');
$reports = Report::query()->orderBy('created_at', 'desc')->get();
$response->assertJson(fn (AssertableJson $json) => $json
->has('data')
->has('links')
->has('meta')
->count('data', 4)
->where('data.0.id', $reports->get(0)->getKey())
->where('data.1.id', $reports->get(1)->getKey())
->where('data.2.id', $reports->get(2)->getKey())
->where('data.3.id', $reports->get(3)->getKey())
);
}
public function test_store_endpoint_fails_if_user_has_no_permission_to_create_report(): void
{
// Arrange
$data = $this->createUserWithPermission();
Passport::actingAs($data->user);
// Act
$response = $this->postJson(route('api.v1.reports.store', [$data->organization->getKey()]), [
'name' => 'Test Report',
'is_public' => false,
'properties' => [
'group' => TimeEntryAggregationType::Project->value,
'sub_group' => TimeEntryAggregationType::Task->value,
],
]);
// Assert
$response->assertForbidden();
}
public function test_store_endpoint_creates_new_report_with_minimal_properties(): void
{
// Arrange
$data = $this->createUserWithPermission([
'reports:create',
]);
Passport::actingAs($data->user);
// Act
$response = $this->postJson(route('api.v1.reports.store', [$data->organization->getKey()]), [
'name' => 'Test Report',
'is_public' => false,
'properties' => [
'group' => TimeEntryAggregationType::Project->value,
'sub_group' => TimeEntryAggregationType::Task->value,
],
]);
// Assert
$response->assertStatus(201);
$response->assertJson(fn (AssertableJson $json) => $json
->has('data')
->where('data.name', 'Test Report')
->where('data.description', null)
->where('data.is_public', false)
->where('data.shareable_link', null)
->where('data.properties.group', TimeEntryAggregationType::Project->value)
->where('data.properties.sub_group', TimeEntryAggregationType::Task->value)
);
}
public function test_store_endpoint_creates_new_report_with_all_properties(): void
{
// Arrange
$data = $this->createUserWithPermission([
'reports:create',
]);
Passport::actingAs($data->user);
// Act
$response = $this->postJson(route('api.v1.reports.store', [$data->organization->getKey()]), [
'name' => 'Test Report',
'description' => 'Test description',
'is_public' => true,
'public_until' => Carbon::now()->addDays(30)->toIso8601ZuluString(),
'properties' => [
'start' => Carbon::now()->subDays(30)->toIso8601ZuluString(),
'end' => Carbon::now()->toIso8601ZuluString(),
'active' => true,
'member_ids' => [],
'billable' => true,
'client_ids' => [],
'project_ids' => [],
'tag_ids' => [],
'task_ids' => [],
'group' => TimeEntryAggregationType::Project->value,
'sub_group' => TimeEntryAggregationType::Task->value,
],
]);
// Assert
$response->assertStatus(201);
/** @var Report $report */
$report = Report::query()->findOrFail($response->json('data.id'));
$response->assertJson(fn (AssertableJson $json) => $json
->has('data')
->where('data.name', 'Test Report')
->where('data.description', 'Test description')
->where('data.is_public', true)
->where('data.shareable_link', $report->getShareableLink())
->where('data.properties.group', TimeEntryAggregationType::Project->value)
->where('data.properties.sub_group', TimeEntryAggregationType::Task->value)
);
}
public function test_update_endpoint_fails_if_user_has_no_permission_to_update_report(): void
{
// Arrange
$data = $this->createUserWithPermission();
$report = Report::factory()->forOrganization($data->organization)->create();
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.reports.update', [$data->organization->getKey(), $report->getKey()]), [
'name' => 'Updated Report',
]);
// Assert
$response->assertForbidden();
}
public function test_update_endpoint_fails_if_report_does_not_exist(): void
{
// Arrange
$data = $this->createUserWithPermission([
'reports:update',
]);
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.reports.update', [$data->organization->getKey(), 1]), [
'name' => 'Updated Report',
]);
// Assert
$response->assertNotFound();
}
public function test_update_endpoint_fails_if_report_does_not_belong_to_organization(): void
{
// Arrange
$data = $this->createUserWithPermission([
'reports:update',
]);
$report = Report::factory()->create();
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.reports.update', [$data->organization->getKey(), $report->getKey()]), [
'name' => 'Updated Report',
]);
// Assert
$response->assertForbidden();
}
public function test_update_endpoint_can_update_only_the_name_of_the_report(): void
{
// Arrange
$data = $this->createUserWithPermission([
'reports:update',
]);
$report = Report::factory()->forOrganization($data->organization)->create();
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.reports.update', [$data->organization->getKey(), $report->getKey()]), [
'name' => 'Updated Report',
]);
// Assert
$report->refresh();
$this->assertSame('Updated Report', $report->name);
$response->assertStatus(200);
$response->assertJson(fn (AssertableJson $json) => $json
->has('data')
->where('data.name', 'Updated Report')
);
}
public function test_update_endpoint_can_update_only_the_description_of_the_report(): void
{
// Arrange
$data = $this->createUserWithPermission([
'reports:update',
]);
$report = Report::factory()->forOrganization($data->organization)->create();
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.reports.update', [$data->organization->getKey(), $report->getKey()]), [
'description' => 'Updated description',
]);
// Assert
$report->refresh();
$this->assertSame('Updated description', $report->description);
$response->assertStatus(200);
$response->assertJson(fn (AssertableJson $json) => $json
->has('data')
->where('data.description', 'Updated description')
);
}
public function test_update_endpoint_can_set_a_report_to_public_which_generates_a_new_secret(): void
{
// Arrange
$data = $this->createUserWithPermission([
'reports:update',
]);
$report = Report::factory()->private()->forOrganization($data->organization)->create();
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.reports.update', [$data->organization->getKey(), $report->getKey()]), [
'is_public' => true,
]);
// Assert
$report->refresh();
$this->assertTrue($report->is_public);
$this->assertNotNull($report->share_secret);
$response->assertStatus(200);
$response->assertJson(fn (AssertableJson $json) => $json
->has('data')
->where('data.is_public', true)
->where('data.shareable_link', $report->getShareableLink())
);
}
public function test_update_endpoint_can_set_a_report_to_private_which_resets_the_secret(): void
{
// Arrange
$data = $this->createUserWithPermission([
'reports:update',
]);
$report = Report::factory()->public()->forOrganization($data->organization)->create();
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.reports.update', [$data->organization->getKey(), $report->getKey()]), [
'is_public' => false,
]);
// Assert
$report->refresh();
$this->assertFalse($report->is_public);
$this->assertNull($report->share_secret);
$response->assertStatus(200);
$response->assertJson(fn (AssertableJson $json) => $json
->has('data')
->where('data.is_public', false)
->where('data.shareable_link', null)
);
}
public function test_update_endpoint_does_not_change_the_secret_of_a_public_report_if_it_is_set_to_public_again(): void
{
// Arrange
$data = $this->createUserWithPermission([
'reports:update',
]);
$report = Report::factory()->public()->forOrganization($data->organization)->create();
$secret = $report->share_secret;
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.reports.update', [$data->organization->getKey(), $report->getKey()]), [
'is_public' => true,
]);
// Assert
$report->refresh();
$this->assertTrue($report->is_public);
$this->assertSame($secret, $report->share_secret);
$response->assertStatus(200);
$response->assertJson(fn (AssertableJson $json) => $json
->has('data')
->where('data.is_public', true)
->where('data.shareable_link', $report->getShareableLink())
);
}
public function test_update_endpoint_can_update_the_report_all_properties_set(): void
{
// Arrange
$data = $this->createUserWithPermission([
'reports:update',
]);
$report = Report::factory()->forOrganization($data->organization)->create();
Passport::actingAs($data->user);
// Act
$response = $this->putJson(route('api.v1.reports.update', [$data->organization->getKey(), $report->getKey()]), [
'name' => 'Updated Report',
'description' => 'Updated description',
'is_public' => true,
'public_until' => Carbon::now()->addDays(30)->toIso8601ZuluString(),
]);
// Assert
$response->assertStatus(200);
$response->assertJson(fn (AssertableJson $json) => $json
->has('data')
->where('data.name', 'Updated Report')
->where('data.description', 'Updated description')
->where('data.is_public', true)
->where('data.properties.group', TimeEntryAggregationType::Project->value)
->where('data.properties.sub_group', TimeEntryAggregationType::Task->value)
);
}
public function test_show_endpoint_fails_if_user_has_no_permission_to_view_report(): void
{
// Arrange
$data = $this->createUserWithPermission();
$report = Report::factory()->forOrganization($data->organization)->create();
Passport::actingAs($data->user);
// Act
$response = $this->getJson(route('api.v1.reports.show', [$data->organization->getKey(), $report->getKey()]));
// Assert
$response->assertForbidden();
}
public function test_show_endpoint_fails_if_report_does_not_exist(): void
{
// Arrange
$data = $this->createUserWithPermission([
'reports:view',
]);
Passport::actingAs($data->user);
// Act
$response = $this->getJson(route('api.v1.reports.show', [$data->organization->getKey(), 1]));
// Assert
$response->assertNotFound();
}
public function test_show_endpoint_fails_if_report_does_not_belong_to_organization(): void
{
// Arrange
$data = $this->createUserWithPermission([
'reports:view',
]);
$report = Report::factory()->create();
Passport::actingAs($data->user);
// Act
$response = $this->getJson(route('api.v1.reports.show', [$data->organization->getKey(), $report->getKey()]));
// Assert
$response->assertForbidden();
}
public function test_show_endpoint_returns_detailed_report(): void
{
// Arrange
$data = $this->createUserWithPermission([
'reports:view',
]);
$report = Report::factory()->forOrganization($data->organization)->create();
Passport::actingAs($data->user);
// Act
$response = $this->getJson(route('api.v1.reports.show', [$data->organization->getKey(), $report->getKey()]));
// Assert
$response->assertStatus(200);
$response->assertJson(fn (AssertableJson $json) => $json
->has('data')
->where('data.id', $report->getKey())
);
}
public function test_destroy_endpoint_fails_if_user_has_no_permission_to_delete_report(): void
{
// Arrange
$data = $this->createUserWithPermission();
$report = Report::factory()->forOrganization($data->organization)->create();
Passport::actingAs($data->user);
// Act
$response = $this->deleteJson(route('api.v1.reports.destroy', [$data->organization->getKey(), $report->getKey()]));
// Assert
$response->assertForbidden();
}
public function test_destroy_endpoint_fails_if_report_belongs_to_another_organization(): void
{
// Arrange
$data = $this->createUserWithPermission([
'reports:delete',
]);
$report = Report::factory()->create();
Passport::actingAs($data->user);
// Act
$response = $this->deleteJson(route('api.v1.reports.destroy', [$data->organization->getKey(), $report->getKey()]));
// Assert
$response->assertForbidden();
}
public function test_destroy_endpoint_fails_if_report_does_not_exist(): void
{
// Arrange
$data = $this->createUserWithPermission([
'reports:delete',
]);
Passport::actingAs($data->user);
// Act
$response = $this->deleteJson(route('api.v1.reports.destroy', [$data->organization->getKey(), 1]));
// Assert
$response->assertNotFound();
}
public function test_destroy_endpoint_deletes_a_report(): void
{
// Arrange
$data = $this->createUserWithPermission([
'reports:delete',
]);
$report = Report::factory()->forOrganization($data->organization)->create();
Passport::actingAs($data->user);
// Act
$response = $this->deleteJson(route('api.v1.reports.destroy', [$data->organization->getKey(), $report->getKey()]));
// Assert
$response->assertNoContent();
$this->assertDatabaseMissing(Report::class, [
'id' => $report->getKey(),
]);
}
}

View File

@@ -930,6 +930,25 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
$response->assertForbidden();
}
public function test_aggregate_endpoint_fails_if_request_has_sub_group_but_no_group(): void
{
// Arrange
$data = $this->createUserWithPermission([
'time-entries:view:all',
]);
Passport::actingAs($data->user);
// Act
$response = $this->getJson(route('api.v1.time-entries.aggregate', [
$data->organization->getKey(),
'sub_group' => TimeEntryAggregationType::Task->value,
]));
// Assert
$response->assertStatus(422);
$response->assertJsonValidationErrorFor('group');
}
public function test_aggregate_endpoint_works_for_user_with_only_access_to_own_time_entries(): void
{
// Arrange

View File

@@ -0,0 +1,83 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Model;
use App\Models\Organization;
use App\Models\Report;
use App\Service\ReportService;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
#[CoversClass(Report::class)]
#[UsesClass(Report::class)]
class ReportModelTest extends ModelTestAbstract
{
public function test_it_belongs_to_a_organization(): void
{
// Arrange
$organization = Organization::factory()->create();
$report = Report::factory()->forOrganization($organization)->create();
// Act
$report->refresh();
$organizationRel = $report->organization;
// Assert
$this->assertNotNull($organizationRel);
$this->assertTrue($organizationRel->is($organization));
}
public function test_shareable_link_is_null_when_report_is_private_but_share_secret_exists(): void
{
// Arrange
$report = Report::factory()->private()->create([
'share_secret' => app(ReportService::class)->generateSecret(),
]);
// Act
$report->refresh();
// Assert
$this->assertNull($report->getShareableLink());
}
public function test_shareable_link_is_null_when_report_is_public_but_share_secret_is_null(): void
{
// Arrange
$report = Report::factory()->public()->create([
'share_secret' => null,
]);
// Act
$report->refresh();
// Assert
$this->assertNull($report->getShareableLink());
}
public function test_shareable_link_is_null_when_report_is_public(): void
{
// Arrange
$report = Report::factory()->public()->create();
// Act
$report->refresh();
// Assert
$this->assertNotNull($report->getShareableLink());
}
public function test_shareable_link_is_url_to_web_endpoint_when_report_is_public(): void
{
// Arrange
$report = Report::factory()->public()->create();
// Act
$report->refresh();
// Assert
$this->assertSame(url('/shared-report#'.$report->share_secret), $report->getShareableLink());
}
}