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;
|
|
|
|
|
|
|
|
use JKingWeb\Arsse\Arsse;
|
|
|
|
use JKingWeb\Arsse\Database;
|
|
|
|
use JKingWeb\Arsse\User;
|
|
|
|
use JKingWeb\Arsse\AbstractException as Exception;
|
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 {
|
2018-11-23 15:01:17 +00:00
|
|
|
self::clearData();
|
2018-11-23 00:55:54 +00:00
|
|
|
self::setConf();
|
2018-11-02 14:02:37 +00:00
|
|
|
// create a mock database interface
|
2019-09-05 14:03:32 +00:00
|
|
|
Arsse::$db = \Phake::mock(Database::class);
|
2019-09-05 14:13:17 +00:00
|
|
|
\Phake::when(Arsse::$db)->begin->thenReturn(\Phake::mock(\JKingWeb\Arsse\Db\Transaction::class));
|
2018-11-02 21:28:12 +00:00
|
|
|
// create a mock user driver
|
2019-09-05 14:03:32 +00:00
|
|
|
$this->drv = \Phake::mock(Driver::class);
|
2018-11-02 14:02:37 +00:00
|
|
|
}
|
2020-11-15 21:24:26 +00:00
|
|
|
|
|
|
|
public function tearDown(): void {
|
|
|
|
\Phake::verifyNoOtherInteractions($this->drv);
|
|
|
|
\Phake::verifyNoOtherInteractions(Arsse::$db);
|
|
|
|
}
|
2018-11-04 17:06:30 +00:00
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testConstruct(): void {
|
2018-11-02 21:28:12 +00:00
|
|
|
$this->assertInstanceOf(User::class, new User($this->drv));
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @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;
|
2019-09-05 14:03:32 +00:00
|
|
|
\Phake::when($this->drv)->auth->thenReturn(false);
|
|
|
|
\Phake::when($this->drv)->auth("john.doe@example.com", "secret")->thenReturn(true);
|
|
|
|
\Phake::when($this->drv)->auth("jane.doe@example.com", "superman")->thenReturn(true);
|
|
|
|
\Phake::when(Arsse::$db)->userExists("john.doe@example.com")->thenReturn(true);
|
|
|
|
\Phake::when(Arsse::$db)->userExists("jane.doe@example.com")->thenReturn(false);
|
|
|
|
\Phake::when(Arsse::$db)->userAdd->thenReturn("");
|
2018-11-02 21:28:12 +00:00
|
|
|
$u = new User($this->drv);
|
2018-11-02 14:02:37 +00:00
|
|
|
$this->assertSame($exp, $u->auth($user, $password));
|
|
|
|
$this->assertNull($u->id);
|
2020-11-15 21:24:26 +00:00
|
|
|
\Phake::verify($this->drv, \Phake::times((int) !$preAuth))->auth($user, $password);
|
2019-09-05 14:03:32 +00:00
|
|
|
\Phake::verify(Arsse::$db, \Phake::times($exp ? 1 : 0))->userExists($user);
|
|
|
|
\Phake::verify(Arsse::$db, \Phake::times($exp && $user === "jane.doe@example.com" ? 1 : 0))->userAdd($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"];
|
2018-11-02 21:28:12 +00:00
|
|
|
$u = new User($this->drv);
|
2019-09-05 14:03:32 +00:00
|
|
|
\Phake::when($this->drv)->userList->thenReturn(["john.doe@example.com", "jane.doe@example.com"]);
|
2018-11-02 14:02:37 +00:00
|
|
|
$this->assertSame($exp, $u->list());
|
2020-11-15 21:24:26 +00:00
|
|
|
\Phake::verify($this->drv)->userList();
|
2018-11-02 14:02:37 +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";
|
2018-11-02 21:28:12 +00:00
|
|
|
$u = new User($this->drv);
|
2020-11-15 21:24:26 +00:00
|
|
|
\Phake::when($this->drv)->userAdd->thenReturn($pass);
|
|
|
|
\Phake::when(Arsse::$db)->userExists->thenReturn(true);
|
|
|
|
$this->assertSame($pass, $u->add($user, $pass));
|
|
|
|
\Phake::verify($this->drv)->userAdd($user, $pass);
|
|
|
|
\Phake::verify(Arsse::$db)->userExists($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";
|
2018-11-02 21:28:12 +00:00
|
|
|
$u = new User($this->drv);
|
2020-11-15 21:24:26 +00:00
|
|
|
\Phake::when($this->drv)->userAdd->thenReturn($pass);
|
|
|
|
\Phake::when(Arsse::$db)->userExists->thenReturn(false);
|
|
|
|
$this->assertSame($pass, $u->add($user, $pass));
|
|
|
|
\Phake::verify($this->drv)->userAdd($user, $pass);
|
|
|
|
\Phake::verify(Arsse::$db)->userExists($user);
|
|
|
|
\Phake::verify(Arsse::$db)->userAdd($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";
|
2018-11-03 17:26:22 +00:00
|
|
|
$u = new User($this->drv);
|
2020-11-15 21:24:26 +00:00
|
|
|
\Phake::when($this->drv)->userAdd->thenThrow(new ExceptionConflict("alreadyExists"));
|
|
|
|
\Phake::when(Arsse::$db)->userExists->thenReturn(true);
|
|
|
|
$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 {
|
2020-11-15 21:24:26 +00:00
|
|
|
\Phake::verify(Arsse::$db)->userExists($user);
|
|
|
|
\Phake::verify($this->drv)->userAdd($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";
|
2019-03-24 18:42:23 +00:00
|
|
|
$u = new User($this->drv);
|
2020-11-15 21:24:26 +00:00
|
|
|
\Phake::when($this->drv)->userAdd->thenThrow(new ExceptionConflict("alreadyExists"));
|
|
|
|
\Phake::when(Arsse::$db)->userExists->thenReturn(false);
|
|
|
|
$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 {
|
2020-11-15 21:24:26 +00:00
|
|
|
\Phake::verify(Arsse::$db)->userExists($user);
|
|
|
|
\Phake::verify(Arsse::$db)->userAdd($user, null);
|
|
|
|
\Phake::verify($this->drv)->userAdd($user, $pass);
|
2019-03-24 18:42:23 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-16 05:11:19 +00:00
|
|
|
|
|
|
|
public function testAddAnInvalidUser(): void {
|
|
|
|
$user = "john:doe@example.com";
|
|
|
|
$pass = "secret";
|
|
|
|
$u = new User($this->drv);
|
|
|
|
\Phake::when($this->drv)->userAdd->thenThrow(new ExceptionInput("invalidUsername"));
|
|
|
|
$this->assertException("invalidUsername", "User", "ExceptionInput");
|
|
|
|
$u->add($user, $pass);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddAUserWithARandomPassword(): void {
|
|
|
|
$user = "john.doe@example.com";
|
|
|
|
$pass = "random password";
|
|
|
|
$u = \Phake::partialMock(User::class, $this->drv);
|
|
|
|
\Phake::when($u)->generatePassword->thenReturn($pass);
|
|
|
|
\Phake::when($this->drv)->userAdd->thenReturn(null)->thenReturn($pass);
|
|
|
|
\Phake::when(Arsse::$db)->userExists->thenReturn(true);
|
|
|
|
$this->assertSame($pass, $u->add($user));
|
|
|
|
\Phake::verify($this->drv)->userAdd($user, null);
|
|
|
|
\Phake::verify($this->drv)->userAdd($user, $pass);
|
|
|
|
\Phake::verify(Arsse::$db)->userExists($user);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemoveAUser(): void {
|
|
|
|
$user = "john.doe@example.com";
|
|
|
|
$pass = "secret";
|
|
|
|
$u = new User($this->drv);
|
|
|
|
\Phake::when($this->drv)->userRemove->thenReturn(true);
|
|
|
|
\Phake::when(Arsse::$db)->userExists->thenReturn(true);
|
|
|
|
$this->assertTrue($u->remove($user));
|
|
|
|
\Phake::verify(Arsse::$db)->userExists($user);
|
|
|
|
\Phake::verify(Arsse::$db)->userRemove($user);
|
|
|
|
\Phake::verify($this->drv)->userRemove($user);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemoveAUserWeDoNotKnow(): void {
|
|
|
|
$user = "john.doe@example.com";
|
|
|
|
$pass = "secret";
|
|
|
|
$u = new User($this->drv);
|
|
|
|
\Phake::when($this->drv)->userRemove->thenReturn(true);
|
|
|
|
\Phake::when(Arsse::$db)->userExists->thenReturn(false);
|
|
|
|
$this->assertTrue($u->remove($user));
|
|
|
|
\Phake::verify(Arsse::$db)->userExists($user);
|
|
|
|
\Phake::verify($this->drv)->userRemove($user);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemoveAMissingUser(): void {
|
|
|
|
$user = "john.doe@example.com";
|
|
|
|
$pass = "secret";
|
|
|
|
$u = new User($this->drv);
|
|
|
|
\Phake::when($this->drv)->userRemove->thenThrow(new ExceptionConflict("doesNotExist"));
|
|
|
|
\Phake::when(Arsse::$db)->userExists->thenReturn(true);
|
|
|
|
$this->assertException("doesNotExist", "User", "ExceptionConflict");
|
|
|
|
try {
|
|
|
|
$u->remove($user);
|
|
|
|
} finally {
|
|
|
|
\Phake::verify(Arsse::$db)->userExists($user);
|
|
|
|
\Phake::verify(Arsse::$db)->userRemove($user);
|
|
|
|
\Phake::verify($this->drv)->userRemove($user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemoveAMissingUserWeDoNotKnow(): void {
|
|
|
|
$user = "john.doe@example.com";
|
|
|
|
$pass = "secret";
|
|
|
|
$u = new User($this->drv);
|
|
|
|
\Phake::when($this->drv)->userRemove->thenThrow(new ExceptionConflict("doesNotExist"));
|
|
|
|
\Phake::when(Arsse::$db)->userExists->thenReturn(false);
|
|
|
|
$this->assertException("doesNotExist", "User", "ExceptionConflict");
|
|
|
|
try {
|
|
|
|
$u->remove($user);
|
|
|
|
} finally {
|
|
|
|
\Phake::verify(Arsse::$db)->userExists($user);
|
|
|
|
\Phake::verify($this->drv)->userRemove($user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetAPassword(): void {
|
|
|
|
$user = "john.doe@example.com";
|
|
|
|
$pass = "secret";
|
|
|
|
$u = new User($this->drv);
|
|
|
|
\Phake::when($this->drv)->userPasswordSet->thenReturn($pass);
|
|
|
|
\Phake::when(Arsse::$db)->userPasswordSet->thenReturn($pass);
|
|
|
|
\Phake::when(Arsse::$db)->userExists->thenReturn(true);
|
|
|
|
$this->assertSame($pass, $u->passwordSet($user, $pass));
|
|
|
|
\Phake::verify($this->drv)->userPasswordSet($user, $pass, null);
|
|
|
|
\Phake::verify(Arsse::$db)->userPasswordSet($user, $pass, null);
|
|
|
|
\Phake::verify(Arsse::$db)->sessionDestroy($user);
|
|
|
|
\Phake::verify(Arsse::$db)->userExists($user);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetARandomPassword(): void {
|
|
|
|
$user = "john.doe@example.com";
|
|
|
|
$pass = "random password";
|
|
|
|
$u = \Phake::partialMock(User::class, $this->drv);
|
|
|
|
\Phake::when($u)->generatePassword->thenReturn($pass);
|
|
|
|
\Phake::when($this->drv)->userPasswordSet->thenReturn(null)->thenReturn($pass);
|
|
|
|
\Phake::when(Arsse::$db)->userPasswordSet->thenReturn($pass);
|
|
|
|
\Phake::when(Arsse::$db)->userExists->thenReturn(true);
|
|
|
|
$this->assertSame($pass, $u->passwordSet($user, null));
|
|
|
|
\Phake::verify($this->drv)->userPasswordSet($user, null, null);
|
|
|
|
\Phake::verify($this->drv)->userPasswordSet($user, $pass, null);
|
|
|
|
\Phake::verify(Arsse::$db)->userPasswordSet($user, $pass, null);
|
|
|
|
\Phake::verify(Arsse::$db)->sessionDestroy($user);
|
|
|
|
\Phake::verify(Arsse::$db)->userExists($user);
|
|
|
|
}
|
2018-11-02 14:02:37 +00:00
|
|
|
}
|