1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-23 09:02:41 +00:00
Arsse/tests/cases/User/TestUser.php

336 lines
16 KiB
PHP
Raw Normal View History

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;
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));
// 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
}
2018-11-03 17:26:22 +00:00
public function testListDrivers() {
$exp = [
'JKingWeb\\Arsse\\User\\Internal\\Driver' => Arsse::$lang->msg("Driver.User.Internal.Name"),
];
$this->assertArraySubset($exp, User::driverList());
}
2018-11-02 14:02:37 +00:00
public function testConstruct() {
$this->assertInstanceOf(User::class, new User($this->drv));
2018-11-02 14:02:37 +00:00
$this->assertInstanceOf(User::class, new User);
}
public function testConversionToString() {
$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 */
public function testAuthenticateAUser(bool $preAuth, string $user, string $password, bool $exp) {
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("");
$u = new User($this->drv);
2018-11-02 14:02:37 +00:00
$this->assertSame($exp, $u->auth($user, $password));
$this->assertNull($u->id);
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";
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],
];
}
/** @dataProvider provideUserList */
public function testListUsers(bool $authorized, $exp) {
$u = new User($this->drv);
2019-09-05 14:03:32 +00:00
\Phake::when($this->drv)->authorize->thenReturn($authorized);
\Phake::when($this->drv)->userList->thenReturn(["john.doe@example.com", "jane.doe@example.com"]);
2018-11-02 14:02:37 +00:00
if ($exp instanceof Exception) {
$this->assertException("notAuthorized", "User", "ExceptionAuthz");
}
$this->assertSame($exp, $u->list());
}
2019-10-16 18:42:43 +00:00
public function provideUserList(): iterable {
2018-11-02 14:02:37 +00:00
$john = "john.doe@example.com";
$jane = "jane.doe@example.com";
return [
2018-11-02 14:02:37 +00:00
[false, new \JKingWeb\Arsse\User\ExceptionAuthz("notAuthorized")],
[true, [$john, $jane]],
];
}
/** @dataProvider provideExistence */
public function testCheckThatAUserExists(bool $authorized, string $user, $exp) {
$u = new User($this->drv);
2019-09-05 14:03:32 +00:00
\Phake::when($this->drv)->authorize->thenReturn($authorized);
\Phake::when($this->drv)->userExists("john.doe@example.com")->thenReturn(true);
\Phake::when($this->drv)->userExists("jane.doe@example.com")->thenReturn(false);
2018-11-02 14:02:37 +00:00
if ($exp instanceof Exception) {
$this->assertException("notAuthorized", "User", "ExceptionAuthz");
}
$this->assertSame($exp, $u->exists($user));
}
2019-10-16 18:42:43 +00:00
public function provideExistence(): iterable {
2018-11-02 14:02:37 +00:00
$john = "john.doe@example.com";
$jane = "jane.doe@example.com";
return [
2018-11-02 14:02:37 +00:00
[false, $john, new \JKingWeb\Arsse\User\ExceptionAuthz("notAuthorized")],
[false, $jane, new \JKingWeb\Arsse\User\ExceptionAuthz("notAuthorized")],
[true, $john, true],
[true, $jane, false],
];
}
/** @dataProvider provideAdditions */
public function testAddAUser(bool $authorized, string $user, $password, $exp) {
$u = new User($this->drv);
2019-09-05 14:03:32 +00:00
\Phake::when($this->drv)->authorize->thenReturn($authorized);
\Phake::when($this->drv)->userAdd("john.doe@example.com", $this->anything())->thenThrow(new \JKingWeb\Arsse\User\Exception("alreadyExists"));
\Phake::when($this->drv)->userAdd("jane.doe@example.com", $this->anything())->thenReturnCallback(function($user, $pass) {
2018-11-02 14:02:37 +00:00
return $pass ?? "random password";
});
if ($exp instanceof Exception) {
if ($exp instanceof \JKingWeb\Arsse\User\ExceptionAuthz) {
$this->assertException("notAuthorized", "User", "ExceptionAuthz");
} else {
$this->assertException("alreadyExists", "User");
}
}
$this->assertSame($exp, $u->add($user, $password));
}
2018-11-03 17:26:22 +00:00
/** @dataProvider provideAdditions */
public function testAddAUserWithARandomPassword(bool $authorized, string $user, $password, $exp) {
2019-09-05 14:03:32 +00:00
$u = \Phake::partialMock(User::class, $this->drv);
\Phake::when($this->drv)->authorize->thenReturn($authorized);
\Phake::when($this->drv)->userAdd($this->anything(), $this->isNull())->thenReturn(null);
\Phake::when($this->drv)->userAdd("john.doe@example.com", $this->logicalNot($this->isNull()))->thenThrow(new \JKingWeb\Arsse\User\Exception("alreadyExists"));
\Phake::when($this->drv)->userAdd("jane.doe@example.com", $this->logicalNot($this->isNull()))->thenReturnCallback(function($user, $pass) {
2018-11-03 17:26:22 +00:00
return $pass;
});
if ($exp instanceof Exception) {
if ($exp instanceof \JKingWeb\Arsse\User\ExceptionAuthz) {
$this->assertException("notAuthorized", "User", "ExceptionAuthz");
$calls = 0;
} else {
$this->assertException("alreadyExists", "User");
$calls = 2;
}
} else {
$calls = 4;
}
try {
$pass1 = $u->add($user, null);
$pass2 = $u->add($user, null);
$this->assertNotEquals($pass1, $pass2);
} finally {
2019-09-05 14:03:32 +00:00
\Phake::verify($this->drv, \Phake::times($calls))->userAdd;
\Phake::verify($u, \Phake::times($calls / 2))->generatePassword;
2018-11-03 17:26:22 +00:00
}
}
2019-10-16 18:42:43 +00:00
public function provideAdditions(): iterable {
2018-11-02 14:02:37 +00:00
$john = "john.doe@example.com";
$jane = "jane.doe@example.com";
return [
2018-11-02 14:02:37 +00:00
[false, $john, "secret", new \JKingWeb\Arsse\User\ExceptionAuthz("notAuthorized")],
[false, $jane, "superman", new \JKingWeb\Arsse\User\ExceptionAuthz("notAuthorized")],
[true, $john, "secret", new \JKingWeb\Arsse\User\Exception("alreadyExists")],
[true, $jane, "superman", "superman"],
[true, $jane, null, "random password"],
];
}
/** @dataProvider provideRemovals */
public function testRemoveAUser(bool $authorized, string $user, bool $exists, $exp) {
$u = new User($this->drv);
2019-09-05 14:03:32 +00:00
\Phake::when($this->drv)->authorize->thenReturn($authorized);
\Phake::when($this->drv)->userRemove("john.doe@example.com")->thenReturn(true);
\Phake::when($this->drv)->userRemove("jane.doe@example.com")->thenThrow(new \JKingWeb\Arsse\User\Exception("doesNotExist"));
\Phake::when(Arsse::$db)->userExists->thenReturn($exists);
\Phake::when(Arsse::$db)->userRemove->thenReturn(true);
2018-11-02 14:02:37 +00:00
if ($exp instanceof Exception) {
if ($exp instanceof \JKingWeb\Arsse\User\ExceptionAuthz) {
$this->assertException("notAuthorized", "User", "ExceptionAuthz");
} else {
$this->assertException("doesNotExist", "User");
}
}
try {
$this->assertSame($exp, $u->remove($user));
} finally {
2019-09-05 14:03:32 +00:00
\Phake::verify(Arsse::$db, \Phake::times((int) $authorized))->userExists($user);
\Phake::verify(Arsse::$db, \Phake::times((int) ($authorized && $exists)))->userRemove($user);
2018-11-02 14:02:37 +00:00
}
}
2019-10-16 18:42:43 +00:00
public function provideRemovals(): iterable {
2018-11-02 14:02:37 +00:00
$john = "john.doe@example.com";
$jane = "jane.doe@example.com";
return [
2018-11-02 14:02:37 +00:00
[false, $john, true, new \JKingWeb\Arsse\User\ExceptionAuthz("notAuthorized")],
[false, $john, false, new \JKingWeb\Arsse\User\ExceptionAuthz("notAuthorized")],
[false, $jane, true, new \JKingWeb\Arsse\User\ExceptionAuthz("notAuthorized")],
[false, $jane, false, new \JKingWeb\Arsse\User\ExceptionAuthz("notAuthorized")],
[true, $john, true, true],
[true, $john, false, true],
[true, $jane, true, new \JKingWeb\Arsse\User\Exception("doesNotExist")],
[true, $jane, false, new \JKingWeb\Arsse\User\Exception("doesNotExist")],
];
}
2018-11-03 17:26:22 +00:00
/** @dataProvider providePasswordChanges */
public function testChangeAPassword(bool $authorized, string $user, $password, bool $exists, $exp) {
$u = new User($this->drv);
2019-09-05 14:03:32 +00:00
\Phake::when($this->drv)->authorize->thenReturn($authorized);
\Phake::when($this->drv)->userPasswordSet("john.doe@example.com", $this->anything(), $this->anything())->thenReturnCallback(function($user, $pass, $old) {
2018-11-03 17:26:22 +00:00
return $pass ?? "random password";
});
2019-09-05 14:03:32 +00:00
\Phake::when($this->drv)->userPasswordSet("jane.doe@example.com", $this->anything(), $this->anything())->thenThrow(new \JKingWeb\Arsse\User\Exception("doesNotExist"));
\Phake::when(Arsse::$db)->userExists->thenReturn($exists);
2018-11-03 17:26:22 +00:00
if ($exp instanceof Exception) {
if ($exp instanceof \JKingWeb\Arsse\User\ExceptionAuthz) {
$this->assertException("notAuthorized", "User", "ExceptionAuthz");
} else {
$this->assertException("doesNotExist", "User");
}
$calls = 0;
2018-12-05 22:28:11 +00:00
} else {
2018-11-03 17:26:22 +00:00
$calls = 1;
}
try {
$this->assertSame($exp, $u->passwordSet($user, $password));
} finally {
2019-09-05 14:03:32 +00:00
\Phake::verify(Arsse::$db, \Phake::times($calls))->userExists($user);
\Phake::verify(Arsse::$db, \Phake::times($exists ? $calls : 0))->userPasswordSet($user, $password ?? "random password", null);
2018-11-03 17:26:22 +00:00
}
}
/** @dataProvider providePasswordChanges */
public function testChangeAPasswordToARandomPassword(bool $authorized, string $user, $password, bool $exists, $exp) {
2019-09-05 14:03:32 +00:00
$u = \Phake::partialMock(User::class, $this->drv);
\Phake::when($this->drv)->authorize->thenReturn($authorized);
\Phake::when($this->drv)->userPasswordSet($this->anything(), $this->isNull(), $this->anything())->thenReturn(null);
\Phake::when($this->drv)->userPasswordSet("john.doe@example.com", $this->logicalNot($this->isNull()), $this->anything())->thenReturnCallback(function($user, $pass, $old) {
2018-11-03 17:26:22 +00:00
return $pass ?? "random password";
});
2019-09-05 14:03:32 +00:00
\Phake::when($this->drv)->userPasswordSet("jane.doe@example.com", $this->logicalNot($this->isNull()), $this->anything())->thenThrow(new \JKingWeb\Arsse\User\Exception("doesNotExist"));
\Phake::when(Arsse::$db)->userExists->thenReturn($exists);
2018-11-03 17:26:22 +00:00
if ($exp instanceof Exception) {
if ($exp instanceof \JKingWeb\Arsse\User\ExceptionAuthz) {
$this->assertException("notAuthorized", "User", "ExceptionAuthz");
$calls = 0;
} else {
$this->assertException("doesNotExist", "User");
$calls = 2;
}
} else {
$calls = 4;
}
try {
$pass1 = $u->passwordSet($user, null);
$pass2 = $u->passwordSet($user, null);
$this->assertNotEquals($pass1, $pass2);
} finally {
2019-09-05 14:03:32 +00:00
\Phake::verify($this->drv, \Phake::times($calls))->userPasswordSet;
\Phake::verify($u, \Phake::times($calls / 2))->generatePassword;
\Phake::verify(Arsse::$db, \Phake::times($calls == 4 ? 2 : 0))->userExists($user);
2018-11-03 17:26:22 +00:00
if ($calls == 4) {
2019-09-05 14:03:32 +00:00
\Phake::verify(Arsse::$db, \Phake::times($exists ? 1 : 0))->userPasswordSet($user, $pass1, null);
\Phake::verify(Arsse::$db, \Phake::times($exists ? 1 : 0))->userPasswordSet($user, $pass2, null);
2018-11-03 17:26:22 +00:00
} else {
2019-09-05 14:03:32 +00:00
\Phake::verify(Arsse::$db, \Phake::times(0))->userPasswordSet;
2018-11-03 17:26:22 +00:00
}
}
}
2019-10-16 18:42:43 +00:00
public function providePasswordChanges(): iterable {
2018-11-03 17:26:22 +00:00
$john = "john.doe@example.com";
$jane = "jane.doe@example.com";
return [
[false, $john, "secret", true, new \JKingWeb\Arsse\User\ExceptionAuthz("notAuthorized")],
[false, $jane, "superman", false, new \JKingWeb\Arsse\User\ExceptionAuthz("notAuthorized")],
[true, $john, "superman", true, "superman"],
[true, $john, null, true, "random password"],
[true, $john, "superman", false, "superman"],
[true, $john, null, false, "random password"],
[true, $jane, "secret", true, new \JKingWeb\Arsse\User\Exception("doesNotExist")],
];
}
/** @dataProvider providePasswordClearings */
public function testClearAPassword(bool $authorized, bool $exists, string $user, $exp) {
2019-09-05 14:03:32 +00:00
\Phake::when($this->drv)->authorize->thenReturn($authorized);
\Phake::when($this->drv)->userPasswordUnset->thenReturn(true);
\Phake::when($this->drv)->userPasswordUnset("jane.doe@example.net", null)->thenThrow(new \JKingWeb\Arsse\User\Exception("doesNotExist"));
\Phake::when(Arsse::$db)->userExists->thenReturn($exists);
$u = new User($this->drv);
try {
if ($exp instanceof \JKingWeb\Arsse\AbstractException) {
$this->assertException($exp);
$u->passwordUnset($user);
} else {
$this->assertSame($exp, $u->passwordUnset($user));
}
} finally {
2019-09-05 14:03:32 +00:00
\Phake::verify(Arsse::$db, \Phake::times((int) ($authorized && $exists && is_bool($exp))))->userPasswordSet($user, null);
}
}
2019-10-16 18:42:43 +00:00
public function providePasswordClearings(): iterable {
$forbidden = new \JKingWeb\Arsse\User\ExceptionAuthz("notAuthorized");
$missing = new \JKingWeb\Arsse\User\Exception("doesNotExist");
return [
[false, true, "jane.doe@example.com", $forbidden],
[false, true, "john.doe@example.com", $forbidden],
[false, true, "jane.doe@example.net", $forbidden],
[false, false, "jane.doe@example.com", $forbidden],
[false, false, "john.doe@example.com", $forbidden],
[false, false, "jane.doe@example.net", $forbidden],
[true, true, "jane.doe@example.com", true],
[true, true, "john.doe@example.com", true],
[true, true, "jane.doe@example.net", $missing],
[true, false, "jane.doe@example.com", true],
[true, false, "john.doe@example.com", true],
[true, false, "jane.doe@example.net", $missing],
];
}
2018-11-02 14:02:37 +00:00
}