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

More Miniflux user tests

Also added user lookup functionality
This commit is contained in:
J. King 2020-12-10 20:08:00 -05:00
parent 7c841b5fc2
commit ebdfad535c
6 changed files with 61 additions and 2 deletions

View file

@ -245,6 +245,15 @@ class Database {
return (bool) $this->db->prepare("SELECT count(*) from arsse_users where id = ?", "str")->run($user)->getValue(); 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 /** Adds a user to the database
* *
* @param string $user The user to add * @param string $user The user to add

View file

@ -287,7 +287,12 @@ class V1 extends \JKingWeb\Arsse\REST\AbstractHandler {
} }
protected function getUserByNum(array $path, array $query, array $data) { 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) { protected function getCurrentUser(array $path, array $query, array $data) {

View file

@ -62,6 +62,11 @@ class User {
return $this->u->userList(); 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 { public function add(string $user, ?string $password = null): string {
// ensure the user name does not contain any U+003A COLON characters, as // ensure the user name does not contain any U+003A COLON characters, as
// this is incompatible with HTTP Basic authentication // this is incompatible with HTTP Basic authentication

View file

@ -169,4 +169,15 @@ trait SeriesUser {
$this->assertException("doesNotExist", "User", "ExceptionConflict"); $this->assertException("doesNotExist", "User", "ExceptionConflict");
Arsse::$db->userPropertiesSet("john.doe@example.org", ['admin' => true]); 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);
}
} }

View file

@ -196,8 +196,28 @@ class TestV1 extends \JKingWeb\Arsse\Test\AbstractTest {
throw $u[2]; 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 = $this->createPartialMock(V1::class, ["now"]);
$this->h->method("now")->willReturn($now); $this->h->method("now")->willReturn($now);
// list all users
$this->assertMessage(new Response($exp), $this->req("GET", "/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"));
} }
} }

View file

@ -90,6 +90,15 @@ class TestUser extends \JKingWeb\Arsse\Test\AbstractTest {
\Phake::verify($this->drv)->userList(); \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 { public function testAddAUser(): void {
$user = "john.doe@example.com"; $user = "john.doe@example.com";
$pass = "secret"; $pass = "secret";