Skip to content

A free open source IT asset/license management system

Added basic Department UI tests

snipe0a7e0539850a
diff --git a/tests/Feature/Departments/Api/CreateDepartmentsTest.php b/tests/Feature/Departments/Api/CreateDepartmentsTest.php
new file mode 100644
index 0000000000..be2bbbc18d
--- /dev/null
+++ b/tests/Feature/Departments/Api/CreateDepartmentsTest.php
@@ -0,0 +1,23 @@
+<?php
+
+namespace Tests\Feature\Department\Api;
+
+use App\Models\AssetModel;
+use App\Models\Department;
+use App\Models\Category;
+use App\Models\User;
+use Illuminate\Testing\Fluent\AssertableJson;
+use Tests\TestCase;
+
+class CreateDepartmentsTest extends TestCase
+{
+
+
+ public function testRequiresPermissionToCreateDepartment()
+ {
+ $this->actingAsForApi(User::factory()->create())
+ ->postJson(route('api.departments.store'))
+ ->assertForbidden();
+ }
+
+}
diff --git a/tests/Feature/Departments/Api/DepartmentIndexTest.php b/tests/Feature/Departments/Api/DepartmentsIndexTest.php
similarity index 98%
rename from tests/Feature/Departments/Api/DepartmentIndexTest.php
rename to tests/Feature/Departments/Api/DepartmentsIndexTest.php
index 462c0dc8e8..e3616e2491 100644
--- a/tests/Feature/Departments/Api/DepartmentIndexTest.php
+++ b/tests/Feature/Departments/Api/DepartmentsIndexTest.php
@@ -8,7 +8,7 @@ use App\Models\User;
use Illuminate\Testing\Fluent\AssertableJson;
use Tests\TestCase;
-class DepartmentIndexTest extends TestCase
+class DepartmentsIndexTest extends TestCase
{
public function testViewingDepartmentIndexRequiresAuthentication()
{
diff --git a/tests/Feature/Departments/Api/UpdateDepartmentTest.php b/tests/Feature/Departments/Api/UpdateDepartmentTest.php
new file mode 100644
index 0000000000..2a6401e7f1
--- /dev/null
+++ b/tests/Feature/Departments/Api/UpdateDepartmentTest.php
@@ -0,0 +1,41 @@
+<?php
+
+namespace Tests\Feature\Departments\Api;
+
+use App\Models\Department;
+use App\Models\Category;
+use App\Models\User;
+use Tests\TestCase;
+
+class UpdateDepartmentsTest extends TestCase
+{
+
+ public function testRequiresPermissionToEditDepartment()
+ {
+ $department = Department::factory()->create();
+ $this->actingAsForApi(User::factory()->create())
+ ->patchJson(route('api.departments.update', $department))
+ ->assertForbidden();
+ }
+
+ public function testCanUpdateDepartmentViaPatch()
+ {
+ $department = Department::factory()->create();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->patchJson(route('api.departments.update', $department), [
+ 'name' => 'Test Department',
+ ])
+ ->assertOk()
+ ->assertStatusMessageIs('success')
+ ->assertStatus(200)
+ ->json();
+
+ $department->refresh();
+ $this->assertEquals('Test Department', $department->name, 'Name was not updated');
+
+ }
+
+
+
+}
diff --git a/tests/Feature/Departments/Ui/CreateDepartmentsTest.php b/tests/Feature/Departments/Ui/CreateDepartmentsTest.php
new file mode 100644
index 0000000000..17045cbc0b
--- /dev/null
+++ b/tests/Feature/Departments/Ui/CreateDepartmentsTest.php
@@ -0,0 +1,37 @@
+<?php
+
+namespace Tests\Feature\Departments\Ui;
+
+use App\Models\Department;
+use App\Models\Company;
+use App\Models\User;
+use Tests\TestCase;
+
+class CreateDepartmentsTest extends TestCase
+{
+ public function testPermissionRequiredToCreateDepartment()
+ {
+ $this->actingAs(User::factory()->create())
+ ->post(route('departments.store'), [
+ 'name' => 'Test Department',
+ 'company_id' => Company::factory()->create()->id
+ ])
+ ->assertForbidden();
+ }
+
+ public function testUserCanCreateDepartments()
+ {
+ $this->assertFalse(Department::where('name', 'Test Department')->exists());
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('departments.store'), [
+ 'name' => 'Test Department',
+ 'company_id' => Company::factory()->create()->id
+ ])
+ ->assertRedirect(route('departments.index'));
+
+ $this->assertTrue(Department::where('name', 'Test Department')->exists());
+ }
+
+
+}
diff --git a/tests/Feature/Departments/Ui/IndexDepartmentsTest.php b/tests/Feature/Departments/Ui/IndexDepartmentsTest.php
new file mode 100644
index 0000000000..461ec8fffb
--- /dev/null
+++ b/tests/Feature/Departments/Ui/IndexDepartmentsTest.php
@@ -0,0 +1,23 @@
+<?php
+
+namespace Tests\Feature\Departments\Ui;
+
+use App\Models\User;
+use Tests\TestCase;
+
+class IndexDepartmentsTest extends TestCase
+{
+ public function testPermissionRequiredToViewDepartmentsList()
+ {
+ $this->actingAs(User::factory()->create())
+ ->get(route('departments.index'))
+ ->assertForbidden();
+ }
+
+ public function testUserCanListDepartments()
+ {
+ $this->actingAs(User::factory()->superuser()->create())
+ ->get(route('departments.index'))
+ ->assertOk();
+ }
+}
diff --git a/tests/Feature/Departments/Ui/UpdateDepartmentsTest.php b/tests/Feature/Departments/Ui/UpdateDepartmentsTest.php
new file mode 100644
index 0000000000..e4ae7c6240
--- /dev/null
+++ b/tests/Feature/Departments/Ui/UpdateDepartmentsTest.php
@@ -0,0 +1,61 @@
+<?php
+
+namespace Tests\Feature\Departments\Ui;
+
+use App\Models\Department;
+use App\Models\Category;
+use App\Models\User;
+use Tests\TestCase;
+
+class UpdateDepartmentsTest extends TestCase
+{
+ public function testPermissionRequiredToStoreDepartment()
+ {
+ $this->actingAs(User::factory()->create())
+ ->post(route('departments.store'), [
+ 'name' => 'Test Department',
+ 'category_id' => Category::factory()->create()->id
+ ])
+ ->assertStatus(403)
+ ->assertForbidden();
+ }
+
+ public function testUserCanCreateDepartments()
+ {
+ $this->assertFalse(Department::where('name', 'Test Department')->exists());
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('departments.store'), [
+ 'name' => 'Test Department',
+ 'category_id' => Category::factory()->create()->id
+ ])
+ ->assertStatus(302)
+ ->assertSessionHasNoErrors()
+ ->assertRedirect(route('departments.index'));
+
+ $this->assertTrue(Department::where('name', 'Test Department')->exists());
+ }
+
+ public function testUserCanEditDepartments()
+ {
+ $category = Category::factory()->forAssets()->create();
+ $department = Department::factory()->create(['name' => 'Test Department']);
+ $this->assertTrue(Department::where('name', 'Test Department')->exists());
+
+ $response = $this->actingAs(User::factory()->superuser()->create())
+ ->put(route('departments.update', ['department' => $department]), [
+ 'name' => 'Test Department Edited',
+ 'category_id' => $department->category_id,
+ ])
+ ->assertStatus(302)
+ ->assertSessionHasNoErrors()
+ ->assertRedirect(route('departments.index'));
+
+ $this->followRedirects($response)->assertSee('Success');
+ $this->assertTrue(Department::where('name', 'Test Department Edited')->exists());
+
+ }
+
+
+
+}