Skip to content

A free open source IT asset/license management system

Add simple test cases for api department index

Marcus Moorecdfe8e459dc7
diff --git a/tests/Feature/Api/Departments/DepartmentIndexTest.php b/tests/Feature/Api/Departments/DepartmentIndexTest.php
new file mode 100644
index 0000000000..505e92bd2f
--- /dev/null
+++ b/tests/Feature/Api/Departments/DepartmentIndexTest.php
@@ -0,0 +1,81 @@
+<?php
+
+namespace Tests\Feature\Api\Departments;
+
+use App\Models\Company;
+use App\Models\Department;
+use App\Models\User;
+use Illuminate\Testing\Fluent\AssertableJson;
+use Tests\Support\InteractsWithSettings;
+use Tests\TestCase;
+
+class DepartmentIndexTest extends TestCase
+{
+ use InteractsWithSettings;
+
+ public function testDepartmentIndexReturnsExpectedDepartments()
+ {
+ Department::factory()->count(3)->create();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(
+ route('api.departments.index', [
+ 'sort' => 'name',
+ 'order' => 'asc',
+ 'offset' => '0',
+ 'limit' => '20',
+ ]))
+ ->assertOk()
+ ->assertJsonStructure([
+ 'total',
+ 'rows',
+ ])
+ ->assertJson(fn(AssertableJson $json) => $json->has('rows', 3)->etc());
+ }
+
+ public function testDepartmentIndexAdheresToCompanyScoping()
+ {
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $departmentA = Department::factory()->for($companyA)->create();
+ $departmentB = Department::factory()->for($companyB)->create();
+
+ $superUser = $companyA->users()->save(User::factory()->superuser()->make());
+ $userInCompanyA = $companyA->users()->save(User::factory()->viewDepartments()->make());
+ $userInCompanyB = $companyB->users()->save(User::factory()->viewDepartments()->make());
+
+ $this->settings->disableMultipleFullCompanySupport();
+
+ $this->actingAsForApi($superUser)
+ ->getJson(route('api.departments.index'))
+ ->assertResponseContainsInRows($departmentA)
+ ->assertResponseContainsInRows($departmentB);
+
+ $this->actingAsForApi($userInCompanyA)
+ ->getJson(route('api.departments.index'))
+ ->assertResponseContainsInRows($departmentA)
+ ->assertResponseContainsInRows($departmentB);
+
+ $this->actingAsForApi($userInCompanyB)
+ ->getJson(route('api.departments.index'))
+ ->assertResponseContainsInRows($departmentA)
+ ->assertResponseContainsInRows($departmentB);
+
+ $this->settings->enableMultipleFullCompanySupport();
+
+ $this->actingAsForApi($superUser)
+ ->getJson(route('api.departments.index'))
+ ->assertResponseContainsInRows($departmentA)
+ ->assertResponseContainsInRows($departmentB);
+
+ $this->actingAsForApi($userInCompanyA)
+ ->getJson(route('api.departments.index'))
+ ->assertResponseContainsInRows($departmentA)
+ ->assertResponseDoesNotContainInRows($departmentB);
+
+ $this->actingAsForApi($userInCompanyB)
+ ->getJson(route('api.departments.index'))
+ ->assertResponseDoesNotContainInRows($departmentA)
+ ->assertResponseContainsInRows($departmentB);
+ }
+}