mirror of
https://code.mensbeam.com/MensBeam/Arsse.git
synced 2024-12-22 21:22:40 +00:00
Cover authorization exceptions
Check each of the current User class features and ensure they trigger an exception when they should and do not when they shouldn't. Tests check against both an internal and external mock driver
This commit is contained in:
parent
766f2b65a4
commit
2c9f6bd5a7
1 changed files with 86 additions and 2 deletions
|
@ -45,13 +45,15 @@ class TestAuthorization extends \PHPUnit\Framework\TestCase {
|
|||
|
||||
protected $data;
|
||||
|
||||
function setUp() {
|
||||
$drv = Test\User\DriverInternalMock::class;
|
||||
function setUp(string $drv = Test\User\DriverInternalMock::class, string $db = null) {
|
||||
$conf = new Conf();
|
||||
$conf->userDriver = $drv;
|
||||
$conf->userAuthPreferHTTP = true;
|
||||
$conf->userComposeNames = true;
|
||||
$this->data = new Test\RuntimeData($conf);
|
||||
if($db !== null) {
|
||||
$this->data->db = new $db($this->data);
|
||||
}
|
||||
$this->data->user = new User($this->data);
|
||||
$this->data->user->authorizationEnabled(false);
|
||||
foreach(self::USERS as $user => $level) {
|
||||
|
@ -247,4 +249,86 @@ class TestAuthorization extends \PHPUnit\Framework\TestCase {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
function testInternalExceptionLogic() {
|
||||
$failTest = [
|
||||
'userExists',
|
||||
'userRemove',
|
||||
'userAdd',
|
||||
'userPasswordSet',
|
||||
'userPropertiesGet',
|
||||
'userPropertiesSet',
|
||||
'userRightsGet',
|
||||
'userRightsSet',
|
||||
'userList',
|
||||
];
|
||||
$passTest = [
|
||||
'userRightsSet',
|
||||
'userList'
|
||||
];
|
||||
sort($failTest);
|
||||
sort($passTest);
|
||||
$actor = "user@example.com";
|
||||
$this->data->user->auth($actor, "");
|
||||
$this->assertEquals($passTest, $this->checkExceptions($actor));
|
||||
$this->assertEquals($failTest, $this->checkExceptions("user@example.org"));
|
||||
}
|
||||
|
||||
function testExternalExceptionLogic() {
|
||||
// set up the test for an external driver
|
||||
$this->setUp(Test\User\DriverExternalMock::class, Test\User\Database::class);
|
||||
// run the previous test with the external driver set up
|
||||
$this->testInternalExceptionLogic();
|
||||
}
|
||||
|
||||
protected function checkExceptions(string $user): array {
|
||||
$err = [];
|
||||
try {
|
||||
$this->data->user->exists($user);
|
||||
} catch(User\ExceptionAuthz $e) {
|
||||
$err[] = "userExists";
|
||||
}
|
||||
try {
|
||||
$this->data->user->remove($user);
|
||||
} catch(User\ExceptionAuthz $e) {
|
||||
$err[] = "userRemove";
|
||||
}
|
||||
try {
|
||||
$this->data->user->add($user, "");
|
||||
} catch(User\ExceptionAuthz $e) {
|
||||
$err[] = "userAdd";
|
||||
}
|
||||
try {
|
||||
$this->data->user->passwordSet($user, "");
|
||||
} catch(User\ExceptionAuthz $e) {
|
||||
$err[] = "userPasswordSet";
|
||||
}
|
||||
try {
|
||||
$this->data->user->propertiesGet($user);
|
||||
} catch(User\ExceptionAuthz $e) {
|
||||
$err[] = "userPropertiesGet";
|
||||
}
|
||||
try {
|
||||
$this->data->user->propertiesSet($user, []);
|
||||
} catch(User\ExceptionAuthz $e) {
|
||||
$err[] = "userPropertiesSet";
|
||||
}
|
||||
try {
|
||||
$this->data->user->rightsGet($user);
|
||||
} catch(User\ExceptionAuthz $e) {
|
||||
$err[] = "userRightsGet";
|
||||
}
|
||||
try {
|
||||
$this->data->user->rightsSet($user, 10000);
|
||||
} catch(User\ExceptionAuthz $e) {
|
||||
$err[] = "userRightsSet";
|
||||
}
|
||||
try {
|
||||
$this->data->user->list();
|
||||
} catch(User\ExceptionAuthz $e) {
|
||||
$err[] = "userList";
|
||||
}
|
||||
sort($err);
|
||||
return $err;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue