More tests

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2024-07-05 05:59:28 +01:00
parent 2a796fdc09
commit b3cc7b1578
2 changed files with 187 additions and 0 deletions

View file

@ -0,0 +1,67 @@
<?php
namespace Tests\Feature\AssetModels\Api;
use App\Models\Company;
use App\Models\AssetModel;
use App\Models\User;
use Illuminate\Testing\Fluent\AssertableJson;
use Tests\TestCase;
class IndexAssetModelsTest extends TestCase
{
public function testViewingAssetModelIndexRequiresAuthentication()
{
$this->getJson(route('api.models.index'))->assertRedirect();
}
public function testViewingAssetModelIndexRequiresPermission()
{
$this->actingAsForApi(User::factory()->create())
->getJson(route('api.models.index'))
->assertForbidden();
}
public function testAssetModelIndexReturnsExpectedAssetModels()
{
AssetModel::factory()->count(3)->create();
$this->actingAsForApi(User::factory()->superuser()->create())
->getJson(
route('api.models.index', [
'sort' => 'name',
'order' => 'asc',
'offset' => '0',
'limit' => '20',
]))
->assertOk()
->assertJsonStructure([
'total',
'rows',
])
->assertJson(fn(AssertableJson $json) => $json->has('rows', 3)->etc());
}
public function testAssetModelIndexSearchReturnsExpectedAssetModels()
{
AssetModel::factory()->count(3)->create();
AssetModel::factory()->count(1)->create(['name' => 'Test Model']);
$this->actingAsForApi(User::factory()->superuser()->create())
->getJson(
route('api.models.index', [
'search' => 'Test Model',
'sort' => 'id',
'order' => 'asc',
'offset' => '0',
'limit' => '20',
]))
->assertOk()
->assertJsonStructure([
'total',
'rows',
])
->assertJson(fn(AssertableJson $json) => $json->has('rows', 1)->etc());
}
}

View file

@ -0,0 +1,120 @@
<?php
namespace Tests\Feature\AssetModels\Api;
use App\Models\AssetModel;
use App\Models\Category;
use App\Models\User;
use Tests\TestCase;
class UpdateAssetModelsTest extends TestCase
{
public function testRequiresPermissionToEditAssetModel()
{
$model = AssetModel::factory()->create();
$this->actingAsForApi(User::factory()->create())
->patchJson(route('api.models.update', $model))
->assertForbidden();
}
public function testCanUpdateAssetModelViaPatch()
{
$model = AssetModel::factory()->create();
$this->actingAsForApi(User::factory()->superuser()->create())
->patchJson(route('api.models.update', $model), [
'name' => 'Test Model',
'category_id' => Category::factory()->forAssets()->create()->id,
])
->assertOk()
->assertStatusMessageIs('success')
->assertStatus(200)
->json();
$model->refresh();
$this->assertEquals('Test Model', $model->name, 'Name was not updated');
}
public function testCannotUpdateAssetModelViaPatchWithAccessoryCategory()
{
$category = Category::factory()->forAccessories()->create();
$model = AssetModel::factory()->create();
$this->actingAsForApi(User::factory()->superuser()->create())
->patchJson(route('api.models.update', $model), [
'name' => 'Test Model',
'category_id' => $category->id,
])
->assertOk()
->assertStatusMessageIs('error')
->assertStatus(200)
->json();
$category->refresh();
$this->assertNotEquals('Test Model', $model->name, 'Name was not updated');
$this->assertNotEquals('category_id', $category->id, 'Category ID was not updated');
}
public function testCannotUpdateAssetModelViaPatchWithLicenseCategory()
{
$category = Category::factory()->forLicenses()->create();
$model = AssetModel::factory()->create();
$this->actingAsForApi(User::factory()->superuser()->create())
->patchJson(route('api.models.update', $model), [
'name' => 'Test Model',
'category_id' => $category->id,
])
->assertOk()
->assertStatusMessageIs('error')
->assertStatus(200)
->json();
$category->refresh();
$this->assertNotEquals('Test Model', $model->name, 'Name was not updated');
$this->assertNotEquals('category_id', $category->id, 'Category ID was not updated');
}
public function testCannotUpdateAssetModelViaPatchWithConsumableCategory()
{
$category = Category::factory()->forConsumables()->create();
$model = AssetModel::factory()->create();
$this->actingAsForApi(User::factory()->superuser()->create())
->patchJson(route('api.models.update', $model), [
'name' => 'Test Model',
'category_id' => $category->id,
])
->assertOk()
->assertStatusMessageIs('error')
->assertStatus(200)
->json();
$category->refresh();
$this->assertNotEquals('Test Model', $model->name, 'Name was not updated');
$this->assertNotEquals('category_id', $category->id, 'Category ID was not updated');
}
public function testCannotUpdateAssetModelViaPatchWithComponentCategory()
{
$category = Category::factory()->forComponents()->create();
$model = AssetModel::factory()->create();
$this->actingAsForApi(User::factory()->superuser()->create())
->patchJson(route('api.models.update', $model), [
'name' => 'Test Model',
'category_id' => $category->id,
])
->assertOk()
->assertStatusMessageIs('error')
->assertStatus(200)
->json();
$category->refresh();
$this->assertNotEquals('Test Model', $model->name, 'Name was not updated');
$this->assertNotEquals('category_id', $category->id, 'Category ID was not updated');
}
}