Skip to content

A free open source IT asset/license management system

Added category tests

snipe4089b4cd68ec
diff --git a/tests/Feature/Categories/Api/CreateCategoriesTest.php b/tests/Feature/Categories/Api/CreateCategoriesTest.php
new file mode 100644
index 0000000000..db5810cf14
--- /dev/null
+++ b/tests/Feature/Categories/Api/CreateCategoriesTest.php
@@ -0,0 +1,60 @@
+<?php
+
+namespace Tests\Feature\Categories\Api;
+
+use App\Models\Asset;
+use App\Models\AssetModel;
+use App\Models\Category;
+use App\Models\User;
+use Illuminate\Testing\Fluent\AssertableJson;
+use Tests\TestCase;
+
+class CreateCategoriesTest extends TestCase
+{
+
+
+ public function testRequiresPermissionToCreateCategory()
+ {
+ $this->actingAsForApi(User::factory()->create())
+ ->postJson(route('api.categories.store'))
+ ->assertForbidden();
+ }
+
+ public function testCanCreateCategoryWithCategoryType()
+ {
+ $response = $this->actingAsForApi(User::factory()->superuser()->create())
+ ->postJson(route('api.categories.store'), [
+ 'name' => 'Test Category',
+ 'eula_text' => 'Test EULA',
+ 'category_type' => 'accessory',
+ ])
+ ->assertOk()
+ ->assertStatusMessageIs('success')
+ ->assertStatus(200)
+ ->json();
+
+ $this->assertTrue(Category::where('name', 'Test Category')->exists());
+
+ $category = Category::find($response['payload']['id']);
+ $this->assertEquals('Test Category', $category->name);
+ $this->assertEquals('Test EULA', $category->eula_text);
+ $this->assertEquals('accessory', $category->category_type);
+ }
+
+ public function testCannotCreateCategoryWithoutCategoryType()
+ {
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->postJson(route('api.categories.store'), [
+ 'name' => 'Test Category',
+ 'eula_text' => 'Test EULA',
+ ])
+ ->assertOk()
+ ->assertStatusMessageIs('error')
+ ->assertStatus(200)
+ ->json();
+
+ $this->assertFalse(Category::where('name', 'Test Category')->exists());
+
+ }
+
+}
diff --git a/tests/Feature/Categories/Api/UpdateCategoryTest.php b/tests/Feature/Categories/Api/UpdateCategoryTest.php
new file mode 100644
index 0000000000..5c67ea76e5
--- /dev/null
+++ b/tests/Feature/Categories/Api/UpdateCategoryTest.php
@@ -0,0 +1,56 @@
+<?php
+
+namespace Tests\Feature\Categories\Api;
+
+use App\Models\Category;
+use App\Models\User;
+use Tests\TestCase;
+
+class UpdateCategoriesTest extends TestCase
+{
+
+ public function testCanUpdateCategoryViaPatchWithoutCategoryType()
+ {
+ $category = Category::factory()->create();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->patchJson(route('api.categories.update', $category), [
+ 'name' => 'Test Category',
+ 'eula_text' => 'Test EULA',
+ ])
+ ->assertOk()
+ ->assertStatusMessageIs('success')
+ ->assertStatus(200)
+ ->json();
+
+ //dd($response);
+ $category->refresh();
+ $this->assertEquals('Test Category', $category->name, 'Name was not updated');
+ $this->assertEquals('Test EULA', $category->eula_text, 'EULA was not updated');
+
+ }
+
+ public function testCannotUpdateCategoryViaPatchWithCategoryType()
+ {
+ $category = Category::factory()->create();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->patchJson(route('api.categories.update', $category), [
+ 'name' => 'Test Category',
+ 'eula_text' => 'Test EULA',
+ 'category_type' => 'accessory',
+ ])
+ ->assertOk()
+ ->assertStatusMessageIs('error')
+ ->assertStatus(200)
+ ->json();
+
+ //dd($response);
+ $category->refresh();
+ $this->assertNotEquals('Test Category', $category->name, 'Name was not updated');
+ $this->assertNotEquals('Test EULA', $category->eula_text, 'EULA was not updated');
+ $this->assertNotEquals('accessory', $category->category_type, 'EULA was not updated');
+
+ }
+
+}