2018-11-02 14:02:37 +00:00
|
|
|
<?php
|
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace JKingWeb\Arsse\TestCase\User;
|
|
|
|
|
2021-03-01 23:01:25 +00:00
|
|
|
use Eloquent\Phony\Phpunit\Phony;
|
2018-11-02 14:02:37 +00:00
|
|
|
use JKingWeb\Arsse\Arsse;
|
|
|
|
use JKingWeb\Arsse\Database;
|
|
|
|
use JKingWeb\Arsse\User;
|
2020-12-25 22:47:36 +00:00
|
|
|
use JKingWeb\Arsse\Db\Transaction;
|
2020-11-15 21:24:26 +00:00
|
|
|
use JKingWeb\Arsse\User\ExceptionConflict;
|
2020-11-16 05:11:19 +00:00
|
|
|
use JKingWeb\Arsse\User\ExceptionInput;
|
2018-11-02 14:02:37 +00:00
|
|
|
use JKingWeb\Arsse\User\Driver;
|
|
|
|
|
|
|
|
/** @covers \JKingWeb\Arsse\User */
|
|
|
|
class TestUser extends \JKingWeb\Arsse\Test\AbstractTest {
|
2019-10-16 18:42:43 +00:00
|
|
|
public function setUp(): void {
|
2021-02-27 20:24:02 +00:00
|
|
|
parent::setUp();
|
2018-11-23 00:55:54 +00:00
|
|
|
self::setConf();
|
2018-11-02 14:02:37 +00:00
|
|
|
// create a mock database interface
|
2021-07-06 01:47:44 +00:00
|
|
|
$this->dbMock = $this->mock(Database::class);
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->dbMock->begin->returns($this->mock(\JKingWeb\Arsse\Db\Transaction::class));
|
2018-11-02 21:28:12 +00:00
|
|
|
// create a mock user driver
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv = $this->mock(Driver::class);
|
|
|
|
}
|
2021-07-06 01:47:44 +00:00
|
|
|
|
2021-03-01 23:01:25 +00:00
|
|
|
protected function prepTest(?\Closure $partialMockDef = null): User {
|
|
|
|
Arsse::$db = $this->dbMock->get();
|
|
|
|
if ($partialMockDef) {
|
|
|
|
$this->userMock = $this->partialMock(User::class, $this->drv->get());
|
|
|
|
$partialMockDef($this->userMock);
|
|
|
|
return $this->userMock->get();
|
|
|
|
} else {
|
|
|
|
return new User($this->drv->get());
|
|
|
|
}
|
2020-11-15 21:24:26 +00:00
|
|
|
}
|
2018-11-04 17:06:30 +00:00
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testConstruct(): void {
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->assertInstanceOf(User::class, new User($this->drv->get()));
|
2018-11-02 14:02:37 +00:00
|
|
|
$this->assertInstanceOf(User::class, new User);
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testConversionToString(): void {
|
2018-11-02 14:02:37 +00:00
|
|
|
$u = new User;
|
|
|
|
$u->id = "john.doe@example.com";
|
|
|
|
$this->assertSame("john.doe@example.com", (string) $u);
|
|
|
|
$u->id = null;
|
|
|
|
$this->assertSame("", (string) $u);
|
|
|
|
}
|
2020-11-23 14:31:50 +00:00
|
|
|
|
2020-12-25 22:47:36 +00:00
|
|
|
public function testStartATransaction(): void {
|
2021-03-01 23:01:25 +00:00
|
|
|
$u = $this->prepTest();
|
2020-12-25 22:47:36 +00:00
|
|
|
$this->assertInstanceOf(Transaction::class, $u->begin());
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->dbMock->begin->calledWith();
|
2020-12-25 22:47:36 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 21:23:36 +00:00
|
|
|
public function testGeneratePasswords(): void {
|
2021-03-01 23:01:25 +00:00
|
|
|
$u = $this->prepTest();
|
2020-11-17 21:23:36 +00:00
|
|
|
$pass1 = $u->generatePassword();
|
|
|
|
$pass2 = $u->generatePassword();
|
|
|
|
$this->assertNotEquals($pass1, $pass2);
|
|
|
|
}
|
2018-11-02 14:02:37 +00:00
|
|
|
|
|
|
|
/** @dataProvider provideAuthentication */
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testAuthenticateAUser(bool $preAuth, string $user, string $password, bool $exp): void {
|
2018-11-02 14:02:37 +00:00
|
|
|
Arsse::$conf->userPreAuth = $preAuth;
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->auth->returns(false);
|
|
|
|
$this->drv->auth->with("john.doe@example.com", "secret")->returns(true);
|
|
|
|
$this->drv->auth->with("jane.doe@example.com", "superman")->returns(true);
|
|
|
|
$this->dbMock->userExists->with("john.doe@example.com")->returns(true);
|
|
|
|
$this->dbMock->userExists->with("jane.doe@example.com")->returns(false);
|
|
|
|
$this->dbMock->userAdd->returns("");
|
|
|
|
$u = $this->prepTest();
|
2018-11-02 14:02:37 +00:00
|
|
|
$this->assertSame($exp, $u->auth($user, $password));
|
|
|
|
$this->assertNull($u->id);
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->auth->times((int) !$preAuth)->called();
|
|
|
|
$this->dbMock->userExists->times($exp ? 1 : 0)->calledWith($user);
|
|
|
|
$this->dbMock->userAdd->times($exp && $user === "jane.doe@example.com" ? 1 : 0)->calledWith($user, $password);
|
2018-11-02 14:02:37 +00:00
|
|
|
}
|
|
|
|
|
2019-10-16 18:42:43 +00:00
|
|
|
public function provideAuthentication(): iterable {
|
2018-11-02 14:02:37 +00:00
|
|
|
$john = "john.doe@example.com";
|
|
|
|
$jane = "jane.doe@example.com";
|
2018-11-02 21:28:12 +00:00
|
|
|
return [
|
2018-11-02 14:02:37 +00:00
|
|
|
[false, $john, "secret", true],
|
|
|
|
[false, $john, "superman", false],
|
|
|
|
[false, $jane, "secret", false],
|
|
|
|
[false, $jane, "superman", true],
|
|
|
|
[true, $john, "secret", true],
|
|
|
|
[true, $john, "superman", true],
|
|
|
|
[true, $jane, "secret", true],
|
|
|
|
[true, $jane, "superman", true],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-11-15 21:24:26 +00:00
|
|
|
public function testListUsers(): void {
|
|
|
|
$exp = ["john.doe@example.com", "jane.doe@example.com"];
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userList->returns(["john.doe@example.com", "jane.doe@example.com"]);
|
|
|
|
$u = $this->prepTest();
|
2018-11-02 14:02:37 +00:00
|
|
|
$this->assertSame($exp, $u->list());
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userList->calledWith();
|
2018-11-02 14:02:37 +00:00
|
|
|
}
|
|
|
|
|
2020-12-11 01:08:00 +00:00
|
|
|
public function testLookUpAUserByNumber(): void {
|
|
|
|
$exp = "john.doe@example.com";
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->dbMock->userLookup->returns($exp);
|
|
|
|
$u = $this->prepTest();
|
2020-12-11 01:08:00 +00:00
|
|
|
$this->assertSame($exp, $u->lookup(2112));
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->dbMock->userLookup->calledWith(2112);
|
2020-12-11 01:08:00 +00:00
|
|
|
}
|
|
|
|
|
2020-11-15 21:24:26 +00:00
|
|
|
public function testAddAUser(): void {
|
2020-11-16 05:11:19 +00:00
|
|
|
$user = "john.doe@example.com";
|
2020-11-15 21:24:26 +00:00
|
|
|
$pass = "secret";
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userAdd->returns($pass);
|
|
|
|
$this->dbMock->userExists->returns(true);
|
|
|
|
$u = $this->prepTest();
|
2020-11-15 21:24:26 +00:00
|
|
|
$this->assertSame($pass, $u->add($user, $pass));
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userAdd->calledWith($user, $pass);
|
|
|
|
$this->dbMock->userExists->calledWith($user);
|
2018-11-02 14:02:37 +00:00
|
|
|
}
|
|
|
|
|
2020-11-15 21:24:26 +00:00
|
|
|
public function testAddAUserWeDoNotKnow(): void {
|
2020-11-16 05:11:19 +00:00
|
|
|
$user = "john.doe@example.com";
|
2020-11-15 21:24:26 +00:00
|
|
|
$pass = "secret";
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userAdd->returns($pass);
|
|
|
|
$this->dbMock->userExists->returns(false);
|
|
|
|
$u = $this->prepTest();
|
2020-11-15 21:24:26 +00:00
|
|
|
$this->assertSame($pass, $u->add($user, $pass));
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userAdd->calledWith($user, $pass);
|
|
|
|
$this->dbMock->userExists->calledWith($user);
|
|
|
|
$this->dbMock->userAdd->calledWith($user, $pass);
|
2018-11-02 14:02:37 +00:00
|
|
|
}
|
|
|
|
|
2020-11-15 21:24:26 +00:00
|
|
|
public function testAddADuplicateUser(): void {
|
2020-11-16 05:11:19 +00:00
|
|
|
$user = "john.doe@example.com";
|
2020-11-15 21:24:26 +00:00
|
|
|
$pass = "secret";
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userAdd->throws(new ExceptionConflict("alreadyExists"));
|
|
|
|
$this->dbMock->userExists->returns(true);
|
|
|
|
$u = $this->prepTest();
|
2020-11-15 21:24:26 +00:00
|
|
|
$this->assertException("alreadyExists", "User", "ExceptionConflict");
|
2018-11-03 17:26:22 +00:00
|
|
|
try {
|
2020-11-15 21:24:26 +00:00
|
|
|
$u->add($user, $pass);
|
2018-11-03 17:26:22 +00:00
|
|
|
} finally {
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->dbMock->userExists->calledWith($user);
|
|
|
|
$this->drv->userAdd->calledWith($user, $pass);
|
2018-11-03 17:26:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-15 21:24:26 +00:00
|
|
|
public function testAddADuplicateUserWeDoNotKnow(): void {
|
2020-11-16 05:11:19 +00:00
|
|
|
$user = "john.doe@example.com";
|
2020-11-15 21:24:26 +00:00
|
|
|
$pass = "secret";
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userAdd->throws(new ExceptionConflict("alreadyExists"));
|
|
|
|
$this->dbMock->userExists->returns(false);
|
|
|
|
$u = $this->prepTest();
|
2020-11-15 21:24:26 +00:00
|
|
|
$this->assertException("alreadyExists", "User", "ExceptionConflict");
|
2019-03-24 18:42:23 +00:00
|
|
|
try {
|
2020-11-15 21:24:26 +00:00
|
|
|
$u->add($user, $pass);
|
2019-03-24 18:42:23 +00:00
|
|
|
} finally {
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->dbMock->userExists->calledWith($user);
|
|
|
|
$this->dbMock->userAdd->calledWith($user, null);
|
|
|
|
$this->drv->userAdd->calledWith($user, $pass);
|
2019-03-24 18:42:23 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-16 05:11:19 +00:00
|
|
|
|
2020-12-27 15:08:00 +00:00
|
|
|
/** @dataProvider provideInvalidUserNames */
|
|
|
|
public function testAddAnInvalidUser(string $user): void {
|
2021-03-01 23:01:25 +00:00
|
|
|
$u = $this->prepTest();
|
2020-11-16 05:11:19 +00:00
|
|
|
$this->assertException("invalidUsername", "User", "ExceptionInput");
|
2020-12-27 15:08:00 +00:00
|
|
|
$u->add($user, "secret");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideInvalidUserNames(): iterable {
|
|
|
|
// output names with control characters
|
|
|
|
foreach (array_merge(range(0x00, 0x1F), [0x7F]) as $ord) {
|
|
|
|
yield [chr($ord)];
|
|
|
|
yield ["john".chr($ord)."doe@example.com"];
|
|
|
|
}
|
|
|
|
// also handle colons
|
|
|
|
yield [":"];
|
|
|
|
yield ["john:doe@example.com"];
|
2020-11-16 05:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddAUserWithARandomPassword(): void {
|
|
|
|
$user = "john.doe@example.com";
|
|
|
|
$pass = "random password";
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userAdd->returns(null)->returns($pass);
|
|
|
|
$this->dbMock->userExists->returns(true);
|
2021-07-06 01:47:44 +00:00
|
|
|
$u = $this->prepTest(function($u) use ($pass) {
|
2021-03-01 23:01:25 +00:00
|
|
|
$u->generatePassword->returns($pass);
|
|
|
|
});
|
2020-11-16 05:11:19 +00:00
|
|
|
$this->assertSame($pass, $u->add($user));
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userAdd->calledWith($user, null);
|
|
|
|
$this->drv->userAdd->calledWith($user, $pass);
|
|
|
|
$this->dbMock->userExists->calledWith($user);
|
2020-11-16 05:11:19 +00:00
|
|
|
}
|
2020-11-23 14:31:50 +00:00
|
|
|
|
2020-12-25 22:47:36 +00:00
|
|
|
public function testRenameAUser(): void {
|
2021-03-01 23:01:25 +00:00
|
|
|
$tr = $this->mock(Transaction::class);
|
|
|
|
$this->dbMock->begin->returns($tr);
|
|
|
|
$this->dbMock->userExists->returns(true);
|
|
|
|
$this->dbMock->userAdd->returns(true);
|
|
|
|
$this->dbMock->userRename->returns(true);
|
|
|
|
$this->drv->userRename->returns(true);
|
|
|
|
$u = $this->prepTest();
|
2020-12-25 22:47:36 +00:00
|
|
|
$old = "john.doe@example.com";
|
2021-01-08 20:47:19 +00:00
|
|
|
$new = "jane.doe@example.com";
|
2020-12-25 22:47:36 +00:00
|
|
|
$this->assertTrue($u->rename($old, $new));
|
2021-03-01 23:01:25 +00:00
|
|
|
Phony::inOrder(
|
|
|
|
$this->drv->userRename->calledWith($old, $new),
|
|
|
|
$this->dbMock->begin->calledWith(),
|
|
|
|
$this->dbMock->userExists->calledWith($old),
|
|
|
|
$this->dbMock->userRename->calledWith($old, $new),
|
|
|
|
$this->dbMock->sessionDestroy->calledWith($new),
|
|
|
|
$this->dbMock->tokenRevoke->calledWith($new, "fever.login"),
|
|
|
|
$tr->commit->called()
|
2020-12-26 03:22:37 +00:00
|
|
|
);
|
2020-12-25 22:47:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testRenameAUserWeDoNotKnow(): void {
|
2021-03-01 23:01:25 +00:00
|
|
|
$tr = $this->mock(Transaction::class);
|
|
|
|
$this->dbMock->begin->returns($tr);
|
|
|
|
$this->dbMock->userExists->returns(false);
|
|
|
|
$this->dbMock->userAdd->returns(true);
|
|
|
|
$this->dbMock->userRename->returns(true);
|
|
|
|
$this->drv->userRename->returns(true);
|
|
|
|
$u = $this->prepTest();
|
2020-12-25 22:47:36 +00:00
|
|
|
$old = "john.doe@example.com";
|
2021-01-08 20:47:19 +00:00
|
|
|
$new = "jane.doe@example.com";
|
2020-12-25 22:47:36 +00:00
|
|
|
$this->assertTrue($u->rename($old, $new));
|
2021-03-01 23:01:25 +00:00
|
|
|
Phony::inOrder(
|
|
|
|
$this->drv->userRename->calledWith($old, $new),
|
|
|
|
$this->dbMock->begin->calledWith(),
|
|
|
|
$this->dbMock->userExists->calledWith($old),
|
|
|
|
$this->dbMock->userAdd->calledWith($new, null),
|
|
|
|
$tr->commit->called()
|
2020-12-26 03:22:37 +00:00
|
|
|
);
|
2020-12-25 22:47:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testRenameAUserWithoutEffect(): void {
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->dbMock->userExists->returns(false);
|
|
|
|
$this->dbMock->userAdd->returns(true);
|
|
|
|
$this->dbMock->userRename->returns(true);
|
|
|
|
$this->drv->userRename->returns(false);
|
|
|
|
$u = $this->prepTest();
|
2020-12-25 22:47:36 +00:00
|
|
|
$old = "john.doe@example.com";
|
|
|
|
$this->assertFalse($u->rename($old, $old));
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userRename->calledWith($old, $old);
|
2020-12-25 22:47:36 +00:00
|
|
|
}
|
|
|
|
|
2020-12-27 15:08:00 +00:00
|
|
|
/** @dataProvider provideInvalidUserNames */
|
|
|
|
public function testRenameAUserToAnInvalidName(string $new): void {
|
2021-03-01 23:01:25 +00:00
|
|
|
$u = $this->prepTest();
|
2020-12-27 15:08:00 +00:00
|
|
|
$this->assertException("invalidUsername", "User", "ExceptionInput");
|
|
|
|
$u->rename("john.doe@example.com", $new);
|
|
|
|
}
|
|
|
|
|
2020-11-16 05:11:19 +00:00
|
|
|
public function testRemoveAUser(): void {
|
|
|
|
$user = "john.doe@example.com";
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userRemove->returns(true);
|
|
|
|
$this->dbMock->userExists->returns(true);
|
|
|
|
$u = $this->prepTest();
|
2020-11-16 05:11:19 +00:00
|
|
|
$this->assertTrue($u->remove($user));
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->dbMock->userExists->calledWith($user);
|
|
|
|
$this->dbMock->userRemove->calledWith($user);
|
|
|
|
$this->drv->userRemove->calledWith($user);
|
2020-11-16 05:11:19 +00:00
|
|
|
}
|
2020-11-23 14:31:50 +00:00
|
|
|
|
2020-11-16 05:11:19 +00:00
|
|
|
public function testRemoveAUserWeDoNotKnow(): void {
|
|
|
|
$user = "john.doe@example.com";
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userRemove->returns(true);
|
|
|
|
$this->dbMock->userExists->returns(false);
|
|
|
|
$u = $this->prepTest();
|
2020-11-16 05:11:19 +00:00
|
|
|
$this->assertTrue($u->remove($user));
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->dbMock->userExists->calledWith($user);
|
|
|
|
$this->drv->userRemove->calledWith($user);
|
2020-11-16 05:11:19 +00:00
|
|
|
}
|
2020-11-23 14:31:50 +00:00
|
|
|
|
2020-11-16 05:11:19 +00:00
|
|
|
public function testRemoveAMissingUser(): void {
|
|
|
|
$user = "john.doe@example.com";
|
|
|
|
$pass = "secret";
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userRemove->throws(new ExceptionConflict("doesNotExist"));
|
|
|
|
$this->dbMock->userExists->returns(true);
|
|
|
|
$u = $this->prepTest();
|
2020-11-16 05:11:19 +00:00
|
|
|
$this->assertException("doesNotExist", "User", "ExceptionConflict");
|
|
|
|
try {
|
|
|
|
$u->remove($user);
|
|
|
|
} finally {
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->dbMock->userExists->calledWith($user);
|
|
|
|
$this->dbMock->userRemove->calledWith($user);
|
|
|
|
$this->drv->userRemove->calledWith($user);
|
2020-11-16 05:11:19 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-23 14:31:50 +00:00
|
|
|
|
2020-11-16 05:11:19 +00:00
|
|
|
public function testRemoveAMissingUserWeDoNotKnow(): void {
|
|
|
|
$user = "john.doe@example.com";
|
|
|
|
$pass = "secret";
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userRemove->throws(new ExceptionConflict("doesNotExist"));
|
|
|
|
$this->dbMock->userExists->returns(false);
|
|
|
|
$u = $this->prepTest();
|
2020-11-16 05:11:19 +00:00
|
|
|
$this->assertException("doesNotExist", "User", "ExceptionConflict");
|
|
|
|
try {
|
|
|
|
$u->remove($user);
|
|
|
|
} finally {
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->dbMock->userExists->calledWith($user);
|
|
|
|
$this->drv->userRemove->calledWith($user);
|
2020-11-16 05:11:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetAPassword(): void {
|
|
|
|
$user = "john.doe@example.com";
|
|
|
|
$pass = "secret";
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userPasswordSet->returns($pass);
|
|
|
|
$this->dbMock->userPasswordSet->returns($pass);
|
|
|
|
$this->dbMock->userExists->returns(true);
|
|
|
|
$u = $this->prepTest();
|
2020-11-16 05:11:19 +00:00
|
|
|
$this->assertSame($pass, $u->passwordSet($user, $pass));
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userPasswordSet->calledWith($user, $pass, null);
|
|
|
|
$this->dbMock->userPasswordSet->calledWith($user, $pass);
|
|
|
|
$this->dbMock->sessionDestroy->calledWith($user);
|
|
|
|
$this->dbMock->userExists->calledWith($user);
|
2020-11-16 05:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetARandomPassword(): void {
|
|
|
|
$user = "john.doe@example.com";
|
|
|
|
$pass = "random password";
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userPasswordSet->returns(null)->returns($pass);
|
|
|
|
$this->dbMock->userPasswordSet->returns($pass);
|
|
|
|
$this->dbMock->userExists->returns(true);
|
2021-07-06 01:47:44 +00:00
|
|
|
$u = $this->prepTest(function($u) use ($pass) {
|
2021-03-01 23:01:25 +00:00
|
|
|
$u->generatePassword->returns($pass);
|
|
|
|
});
|
2020-11-16 05:11:19 +00:00
|
|
|
$this->assertSame($pass, $u->passwordSet($user, null));
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userPasswordSet->calledWith($user, null, null);
|
|
|
|
$this->drv->userPasswordSet->calledWith($user, $pass, null);
|
|
|
|
$this->dbMock->userPasswordSet->calledWith($user, $pass);
|
|
|
|
$this->dbMock->sessionDestroy->calledWith($user);
|
|
|
|
$this->dbMock->userExists->calledWith($user);
|
2020-11-16 05:11:19 +00:00
|
|
|
}
|
2020-11-16 15:24:06 +00:00
|
|
|
|
|
|
|
public function testSetAPasswordForAUserWeDoNotKnow(): void {
|
|
|
|
$user = "john.doe@example.com";
|
|
|
|
$pass = "secret";
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userPasswordSet->returns($pass);
|
|
|
|
$this->dbMock->userPasswordSet->returns($pass);
|
|
|
|
$this->dbMock->userExists->returns(false);
|
|
|
|
$u = $this->prepTest();
|
2020-11-16 15:24:06 +00:00
|
|
|
$this->assertSame($pass, $u->passwordSet($user, $pass));
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userPasswordSet->calledWith($user, $pass, null);
|
|
|
|
$this->dbMock->userAdd->calledWith($user, $pass);
|
|
|
|
$this->dbMock->userExists->calledWith($user);
|
2020-11-16 15:24:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetARandomPasswordForAUserWeDoNotKnow(): void {
|
|
|
|
$user = "john.doe@example.com";
|
|
|
|
$pass = "random password";
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userPasswordSet->returns(null)->returns($pass);
|
|
|
|
$this->dbMock->userPasswordSet->returns($pass);
|
|
|
|
$this->dbMock->userExists->returns(false);
|
2021-07-06 01:47:44 +00:00
|
|
|
$u = $this->prepTest(function($u) use ($pass) {
|
2021-03-01 23:01:25 +00:00
|
|
|
$u->generatePassword->returns($pass);
|
|
|
|
});
|
2020-11-16 15:24:06 +00:00
|
|
|
$this->assertSame($pass, $u->passwordSet($user, null));
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userPasswordSet->calledWith($user, null, null);
|
|
|
|
$this->drv->userPasswordSet->calledWith($user, $pass, null);
|
|
|
|
$this->dbMock->userAdd->calledWith($user, $pass);
|
|
|
|
$this->dbMock->userExists->calledWith($user);
|
2020-11-16 15:24:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetARandomPasswordForAMissingUser(): void {
|
|
|
|
$user = "john.doe@example.com";
|
|
|
|
$pass = "random password";
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userPasswordSet->throws(new ExceptionConflict("doesNotExist"));
|
2021-07-06 01:47:44 +00:00
|
|
|
$u = $this->prepTest(function($u) use ($pass) {
|
2021-03-01 23:01:25 +00:00
|
|
|
$u->generatePassword->returns($pass);
|
|
|
|
});
|
2020-11-16 15:24:06 +00:00
|
|
|
$this->assertException("doesNotExist", "User", "ExceptionConflict");
|
|
|
|
try {
|
|
|
|
$u->passwordSet($user, null);
|
|
|
|
} finally {
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userPasswordSet->calledWith($user, null, null);
|
2020-11-16 15:24:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUnsetAPassword(): void {
|
|
|
|
$user = "john.doe@example.com";
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userPasswordUnset->returns(true);
|
|
|
|
$this->dbMock->userPasswordSet->returns(true);
|
|
|
|
$this->dbMock->userExists->returns(true);
|
|
|
|
$u = $this->prepTest();
|
2020-11-16 15:24:06 +00:00
|
|
|
$this->assertTrue($u->passwordUnset($user));
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userPasswordUnset->calledWith($user, null);
|
|
|
|
$this->dbMock->userPasswordSet->calledWith($user, null);
|
|
|
|
$this->dbMock->sessionDestroy->calledWith($user);
|
|
|
|
$this->dbMock->userExists->calledWith($user);
|
2020-11-16 15:24:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUnsetAPasswordForAUserWeDoNotKnow(): void {
|
|
|
|
$user = "john.doe@example.com";
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userPasswordUnset->returns(true);
|
|
|
|
$this->dbMock->userPasswordSet->returns(true);
|
|
|
|
$this->dbMock->userExists->returns(false);
|
|
|
|
$u = $this->prepTest();
|
2020-11-16 15:24:06 +00:00
|
|
|
$this->assertTrue($u->passwordUnset($user));
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userPasswordUnset->calledWith($user, null);
|
|
|
|
$this->dbMock->userExists->calledWith($user);
|
2020-11-16 15:24:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUnsetAPasswordForAMissingUser(): void {
|
|
|
|
$user = "john.doe@example.com";
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userPasswordUnset->throws(new ExceptionConflict("doesNotExist"));
|
|
|
|
$u = $this->prepTest();
|
2020-11-16 15:24:06 +00:00
|
|
|
$this->assertException("doesNotExist", "User", "ExceptionConflict");
|
|
|
|
try {
|
|
|
|
$u->passwordUnset($user);
|
|
|
|
} finally {
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userPasswordUnset->calledWith($user, null);
|
2020-11-16 15:24:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @dataProvider provideProperties */
|
|
|
|
public function testGetThePropertiesOfAUser(array $exp, array $base, array $extra): void {
|
|
|
|
$user = "john.doe@example.com";
|
2020-12-07 05:07:10 +00:00
|
|
|
$exp = array_merge(['num' => null], array_combine(array_keys(User::PROPERTIES), array_fill(0, sizeof(User::PROPERTIES), null)), $exp);
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userPropertiesGet->returns($extra);
|
|
|
|
$this->dbMock->userPropertiesGet->returns($base);
|
|
|
|
$this->dbMock->userExists->returns(true);
|
|
|
|
$u = $this->prepTest();
|
2020-11-16 15:24:06 +00:00
|
|
|
$this->assertSame($exp, $u->propertiesGet($user));
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userPropertiesGet->calledWith($user, true);
|
|
|
|
$this->dbMock->userPropertiesGet->calledWith($user, true);
|
|
|
|
$this->dbMock->userExists->calledWith($user);
|
2020-11-16 15:24:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideProperties(): iterable {
|
|
|
|
$defaults = ['num' => 1, 'admin' => false, 'lang' => null, 'tz' => "Etc/UTC", 'sort_asc' => false];
|
|
|
|
return [
|
|
|
|
[$defaults, $defaults, []],
|
|
|
|
[$defaults, $defaults, ['num' => 2112, 'blah' => "bloo"]],
|
|
|
|
[['num' => 1, 'admin' => true, 'lang' => "fr", 'tz' => "America/Toronto", 'sort_asc' => true], $defaults, ['admin' => true, 'lang' => "fr", 'tz' => "America/Toronto", 'sort_asc' => true]],
|
|
|
|
[['num' => 1, 'admin' => true, 'lang' => null, 'tz' => "America/Toronto", 'sort_asc' => true], ['num' => 1, 'admin' => true, 'lang' => "fr", 'tz' => "America/Toronto", 'sort_asc' => true], ['lang' => null]],
|
|
|
|
];
|
|
|
|
}
|
2020-11-17 21:23:36 +00:00
|
|
|
|
|
|
|
public function testGetThePropertiesOfAUserWeDoNotKnow(): void {
|
|
|
|
$user = "john.doe@example.com";
|
|
|
|
$extra = ['tz' => "Europe/Istanbul"];
|
|
|
|
$base = ['num' => 47, 'admin' => false, 'lang' => null, 'tz' => "Etc/UTC", 'sort_asc' => false];
|
|
|
|
$exp = ['num' => 47, 'admin' => false, 'lang' => null, 'tz' => "Europe/Istanbul", 'sort_asc' => false];
|
2020-12-07 05:07:10 +00:00
|
|
|
$exp = array_merge(['num' => null], array_combine(array_keys(User::PROPERTIES), array_fill(0, sizeof(User::PROPERTIES), null)), $exp);
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userPropertiesGet->returns($extra);
|
|
|
|
$this->dbMock->userPropertiesGet->returns($base);
|
|
|
|
$this->dbMock->userAdd->returns(true);
|
|
|
|
$this->dbMock->userExists->returns(false);
|
|
|
|
$u = $this->prepTest();
|
2020-11-17 21:23:36 +00:00
|
|
|
$this->assertSame($exp, $u->propertiesGet($user));
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userPropertiesGet->calledWith($user, true);
|
|
|
|
$this->dbMock->userPropertiesGet->calledWith($user, true);
|
|
|
|
$this->dbMock->userPropertiesSet->calledWith($user, $extra);
|
|
|
|
$this->dbMock->userAdd->calledWith($user, null);
|
|
|
|
$this->dbMock->userExists->calledWith($user);
|
2020-11-17 21:23:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetThePropertiesOfAMissingUser(): void {
|
|
|
|
$user = "john.doe@example.com";
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userPropertiesGet->throws(new ExceptionConflict("doesNotExist"));
|
|
|
|
$u = $this->prepTest();
|
2020-11-17 21:23:36 +00:00
|
|
|
$this->assertException("doesNotExist", "User", "ExceptionConflict");
|
|
|
|
try {
|
|
|
|
$u->propertiesGet($user);
|
|
|
|
} finally {
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userPropertiesGet->calledWith($user, true);
|
2020-11-17 21:23:36 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-23 14:31:50 +00:00
|
|
|
|
2020-11-17 21:23:36 +00:00
|
|
|
/** @dataProvider providePropertyChanges */
|
|
|
|
public function testSetThePropertiesOfAUser(array $in, $out): void {
|
|
|
|
$user = "john.doe@example.com";
|
|
|
|
if ($out instanceof \Exception) {
|
2021-03-01 23:01:25 +00:00
|
|
|
$u = $this->prepTest();
|
2020-11-17 21:23:36 +00:00
|
|
|
$this->assertException($out);
|
|
|
|
$u->propertiesSet($user, $in);
|
|
|
|
} else {
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->dbMock->userExists->returns(true);
|
|
|
|
$this->drv->userPropertiesSet->returns($out);
|
|
|
|
$this->dbMock->userPropertiesSet->returns(true);
|
|
|
|
$u = $this->prepTest();
|
2020-11-17 21:23:36 +00:00
|
|
|
$this->assertSame($out, $u->propertiesSet($user, $in));
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userPropertiesSet->calledWith($user, $in);
|
|
|
|
$this->dbMock->userPropertiesSet->calledWith($user, $out);
|
|
|
|
$this->dbMock->userExists->calledWith($user);
|
2020-11-17 21:23:36 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-23 14:31:50 +00:00
|
|
|
|
2020-11-17 21:23:36 +00:00
|
|
|
/** @dataProvider providePropertyChanges */
|
|
|
|
public function testSetThePropertiesOfAUserWeDoNotKnow(array $in, $out): void {
|
|
|
|
$user = "john.doe@example.com";
|
|
|
|
if ($out instanceof \Exception) {
|
2021-03-01 23:01:25 +00:00
|
|
|
$u = $this->prepTest();
|
2020-11-17 21:23:36 +00:00
|
|
|
$this->assertException($out);
|
|
|
|
$u->propertiesSet($user, $in);
|
|
|
|
} else {
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->dbMock->userExists->returns(false);
|
|
|
|
$this->drv->userPropertiesSet->returns($out);
|
|
|
|
$this->dbMock->userPropertiesSet->returns(true);
|
|
|
|
$u = $this->prepTest();
|
2020-11-17 21:23:36 +00:00
|
|
|
$this->assertSame($out, $u->propertiesSet($user, $in));
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userPropertiesSet->calledWith($user, $in);
|
|
|
|
$this->dbMock->userPropertiesSet->calledWith($user, $out);
|
|
|
|
$this->dbMock->userExists->calledWith($user);
|
|
|
|
$this->dbMock->userAdd->calledWith($user, null);
|
2020-11-17 21:23:36 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-23 14:31:50 +00:00
|
|
|
|
2020-11-17 21:23:36 +00:00
|
|
|
public function providePropertyChanges(): iterable {
|
|
|
|
return [
|
|
|
|
[['admin' => true], ['admin' => true]],
|
2020-12-07 05:07:10 +00:00
|
|
|
[['admin' => 2], new ExceptionInput("invalidValue")],
|
|
|
|
[['sort_asc' => 2], new ExceptionInput("invalidValue")],
|
2020-11-17 21:23:36 +00:00
|
|
|
[['tz' => "Etc/UTC"], ['tz' => "Etc/UTC"]],
|
|
|
|
[['tz' => "Etc/blah"], new ExceptionInput("invalidTimezone")],
|
2020-12-07 05:07:10 +00:00
|
|
|
[['tz' => false], new ExceptionInput("invalidValue")],
|
2020-11-17 21:23:36 +00:00
|
|
|
[['lang' => "en-ca"], ['lang' => "en-CA"]],
|
|
|
|
[['lang' => null], ['lang' => null]],
|
2020-12-22 21:13:12 +00:00
|
|
|
[['page_size' => 0], new ExceptionInput("invalidNonZeroInteger")],
|
2020-11-17 21:23:36 +00:00
|
|
|
];
|
|
|
|
}
|
2020-11-23 14:31:50 +00:00
|
|
|
|
2020-11-17 21:23:36 +00:00
|
|
|
public function testSetThePropertiesOfAMissingUser(): void {
|
|
|
|
$user = "john.doe@example.com";
|
|
|
|
$in = ['admin' => true];
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userPropertiesSet->throws(new ExceptionConflict("doesNotExist"));
|
|
|
|
$u = $this->prepTest();
|
2020-11-17 21:23:36 +00:00
|
|
|
$this->assertException("doesNotExist", "User", "ExceptionConflict");
|
|
|
|
try {
|
|
|
|
$u->propertiesSet($user, $in);
|
|
|
|
} finally {
|
2021-03-01 23:01:25 +00:00
|
|
|
$this->drv->userPropertiesSet->calledWith($user, $in);
|
2020-11-17 21:23:36 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-02 14:02:37 +00:00
|
|
|
}
|