1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-24 01:22:41 +00:00
Arsse/tests/cases/REST/TinyTinyRSS/TestIcon.php

111 lines
5.5 KiB
PHP
Raw Normal View History

<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
2017-12-22 03:47:19 +00:00
namespace JKingWeb\Arsse\TestCase\REST\TinyTinyRSS;
2017-12-22 03:47:19 +00:00
use JKingWeb\Arsse\Arsse;
use JKingWeb\Arsse\User;
use JKingWeb\Arsse\Database;
2020-11-18 15:01:20 +00:00
use JKingWeb\Arsse\Db\ExceptionInput;
2017-12-22 03:47:19 +00:00
use JKingWeb\Arsse\REST\TinyTinyRSS\Icon;
use Psr\Http\Message\ResponseInterface;
use Laminas\Diactoros\Response\EmptyResponse as Response;
/** @covers \JKingWeb\Arsse\REST\TinyTinyRSS\Icon<extended> */
2017-12-22 03:47:19 +00:00
class TestIcon extends \JKingWeb\Arsse\Test\AbstractTest {
protected $h;
protected $user = "john.doe@example.com";
2019-10-16 18:42:43 +00:00
public function setUp(): void {
2021-02-27 20:24:02 +00:00
parent::setUp();
2018-11-23 00:55:54 +00:00
self::setConf();
2021-03-01 23:01:25 +00:00
Arsse::$user = $this->mock(User::class)->get();
// create a mock database interface
2021-03-01 23:01:25 +00:00
$this->dbMock = $this->mock(Database::class);
2017-12-22 03:47:19 +00:00
$this->h = new Icon();
}
protected function req(string $target, string $method = "GET", string $user = null): ResponseInterface {
2021-03-01 23:01:25 +00:00
Arsse::$db = $this->dbMock->get();
$prefix = "/tt-rss/feed-icons/";
$url = $prefix.$target;
$req = $this->serverRequest($method, $url, $prefix, [], [], null, "", [], $user);
return $this->h->dispatch($req);
}
protected function reqAuth(string $target, string $method = "GET"): ResponseInterface {
return $this->req($target, $method, $this->user);
}
protected function reqAuthFailed(string $target, string $method = "GET"): ResponseInterface {
return $this->req($target, $method, "");
}
2020-01-20 18:52:48 +00:00
public function testRetrieveFavion(): void {
2021-03-01 23:01:25 +00:00
$this->dbMock->subscriptionIcon->returns(['url' => null]);
$this->dbMock->subscriptionIcon->with($this->anything(), 1123, false)->throws(new ExceptionInput("subjectMissing"));
$this->dbMock->subscriptionIcon->with($this->anything(), 42, false)->returns(['url' => "http://example.com/favicon.ico"]);
$this->dbMock->subscriptionIcon->with($this->anything(), 2112, false)->returns(['url' => "http://example.net/logo.png"]);
$this->dbMock->subscriptionIcon->with($this->anything(), 1337, false)->returns(['url' => "http://example.org/icon.gif\r\nLocation: http://bad.example.com/"]);
// these requests should succeed
$exp = new Response(301, ['Location' => "http://example.com/favicon.ico"]);
$this->assertMessage($exp, $this->req("42.ico"));
$exp = new Response(301, ['Location' => "http://example.net/logo.png"]);
$this->assertMessage($exp, $this->req("2112.ico"));
$exp = new Response(301, ['Location' => "http://example.org/icon.gif"]);
$this->assertMessage($exp, $this->req("1337.ico"));
// these requests should fail
$exp = new Response(404);
$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"));
2020-11-18 15:01:20 +00:00
$this->assertMessage($exp, $this->req("1123.ico"));
// only GET is allowed
$exp = new Response(405, ['Allow' => "GET"]);
$this->assertMessage($exp, $this->req("2112.ico", "PUT"));
}
2020-01-20 18:52:48 +00:00
public function testRetrieveFavionWithHttpAuthentication(): void {
2020-11-06 15:27:30 +00:00
$url = ['url' => "http://example.org/icon.gif\r\nLocation: http://bad.example.com/"];
2021-03-01 23:01:25 +00:00
$this->dbMock->subscriptionIcon->returns(['url' => null]);
$this->dbMock->subscriptionIcon->with($this->user, 42, false)->returns($url);
$this->dbMock->subscriptionIcon->with("jane.doe", 2112, false)->returns($url);
$this->dbMock->subscriptionIcon->with($this->user, 1337, false)->returns($url);
$this->dbMock->subscriptionIcon->with(null, 42, false)->returns($url);
$this->dbMock->subscriptionIcon->with(null, 2112, false)->returns($url);
$this->dbMock->subscriptionIcon->with(null, 1337, false)->returns($url);
// 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]);
$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"));
}
}