diff --git a/lib/Database.php b/lib/Database.php index b2a7aa38..799968fb 100644 --- a/lib/Database.php +++ b/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 diff --git a/lib/REST/Miniflux/V1.php b/lib/REST/Miniflux/V1.php index 5b1f51df..1107b600 100644 --- a/lib/REST/Miniflux/V1.php +++ b/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) { diff --git a/lib/User.php b/lib/User.php index bf457a95..5ab1b111 100644 --- a/lib/User.php +++ b/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 diff --git a/tests/cases/Database/SeriesUser.php b/tests/cases/Database/SeriesUser.php index 9ca140c7..7ed01822 100644 --- a/tests/cases/Database/SeriesUser.php +++ b/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); + } } diff --git a/tests/cases/REST/Miniflux/TestV1.php b/tests/cases/REST/Miniflux/TestV1.php index 401c1bf9..065b2762 100644 --- a/tests/cases/REST/Miniflux/TestV1.php +++ b/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")); } } diff --git a/tests/cases/User/TestUser.php b/tests/cases/User/TestUser.php index 84228cac..b7d1266e 100644 --- a/tests/cases/User/TestUser.php +++ b/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";