From b3cc7b1578cdf7236930f460f0ec2f417d79126e Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 5 Jul 2024 05:59:28 +0100 Subject: [PATCH] More tests Signed-off-by: snipe --- .../AssetModels/Api/IndexAssetModelsTest.php | 67 ++++++++++ .../AssetModels/Api/UpdateAssetModelsTest.php | 120 ++++++++++++++++++ 2 files changed, 187 insertions(+) create mode 100644 tests/Feature/AssetModels/Api/IndexAssetModelsTest.php create mode 100644 tests/Feature/AssetModels/Api/UpdateAssetModelsTest.php 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 @@ +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 @@ +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'); + } + +}