-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13415 from marcusmoore/feature/department-scoping
Added `CompanyableTrait` to `Department` model
- Loading branch information
Showing
3 changed files
with
98 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
|
||
class Department extends SnipeModel | ||
{ | ||
use CompanyableTrait; | ||
use HasFactory; | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
namespace Tests\Feature\Api\Departments; | ||
|
||
use App\Models\Company; | ||
use App\Models\Department; | ||
use App\Models\User; | ||
use Illuminate\Routing\Route; | ||
use Illuminate\Testing\Fluent\AssertableJson; | ||
use Tests\Support\InteractsWithSettings; | ||
use Tests\TestCase; | ||
|
||
class DepartmentIndexTest extends TestCase | ||
{ | ||
use InteractsWithSettings; | ||
|
||
public function testViewingDepartmentIndexRequiresAuthentication() | ||
{ | ||
$this->getJson(route('api.departments.index'))->assertRedirect(); | ||
} | ||
|
||
public function testViewingDepartmentIndexRequiresPermission() | ||
{ | ||
$this->actingAsForApi(User::factory()->create()) | ||
->getJson(route('api.departments.index')) | ||
->assertForbidden(); | ||
} | ||
|
||
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); | ||
} | ||
} |