2024-05-18 05:04:34 -07:00
|
|
|
<?php
|
|
|
|
|
2024-06-13 14:25:25 -07:00
|
|
|
namespace Tests\Feature\Assets\Api;
|
2024-05-18 05:04:34 -07:00
|
|
|
|
2024-05-27 03:38:30 -07:00
|
|
|
use App\Models\Asset;
|
2024-06-13 14:25:25 -07:00
|
|
|
use App\Models\User;
|
2024-05-27 04:50:20 -07:00
|
|
|
use Illuminate\Http\UploadedFile;
|
2024-06-13 14:25:25 -07:00
|
|
|
use Tests\TestCase;
|
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(
|
2024-07-12 12:58:17 -07:00
|
|
|
route('api.assets.files.store', ['asset_id' => $asset[0]["id"]]), [
|
2024-05-27 04:50:20 -07:00
|
|
|
'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();
|
2024-05-27 05:08:18 -07:00
|
|
|
|
|
|
|
// List the files
|
2024-05-27 03:38:30 -07:00
|
|
|
$this->actingAsForApi($user)
|
|
|
|
->getJson(
|
2024-07-12 12:58:17 -07:00
|
|
|
route('api.assets.files.index', ['asset_id' => $asset[0]["id"]]))
|
2024-05-27 03:38:30 -07:00
|
|
|
->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(
|
2024-07-12 12:58:17 -07:00
|
|
|
route('api.assets.files.store', ['asset_id' => $asset[0]["id"]]), [
|
2024-05-27 04:50:20 -07:00
|
|
|
'file' => [UploadedFile::fake()->create("test.jpg", 100)]
|
2024-05-27 05:08:18 -07:00
|
|
|
])
|
|
|
|
->assertOk();
|
|
|
|
|
|
|
|
// List the files to get the file ID
|
|
|
|
$result = $this->actingAsForApi($user)
|
|
|
|
->getJson(
|
2024-07-12 12:58:17 -07:00
|
|
|
route('api.assets.files.index', ['asset_id' => $asset[0]["id"]]))
|
2024-05-27 05:08:18 -07:00
|
|
|
->assertOk();
|
2024-05-27 04:50:20 -07:00
|
|
|
|
|
|
|
// Get the file
|
|
|
|
$this->actingAsForApi($user)
|
|
|
|
->get(
|
2024-07-12 12:58:17 -07:00
|
|
|
route('api.assets.files.show', [
|
2024-05-27 04:50:20 -07:00
|
|
|
'asset_id' => $asset[0]["id"],
|
2024-05-27 05:08:18 -07:00
|
|
|
'file_id' => $result->decodeResponseJson()->json()["payload"][0]["id"],
|
2024-05-27 04:50:20 -07:00
|
|
|
]))
|
|
|
|
->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(
|
2024-07-12 12:58:17 -07:00
|
|
|
route('api.assets.files.store', ['asset_id' => $asset[0]["id"]]), [
|
2024-05-27 04:50:20 -07:00
|
|
|
'file' => [UploadedFile::fake()->create("test.jpg", 100)]
|
|
|
|
])
|
|
|
|
->assertOk();
|
|
|
|
|
2024-05-27 05:08:18 -07:00
|
|
|
// List the files to get the file ID
|
|
|
|
$result = $this->actingAsForApi($user)
|
|
|
|
->getJson(
|
2024-07-12 12:58:17 -07:00
|
|
|
route('api.assets.files.index', ['asset_id' => $asset[0]["id"]]))
|
2024-05-27 05:08:18 -07:00
|
|
|
->assertOk();
|
|
|
|
|
2024-05-27 04:50:20 -07:00
|
|
|
// Delete the file
|
|
|
|
$this->actingAsForApi($user)
|
|
|
|
->delete(
|
2024-07-12 12:58:17 -07:00
|
|
|
route('api.assets.files.destroy', [
|
2024-05-27 04:50:20 -07:00
|
|
|
'asset_id' => $asset[0]["id"],
|
2024-05-27 05:08:18 -07:00
|
|
|
'file_id' => $result->decodeResponseJson()->json()["payload"][0]["id"],
|
2024-05-27 04:50:20 -07:00
|
|
|
]))
|
|
|
|
->assertOk();
|
2024-05-18 05:04:34 -07:00
|
|
|
}
|
|
|
|
}
|