snipe-it/tests/Feature/Assets/Api/AssetFilesTest.php

121 lines
3.3 KiB
PHP
Raw Normal View History

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
use App\Models\Asset;
2024-06-13 14:25:25 -07:00
use App\Models\User;
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
{
public function testAssetApiAcceptsFileUpload()
2024-05-18 05:04:34 -07:00
{
// Upload a file to 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();
//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"]]), [
'file' => [UploadedFile::fake()->create("test.jpg", 100)]
])
->assertOk();
}
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();
// List the files
$this->actingAsForApi($user)
->getJson(
2024-07-12 12:58:17 -07:00
route('api.assets.files.index', ['asset_id' => $asset[0]["id"]]))
->assertOk()
->assertJsonStructure([
'status',
'messages',
'payload',
]);
}
public function testAssetApiDownloadsFile()
{
// Download a file from 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();
//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"]]), [
'file' => [UploadedFile::fake()->create("test.jpg", 100)]
])
->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"]]))
->assertOk();
// Get the file
$this->actingAsForApi($user)
->get(
2024-07-12 12:58:17 -07:00
route('api.assets.files.show', [
'asset_id' => $asset[0]["id"],
'file_id' => $result->decodeResponseJson()->json()["payload"][0]["id"],
]))
->assertOk();
}
public function testAssetApiDeletesFile()
{
// Delete a file from 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();
//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"]]), [
'file' => [UploadedFile::fake()->create("test.jpg", 100)]
])
->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"]]))
->assertOk();
// Delete the file
$this->actingAsForApi($user)
->delete(
2024-07-12 12:58:17 -07:00
route('api.assets.files.destroy', [
'asset_id' => $asset[0]["id"],
'file_id' => $result->decodeResponseJson()->json()["payload"][0]["id"],
]))
->assertOk();
2024-05-18 05:04:34 -07:00
}
}