license: built full stack, test and documentation
Some checks failed
docs / build (push) Has been cancelled
docs / deploy (push) Has been cancelled

This commit is contained in:
Giuseppe Naponiello
2026-07-03 12:47:47 +02:00
parent f8206b5840
commit 271acacb1d
39 changed files with 4078 additions and 57 deletions

View File

@@ -0,0 +1,177 @@
<?php
namespace Tests\Feature;
use App\Models\Lists\License;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class LicenseControllerTest extends TestCase
{
use RefreshDatabase;
// --- Lettura (pubblica) ---------------------------------------------------
public function test_index_is_public_and_ordered_by_sort_order(): void
{
License::factory()->create(['acronym' => 'LAST', 'sort_order' => 90]);
License::factory()->create(['acronym' => 'FIRST', 'sort_order' => 10]);
$this->getJson('/api/licenses')
->assertOk()
->assertJsonCount(2, 'data')
->assertJsonPath('data.0.acronym', 'FIRST')
->assertJsonPath('data.1.acronym', 'LAST');
}
public function test_index_filters_by_record_scope(): void
{
License::factory()->count(2)->create();
$open = License::factory()->forRecord()->create();
$this->getJson('/api/licenses?scope=record')
->assertOk()
->assertJsonCount(1, 'data')
->assertJsonPath('data.0.id', $open->id);
}
public function test_index_filters_by_media_scope(): void
{
License::factory()->create(['for_media' => false, 'for_record' => true]);
$media = License::factory()->create();
$this->getJson('/api/licenses?scope=media')
->assertOk()
->assertJsonCount(1, 'data')
->assertJsonPath('data.0.id', $media->id);
}
public function test_index_filters_by_active(): void
{
License::factory()->inactive()->create();
$active = License::factory()->create();
$this->getJson('/api/licenses?active=1')
->assertOk()
->assertJsonCount(1, 'data')
->assertJsonPath('data.0.id', $active->id);
}
public function test_index_rejects_an_unknown_scope(): void
{
$this->getJson('/api/licenses?scope=banana')
->assertStatus(422)
->assertJsonValidationErrors('scope');
}
public function test_show_is_public(): void
{
$license = License::factory()->create();
$this->getJson("/api/licenses/{$license->id}")
->assertOk()
->assertJsonPath('data.id', $license->id)
->assertJsonStructure(['data' => [
'id', 'name', 'acronym', 'spdx_id', 'kind', 'description',
'uri', 'file', 'for_record', 'for_media', 'sort_order', 'active',
]]);
}
// --- Stato d'uso (tier.app) ------------------------------------------------
public function test_usage_requires_authentication(): void
{
$license = License::factory()->create();
$this->getJson("/api/licenses/{$license->id}/usage")->assertUnauthorized();
}
public function test_usage_reports_an_unreferenced_license_as_free(): void
{
$this->actingAs($this->operationalUser(), 'sanctum');
$license = License::factory()->create();
$this->getJson("/api/licenses/{$license->id}/usage")
->assertOk()
->assertJsonPath('in_use', false);
}
// --- Scrittura (tier.admin) -------------------------------------------------
public function test_admin_can_create_a_license(): void
{
$this->actingAs($this->adminUser(), 'sanctum');
$this->postJson('/api/licenses', [
'name' => 'In Copyright - Non-Commercial Use Permitted',
'acronym' => 'InC-NC',
'kind' => 'rights_statement',
'description' => 'Copyrighted, but non-commercial use is permitted.',
'uri' => 'http://rightsstatements.org/vocab/InC-NC/1.0/',
])
->assertCreated()
->assertJsonPath('data.acronym', 'InC-NC');
$this->assertDatabaseHas('licenses', ['acronym' => 'InC-NC', 'kind' => 'rights_statement']);
}
public function test_create_rejects_a_duplicate_acronym(): void
{
$this->actingAs($this->adminUser(), 'sanctum');
License::factory()->create(['acronym' => 'CC BY 4.0']);
$this->postJson('/api/licenses', [
'name' => 'Attribution 4.0 International',
'acronym' => 'CC BY 4.0',
'kind' => 'license',
])
->assertStatus(422)
->assertJsonValidationErrors('acronym');
}
public function test_create_rejects_an_unknown_kind(): void
{
$this->actingAs($this->adminUser(), 'sanctum');
$this->postJson('/api/licenses', [
'name' => 'Some License',
'acronym' => 'SL',
'kind' => 'banana',
])
->assertStatus(422)
->assertJsonValidationErrors('kind');
}
public function test_admin_can_retire_a_license_with_a_partial_update(): void
{
$this->actingAs($this->adminUser(), 'sanctum');
$license = License::factory()->create();
$this->patchJson("/api/licenses/{$license->id}", ['active' => false])
->assertOk()
->assertJsonPath('data.active', false);
$this->assertSame($license->acronym, $license->fresh()->acronym);
}
public function test_admin_can_delete_an_unused_license(): void
{
$this->actingAs($this->adminUser(), 'sanctum');
$license = License::factory()->create();
$this->deleteJson("/api/licenses/{$license->id}")->assertOk();
$this->assertDatabaseMissing('licenses', ['id' => $license->id]);
}
public function test_a_non_admin_cannot_write(): void
{
$this->actingAs($this->operationalUser(), 'sanctum');
$license = License::factory()->create();
$this->postJson('/api/licenses', ['name' => 'X', 'acronym' => 'X', 'kind' => 'license'])
->assertForbidden();
$this->patchJson("/api/licenses/{$license->id}", ['active' => false])->assertForbidden();
$this->deleteJson("/api/licenses/{$license->id}")->assertForbidden();
}
}