2017-02-18 00:22:50 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
2017-03-28 04:12:12 +00:00
|
|
|
namespace JKingWeb\Arsse\Test\User;
|
2017-03-28 22:50:00 +00:00
|
|
|
use JKingWeb\Arsse\Data;
|
|
|
|
use JKingWeb\Arsse\Conf;
|
|
|
|
use JKingWeb\Arsse\User;
|
2017-03-28 04:12:12 +00:00
|
|
|
use JKingWeb\Arsse\User\Driver;
|
2017-02-19 05:22:16 +00:00
|
|
|
|
2017-03-03 01:47:00 +00:00
|
|
|
trait CommonTests {
|
2017-03-28 22:50:00 +00:00
|
|
|
|
|
|
|
function setUp() {
|
|
|
|
$this->clearData();
|
|
|
|
$conf = new Conf();
|
|
|
|
$conf->userDriver = $this->drv;
|
|
|
|
$conf->userAuthPreferHTTP = true;
|
|
|
|
Data::$conf = $conf;
|
|
|
|
Data::$db = new Database();
|
|
|
|
Data::$user = new User();
|
|
|
|
Data::$user->authorizationEnabled(false);
|
|
|
|
$_SERVER['PHP_AUTH_USER'] = self::USER1;
|
|
|
|
$_SERVER['PHP_AUTH_PW'] = "secret";
|
|
|
|
// call the additional setup method if it exists
|
|
|
|
if(method_exists($this, "setUpSeries")) $this->setUpSeries();
|
|
|
|
}
|
|
|
|
|
|
|
|
function tearDown() {
|
|
|
|
$this->clearData();
|
|
|
|
// call the additional teardiwn method if it exists
|
|
|
|
if(method_exists($this, "tearDownSeries")) $this->tearDownSeries();
|
|
|
|
}
|
|
|
|
|
2017-02-20 22:04:13 +00:00
|
|
|
function testListUsers() {
|
2017-03-28 22:50:00 +00:00
|
|
|
$this->assertCount(0,Data::$user->list());
|
2017-02-20 22:04:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testCheckIfAUserDoesNotExist() {
|
2017-03-28 22:50:00 +00:00
|
|
|
$this->assertFalse(Data::$user->exists(self::USER1));
|
2017-02-20 22:04:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testAddAUser() {
|
2017-03-28 22:50:00 +00:00
|
|
|
Data::$user->add(self::USER1, "");
|
|
|
|
$this->assertCount(1,Data::$user->list());
|
2017-02-20 22:04:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testCheckIfAUserDoesExist() {
|
2017-03-28 22:50:00 +00:00
|
|
|
Data::$user->add(self::USER1, "");
|
|
|
|
$this->assertTrue(Data::$user->exists(self::USER1));
|
2017-02-20 22:04:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testAddADuplicateUser() {
|
2017-03-28 22:50:00 +00:00
|
|
|
Data::$user->add(self::USER1, "");
|
2017-02-19 05:22:16 +00:00
|
|
|
$this->assertException("alreadyExists", "User");
|
2017-03-28 22:50:00 +00:00
|
|
|
Data::$user->add(self::USER1, "");
|
2017-02-20 22:04:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testAddMultipleUsers() {
|
2017-03-28 22:50:00 +00:00
|
|
|
Data::$user->add(self::USER1, "");
|
|
|
|
Data::$user->add(self::USER2, "");
|
|
|
|
$this->assertCount(2,Data::$user->list());
|
2017-02-20 22:04:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testRemoveAUser() {
|
2017-03-28 22:50:00 +00:00
|
|
|
Data::$user->add(self::USER1, "");
|
|
|
|
$this->assertCount(1,Data::$user->list());
|
|
|
|
Data::$user->remove(self::USER1);
|
|
|
|
$this->assertCount(0,Data::$user->list());
|
2017-02-20 22:04:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testRemoveAMissingUser() {
|
|
|
|
$this->assertException("doesNotExist", "User");
|
2017-03-28 22:50:00 +00:00
|
|
|
Data::$user->remove(self::USER1);
|
2017-02-20 22:04:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testAuthenticateAUser() {
|
2017-03-03 01:47:00 +00:00
|
|
|
$_SERVER['PHP_AUTH_USER'] = self::USER1;
|
|
|
|
$_SERVER['PHP_AUTH_PW'] = "secret";
|
2017-03-28 22:50:00 +00:00
|
|
|
Data::$user->add(self::USER1, "secret");
|
|
|
|
Data::$user->add(self::USER2, "");
|
|
|
|
$this->assertTrue(Data::$user->auth());
|
|
|
|
$this->assertTrue(Data::$user->auth(self::USER1, "secret"));
|
|
|
|
$this->assertFalse(Data::$user->auth(self::USER1, "superman"));
|
|
|
|
$this->assertTrue(Data::$user->auth(self::USER2, ""));
|
2017-02-18 00:22:50 +00:00
|
|
|
}
|
2017-02-21 00:04:08 +00:00
|
|
|
|
|
|
|
function testChangeAPassword() {
|
2017-03-28 22:50:00 +00:00
|
|
|
Data::$user->add(self::USER1, "secret");
|
|
|
|
$this->assertEquals("superman", Data::$user->passwordSet(self::USER1, "superman"));
|
|
|
|
$this->assertTrue(Data::$user->auth(self::USER1, "superman"));
|
|
|
|
$this->assertFalse(Data::$user->auth(self::USER1, "secret"));
|
|
|
|
$this->assertEquals("", Data::$user->passwordSet(self::USER1, ""));
|
|
|
|
$this->assertTrue(Data::$user->auth(self::USER1, ""));
|
|
|
|
$this->assertEquals(Data::$conf->userTempPasswordLength, strlen(Data::$user->passwordSet(self::USER1)));
|
2017-02-21 00:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testChangeAPasswordForAMissingUser() {
|
|
|
|
$this->assertException("doesNotExist", "User");
|
2017-03-28 22:50:00 +00:00
|
|
|
Data::$user->passwordSet(self::USER1, "superman");
|
2017-02-21 00:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testGetThePropertiesOfAUser() {
|
2017-03-28 22:50:00 +00:00
|
|
|
Data::$user->add(self::USER1, "secret");
|
|
|
|
$p = Data::$user->propertiesGet(self::USER1);
|
2017-02-25 17:59:39 +00:00
|
|
|
$this->assertArrayHasKey('id', $p);
|
2017-02-21 00:04:08 +00:00
|
|
|
$this->assertArrayHasKey('name', $p);
|
2017-02-25 17:59:39 +00:00
|
|
|
$this->assertArrayHasKey('domain', $p);
|
|
|
|
$this->assertArrayHasKey('rights', $p);
|
2017-02-21 00:04:08 +00:00
|
|
|
$this->assertArrayNotHasKey('password', $p);
|
|
|
|
$this->assertEquals(self::USER1, $p['name']);
|
|
|
|
}
|
2017-02-25 17:59:39 +00:00
|
|
|
|
|
|
|
function testSetThePropertiesOfAUser() {
|
|
|
|
$pSet = [
|
2017-02-27 13:05:10 +00:00
|
|
|
'name' => 'John Doe',
|
|
|
|
'id' => 'invalid',
|
|
|
|
'domain' => 'localhost',
|
2017-03-03 01:47:00 +00:00
|
|
|
'rights' => Driver::RIGHTS_GLOBAL_ADMIN,
|
2017-02-25 17:59:39 +00:00
|
|
|
'password' => 'superman',
|
|
|
|
];
|
|
|
|
$pGet = [
|
2017-02-27 13:05:10 +00:00
|
|
|
'name' => 'John Doe',
|
|
|
|
'id' => self::USER1,
|
2017-02-25 17:59:39 +00:00
|
|
|
'domain' => 'example.com',
|
2017-03-03 01:47:00 +00:00
|
|
|
'rights' => Driver::RIGHTS_NONE,
|
2017-02-25 17:59:39 +00:00
|
|
|
];
|
2017-03-28 22:50:00 +00:00
|
|
|
Data::$user->add(self::USER1, "secret");
|
|
|
|
Data::$user->propertiesSet(self::USER1, $pSet);
|
|
|
|
$p = Data::$user->propertiesGet(self::USER1);
|
2017-02-25 17:59:39 +00:00
|
|
|
$this->assertArraySubset($pGet, $p);
|
|
|
|
$this->assertArrayNotHasKey('password', $p);
|
2017-03-28 22:50:00 +00:00
|
|
|
$this->assertFalse(Data::$user->auth(self::USER1, "superman"));
|
2017-02-25 17:59:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testGetTheRightsOfAUser() {
|
2017-03-28 22:50:00 +00:00
|
|
|
Data::$user->add(self::USER1, "");
|
|
|
|
$this->assertEquals(Driver::RIGHTS_NONE, Data::$user->rightsGet(self::USER1));
|
2017-02-25 17:59:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testSetTheRightsOfAUser() {
|
2017-03-28 22:50:00 +00:00
|
|
|
Data::$user->add(self::USER1, "");
|
|
|
|
Data::$user->rightsSet(self::USER1, Driver::RIGHTS_GLOBAL_ADMIN);
|
|
|
|
$this->assertEquals(Driver::RIGHTS_GLOBAL_ADMIN, Data::$user->rightsGet(self::USER1));
|
2017-02-25 17:59:39 +00:00
|
|
|
}
|
2017-03-03 01:47:00 +00:00
|
|
|
}
|