Skip to content

A free open source IT asset/license management system

More tests

snipeb3cc7b1578cd
diff --git a/tests/Feature/AssetModels/Api/IndexAssetModelsTest.php b/tests/Feature/AssetModels/Api/IndexAssetModelsTest.php
new file mode 100644
index 0000000000..8d7e39c489
--- /dev/null
+++ b/tests/Feature/AssetModels/Api/IndexAssetModelsTest.php
@@ -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());
+ }
+
+}
diff --git a/tests/Feature/AssetModels/Api/UpdateAssetModelsTest.php b/tests/Feature/AssetModels/Api/UpdateAssetModelsTest.php
new file mode 100644
index 0000000000..c5ec212650
--- /dev/null
+++ b/tests/Feature/AssetModels/Api/UpdateAssetModelsTest.php
@@ -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');
+ }
+
+}