2023-10-10 17:51:29 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature\Api\Groups;
|
|
|
|
|
2024-05-28 13:02:44 -07:00
|
|
|
use App\Helpers\Helper;
|
2023-10-10 17:51:29 -07:00
|
|
|
use App\Models\Group;
|
|
|
|
use App\Models\User;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
2024-06-03 16:53:15 -07:00
|
|
|
class StoreGroupTest extends TestCase
|
2023-10-10 17:51:29 -07:00
|
|
|
{
|
|
|
|
public function testStoringGroupRequiresSuperAdminPermission()
|
|
|
|
{
|
|
|
|
$this->actingAsForApi(User::factory()->create())
|
|
|
|
->postJson(route('api.groups.store'))
|
|
|
|
->assertForbidden();
|
|
|
|
}
|
|
|
|
|
2024-05-28 07:21:13 -07:00
|
|
|
public function testCanStoreGroupWithPermissionsPassed()
|
2023-10-10 17:51:29 -07:00
|
|
|
{
|
|
|
|
$this->actingAsForApi(User::factory()->superuser()->create())
|
|
|
|
->postJson(route('api.groups.store'), [
|
|
|
|
'name' => 'My Awesome Group',
|
|
|
|
'permissions' => [
|
|
|
|
'admin' => '1',
|
|
|
|
'import' => '1',
|
|
|
|
'reports.view' => '0',
|
|
|
|
],
|
|
|
|
])
|
|
|
|
->assertOk();
|
|
|
|
|
|
|
|
$group = Group::where('name', 'My Awesome Group')->first();
|
|
|
|
|
|
|
|
$this->assertNotNull($group);
|
|
|
|
$this->assertEquals('1', $group->decodePermissions()['admin']);
|
|
|
|
$this->assertEquals('1', $group->decodePermissions()['import']);
|
|
|
|
$this->assertEquals('0', $group->decodePermissions()['reports.view']);
|
|
|
|
}
|
2024-05-28 07:21:13 -07:00
|
|
|
|
|
|
|
public function testStoringGroupWithoutPermissionPassed()
|
|
|
|
{
|
|
|
|
$superuser = User::factory()->superuser()->create();
|
|
|
|
$this->actingAsForApi($superuser)
|
|
|
|
->postJson(route('api.groups.store'), [
|
|
|
|
'name' => 'My Awesome Group'
|
|
|
|
])
|
|
|
|
->assertOk();
|
|
|
|
|
|
|
|
$group = Group::where('name', 'My Awesome Group')->first();
|
|
|
|
|
|
|
|
$this->assertNotNull($group);
|
|
|
|
|
2024-05-28 13:02:44 -07:00
|
|
|
$this->assertEquals(
|
|
|
|
Helper::selectedPermissionsArray(config('permissions'), config('permissions')),
|
|
|
|
$group->decodePermissions(),
|
|
|
|
'Default group permissions were not set as expected',
|
|
|
|
);
|
|
|
|
|
2024-05-28 07:21:13 -07:00
|
|
|
$this->actingAsForApi($superuser)
|
|
|
|
->getJson(route('api.groups.show', ['group' => $group]))
|
|
|
|
->assertOk();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testStoringGroupWithInvalidPermissionDropsBadPermission()
|
|
|
|
{
|
|
|
|
$this->actingAsForApi(User::factory()->superuser()->create())
|
|
|
|
->postJson(route('api.groups.store'), [
|
|
|
|
'name' => 'My Awesome Group',
|
|
|
|
'permissions' => [
|
|
|
|
'admin' => '1',
|
|
|
|
'snipe_is_awesome' => '1',
|
|
|
|
],
|
|
|
|
])
|
|
|
|
->assertOk();
|
|
|
|
|
|
|
|
$group = Group::where('name', 'My Awesome Group')->first();
|
|
|
|
$this->assertNotNull($group);
|
|
|
|
$this->assertEquals('1', $group->decodePermissions()['admin']);
|
|
|
|
$this->assertNotContains('snipe_is_awesome', $group->decodePermissions());
|
|
|
|
|
|
|
|
}
|
2023-10-10 17:51:29 -07:00
|
|
|
}
|