Skip to content

A free open source IT asset/license management system

Added category tests

snipe1f28bf14c193
diff --git a/tests/Feature/Categories/Api/IndexCategoriesTest.php b/tests/Feature/Categories/Api/IndexCategoriesTest.php
new file mode 100644
index 0000000000..f3717e6292
--- /dev/null
+++ b/tests/Feature/Categories/Api/IndexCategoriesTest.php
@@ -0,0 +1,71 @@
+<?php
+
+namespace Tests\Feature\Categories\Api;
+
+use App\Models\Category;
+use App\Models\User;
+use Illuminate\Testing\Fluent\AssertableJson;
+use Tests\TestCase;
+
+class IndexCategoryTest extends TestCase
+{
+
+ public function testViewingCategoryIndexRequiresPermission()
+ {
+ $this->actingAsForApi(User::factory()->create())
+ ->getJson(route('api.departments.index'))
+ ->assertForbidden();
+ }
+
+ public function testCategoryIndexReturnsExpectedSearchResults()
+ {
+ Category::factory()->count(10)->create();
+ Category::factory()->count(1)->forAssets()->create(['name' => 'My Test Category']);
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(
+ route('api.categories.index', [
+ 'search' => 'My Test Category',
+ 'sort' => 'name',
+ 'order' => 'asc',
+ 'offset' => '0',
+ 'limit' => '20',
+ ]))
+ ->assertOk()
+ ->assertOk()
+ ->assertJsonStructure([
+ 'total',
+ 'rows',
+ ])
+ ->assertJson([
+ 'total' => 1,
+ ]);
+
+ }
+
+
+ public function testCategoryIndexReturnsExpectedCategories()
+ {
+ $this->markTestIncomplete('Not sure why the category factory is generating one more than expected here.');
+ Category::factory()->count(3)->create();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(
+ route('api.categories.index', [
+ 'sort' => 'id',
+ 'order' => 'asc',
+ 'offset' => '0',
+ 'limit' => '20',
+ ]))
+ ->assertOk()
+ ->assertJsonStructure([
+ 'total',
+ 'rows',
+ ])
+ ->assertJson([
+ 'total' => 3,
+ ]);
+
+ }
+
+}
diff --git a/tests/Feature/Categories/Ui/CreateCategoriesTest.php b/tests/Feature/Categories/Ui/CreateCategoriesTest.php
new file mode 100644
index 0000000000..87312ad257
--- /dev/null
+++ b/tests/Feature/Categories/Ui/CreateCategoriesTest.php
@@ -0,0 +1,51 @@
+<?php
+
+namespace Tests\Feature\Categories\Ui;
+
+use App\Models\AssetModel;
+use App\Models\Category;
+use App\Models\User;
+use Tests\TestCase;
+
+class CreateCategoriesTest extends TestCase
+{
+ public function testPermissionRequiredToCreateCategories()
+ {
+ $this->actingAs(User::factory()->create())
+ ->post(route('categories.store'), [
+ 'name' => 'Test Category',
+ 'category_type' => 'asset',
+ ])
+ ->assertForbidden();
+ }
+
+ public function testUserCanCreateCategories()
+ {
+ $this->assertFalse(Category::where('name', 'Test Category')->exists());
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('categories.store'), [
+ 'name' => 'Test Category',
+ 'category_type' => 'asset'
+ ])
+ ->assertRedirect(route('categories.index'));
+
+ $this->assertTrue(Category::where('name', 'Test Category')->exists());
+ }
+
+ public function testUserCannotCreateCategoriesWithInvalidType()
+ {
+ $this->assertFalse(Category::where('name', 'Test Category')->exists());
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->from(route('categories.create'))
+ ->post(route('categories.store'), [
+ 'name' => 'Test Category',
+ 'category_type' => 'invalid'
+ ])
+ ->assertRedirect(route('categories.create'));
+
+ $this->assertFalse(Category::where('name', 'Test Category')->exists());
+ }
+
+}
diff --git a/tests/Feature/Categories/Ui/IndexCategoriesTest.php b/tests/Feature/Categories/Ui/IndexCategoriesTest.php
new file mode 100644
index 0000000000..caacba99a9
--- /dev/null
+++ b/tests/Feature/Categories/Ui/IndexCategoriesTest.php
@@ -0,0 +1,23 @@
+<?php
+
+namespace Tests\Feature\Categories\Ui;
+
+use App\Models\User;
+use Tests\TestCase;
+
+class IndexCategoriesTest extends TestCase
+{
+ public function testPermissionRequiredToViewCategoryList()
+ {
+ $this->actingAs(User::factory()->create())
+ ->get(route('categories.index'))
+ ->assertForbidden();
+ }
+
+ public function testUserCanListCategories()
+ {
+ $this->actingAs(User::factory()->superuser()->create())
+ ->get(route('categories.index'))
+ ->assertOk();
+ }
+}
diff --git a/tests/Feature/Categories/Ui/UpdateCategoriesTest.php b/tests/Feature/Categories/Ui/UpdateCategoriesTest.php
new file mode 100644
index 0000000000..98b67c7d32
--- /dev/null
+++ b/tests/Feature/Categories/Ui/UpdateCategoriesTest.php
@@ -0,0 +1,96 @@
+<?php
+
+namespace Tests\Feature\Categories\Ui;
+
+use App\Models\Category;
+use App\Models\Asset;
+use App\Models\User;
+use Tests\TestCase;
+
+class UpdateCategoriesTest extends TestCase
+{
+ public function testPermissionRequiredToStoreCategory()
+ {
+ $this->actingAs(User::factory()->create())
+ ->post(route('categories.store'), [
+ 'name' => 'Test Category',
+ 'category_type' => 'asset'
+ ])
+ ->assertStatus(403)
+ ->assertForbidden();
+ }
+
+ public function testUserCanCreateCategories()
+ {
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('categories.store'), [
+ 'name' => 'Test Category',
+ 'category_type' => 'asset'
+ ])
+ ->assertStatus(302)
+ ->assertSessionHasNoErrors()
+ ->assertRedirect(route('categories.index'));
+
+ $this->assertTrue(Category::where('name', 'Test Category')->exists());
+ }
+
+ public function testUserCanEditAssetCategory()
+ {
+ $category = Category::factory()->forAssets()->create(['name' => 'Test Category']);
+ $this->assertTrue(Category::where('name', 'Test Category')->exists());
+
+ $response = $this->actingAs(User::factory()->superuser()->create())
+ ->put(route('categories.update', ['category' => $category]), [
+ 'name' => 'Test Category Edited',
+ ])
+ ->assertStatus(302)
+ ->assertSessionHasNoErrors()
+ ->assertRedirect(route('categories.index'));
+
+ $this->followRedirects($response)->assertSee('Success');
+ $this->assertTrue(Category::where('name', 'Test Category Edited')->exists());
+
+ }
+
+ public function testUserCanChangeCategoryTypeIfNoAssetsAssociated()
+ {
+ $category = Category::factory()->forAssets()->create(['name' => 'Test Category']);
+ $this->assertTrue(Category::where('name', 'Test Category')->exists());
+
+ $response = $this->actingAs(User::factory()->superuser()->create())
+ ->from(route('categories.edit', ['category' => $category->id]))
+ ->put(route('categories.update', ['category' => $category]), [
+ 'name' => 'Test Category Edited',
+ 'category_type' => 'accessory',
+ ])
+ ->assertSessionHasNoErrors()
+ ->assertStatus(302)
+ ->assertRedirect(route('categories.index'));
+
+ $this->followRedirects($response)->assertSee('Success');
+ $this->assertTrue(Category::where('name', 'Test Category Edited')->exists());
+
+ }
+
+ public function testUserCannotChangeCategoryTypeIfAssetsAreAssociated()
+ {
+ Asset::factory()->count(5)->laptopMbp()->create();
+ $category = Category::where('name', 'Laptops')->first();
+
+ $response = $this->actingAs(User::factory()->superuser()->create())
+ ->from(route('categories.edit', ['category' => $category->id]))
+ ->put(route('categories.update', ['category' => $category]), [
+ 'name' => 'Test Category Edited',
+ 'category_type' => 'accessory',
+ ])
+ ->assertSessionHasErrors(['category_type'])
+ ->assertInvalid(['category_type'])
+ ->assertStatus(302)
+ ->assertRedirect(route('categories.edit', ['category' => $category->id]));
+
+ $this->followRedirects($response)->assertSee(trans('general.error'));
+ $this->assertFalse(Category::where('name', 'Test Category Edited')->exists());
+
+ }
+
+}