mirror of
https://code.mensbeam.com/MensBeam/Arsse.git
synced 2025-01-03 14:32:40 +00:00
Start on tests for authorization
This commit is contained in:
parent
44c2ff5b07
commit
e4852b581a
5 changed files with 52 additions and 1 deletions
|
@ -9,6 +9,7 @@ class User {
|
||||||
protected $u;
|
protected $u;
|
||||||
protected $authz = true;
|
protected $authz = true;
|
||||||
protected $authzSupported = 0;
|
protected $authzSupported = 0;
|
||||||
|
protected $actor = [];
|
||||||
|
|
||||||
static public function listDrivers(): array {
|
static public function listDrivers(): array {
|
||||||
$sep = \DIRECTORY_SEPARATOR;
|
$sep = \DIRECTORY_SEPARATOR;
|
||||||
|
@ -108,6 +109,7 @@ class User {
|
||||||
if($this->data->conf->userAuthPreferHTTP) return $this->authHTTP();
|
if($this->data->conf->userAuthPreferHTTP) return $this->authHTTP();
|
||||||
return $this->authForm();
|
return $this->authForm();
|
||||||
} else {
|
} else {
|
||||||
|
$this->id = $user;
|
||||||
switch($this->u->driverFunctions("auth")) {
|
switch($this->u->driverFunctions("auth")) {
|
||||||
case User\Driver::FUNC_EXTERNAL:
|
case User\Driver::FUNC_EXTERNAL:
|
||||||
$out = $this->u->auth($user, $password);
|
$out = $this->u->auth($user, $password);
|
||||||
|
|
47
tests/User/TestAuthorization.php
Normal file
47
tests/User/TestAuthorization.php
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace JKingWeb\NewsSync;
|
||||||
|
|
||||||
|
|
||||||
|
class TestAuthorization extends \PHPUnit\Framework\TestCase {
|
||||||
|
use Test\Tools;
|
||||||
|
|
||||||
|
const USER1 = "john.doe@example.com";
|
||||||
|
const USER2 = "jane.doe@example.org";
|
||||||
|
|
||||||
|
protected $data;
|
||||||
|
|
||||||
|
function setUp() {
|
||||||
|
$drv = Test\User\DriverInternalMock::class;
|
||||||
|
$conf = new Conf();
|
||||||
|
$conf->userDriver = $drv;
|
||||||
|
$conf->userAuthPreferHTTP = true;
|
||||||
|
$this->data = new Test\RuntimeData($conf);
|
||||||
|
$this->data->user = new User($this->data);
|
||||||
|
$this->data->user->authorizationEnabled(false);
|
||||||
|
$users = [
|
||||||
|
'user@example.com' => User\Driver::RIGHTS_NONE,
|
||||||
|
'user@example.org' => User\Driver::RIGHTS_NONE,
|
||||||
|
'dman@example.com' => User\Driver::RIGHTS_DOMAIN_MANAGER,
|
||||||
|
'dman@example.org' => User\Driver::RIGHTS_DOMAIN_MANAGER,
|
||||||
|
'dadm@example.com' => User\Driver::RIGHTS_DOMAIN_ADMIN,
|
||||||
|
'dadm@example.org' => User\Driver::RIGHTS_DOMAIN_ADMIN,
|
||||||
|
'gman@example.com' => User\Driver::RIGHTS_GLOBAL_MANAGER,
|
||||||
|
'gman@example.org' => User\Driver::RIGHTS_GLOBAL_MANAGER,
|
||||||
|
'gadm@example.com' => User\Driver::RIGHTS_GLOBAL_ADMIN,
|
||||||
|
'gadm@example.org' => User\Driver::RIGHTS_GLOBAL_ADMIN,
|
||||||
|
];
|
||||||
|
foreach($users as $user => $level) {
|
||||||
|
$this->data->user->add($user, "");
|
||||||
|
$this->data->user->rightsSet($user, $level);
|
||||||
|
}
|
||||||
|
$this->data->user->authorizationEnabled(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testRegularUserActingOnSelf() {
|
||||||
|
$u = "user@example.com";
|
||||||
|
$this->data->user->auth($u, "");
|
||||||
|
$this->data->user->remove($u);
|
||||||
|
$this->assertFalse($this->data->user->exists($u));
|
||||||
|
}
|
||||||
|
}
|
|
@ -43,6 +43,7 @@ class DriverInternalMock extends Database implements Driver {
|
||||||
|
|
||||||
function auth(string $user, string $password): bool {
|
function auth(string $user, string $password): bool {
|
||||||
if(!$this->userExists($user)) return false;
|
if(!$this->userExists($user)) return false;
|
||||||
|
if($password==="" && $this->db[$user]['password']==="") return true;
|
||||||
if(password_verify($password, $this->db[$user]['password'])) return true;
|
if(password_verify($password, $this->db[$user]['password'])) return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@ abstract class DriverSkeleton {
|
||||||
|
|
||||||
function userAdd(string $user, string $password = null): string {
|
function userAdd(string $user, string $password = null): string {
|
||||||
$u = [
|
$u = [
|
||||||
'password' => $password ? password_hash($password, \PASSWORD_DEFAULT) : null,
|
'password' => $password ? password_hash($password, \PASSWORD_DEFAULT) : "",
|
||||||
'rights' => Driver::RIGHTS_NONE,
|
'rights' => Driver::RIGHTS_NONE,
|
||||||
];
|
];
|
||||||
$this->db[$user] = $u;
|
$this->db[$user] = $u;
|
||||||
|
|
|
@ -24,6 +24,7 @@
|
||||||
<testsuite name="User management">
|
<testsuite name="User management">
|
||||||
<file>User/TestUser.php</file>
|
<file>User/TestUser.php</file>
|
||||||
<file>User/TestUserExternal.php</file>
|
<file>User/TestUserExternal.php</file>
|
||||||
|
<file>User/TestAuthorization.php</file>
|
||||||
</testsuite>
|
</testsuite>
|
||||||
|
|
||||||
</phpunit>
|
</phpunit>
|
Loading…
Reference in a new issue