From eae0ba4b68c70e82a31405b9b5372ee999fe5cd4 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Sun, 7 Feb 2021 19:20:10 -0500 Subject: [PATCH] Tests fortoken operations --- lib/REST/Miniflux/V1.php | 2 +- tests/cases/REST/Miniflux/TestV1.php | 40 +++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/lib/REST/Miniflux/V1.php b/lib/REST/Miniflux/V1.php index ee9c3329..097a9c86 100644 --- a/lib/REST/Miniflux/V1.php +++ b/lib/REST/Miniflux/V1.php @@ -1203,7 +1203,7 @@ class V1 extends \JKingWeb\Arsse\REST\AbstractHandler { } public static function tokenGenerate(string $user, string $label): string { - // Miniflux produces tokenss in base64url alphabet + // Miniflux produces tokens in base64url alphabet $t = str_replace(["+", "/"], ["-", "_"], base64_encode(random_bytes(self::TOKEN_LENGTH))); return Arsse::$db->tokenCreate($user, "miniflux.login", $t, null, $label); } diff --git a/tests/cases/REST/Miniflux/TestV1.php b/tests/cases/REST/Miniflux/TestV1.php index 6466fba3..73b9b4a8 100644 --- a/tests/cases/REST/Miniflux/TestV1.php +++ b/tests/cases/REST/Miniflux/TestV1.php @@ -667,6 +667,14 @@ class TestV1 extends \JKingWeb\Arsse\Test\AbstractTest { ]; } + public function testModifyAFeedWithNoBody(): void { + $this->h = \Phake::partialMock(V1::class); + \Phake::when($this->h)->getFeed->thenReturn(new Response(self::FEEDS_OUT[0])); + \Phake::when(Arsse::$db)->subscriptionPropertiesSet->thenReturn(true); + $this->assertMessage(new Response(self::FEEDS_OUT[0]), $this->req("PUT", "/feeds/2112", "")); + \Phake::verify(Arsse::$db)->subscriptionPropertiesSet(Arsse::$user->id, 2112, []); + } + public function testDeleteAFeed(): void { \Phake::when(Arsse::$db)->subscriptionRemove->thenReturn(true); $this->assertMessage(new EmptyResponse(204), $this->req("DELETE", "/feeds/2112")); @@ -971,4 +979,34 @@ class TestV1 extends \JKingWeb\Arsse\Test\AbstractTest { $this->assertMessage(new TextResponse("EXPORT DATA", 200, ['Content-Type' => "application/xml"]), $this->req("GET", "/export")); \Phake::verify($opml)->export(Arsse::$user->id); } -} + + public function testGenerateTokens(): void { + \Phake::when(Arsse::$db)->tokenCreate->thenReturn("RANDOM TOKEN"); + $this->assertSame("RANDOM TOKEN", V1::tokenGenerate("ook", "Eek")); + \Phake::verify(Arsse::$db)->tokenCreate("ook", "miniflux.login", \Phake::capture($token), null, "Eek"); + $this->assertRegExp("/^[A-Za-z0-9_\-]{43}=$/", $token); + } + + public function testListTheTokensOfAUser(): void { + $out = [ + ['id' => "TOKEN 1", 'data' => "Ook"], + ['id' => "TOKEN 2", 'data' => "Eek"], + ['id' => "TOKEN 3", 'data' => "Ack"], + ]; + $exp = [ + ['label' => "Ook", 'id' => "TOKEN 1"], + ['label' => "Eek", 'id' => "TOKEN 2"], + ['label' => "Ack", 'id' => "TOKEN 3"], + ]; + \Phake::when(Arsse::$db)->tokenList->thenReturn(new Result($this->v($out))); + \Phake::when(Arsse::$db)->userExists->thenReturn(true); + $this->assertSame($exp, V1::tokenList("john.doe@example.com")); + \Phake::verify(Arsse::$db)->tokenList("john.doe@example.com", "miniflux.login"); + } + + public function testListTheTokensOfAMissingUser(): void { + \Phake::when(Arsse::$db)->userExists->thenReturn(false); + $this->assertException("doesNotExist", "User", "ExceptionConflict"); + V1::tokenList("john.doe@example.com"); + } +} \ No newline at end of file