Skip to content

A free open source IT asset/license management system

Added tests

snipea6b2f5df1d01
diff --git a/tests/Feature/Consumables/Api/ConsumableUpdateTest.php b/tests/Feature/Consumables/Api/ConsumableUpdateTest.php
new file mode 100644
index 0000000000..1c1e05d4d4
--- /dev/null
+++ b/tests/Feature/Consumables/Api/ConsumableUpdateTest.php
@@ -0,0 +1,52 @@
+<?php
+
+namespace Tests\Feature\Consumables\Api;
+
+use App\Models\Consumable;
+use App\Models\Category;
+use App\Models\User;
+use Tests\TestCase;
+
+class ConsumableUpdateTest extends TestCase
+{
+
+ public function testCanUpdateConsumableViaPatchWithoutCategoryType()
+ {
+ $consumable = Consumable::factory()->create();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->patchJson(route('api.consumables.update', $consumable), [
+ 'name' => 'Test Consumable',
+ ])
+ ->assertOk()
+ ->assertStatusMessageIs('success')
+ ->assertStatus(200)
+ ->json();
+
+ $consumable->refresh();
+ $this->assertEquals('Test Consumable', $consumable->name, 'Name was not updated');
+
+ }
+
+ public function testCannotUpdateConsumableViaPatchWithInvalidCategoryType()
+ {
+ $category = Category::factory()->create(['category_type' => 'asset']);
+ $consumable = Consumable::factory()->create();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->patchJson(route('api.consumables.update', $consumable), [
+ 'name' => 'Test Consumable',
+ 'category_id' => $category->id,
+ ])
+ ->assertOk()
+ ->assertStatusMessageIs('error')
+ ->assertStatus(200)
+ ->json();
+
+ $category->refresh();
+ $this->assertNotEquals('Test Consumable', $consumable->name, 'Name was not updated');
+ $this->assertNotEquals('consumable', $consumable->category_id, 'Category was not updated');
+
+ }
+
+}
diff --git a/tests/Feature/Consumables/Api/ConsumableViewTest.php b/tests/Feature/Consumables/Api/ConsumableViewTest.php
new file mode 100644
index 0000000000..c6410216e1
--- /dev/null
+++ b/tests/Feature/Consumables/Api/ConsumableViewTest.php
@@ -0,0 +1,51 @@
+<?php
+
+namespace Tests\Feature\Consumables\Api;
+
+use App\Models\Company;
+use App\Models\Consumable;
+use App\Models\User;
+use Tests\TestCase;
+
+class ConsumableViewTest extends TestCase
+{
+ public function testConsumableViewAdheresToCompanyScoping()
+ {
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $consumableA = Consumable::factory()->for($companyA)->create();
+ $consumableB = Consumable::factory()->for($companyB)->create();
+
+ $superUser = $companyA->users()->save(User::factory()->superuser()->make());
+ $userInCompanyA = $companyA->users()->save(User::factory()->viewConsumables()->make());
+ $userInCompanyB = $companyB->users()->save(User::factory()->viewConsumables()->make());
+
+ $this->settings->disableMultipleFullCompanySupport();
+
+ $this->actingAsForApi($superUser)
+ ->getJson(route('api.consumables.show', $consumableA))
+ ->assertOk();
+
+ $this->actingAsForApi($userInCompanyA)
+ ->getJson(route('api.consumables.show', $consumableA))
+ ->assertOk();
+
+ $this->actingAsForApi($userInCompanyB)
+ ->getJson(route('api.consumables.show', $consumableB))
+ ->assertOk();
+
+ $this->settings->enableMultipleFullCompanySupport();
+
+ $this->actingAsForApi($superUser)
+ ->getJson(route('api.consumables.show', $consumableA))
+ ->assertOk();
+
+ $this->actingAsForApi($userInCompanyA)
+ ->getJson(route('api.consumables.index'))
+ ->assertOk();
+
+ $this->actingAsForApi($userInCompanyB)
+ ->getJson(route('api.consumables.index'))
+ ->assertOk();
+ }
+}
diff --git a/tests/Feature/Consumables/Ui/ConsumableViewTest.php b/tests/Feature/Consumables/Ui/ConsumableViewTest.php
new file mode 100644
index 0000000000..9633896c2d
--- /dev/null
+++ b/tests/Feature/Consumables/Ui/ConsumableViewTest.php
@@ -0,0 +1,26 @@
+<?php
+
+namespace Tests\Feature\Consumables\Ui;
+
+use App\Models\Consumable;
+use App\Models\User;
+use Tests\TestCase;
+
+class ConsumableViewTest extends TestCase
+{
+ public function testPermissionRequiredToViewConsumable()
+ {
+ $consumable = Consumable::factory()->create();
+ $this->actingAs(User::factory()->create())
+ ->get(route('consumables.show', $consumable))
+ ->assertForbidden();
+ }
+
+ public function testUserCanListConsumables()
+ {
+ $consumable = Consumable::factory()->create();
+ $this->actingAs(User::factory()->superuser()->create())
+ ->get(route('consumables.show', $consumable))
+ ->assertOk();
+ }
+}