2019-03-26 23:34:40 +01:00
|
|
|
<?php
|
2020-02-22 18:14:36 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-01-05 15:55:16 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-03-26 23:34:40 +01:00
|
|
|
/**
|
2019-11-09 00:47:20 +01:00
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
2019-03-26 23:34:40 +01:00
|
|
|
*
|
2019-11-01 13:40:30 +01:00
|
|
|
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
|
2019-03-26 23:34:40 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2022-11-14 20:15:06 +01:00
|
|
|
namespace App\Tests\Services\UserSystem;
|
2019-03-26 23:34:40 +01:00
|
|
|
|
2019-08-16 16:43:31 +02:00
|
|
|
use App\Entity\UserSystem\Group;
|
2022-10-30 21:51:24 +01:00
|
|
|
use App\Entity\UserSystem\PermissionData;
|
2019-08-16 16:43:31 +02:00
|
|
|
use App\Entity\UserSystem\PermissionsEmbed;
|
|
|
|
use App\Entity\UserSystem\User;
|
2022-11-14 20:15:06 +01:00
|
|
|
use App\Services\UserSystem\PermissionManager;
|
2020-01-05 22:49:00 +01:00
|
|
|
use InvalidArgumentException;
|
2019-03-26 23:34:40 +01:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
|
|
|
|
2022-11-14 20:15:06 +01:00
|
|
|
class PermissionManagerTest extends WebTestCase
|
2019-03-26 23:34:40 +01:00
|
|
|
{
|
2020-01-05 22:49:00 +01:00
|
|
|
protected $user_withoutGroup;
|
|
|
|
|
|
|
|
protected $user;
|
|
|
|
protected $group;
|
2019-03-26 23:34:40 +01:00
|
|
|
/**
|
2022-11-14 20:15:06 +01:00
|
|
|
* @var PermissionManager
|
2019-03-26 23:34:40 +01:00
|
|
|
*/
|
|
|
|
protected $service;
|
|
|
|
|
2020-01-05 15:55:16 +01:00
|
|
|
protected function setUp(): void
|
2019-03-26 23:34:40 +01:00
|
|
|
{
|
|
|
|
parent::setUp(); // TODO: Change the autogenerated stub
|
|
|
|
|
|
|
|
//Get an service instance.
|
|
|
|
self::bootKernel();
|
2022-11-14 20:15:06 +01:00
|
|
|
$this->service = self::$container->get(PermissionManager::class);
|
2019-03-26 23:34:40 +01:00
|
|
|
|
|
|
|
//Set up a mocked user
|
2022-10-30 21:51:24 +01:00
|
|
|
$user_perms = new PermissionData();
|
|
|
|
$user_perms->setPermissionValue('parts', 'read', true) //read
|
|
|
|
->setPermissionValue('parts', 'edit', false) //edit
|
|
|
|
->setPermissionValue('parts', 'create', null) //create
|
|
|
|
->setPermissionValue('parts', 'move', null) //move
|
|
|
|
->setPermissionValue('parts', 'delete', null); //delete
|
2019-03-26 23:34:40 +01:00
|
|
|
|
|
|
|
$this->user = $this->createMock(User::class);
|
2022-10-30 21:51:24 +01:00
|
|
|
$this->user->method('getPermissions')->willReturn($user_perms);
|
2019-03-26 23:34:40 +01:00
|
|
|
|
2019-10-26 22:27:04 +02:00
|
|
|
$this->user_withoutGroup = $this->createMock(User::class);
|
2022-10-30 21:51:24 +01:00
|
|
|
$this->user_withoutGroup->method('getPermissions')->willReturn($user_perms);
|
2019-10-26 22:27:04 +02:00
|
|
|
$this->user_withoutGroup->method('getGroup')->willReturn(null);
|
|
|
|
|
2019-03-26 23:34:40 +01:00
|
|
|
//Set up a faked group
|
2022-10-30 21:51:24 +01:00
|
|
|
$group1_perms = new PermissionData();
|
|
|
|
$group1_perms
|
|
|
|
->setPermissionValue('parts', 'delete', false)
|
|
|
|
->setPermissionValue('parts', 'search', null)
|
|
|
|
->setPermissionValue('parts', 'read', false)
|
|
|
|
->setPermissionValue('parts', 'show_history', true)
|
|
|
|
->setPermissionValue('parts', 'edit', true);
|
2019-03-26 23:34:40 +01:00
|
|
|
|
|
|
|
$this->group = $this->createMock(Group::class);
|
2022-10-30 21:51:24 +01:00
|
|
|
$this->group->method('getPermissions')->willReturn($group1_perms);
|
2019-03-26 23:34:40 +01:00
|
|
|
|
|
|
|
//Set this group for the user
|
|
|
|
$this->user->method('getGroup')->willReturn($this->group);
|
|
|
|
|
|
|
|
//parent group
|
2022-10-30 21:51:24 +01:00
|
|
|
$parent_group_perms = new PermissionData();
|
|
|
|
$parent_group_perms->setPermissionValue('parts', 'all_parts', true)
|
|
|
|
->setPermissionValue('parts', 'no_price_parts', false)
|
|
|
|
->setPermissionValue('parts', 'obsolete_parts', null);
|
2019-03-26 23:34:40 +01:00
|
|
|
$parent_group = $this->createMock(Group::class);
|
2022-10-30 21:51:24 +01:00
|
|
|
$parent_group->method('getPermissions')->willReturn($parent_group_perms);
|
2019-03-26 23:34:40 +01:00
|
|
|
|
|
|
|
$this->group->method('getParent')->willReturn($parent_group);
|
|
|
|
}
|
|
|
|
|
2020-03-29 22:47:25 +02:00
|
|
|
public function getPermissionNames(): array
|
2019-03-26 23:34:40 +01:00
|
|
|
{
|
|
|
|
//List all possible operation names.
|
|
|
|
return [
|
|
|
|
[PermissionsEmbed::PARTS],
|
|
|
|
[PermissionsEmbed::USERS],
|
|
|
|
[PermissionsEmbed::PARTS_ORDERDETAILS],
|
|
|
|
[PermissionsEmbed::PARTS_NAME],
|
|
|
|
[PermissionsEmbed::PARTS_ORDER],
|
2019-09-22 21:35:22 +02:00
|
|
|
[PermissionsEmbed::PARTS_MINAMOUNT],
|
2019-03-26 23:34:40 +01:00
|
|
|
[PermissionsEmbed::PARTS_MANUFACTURER],
|
|
|
|
[PermissionsEmbed::DEVICES],
|
|
|
|
[PermissionsEmbed::PARTS_FOOTPRINT],
|
|
|
|
[PermissionsEmbed::PARTS_DESCRIPTION],
|
|
|
|
[PermissionsEmbed::PARTS_COMMENT],
|
|
|
|
[PermissionsEmbed::PARTS_ATTACHMENTS],
|
|
|
|
[PermissionsEmbed::MANUFACTURERS],
|
|
|
|
[PermissionsEmbed::LABELS],
|
|
|
|
[PermissionsEmbed::DATABASE],
|
|
|
|
[PermissionsEmbed::GROUPS],
|
|
|
|
[PermissionsEmbed::FOOTRPINTS],
|
|
|
|
[PermissionsEmbed::DEVICE_PARTS],
|
|
|
|
[PermissionsEmbed::CATEGORIES],
|
|
|
|
[PermissionsEmbed::PARTS_PRICES],
|
|
|
|
[PermissionsEmbed::ATTACHMENT_TYPES],
|
2019-11-09 00:47:20 +01:00
|
|
|
[PermissionsEmbed::CONFIG],
|
2019-03-26 23:34:40 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getPermissionNames
|
|
|
|
*/
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testListOperationsForPermission($perm_name): void
|
2019-03-26 23:34:40 +01:00
|
|
|
{
|
|
|
|
$arr = $this->service->listOperationsForPermission($perm_name);
|
|
|
|
|
|
|
|
//Every entry should not be empty.
|
|
|
|
$this->assertNotEmpty($arr);
|
|
|
|
}
|
|
|
|
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testInvalidListOperationsForPermission(): void
|
2019-03-26 23:34:40 +01:00
|
|
|
{
|
2020-01-05 22:49:00 +01:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2019-03-26 23:34:40 +01:00
|
|
|
//Must throw an exception
|
|
|
|
$this->service->listOperationsForPermission('invalid');
|
|
|
|
}
|
|
|
|
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testisValidPermission(): void
|
2019-03-26 23:34:40 +01:00
|
|
|
{
|
|
|
|
$this->assertTrue($this->service->isValidPermission('parts'));
|
|
|
|
$this->assertFalse($this->service->isValidPermission('invalid'));
|
|
|
|
}
|
|
|
|
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testIsValidOperation(): void
|
2019-03-26 23:34:40 +01:00
|
|
|
{
|
|
|
|
$this->assertTrue($this->service->isValidOperation('parts', 'read'));
|
|
|
|
|
|
|
|
//Must return false if either the permission or the operation is not existing
|
|
|
|
$this->assertFalse($this->service->isValidOperation('parts', 'invalid'));
|
|
|
|
$this->assertFalse($this->service->isValidOperation('invalid', 'read'));
|
|
|
|
$this->assertFalse($this->service->isValidOperation('invalid', 'invalid'));
|
|
|
|
}
|
|
|
|
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testDontInherit(): void
|
2019-03-26 23:34:40 +01:00
|
|
|
{
|
|
|
|
//Check with faked object
|
|
|
|
$this->assertTrue($this->service->dontInherit($this->user, 'parts', 'read'));
|
|
|
|
$this->assertFalse($this->service->dontInherit($this->user, 'parts', 'edit'));
|
|
|
|
$this->assertNull($this->service->dontInherit($this->user, 'parts', 'create'));
|
2019-09-22 21:35:22 +02:00
|
|
|
$this->assertNull($this->service->dontInherit($this->user, 'parts', 'show_history'));
|
2019-03-26 23:34:40 +01:00
|
|
|
$this->assertNull($this->service->dontInherit($this->user, 'parts', 'delete'));
|
2019-10-26 22:27:04 +02:00
|
|
|
|
|
|
|
//Test for user without group
|
|
|
|
$this->assertTrue($this->service->dontInherit($this->user_withoutGroup, 'parts', 'read'));
|
|
|
|
$this->assertFalse($this->service->dontInherit($this->user_withoutGroup, 'parts', 'edit'));
|
|
|
|
$this->assertNull($this->service->dontInherit($this->user_withoutGroup, 'parts', 'create'));
|
|
|
|
$this->assertNull($this->service->dontInherit($this->user_withoutGroup, 'parts', 'show_history'));
|
|
|
|
$this->assertNull($this->service->dontInherit($this->user_withoutGroup, 'parts', 'delete'));
|
2019-03-26 23:34:40 +01:00
|
|
|
}
|
|
|
|
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testInherit(): void
|
2019-03-26 23:34:40 +01:00
|
|
|
{
|
|
|
|
//Not inherited values should be same as dont inherit:
|
2019-10-26 22:27:04 +02:00
|
|
|
$this->assertTrue($this->service->inherit($this->user, 'parts', 'read'));
|
|
|
|
$this->assertFalse($this->service->inherit($this->user, 'parts', 'edit'));
|
2019-03-26 23:34:40 +01:00
|
|
|
//When thing can not be resolved null should be returned
|
2019-10-26 22:27:04 +02:00
|
|
|
$this->assertNull($this->service->inherit($this->user, 'parts', 'create'));
|
2019-03-26 23:34:40 +01:00
|
|
|
|
|
|
|
//Check for inherit from group
|
2019-09-22 21:35:22 +02:00
|
|
|
$this->assertTrue($this->service->inherit($this->user, 'parts', 'show_history'));
|
2019-03-26 23:34:40 +01:00
|
|
|
$this->assertFalse($this->service->inherit($this->user, 'parts', 'delete'));
|
|
|
|
$this->assertNull($this->service->inherit($this->user, 'parts', 'search'));
|
|
|
|
|
|
|
|
//Check for inherit from group and parent group
|
|
|
|
$this->assertTrue($this->service->inherit($this->user, 'parts', 'all_parts'));
|
|
|
|
$this->assertFalse($this->service->inherit($this->user, 'parts', 'no_price_parts'));
|
|
|
|
$this->assertNull($this->service->inherit($this->user, 'parts', 'obsolete_parts'));
|
2019-10-26 22:27:04 +02:00
|
|
|
|
|
|
|
//Test for user without group
|
|
|
|
$this->assertTrue($this->service->inherit($this->user_withoutGroup, 'parts', 'read'));
|
|
|
|
$this->assertFalse($this->service->inherit($this->user_withoutGroup, 'parts', 'edit'));
|
|
|
|
$this->assertNull($this->service->inherit($this->user_withoutGroup, 'parts', 'create'));
|
|
|
|
$this->assertNull($this->service->inherit($this->user_withoutGroup, 'parts', 'show_history'));
|
|
|
|
$this->assertNull($this->service->inherit($this->user_withoutGroup, 'parts', 'delete'));
|
2019-03-26 23:34:40 +01:00
|
|
|
}
|
2019-11-09 00:47:20 +01:00
|
|
|
}
|