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);
|
|
|
|
namespace JKingWeb\Arsse\TestCase\REST\NextcloudNews;
|
|
|
|
|
|
|
|
use JKingWeb\Arsse\Arsse;
|
|
|
|
use JKingWeb\Arsse\User;
|
|
|
|
use JKingWeb\Arsse\Database;
|
|
|
|
use JKingWeb\Arsse\Db\Transaction;
|
|
|
|
use JKingWeb\Arsse\REST\Miniflux\V1;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
|
|
|
|
/** @covers \JKingWeb\Arsse\REST\Miniflux\V1<extended> */
|
|
|
|
class TestV1 extends \JKingWeb\Arsse\Test\AbstractTest {
|
|
|
|
protected $h;
|
|
|
|
protected $transaction;
|
|
|
|
|
|
|
|
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(): void {
|
2020-11-23 14:31:50 +00:00
|
|
|
$exp = new EmptyResponse(401);
|
|
|
|
$this->assertMessage($exp, $this->req("GET", "/", "", [], false));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @dataProvider provideInvalidPaths */
|
|
|
|
public function testRespondToInvalidPaths($path, $method, $code, $allow = null): void {
|
|
|
|
$exp = new EmptyResponse($code, $allow ? ['Allow' => $allow] : []);
|
|
|
|
$this->assertMessage($exp, $this->req($method, $path));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideInvalidPaths(): array {
|
|
|
|
return [
|
|
|
|
["/", "GET", 404],
|
|
|
|
["/version", "POST", 405, "GET"],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRespondToInvalidInputTypes(): void {
|
|
|
|
$exp = new EmptyResponse(415, ['Accept' => "application/json"]);
|
|
|
|
$this->assertMessage($exp, $this->req("PUT", "/folders/1", '<data/>', ['Content-Type' => "application/xml"]));
|
|
|
|
$exp = new EmptyResponse(400);
|
|
|
|
$this->assertMessage($exp, $this->req("PUT", "/folders/1", '<data/>'));
|
|
|
|
$this->assertMessage($exp, $this->req("PUT", "/folders/1", '<data/>', ['Content-Type' => null]));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @dataProvider provideOptionsRequests */
|
|
|
|
public function testRespondToOptionsRequests(string $url, string $allow, string $accept): void {
|
|
|
|
$exp = new EmptyResponse(204, [
|
|
|
|
'Allow' => $allow,
|
|
|
|
'Accept' => $accept,
|
|
|
|
]);
|
|
|
|
$this->assertMessage($exp, $this->req("OPTIONS", $url));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideOptionsRequests(): array {
|
|
|
|
return [
|
|
|
|
["/feeds", "HEAD,GET,POST", "application/json"],
|
|
|
|
["/feeds/2112", "DELETE", "application/json"],
|
|
|
|
["/user", "HEAD,GET", "application/json"],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|