1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-23 17:12:41 +00:00
Arsse/tests/cases/REST/Miniflux/TestV1.php

143 lines
5.8 KiB
PHP
Raw Normal View History

2020-11-23 14:31:50 +00:00
<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
2020-12-01 17:08:45 +00:00
namespace JKingWeb\Arsse\TestCase\REST\Miniflux;
2020-11-23 14:31:50 +00:00
use JKingWeb\Arsse\Arsse;
use JKingWeb\Arsse\User;
use JKingWeb\Arsse\Database;
use JKingWeb\Arsse\Db\Transaction;
use JKingWeb\Arsse\Db\ExceptionInput;
2020-11-23 14:31:50 +00:00
use JKingWeb\Arsse\REST\Miniflux\V1;
use JKingWeb\Arsse\REST\Miniflux\ErrorResponse;
2020-11-23 14:31:50 +00:00
use Psr\Http\Message\ResponseInterface;
use Laminas\Diactoros\Response\JsonResponse as Response;
use Laminas\Diactoros\Response\EmptyResponse;
2020-11-23 14:31:50 +00:00
/** @covers \JKingWeb\Arsse\REST\Miniflux\V1<extended> */
class TestV1 extends \JKingWeb\Arsse\Test\AbstractTest {
protected $h;
protected $transaction;
protected $token = "Tk2o9YubmZIL2fm2w8Z4KlDEQJz532fNSOcTG0s2_xc=";
2020-11-23 14:31:50 +00:00
protected function req(string $method, string $target, $data = "", array $headers = [], bool $authenticated = true, bool $body = true): ResponseInterface {
$prefix = "/v1";
$url = $prefix.$target;
if ($body) {
$params = [];
} else {
$params = $data;
$data = [];
}
$req = $this->serverRequest($method, $url, $prefix, $headers, [], $data, "application/json", $params, $authenticated ? "john.doe@example.com" : "");
return $this->h->dispatch($req);
}
public function setUp(): void {
self::clearData();
self::setConf();
// create a mock user manager
Arsse::$user = \Phake::mock(User::class);
Arsse::$user->id = "john.doe@example.com";
// create a mock database interface
Arsse::$db = \Phake::mock(Database::class);
$this->transaction = \Phake::mock(Transaction::class);
\Phake::when(Arsse::$db)->begin->thenReturn($this->transaction);
//initialize a handler
$this->h = new V1();
}
public function tearDown(): void {
self::clearData();
}
protected function v($value) {
return $value;
}
2020-11-26 13:42:35 +00:00
/** @dataProvider provideAuthResponses */
public function testAuthenticateAUser($token, bool $auth, bool $success): void {
$exp = $success ? new EmptyResponse(404) : new ErrorResponse("401", 401);
$user = "john.doe@example.com";
if ($token !== null) {
$headers = ['X-Auth-Token' => $token];
} else {
$headers = [];
}
Arsse::$user->id = null;
\Phake::when(Arsse::$db)->tokenLookup->thenThrow(new ExceptionInput("subjectMissing"));
\Phake::when(Arsse::$db)->tokenLookup("miniflux.login", $this->token)->thenReturn(['user' => $user]);
$this->assertMessage($exp, $this->req("GET", "/", "", $headers, $auth));
$this->assertSame($success ? $user : null, Arsse::$user->id);
}
public function provideAuthResponses(): iterable {
return [
[null, false, false],
[null, true, true],
[$this->token, false, true],
[[$this->token, "BOGUS"], false, true],
["", true, true],
[["", "BOGUS"], true, true],
["NOT A TOKEN", false, false],
["NOT A TOKEN", true, false],
[["BOGUS", $this->token], false, false],
[["", $this->token], false, false],
];
2020-11-23 14:31:50 +00:00
}
/** @dataProvider provideInvalidPaths */
public function testRespondToInvalidPaths($path, $method, $code, $allow = null): void {
2020-11-23 14:31:50 +00:00
$exp = new EmptyResponse($code, $allow ? ['Allow' => $allow] : []);
$this->assertMessage($exp, $this->req($method, $path));
}
public function provideInvalidPaths(): array {
return [
["/", "GET", 404],
2020-12-01 17:08:45 +00:00
["/", "OPTIONS", 404],
["/me", "POST", 405, "GET"],
2020-12-01 17:08:45 +00:00
["/me/", "GET", 404],
2020-11-23 14:31:50 +00:00
];
}
/** @dataProvider provideOptionsRequests */
2020-12-01 17:08:45 +00:00
public function testRespondToOptionsRequests(string $url, string $allow, string $accept): void {
2020-11-23 14:31:50 +00:00
$exp = new EmptyResponse(204, [
'Allow' => $allow,
'Accept' => $accept,
]);
$this->assertMessage($exp, $this->req("OPTIONS", $url));
}
public function provideOptionsRequests(): array {
return [
2020-12-01 17:08:45 +00:00
["/feeds", "HEAD, GET, POST", "application/json"],
["/feeds/2112", "HEAD, GET, PUT, DELETE", "application/json"],
["/me", "HEAD, GET", "application/json"],
["/users/someone", "HEAD, GET", "application/json"],
["/import", "POST", "application/xml, text/xml, text/x-opml"],
2020-11-23 14:31:50 +00:00
];
}
2020-12-02 23:00:27 +00:00
public function testRejectBadlyTypedData(): void {
$exp = new ErrorResponse(["invalidInputType", 'field' => "url", 'expected' => "string", 'actual' => "integer"], 400);
$this->assertMessage($exp, $this->req("POST", "/discover", ['url' => 2112]));
}
public function testDiscoverFeeds(): void {
$exp = new Response([
['title' => "Feed", 'type' => "rss", 'url' => "http://localhost:8000/Feed/Discovery/Feed"],
['title' => "Feed", 'type' => "rss", 'url' => "http://localhost:8000/Feed/Discovery/Missing"],
]);
$this->assertMessage($exp, $this->req("POST", "/discover", ['url' => "http://localhost:8000/Feed/Discovery/Valid"]));
$exp = new Response([]);
$this->assertMessage($exp, $this->req("POST", "/discover", ['url' => "http://localhost:8000/Feed/Discovery/Invalid"]));
$exp = new ErrorResponse("fetch404", 500);
$this->assertMessage($exp, $this->req("POST", "/discover", ['url' => "http://localhost:8000/Feed/Discovery/Missing"]));
}
2020-11-23 14:31:50 +00:00
}