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