1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 13:12:41 +00:00

Simply user test data providers

The user manager no longer differentiates between the internal driver
and other drivers, making the duplication unnecessary
This commit is contained in:
J. King 2018-11-02 17:28:12 -04:00
parent a52b985826
commit b8f8a617fe

View file

@ -27,19 +27,12 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest {
// create a mock database interface
Arsse::$db = Phake::mock(Database::class);
Phake::when(Arsse::$db)->begin->thenReturn(Phake::mock(\JKingWeb\Arsse\Db\Transaction::class));
// create a mock user driver
$this->drv = Phake::mock(Driver::class);
}
public function provideDriver() {
yield "Internal driver" => [Phake::mock(InternalDriver::class)];
yield "Non-Internal Driver" => [Phake::mock(Driver::class)];
}
/** @dataProvider provideDriver */
public function testConstruct(Driver $driver) {
$this->assertInstanceOf(User::class, new User($driver));
}
public function testConstructConfiguredDriver() {
public function testConstruct() {
$this->assertInstanceOf(User::class, new User($this->drv));
$this->assertInstanceOf(User::class, new User);
}
@ -52,15 +45,15 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest {
}
/** @dataProvider provideAuthentication */
public function testAuthenticateAUser(Driver $driver, bool $preAuth, string $user, string $password, bool $exp) {
public function testAuthenticateAUser(bool $preAuth, string $user, string $password, bool $exp) {
Arsse::$conf->userPreAuth = $preAuth;
Phake::when($driver)->auth->thenReturn(false);
Phake::when($driver)->auth("john.doe@example.com", "secret")->thenReturn(true);
Phake::when($driver)->auth("jane.doe@example.com", "superman")->thenReturn(true);
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($driver);
$u = new User($this->drv);
$this->assertSame($exp, $u->auth($user, $password));
$this->assertNull($u->id);
Phake::verify(Arsse::$db, Phake::times($exp ? 1 : 0))->userExists($user);
@ -70,7 +63,7 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest {
public function provideAuthentication() {
$john = "john.doe@example.com";
$jane = "jane.doe@example.com";
$tests = [
return [
[false, $john, "secret", true],
[false, $john, "superman", false],
[false, $jane, "secret", false],
@ -80,19 +73,13 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest {
[true, $jane, "secret", true],
[true, $jane, "superman", true],
];
for ($a = 0; $a < sizeof($tests); $a++) {
list($preAuth, $user, $password, $outcome) = $tests[$a];
foreach ($this->provideDriver() as $name => list($driver)) {
yield "$name #$a" => [$driver, $preAuth, $user, $password, $outcome];
}
}
}
/** @dataProvider provideUserList */
public function testListUsers(Driver $driver, bool $authorized, $exp) {
$u = new User($driver);
Phake::when($driver)->authorize->thenReturn($authorized);
Phake::when($driver)->userList->thenReturn(["john.doe@example.com", "jane.doe@example.com"]);
public function testListUsers(bool $authorized, $exp) {
$u = new User($this->drv);
Phake::when($this->drv)->authorize->thenReturn($authorized);
Phake::when($this->drv)->userList->thenReturn(["john.doe@example.com", "jane.doe@example.com"]);
if ($exp instanceof Exception) {
$this->assertException("notAuthorized", "User", "ExceptionAuthz");
}
@ -102,24 +89,18 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest {
public function provideUserList() {
$john = "john.doe@example.com";
$jane = "jane.doe@example.com";
$tests = [
return [
[false, new \JKingWeb\Arsse\User\ExceptionAuthz("notAuthorized")],
[true, [$john, $jane]],
];
for ($a = 0; $a < sizeof($tests); $a++) {
list($authorized, $outcome) = $tests[$a];
foreach ($this->provideDriver() as $name => list($driver)) {
yield "$name #$a" => [$driver, $authorized, $outcome];
}
}
}
/** @dataProvider provideExistence */
public function testCheckThatAUserExists(Driver $driver, bool $authorized, string $user, $exp) {
$u = new User($driver);
Phake::when($driver)->authorize->thenReturn($authorized);
Phake::when($driver)->userExists("john.doe@example.com")->thenReturn(true);
Phake::when($driver)->userExists("jane.doe@example.com")->thenReturn(false);
public function testCheckThatAUserExists(bool $authorized, string $user, $exp) {
$u = new User($this->drv);
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);
if ($exp instanceof Exception) {
$this->assertException("notAuthorized", "User", "ExceptionAuthz");
}
@ -129,26 +110,20 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest {
public function provideExistence() {
$john = "john.doe@example.com";
$jane = "jane.doe@example.com";
$tests = [
return [
[false, $john, new \JKingWeb\Arsse\User\ExceptionAuthz("notAuthorized")],
[false, $jane, new \JKingWeb\Arsse\User\ExceptionAuthz("notAuthorized")],
[true, $john, true],
[true, $jane, false],
];
for ($a = 0; $a < sizeof($tests); $a++) {
list($authorized, $user, $outcome) = $tests[$a];
foreach ($this->provideDriver() as $name => list($driver)) {
yield "$name #$a" => [$driver, $authorized, $user, $outcome];
}
}
}
/** @dataProvider provideAdditions */
public function testAddAUser(Driver $driver, bool $authorized, string $user, $password, $exp) {
$u = new User($driver);
Phake::when($driver)->authorize->thenReturn($authorized);
Phake::when($driver)->userAdd("john.doe@example.com", $this->anything())->thenThrow(new \JKingWeb\Arsse\User\Exception("alreadyExists"));
Phake::when($driver)->userAdd("jane.doe@example.com", $this->anything())->thenReturnCallback(function($user, $pass) {
public function testAddAUser(bool $authorized, string $user, $password, $exp) {
$u = new User($this->drv);
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) {
return $pass ?? "random password";
});
if ($exp instanceof Exception) {
@ -164,27 +139,21 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest {
public function provideAdditions() {
$john = "john.doe@example.com";
$jane = "jane.doe@example.com";
$tests = [
return [
[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"],
];
for ($a = 0; $a < sizeof($tests); $a++) {
list($authorized, $user, $password, $outcome) = $tests[$a];
foreach ($this->provideDriver() as $name => list($driver)) {
yield "$name #$a" => [$driver, $authorized, $user, $password, $outcome];
}
}
}
/** @dataProvider provideRemovals */
public function testRemoveAUser(Driver $driver, bool $authorized, string $user, bool $exists, $exp) {
$u = new User($driver);
Phake::when($driver)->authorize->thenReturn($authorized);
Phake::when($driver)->userRemove("john.doe@example.com")->thenReturn(true);
Phake::when($driver)->userRemove("jane.doe@example.com")->thenThrow(new \JKingWeb\Arsse\User\Exception("doesNotExist"));
public function testRemoveAUser(bool $authorized, string $user, bool $exists, $exp) {
$u = new User($this->drv);
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);
if ($exp instanceof Exception) {
@ -205,7 +174,7 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest {
public function provideRemovals() {
$john = "john.doe@example.com";
$jane = "jane.doe@example.com";
$tests = [
return [
[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")],
@ -215,11 +184,5 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest {
[true, $jane, true, new \JKingWeb\Arsse\User\Exception("doesNotExist")],
[true, $jane, false, new \JKingWeb\Arsse\User\Exception("doesNotExist")],
];
for ($a = 0; $a < sizeof($tests); $a++) {
list($authorized, $user, $exists, $outcome) = $tests[$a];
foreach ($this->provideDriver() as $name => list($driver)) {
yield "$name #$a" => [$driver, $authorized, $user, $exists, $outcome];
}
}
}
}