From fd25be5c27346ef3f2a67b3f829376edd1243dfd Mon Sep 17 00:00:00 2001 From: "J. King" Date: Wed, 20 Jan 2021 18:28:51 -0500 Subject: [PATCH] Basic tests for feed creation --- lib/REST/Miniflux/V1.php | 8 ++-- tests/cases/REST/Miniflux/TestV1.php | 62 +++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/lib/REST/Miniflux/V1.php b/lib/REST/Miniflux/V1.php index 7d481da..44c8c3b 100644 --- a/lib/REST/Miniflux/V1.php +++ b/lib/REST/Miniflux/V1.php @@ -278,9 +278,11 @@ class V1 extends \JKingWeb\Arsse\REST\AbstractHandler { $body[$k] = null; } elseif (gettype($body[$k]) !== $t) { return new ErrorResponse(["InvalidInputType", 'field' => $k, 'expected' => $t, 'actual' => gettype($body[$k])], 422); - } elseif (in_array($k, ["keeplist_rules", "blocklist_rules"]) && !Rule::validate($body[$k])) { - return new ErrorResponse(["InvalidInputValue", 'field' => $k], 422); - } elseif (in_array($k, ["url", "feed_url"]) && !URL::absolute($body[$k])) { + } elseif ( + (in_array($k, ["keeplist_rules", "blocklist_rules"]) && !Rule::validate($body[$k])) || + (in_array($k, ["url", "feed_url"]) && !URL::absolute($body[$k])) || + ($k === "category_id" && $body[$k] < 1) + ) { return new ErrorResponse(["InvalidInputValue", 'field' => $k], 422); } } diff --git a/tests/cases/REST/Miniflux/TestV1.php b/tests/cases/REST/Miniflux/TestV1.php index fbcf82c..84965ad 100644 --- a/tests/cases/REST/Miniflux/TestV1.php +++ b/tests/cases/REST/Miniflux/TestV1.php @@ -184,9 +184,12 @@ class TestV1 extends \JKingWeb\Arsse\Test\AbstractTest { ['title' => "Feed", 'type' => "rss", 'url' => "http://localhost:8000/Feed/Discovery/Missing"], ]; return [ - ["http://localhost:8000/Feed/Discovery/Valid", new Response($discovered)], + ["http://localhost:8000/Feed/Discovery/Valid", new Response($discovered)], ["http://localhost:8000/Feed/Discovery/Invalid", new Response([])], ["http://localhost:8000/Feed/Discovery/Missing", new ErrorResponse("Fetch404", 502)], + [1, new ErrorResponse(["InvalidInputType", 'field' => "url", 'expected' => "string", 'actual' => "integer"], 422)], + ["Not a URL", new ErrorResponse(["InvalidInputValue", 'field' => "url"], 422)], + [null, new ErrorResponse(["MissingInputValue", 'field' => "url"], 422)], ]; } @@ -607,4 +610,61 @@ class TestV1 extends \JKingWeb\Arsse\Test\AbstractTest { ]); $this->assertMessage($exp, $this->req("GET", "/feeds")); } + + /** @dataProvider provideFeedCreations */ + public function testCreateAFeed(array $in, $out1, $out2, $out3, ResponseInterface $exp): void { + if ($out1 instanceof \Exception) { + \Phake::when(Arsse::$db)->feedAdd->thenThrow($out1); + } else { + \Phake::when(Arsse::$db)->feedAdd->thenReturn($out1); + } + if ($out2 instanceof \Exception) { + \Phake::when(Arsse::$db)->subscriptionAdd->thenThrow($out2); + } else { + \Phake::when(Arsse::$db)->subscriptionAdd->thenReturn($out2); + } + if ($out3 instanceof \Exception) { + \Phake::when(Arsse::$db)->subscriptionPropertiesSet->thenThrow($out3); + } else { + \Phake::when(Arsse::$db)->subscriptionPropertiesSet->thenReturn($out3); + } + $this->assertMessage($exp, $this->req("POST", "/feeds", $in)); + $in1 = $out1 !== null; + $in2 = $out2 !== null; + $in3 = $out3 !== null; + if ($in1) { + \Phake::verify(Arsse::$db)->feedAdd($in['feed_url'], $in['username'] ?? "", $in['password'] ?? "", true, $in['crawler'] ?? false); + } else { + \Phake::verify(Arsse::$db, \Phake::times(0))->feedAdd; + } + if ($in2) { + \Phake::verify(Arsse::$db)->subscriptionAdd("john.doe@example.com", $in['feed_url'], $in['username'] ?? "", $in['password'] ?? "", true, $in['crawler'] ?? false); + } else { + \Phake::verify(Arsse::$db, \Phake::times(0))->subscriptionAdd; + } + if ($in3) { + $props = [ + 'keep_rule' => $in['keeplist_rules'], + 'block_rule' => $in['blocklist_rules'], + 'folder' => $in['category_id'] - 1, + 'scrape' => $in['crawler'] ?? false, + ]; + \Phake::verify(Arsse::$db)->subscriptionPropertiesSet("john.doe@example.com", $out2, $props); + } else { + \Phake::verify(Arsse::$db, \Phake::times(0))->subscriptionPropertiesSet; + } + } + + public function provideFeedCreations(): iterable { + self::clearData(); + return [ + [['category_id' => 1], null, null, null, new ErrorResponse(["MissingInputValue", 'field' => "feed_url"], 422)], + [['feed_url' => "http://example.com/"], null, null, null, new ErrorResponse(["MissingInputValue", 'field' => "category_id"], 422)], + [['feed_url' => "http://example.com/", 'category_id' => "1"], null, null, null, new ErrorResponse(["InvalidInputType", 'field' => "category_id", 'expected' => "integer", 'actual' => "string"], 422)], + [['feed_url' => "Not a URL", 'category_id' => 1], null, null, null, new ErrorResponse(["InvalidInputValue", 'field' => "feed_url"], 422)], + [['feed_url' => "http://example.com/", 'category_id' => 0], null, null, null, new ErrorResponse(["InvalidInputValue", 'field' => "category_id"], 422)], + [['feed_url' => "http://example.com/", 'category_id' => 1, 'keeplist_rules' => "["], null, null, null, new ErrorResponse(["InvalidInputValue", 'field' => "keeplist_rules"], 422)], + [['feed_url' => "http://example.com/", 'category_id' => 1, 'blocklist_rules' => "["], null, null, null, new ErrorResponse(["InvalidInputValue", 'field' => "blocklist_rules"], 422)], + ]; + } }