Browse Source

Normalize URLs before establishing feed uniqueness

microsub
J. King 5 years ago
parent
commit
3439895779
  1. 3
      lib/Database.php
  2. 11
      tests/cases/Database/SeriesSubscription.php
  3. 2
      tests/cases/Misc/TestURL.php

3
lib/Database.php

@ -12,6 +12,7 @@ use JKingWeb\Arsse\Misc\Query;
use JKingWeb\Arsse\Context\Context; use JKingWeb\Arsse\Context\Context;
use JKingWeb\Arsse\Misc\Date; use JKingWeb\Arsse\Misc\Date;
use JKingWeb\Arsse\Misc\ValueInfo; use JKingWeb\Arsse\Misc\ValueInfo;
use JKingWeb\Arsse\Misc\URL;
/** The high-level interface with the database /** The high-level interface with the database
* *
@ -992,6 +993,8 @@ class Database {
* @param boolean $discover Whether to perform newsfeed discovery if $url points to an HTML document * @param boolean $discover Whether to perform newsfeed discovery if $url points to an HTML document
*/ */
public function feedAdd(string $url, string $fetchUser = "", string $fetchPassword = "", bool $discover = true): int { public function feedAdd(string $url, string $fetchUser = "", string $fetchPassword = "", bool $discover = true): int {
// normalize the input URL
$url = URL::normalize($url);
// check to see if the feed already exists // check to see if the feed already exists
$check = $this->db->prepare("SELECT id from arsse_feeds where url = ? and username = ? and password = ?", "str", "str", "str"); $check = $this->db->prepare("SELECT id from arsse_feeds where url = ? and username = ? and password = ?", "str", "str", "str");
$feedID = $check->run($url, $fetchUser, $fetchPassword)->getValue(); $feedID = $check->run($url, $fetchUser, $fetchPassword)->getValue();

11
tests/cases/Database/SeriesSubscription.php

@ -202,9 +202,10 @@ trait SeriesSubscription {
$url = "http://example.org/feed1"; $url = "http://example.org/feed1";
$feedID = $this->nextID("arsse_feeds"); $feedID = $this->nextID("arsse_feeds");
Phake::when(Arsse::$db)->feedUpdate->thenThrow(new FeedException($url, new \PicoFeed\Client\InvalidUrlException())); Phake::when(Arsse::$db)->feedUpdate->thenThrow(new FeedException($url, new \PicoFeed\Client\InvalidUrlException()));
$this->assertException("invalidUrl", "Feed");
try { try {
Arsse::$db->subscriptionAdd($this->user, $url, "", "", false); Arsse::$db->subscriptionAdd($this->user, $url, "", "", false);
} catch (FeedException $e) { } finally {
Phake::verify(Arsse::$user)->authorize($this->user, "subscriptionAdd"); Phake::verify(Arsse::$user)->authorize($this->user, "subscriptionAdd");
Phake::verify(Arsse::$db)->feedUpdate($feedID, true); Phake::verify(Arsse::$db)->feedUpdate($feedID, true);
$state = $this->primeExpectations($this->data, [ $state = $this->primeExpectations($this->data, [
@ -212,8 +213,6 @@ trait SeriesSubscription {
'arsse_subscriptions' => ['id','owner','feed'], 'arsse_subscriptions' => ['id','owner','feed'],
]); ]);
$this->compareExpectations(static::$drv, $state); $this->compareExpectations(static::$drv, $state);
$this->assertException("invalidUrl", "Feed");
throw $e;
} }
} }
@ -223,6 +222,12 @@ trait SeriesSubscription {
Arsse::$db->subscriptionAdd($this->user, $url); Arsse::$db->subscriptionAdd($this->user, $url);
} }
public function testAddADuplicateSubscriptionWithEquivalentUrl() {
$url = "http://EXAMPLE.COM/feed2";
$this->assertException("constraintViolation", "Db", "ExceptionInput");
Arsse::$db->subscriptionAdd($this->user, $url);
}
public function testAddADuplicateSubscriptionViaRedirection() { public function testAddADuplicateSubscriptionViaRedirection() {
$url = "http://localhost:8000/Feed/Parsing/Valid"; $url = "http://localhost:8000/Feed/Parsing/Valid";
Arsse::$db->subscriptionAdd($this->user, $url); Arsse::$db->subscriptionAdd($this->user, $url);

2
tests/cases/Misc/TestURL.php

@ -23,6 +23,8 @@ class TestURL extends \JKingWeb\Arsse\Test\AbstractTest {
return [ return [
["/", "/"], ["/", "/"],
["//example.com/", "//example.com/"], ["//example.com/", "//example.com/"],
["/ ", "/ "],
["//EXAMPLE.COM/", "//EXAMPLE.COM/"],
["http://example.com/", "http://example.com/"], ["http://example.com/", "http://example.com/"],
["HTTP://example.com/", "http://example.com/"], ["HTTP://example.com/", "http://example.com/"],
["http://example.com", "http://example.com/"], ["http://example.com", "http://example.com/"],

Loading…
Cancel
Save