2017-02-18 00:22:50 +00:00
|
|
|
<?php
|
2017-11-17 01:23:18 +00:00
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
2017-02-18 00:22:50 +00:00
|
|
|
declare(strict_types=1);
|
2017-03-28 04:12:12 +00:00
|
|
|
namespace JKingWeb\Arsse\Test\User;
|
2017-08-29 14:50:31 +00:00
|
|
|
|
2017-07-17 11:47:57 +00:00
|
|
|
use JKingWeb\Arsse\Arsse;
|
2017-03-28 22:50:00 +00:00
|
|
|
use JKingWeb\Arsse\Conf;
|
|
|
|
use JKingWeb\Arsse\User;
|
2017-03-28 04:12:12 +00:00
|
|
|
use JKingWeb\Arsse\User\Driver;
|
2017-05-11 22:00:35 +00:00
|
|
|
use Phake;
|
2017-02-19 05:22:16 +00:00
|
|
|
|
2017-03-03 01:47:00 +00:00
|
|
|
trait CommonTests {
|
2017-08-29 14:50:31 +00:00
|
|
|
public function setUp() {
|
2017-04-07 01:41:21 +00:00
|
|
|
$this->clearData();
|
|
|
|
$conf = new Conf();
|
|
|
|
$conf->userDriver = $this->drv;
|
2017-07-15 17:33:17 +00:00
|
|
|
$conf->userPreAuth = false;
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$conf = $conf;
|
|
|
|
Arsse::$db = new Database();
|
2017-07-22 19:29:12 +00:00
|
|
|
Arsse::$user = Phake::partialMock(User::class);
|
2017-07-17 11:47:57 +00:00
|
|
|
Phake::when(Arsse::$user)->authorize->thenReturn(true);
|
2017-04-07 01:41:21 +00:00
|
|
|
$_SERVER['PHP_AUTH_USER'] = self::USER1;
|
|
|
|
$_SERVER['PHP_AUTH_PW'] = "secret";
|
2017-03-28 22:50:00 +00:00
|
|
|
// call the additional setup method if it exists
|
2017-08-29 14:50:31 +00:00
|
|
|
if (method_exists($this, "setUpSeries")) {
|
|
|
|
$this->setUpSeries();
|
|
|
|
}
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
2017-03-28 22:50:00 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function tearDown() {
|
2017-04-07 01:41:21 +00:00
|
|
|
$this->clearData();
|
2017-03-28 22:50:00 +00:00
|
|
|
// call the additional teardiwn method if it exists
|
2017-08-29 14:50:31 +00:00
|
|
|
if (method_exists($this, "tearDownSeries")) {
|
|
|
|
$this->tearDownSeries();
|
|
|
|
}
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testListUsers() {
|
|
|
|
$this->assertCount(0, Arsse::$user->list());
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testCheckIfAUserDoesNotExist() {
|
2017-07-17 11:47:57 +00:00
|
|
|
$this->assertFalse(Arsse::$user->exists(self::USER1));
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testAddAUser() {
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$user->add(self::USER1, "");
|
2017-08-29 14:50:31 +00:00
|
|
|
$this->assertCount(1, Arsse::$user->list());
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testCheckIfAUserDoesExist() {
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$user->add(self::USER1, "");
|
|
|
|
$this->assertTrue(Arsse::$user->exists(self::USER1));
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testAddADuplicateUser() {
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$user->add(self::USER1, "");
|
2017-04-07 01:41:21 +00:00
|
|
|
$this->assertException("alreadyExists", "User");
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$user->add(self::USER1, "");
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testAddMultipleUsers() {
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$user->add(self::USER1, "");
|
|
|
|
Arsse::$user->add(self::USER2, "");
|
2017-08-29 14:50:31 +00:00
|
|
|
$this->assertCount(2, Arsse::$user->list());
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testRemoveAUser() {
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$user->add(self::USER1, "");
|
2017-08-29 14:50:31 +00:00
|
|
|
$this->assertCount(1, Arsse::$user->list());
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$user->remove(self::USER1);
|
2017-08-29 14:50:31 +00:00
|
|
|
$this->assertCount(0, Arsse::$user->list());
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testRemoveAMissingUser() {
|
2017-04-07 01:41:21 +00:00
|
|
|
$this->assertException("doesNotExist", "User");
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$user->remove(self::USER1);
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testAuthenticateAUser() {
|
2017-04-07 01:41:21 +00:00
|
|
|
$_SERVER['PHP_AUTH_USER'] = self::USER1;
|
|
|
|
$_SERVER['PHP_AUTH_PW'] = "secret";
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$user->add(self::USER1, "secret");
|
|
|
|
Arsse::$user->add(self::USER2, "");
|
|
|
|
$this->assertTrue(Arsse::$user->auth());
|
|
|
|
$this->assertTrue(Arsse::$user->auth(self::USER1, "secret"));
|
|
|
|
$this->assertFalse(Arsse::$user->auth(self::USER1, "superman"));
|
|
|
|
$this->assertTrue(Arsse::$user->auth(self::USER2, ""));
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testChangeAPassword() {
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$user->add(self::USER1, "secret");
|
|
|
|
$this->assertEquals("superman", Arsse::$user->passwordSet(self::USER1, "superman"));
|
|
|
|
$this->assertTrue(Arsse::$user->auth(self::USER1, "superman"));
|
|
|
|
$this->assertFalse(Arsse::$user->auth(self::USER1, "secret"));
|
|
|
|
$this->assertEquals("", Arsse::$user->passwordSet(self::USER1, ""));
|
|
|
|
$this->assertTrue(Arsse::$user->auth(self::USER1, ""));
|
|
|
|
$this->assertEquals(Arsse::$conf->userTempPasswordLength, strlen(Arsse::$user->passwordSet(self::USER1)));
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testChangeAPasswordForAMissingUser() {
|
2017-04-07 01:41:21 +00:00
|
|
|
$this->assertException("doesNotExist", "User");
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$user->passwordSet(self::USER1, "superman");
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testGetThePropertiesOfAUser() {
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$user->add(self::USER1, "secret");
|
|
|
|
$p = Arsse::$user->propertiesGet(self::USER1);
|
2017-04-07 01:41:21 +00:00
|
|
|
$this->assertArrayHasKey('id', $p);
|
|
|
|
$this->assertArrayHasKey('name', $p);
|
|
|
|
$this->assertArrayHasKey('domain', $p);
|
|
|
|
$this->assertArrayHasKey('rights', $p);
|
|
|
|
$this->assertArrayNotHasKey('password', $p);
|
|
|
|
$this->assertEquals(self::USER1, $p['name']);
|
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testSetThePropertiesOfAUser() {
|
2017-04-07 01:41:21 +00:00
|
|
|
$pSet = [
|
|
|
|
'name' => 'John Doe',
|
|
|
|
'id' => 'invalid',
|
|
|
|
'domain' => 'localhost',
|
|
|
|
'rights' => Driver::RIGHTS_GLOBAL_ADMIN,
|
|
|
|
'password' => 'superman',
|
|
|
|
];
|
|
|
|
$pGet = [
|
|
|
|
'name' => 'John Doe',
|
|
|
|
'id' => self::USER1,
|
|
|
|
'domain' => 'example.com',
|
|
|
|
'rights' => Driver::RIGHTS_NONE,
|
|
|
|
];
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$user->add(self::USER1, "secret");
|
|
|
|
Arsse::$user->propertiesSet(self::USER1, $pSet);
|
|
|
|
$p = Arsse::$user->propertiesGet(self::USER1);
|
2017-04-07 01:41:21 +00:00
|
|
|
$this->assertArraySubset($pGet, $p);
|
|
|
|
$this->assertArrayNotHasKey('password', $p);
|
2017-07-17 11:47:57 +00:00
|
|
|
$this->assertFalse(Arsse::$user->auth(self::USER1, "superman"));
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testGetTheRightsOfAUser() {
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$user->add(self::USER1, "");
|
|
|
|
$this->assertEquals(Driver::RIGHTS_NONE, Arsse::$user->rightsGet(self::USER1));
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testSetTheRightsOfAUser() {
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$user->add(self::USER1, "");
|
|
|
|
Arsse::$user->rightsSet(self::USER1, Driver::RIGHTS_GLOBAL_ADMIN);
|
|
|
|
$this->assertEquals(Driver::RIGHTS_GLOBAL_ADMIN, Arsse::$user->rightsGet(self::USER1));
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
}
|