1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2025-01-03 14:32: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:
J. King 2017-02-28 12:37:45 -05:00
parent 766f2b65a4
commit 2c9f6bd5a7

View file

@ -45,13 +45,15 @@ class TestAuthorization extends \PHPUnit\Framework\TestCase {
protected $data; protected $data;
function setUp() { function setUp(string $drv = Test\User\DriverInternalMock::class, string $db = null) {
$drv = Test\User\DriverInternalMock::class;
$conf = new Conf(); $conf = new Conf();
$conf->userDriver = $drv; $conf->userDriver = $drv;
$conf->userAuthPreferHTTP = true; $conf->userAuthPreferHTTP = true;
$conf->userComposeNames = true; $conf->userComposeNames = true;
$this->data = new Test\RuntimeData($conf); $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 = new User($this->data);
$this->data->user->authorizationEnabled(false); $this->data->user->authorizationEnabled(false);
foreach(self::USERS as $user => $level) { 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;
}
} }