2017-11-10 17:02:59 +00:00
|
|
|
<?php
|
2017-11-17 01:51:03 +00:00
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
2017-11-10 17:02:59 +00:00
|
|
|
declare(strict_types=1);
|
2017-12-22 03:47:19 +00:00
|
|
|
namespace JKingWeb\Arsse\TestCase\REST\TinyTinyRSS;
|
2017-11-10 17:02:59 +00:00
|
|
|
|
2017-12-22 03:47:19 +00:00
|
|
|
use JKingWeb\Arsse\Arsse;
|
|
|
|
use JKingWeb\Arsse\User;
|
|
|
|
use JKingWeb\Arsse\Database;
|
|
|
|
use JKingWeb\Arsse\REST\TinyTinyRSS\Icon;
|
2018-01-05 04:08:53 +00:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2020-01-20 15:40:05 +00:00
|
|
|
use Laminas\Diactoros\Response\EmptyResponse as Response;
|
2017-11-10 17:02:59 +00:00
|
|
|
|
|
|
|
/** @covers \JKingWeb\Arsse\REST\TinyTinyRSS\Icon<extended> */
|
2017-12-22 03:47:19 +00:00
|
|
|
class TestIcon extends \JKingWeb\Arsse\Test\AbstractTest {
|
2017-11-10 17:02:59 +00:00
|
|
|
protected $h;
|
2018-10-26 18:40:20 +00:00
|
|
|
protected $user = "john.doe@example.com";
|
2017-11-10 17:02:59 +00:00
|
|
|
|
2019-10-16 18:42:43 +00:00
|
|
|
public function setUp(): void {
|
2018-11-23 15:01:17 +00:00
|
|
|
self::clearData();
|
2018-11-23 00:55:54 +00:00
|
|
|
self::setConf();
|
2017-11-10 17:02:59 +00:00
|
|
|
// create a mock user manager
|
2019-09-05 14:03:32 +00:00
|
|
|
Arsse::$user = \Phake::mock(User::class);
|
2017-11-10 17:02:59 +00:00
|
|
|
// create a mock database interface
|
2019-09-05 14:03:32 +00:00
|
|
|
Arsse::$db = \Phake::mock(Database::class);
|
2017-12-22 03:47:19 +00:00
|
|
|
$this->h = new Icon();
|
2017-11-10 17:02:59 +00:00
|
|
|
}
|
|
|
|
|
2019-10-16 18:42:43 +00:00
|
|
|
public function tearDown(): void {
|
2018-11-23 15:01:17 +00:00
|
|
|
self::clearData();
|
2017-11-10 17:02:59 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 18:40:20 +00:00
|
|
|
protected function req(string $target, string $method = "GET", string $user = null): ResponseInterface {
|
2019-09-25 22:30:53 +00:00
|
|
|
$prefix = "/tt-rss/feed-icons/";
|
|
|
|
$url = $prefix.$target;
|
|
|
|
$req = $this->serverRequest($method, $url, $prefix, [], [], null, "", [], $user);
|
2018-01-05 04:08:53 +00:00
|
|
|
return $this->h->dispatch($req);
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:34:03 +00:00
|
|
|
protected function reqAuth(string $target, string $method = "GET"): ResponseInterface {
|
2018-10-26 18:40:20 +00:00
|
|
|
return $this->req($target, $method, $this->user);
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:34:03 +00:00
|
|
|
protected function reqAuthFailed(string $target, string $method = "GET"): ResponseInterface {
|
2018-10-26 18:40:20 +00:00
|
|
|
return $this->req($target, $method, "");
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testRetrieveFavion(): void {
|
2019-09-05 14:03:32 +00:00
|
|
|
\Phake::when(Arsse::$db)->subscriptionFavicon->thenReturn("");
|
|
|
|
\Phake::when(Arsse::$db)->subscriptionFavicon(42, $this->anything())->thenReturn("http://example.com/favicon.ico");
|
|
|
|
\Phake::when(Arsse::$db)->subscriptionFavicon(2112, $this->anything())->thenReturn("http://example.net/logo.png");
|
|
|
|
\Phake::when(Arsse::$db)->subscriptionFavicon(1337, $this->anything())->thenReturn("http://example.org/icon.gif\r\nLocation: http://bad.example.com/");
|
2017-11-10 17:02:59 +00:00
|
|
|
// these requests should succeed
|
2018-01-04 04:13:08 +00:00
|
|
|
$exp = new Response(301, ['Location' => "http://example.com/favicon.ico"]);
|
2018-01-11 16:09:25 +00:00
|
|
|
$this->assertMessage($exp, $this->req("42.ico"));
|
2018-01-04 04:13:08 +00:00
|
|
|
$exp = new Response(301, ['Location' => "http://example.net/logo.png"]);
|
2018-01-11 16:09:25 +00:00
|
|
|
$this->assertMessage($exp, $this->req("2112.ico"));
|
2018-01-04 04:13:08 +00:00
|
|
|
$exp = new Response(301, ['Location' => "http://example.org/icon.gif"]);
|
2018-01-11 16:09:25 +00:00
|
|
|
$this->assertMessage($exp, $this->req("1337.ico"));
|
2017-11-10 17:02:59 +00:00
|
|
|
// these requests should fail
|
|
|
|
$exp = new Response(404);
|
2018-01-11 16:09:25 +00:00
|
|
|
$this->assertMessage($exp, $this->req("ook.ico"));
|
|
|
|
$this->assertMessage($exp, $this->req("ook"));
|
|
|
|
$this->assertMessage($exp, $this->req("47.ico"));
|
|
|
|
$this->assertMessage($exp, $this->req("2112.png"));
|
2017-11-10 17:02:59 +00:00
|
|
|
// only GET is allowed
|
2018-01-04 04:13:08 +00:00
|
|
|
$exp = new Response(405, ['Allow' => "GET"]);
|
2018-01-11 16:09:25 +00:00
|
|
|
$this->assertMessage($exp, $this->req("2112.ico", "PUT"));
|
2017-11-10 17:02:59 +00:00
|
|
|
}
|
2018-10-26 18:40:20 +00:00
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testRetrieveFavionWithHttpAuthentication(): void {
|
2018-10-26 18:40:20 +00:00
|
|
|
$url = "http://example.org/icon.gif\r\nLocation: http://bad.example.com/";
|
2019-09-05 14:03:32 +00:00
|
|
|
\Phake::when(Arsse::$db)->subscriptionFavicon->thenReturn("");
|
|
|
|
\Phake::when(Arsse::$db)->subscriptionFavicon(42, $this->user)->thenReturn($url);
|
|
|
|
\Phake::when(Arsse::$db)->subscriptionFavicon(2112, "jane.doe")->thenReturn($url);
|
|
|
|
\Phake::when(Arsse::$db)->subscriptionFavicon(1337, $this->user)->thenReturn($url);
|
|
|
|
\Phake::when(Arsse::$db)->subscriptionFavicon(42, null)->thenReturn($url);
|
|
|
|
\Phake::when(Arsse::$db)->subscriptionFavicon(2112, null)->thenReturn($url);
|
|
|
|
\Phake::when(Arsse::$db)->subscriptionFavicon(1337, null)->thenReturn($url);
|
2018-10-26 18:40:20 +00:00
|
|
|
// these requests should succeed
|
|
|
|
$exp = new Response(301, ['Location' => "http://example.org/icon.gif"]);
|
|
|
|
$this->assertMessage($exp, $this->req("42.ico"));
|
|
|
|
$this->assertMessage($exp, $this->req("2112.ico"));
|
|
|
|
$this->assertMessage($exp, $this->req("1337.ico"));
|
|
|
|
$this->assertMessage($exp, $this->reqAuth("42.ico"));
|
|
|
|
$this->assertMessage($exp, $this->reqAuth("1337.ico"));
|
|
|
|
// these requests should fail
|
|
|
|
$exp = new Response(404);
|
|
|
|
$this->assertMessage($exp, $this->reqAuth("2112.ico"));
|
|
|
|
$exp = new Response(401);
|
|
|
|
$this->assertMessage($exp, $this->reqAuthFailed("42.ico"));
|
|
|
|
$this->assertMessage($exp, $this->reqAuthFailed("1337.ico"));
|
|
|
|
// with HTTP auth required, only authenticated requests should succeed
|
2018-11-23 00:55:54 +00:00
|
|
|
self::setConf(['userHTTPAuthRequired' => true]);
|
2018-10-26 18:40:20 +00:00
|
|
|
$exp = new Response(301, ['Location' => "http://example.org/icon.gif"]);
|
|
|
|
$this->assertMessage($exp, $this->reqAuth("42.ico"));
|
|
|
|
$this->assertMessage($exp, $this->reqAuth("1337.ico"));
|
|
|
|
// anything else should fail
|
|
|
|
$exp = new Response(401);
|
|
|
|
$this->assertMessage($exp, $this->req("42.ico"));
|
|
|
|
$this->assertMessage($exp, $this->req("2112.ico"));
|
|
|
|
$this->assertMessage($exp, $this->req("1337.ico"));
|
|
|
|
$this->assertMessage($exp, $this->reqAuthFailed("42.ico"));
|
|
|
|
$this->assertMessage($exp, $this->reqAuthFailed("1337.ico"));
|
|
|
|
// resources for the wrtong user should still fail, too
|
|
|
|
$exp = new Response(404);
|
|
|
|
$this->assertMessage($exp, $this->reqAuth("2112.ico"));
|
|
|
|
}
|
2017-11-10 17:02:59 +00:00
|
|
|
}
|