From 2c9f6bd5a737f3dba9cdbbae0edaaa7040c8f37d Mon Sep 17 00:00:00 2001 From: "J. King" Date: Tue, 28 Feb 2017 12:37:45 -0500 Subject: [PATCH] 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 --- tests/User/TestAuthorization.php | 88 +++++++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 2 deletions(-) diff --git a/tests/User/TestAuthorization.php b/tests/User/TestAuthorization.php index c5966651..58a4700d 100644 --- a/tests/User/TestAuthorization.php +++ b/tests/User/TestAuthorization.php @@ -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; + } } \ No newline at end of file