Skip to content

Commit

Permalink
test: media_format attribute and song resource pivot (#605)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyrch authored Oct 31, 2023
1 parent 3aa49ba commit 6fe645d
Show file tree
Hide file tree
Showing 14 changed files with 1,289 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

declare(strict_types=1);

namespace Http\Api\Pivot\Wiki\SongResource;

use App\Enums\Auth\CrudPermission;
use App\Models\Auth\User;
use App\Models\Wiki\Song;
use App\Models\Wiki\ExternalResource;
use App\Pivots\Wiki\SongResource;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;

/**
* Class SongResourceDestroyTest.
*/
class SongResourceDestroyTest extends TestCase
{
/**
* The Song Resource Destroy Endpoint shall be protected by sanctum.
*
* @return void
*/
public function testProtected(): void
{
$songResource = SongResource::factory()
->for(Song::factory())
->for(ExternalResource::factory(), SongResource::RELATION_RESOURCE)
->createOne();

$response = $this->delete(route('api.songresource.destroy', ['song' => $songResource->song, 'resource' => $songResource->resource]));

$response->assertUnauthorized();
}

/**
* The Song Resource Destroy Endpoint shall forbid users without the delete song & delete resource permissions.
*
* @return void
*/
public function testForbidden(): void
{
$songResource = SongResource::factory()
->for(Song::factory())
->for(ExternalResource::factory(), SongResource::RELATION_RESOURCE)
->createOne();

$user = User::factory()->createOne();

Sanctum::actingAs($user);

$response = $this->delete(route('api.songresource.destroy', ['song' => $songResource->song, 'resource' => $songResource->resource]));

$response->assertForbidden();
}

/**
* The Song Resource Destroy Endpoint shall return an error if the song resource does not exist.
*
* @return void
*/
public function testNotFound(): void
{
$song = Song::factory()->createOne();
$resource = ExternalResource::factory()->createOne();

$user = User::factory()
->withPermissions(
CrudPermission::DELETE->format(Song::class),
CrudPermission::DELETE->format(ExternalResource::class)
)
->createOne();

Sanctum::actingAs($user);

$response = $this->delete(route('api.songresource.destroy', ['song' => $song, 'resource' => $resource]));

$response->assertNotFound();
}

/**
* The Song Resource Destroy Endpoint shall delete the song resource.
*
* @return void
*/
public function testDeleted(): void
{
$songResource = SongResource::factory()
->for(Song::factory())
->for(ExternalResource::factory(), SongResource::RELATION_RESOURCE)
->createOne();

$user = User::factory()
->withPermissions(
CrudPermission::DELETE->format(Song::class),
CrudPermission::DELETE->format(ExternalResource::class)
)
->createOne();

Sanctum::actingAs($user);

$response = $this->delete(route('api.songresource.destroy', ['song' => $songResource->song, 'resource' => $songResource->resource]));

$response->assertOk();
static::assertModelMissing($songResource);
}
}
Loading

0 comments on commit 6fe645d

Please sign in to comment.