Browse Source

More Miniflux user tests

Also added user lookup functionality
rpm
J. King 3 years ago
parent
commit
ebdfad535c
  1. 9
      lib/Database.php
  2. 7
      lib/REST/Miniflux/V1.php
  3. 5
      lib/User.php
  4. 11
      tests/cases/Database/SeriesUser.php
  5. 22
      tests/cases/REST/Miniflux/TestV1.php
  6. 9
      tests/cases/User/TestUser.php

9
lib/Database.php

@ -245,6 +245,15 @@ class Database {
return (bool) $this->db->prepare("SELECT count(*) from arsse_users where id = ?", "str")->run($user)->getValue();
}
/** Returns the username associated with a user number */
public function userLookup(int $num): string {
$out = $this->db->prepare("SELECT id from arsse_users where num = ?", "int")->run($num)->getValue();
if ($out === null) {
throw new User\ExceptionConflict("doesNotExist", ["action" => __FUNCTION__, "user" => $num]);
}
return $out;
}
/** Adds a user to the database
*
* @param string $user The user to add

7
lib/REST/Miniflux/V1.php

@ -287,7 +287,12 @@ class V1 extends \JKingWeb\Arsse\REST\AbstractHandler {
}
protected function getUserByNum(array $path, array $query, array $data) {
return $this->listUsers([Arsse::$user->id], false)[0] ?? [];
try {
$user = Arsse::$user->lookup((int) $path[1]);
return new Response($this->listUsers([$user], true)[0] ?? new \stdClass);
} catch (UserException $e) {
return new ErrorResponse("404", 404);
}
}
protected function getCurrentUser(array $path, array $query, array $data) {

5
lib/User.php

@ -62,6 +62,11 @@ class User {
return $this->u->userList();
}
public function lookup(int $num): string {
// the user number is always stored in the internal database, so the user driver is not called here
return Arsse::$db->userLookup($num);
}
public function add(string $user, ?string $password = null): string {
// ensure the user name does not contain any U+003A COLON characters, as
// this is incompatible with HTTP Basic authentication

11
tests/cases/Database/SeriesUser.php

@ -169,4 +169,15 @@ trait SeriesUser {
$this->assertException("doesNotExist", "User", "ExceptionConflict");
Arsse::$db->userPropertiesSet("john.doe@example.org", ['admin' => true]);
}
public function testLookUpAUserByNumber(): void {
$this->assertSame("admin@example.net", Arsse::$db->userLookup(1));
$this->assertSame("jane.doe@example.com", Arsse::$db->userLookup(2));
$this->assertSame("john.doe@example.com", Arsse::$db->userLookup(3));
}
public function testLookUpAMissingUserByNumber(): void {
$this->assertException("doesNotExist", "User", "ExceptionConflict");
Arsse::$db->userLookup(2112);
}
}

22
tests/cases/REST/Miniflux/TestV1.php

@ -192,12 +192,32 @@ class TestV1 extends \JKingWeb\Arsse\Test\AbstractTest {
return $u[0];
} elseif ($user === "jane.doe@example.com") {
return $u[1];
}else {
} else {
throw $u[2];
}
});
Arsse::$user->method("lookup")->willReturnCallback(function(int $num) use ($u) {
if ($num === 1) {
return "john.doe@example.com";
} elseif ($num === 2) {
return "jane.doe@example.com";
} else {
throw $u[2];
}
});
$this->h = $this->createPartialMock(V1::class, ["now"]);
$this->h->method("now")->willReturn($now);
// list all users
$this->assertMessage(new Response($exp), $this->req("GET", "/users"));
// fetch John
$this->assertMessage(new Response($exp[0]), $this->req("GET", "/me"));
$this->assertMessage(new Response($exp[0]), $this->req("GET", "/users/john.doe@example.com"));
$this->assertMessage(new Response($exp[0]), $this->req("GET", "/users/1"));
// fetch Jane
$this->assertMessage(new Response($exp[1]), $this->req("GET", "/users/jane.doe@example.com"));
$this->assertMessage(new Response($exp[1]), $this->req("GET", "/users/2"));
// fetch no one
$this->assertMessage(new ErrorResponse("404", 404), $this->req("GET", "/users/jack.doe@example.com"));
$this->assertMessage(new ErrorResponse("404", 404), $this->req("GET", "/users/47"));
}
}

9
tests/cases/User/TestUser.php

@ -90,6 +90,15 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest {
\Phake::verify($this->drv)->userList();
}
public function testLookUpAUserByNumber(): void {
$exp = "john.doe@example.com";
$u = new User($this->drv);
\Phake::when(Arsse::$db)->userLookup->thenReturn($exp);
$this->assertSame($exp, $u->lookup(2112));
\Phake::verify(Arsse::$db)->userLookup(2112);
}
public function testAddAUser(): void {
$user = "john.doe@example.com";
$pass = "secret";

Loading…
Cancel
Save