2024-05-18 05:04:34 -07:00
|
|
|
<?php
|
|
|
|
|
2024-05-26 08:47:30 -07:00
|
|
|
namespace Tests\Feature\Api\Assets;
|
2024-05-18 05:04:34 -07:00
|
|
|
|
|
|
|
use Tests\TestCase;
|
2024-05-27 03:38:30 -07:00
|
|
|
use App\Models\User;
|
|
|
|
use App\Models\Asset;
|
2024-05-27 04:50:20 -07:00
|
|
|
use Illuminate\Http\UploadedFile;
|
2024-05-18 05:04:34 -07:00
|
|
|
|
|
|
|
class AssetFilesTest extends TestCase
|
|
|
|
{
|
2024-05-27 03:38:30 -07:00
|
|
|
public function testAssetApiAcceptsFileUpload()
|
2024-05-18 05:04:34 -07:00
|
|
|
{
|
2024-05-27 03:38:30 -07:00
|
|
|
// Upload a file to an asset
|
|
|
|
|
|
|
|
// Create an asset to work with
|
|
|
|
$asset = Asset::factory()->count(1)->create();
|
2024-05-27 04:50:20 -07:00
|
|
|
|
|
|
|
// Create a superuser to run this as
|
|
|
|
$user = User::factory()->superuser()->create();
|
|
|
|
|
|
|
|
//Upload a file
|
|
|
|
$this->actingAsForApi($user)
|
|
|
|
->post(
|
|
|
|
route('api.assets.files', ['asset_id' => $asset[0]["id"]]), [
|
|
|
|
'file' => [UploadedFile::fake()->create("test.jpg", 100)]
|
|
|
|
])
|
|
|
|
->assertOk();
|
2024-05-27 03:38:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAssetApiListsFiles()
|
|
|
|
{
|
|
|
|
// List all files on an asset
|
|
|
|
|
|
|
|
// Create an asset to work with
|
|
|
|
$asset = Asset::factory()->count(1)->create();
|
|
|
|
|
|
|
|
// Create a superuser to run this as
|
|
|
|
$user = User::factory()->superuser()->create();
|
|
|
|
$this->actingAsForApi($user)
|
|
|
|
->getJson(
|
|
|
|
route('api.assets.files', ['asset_id' => $asset[0]["id"]]))
|
|
|
|
->assertOk()
|
|
|
|
->assertJsonStructure([
|
|
|
|
'status',
|
|
|
|
'messages',
|
|
|
|
'payload',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAssetApiDownloadsFile()
|
|
|
|
{
|
|
|
|
// Download a file from an asset
|
2024-05-27 04:50:20 -07:00
|
|
|
|
|
|
|
// Create an asset to work with
|
|
|
|
$asset = Asset::factory()->count(1)->create();
|
|
|
|
|
|
|
|
// Create a superuser to run this as
|
|
|
|
$user = User::factory()->superuser()->create();
|
|
|
|
|
|
|
|
//Upload a file
|
|
|
|
$this->actingAsForApi($user)
|
|
|
|
->post(
|
|
|
|
route('api.assets.files', ['asset_id' => $asset[0]["id"]]), [
|
|
|
|
'file' => [UploadedFile::fake()->create("test.jpg", 100)]
|
|
|
|
])->assertOk();
|
|
|
|
|
|
|
|
// Get the file
|
|
|
|
$this->actingAsForApi($user)
|
|
|
|
->get(
|
|
|
|
route('api.assets.file', [
|
|
|
|
'asset_id' => $asset[0]["id"],
|
|
|
|
'file_id' => 1,
|
|
|
|
]))
|
|
|
|
->assertOk();
|
2024-05-27 03:38:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAssetApiDeletesFile()
|
|
|
|
{
|
|
|
|
// Delete a file from an asset
|
2024-05-27 04:50:20 -07:00
|
|
|
|
|
|
|
// Create an asset to work with
|
|
|
|
$asset = Asset::factory()->count(1)->create();
|
|
|
|
|
|
|
|
// Create a superuser to run this as
|
|
|
|
$user = User::factory()->superuser()->create();
|
|
|
|
|
|
|
|
//Upload a file
|
|
|
|
$this->actingAsForApi($user)
|
|
|
|
->post(
|
|
|
|
route('api.assets.files', ['asset_id' => $asset[0]["id"]]), [
|
|
|
|
'file' => [UploadedFile::fake()->create("test.jpg", 100)]
|
|
|
|
])
|
|
|
|
->assertOk();
|
|
|
|
|
|
|
|
// Delete the file
|
|
|
|
$this->actingAsForApi($user)
|
|
|
|
->delete(
|
|
|
|
route('api.assets.file', [
|
|
|
|
'asset_id' => $asset[0]["id"],
|
|
|
|
'file_id' => 1,
|
|
|
|
]))
|
|
|
|
->assertOk();
|
2024-05-18 05:04:34 -07:00
|
|
|
}
|
|
|
|
}
|