2020-01-18 17:50:11 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Shaarli\Front;
|
|
|
|
|
|
|
|
use Shaarli\Config\ConfigManager;
|
|
|
|
use Shaarli\Container\ShaarliContainer;
|
|
|
|
use Shaarli\Front\Exception\LoginBannedException;
|
2020-07-06 08:04:35 +02:00
|
|
|
use Shaarli\Front\Exception\UnauthorizedException;
|
2020-01-18 17:50:11 +01:00
|
|
|
use Shaarli\Render\PageBuilder;
|
2020-07-06 08:04:35 +02:00
|
|
|
use Shaarli\Render\PageCacheManager;
|
|
|
|
use Shaarli\Security\LoginManager;
|
2020-09-29 14:41:40 +02:00
|
|
|
use Shaarli\TestCase;
|
2020-07-06 08:04:35 +02:00
|
|
|
use Shaarli\Updater\Updater;
|
2020-01-18 17:50:11 +01:00
|
|
|
use Slim\Http\Request;
|
|
|
|
use Slim\Http\Response;
|
2020-06-13 11:22:14 +02:00
|
|
|
use Slim\Http\Uri;
|
2020-01-18 17:50:11 +01:00
|
|
|
|
|
|
|
class ShaarliMiddlewareTest extends TestCase
|
|
|
|
{
|
2020-07-07 10:15:56 +02:00
|
|
|
protected const TMP_MOCK_FILE = '.tmp';
|
|
|
|
|
2020-01-18 17:50:11 +01:00
|
|
|
/** @var ShaarliContainer */
|
|
|
|
protected $container;
|
|
|
|
|
|
|
|
/** @var ShaarliMiddleware */
|
|
|
|
protected $middleware;
|
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
$this->container = $this->createMock(ShaarliContainer::class);
|
2020-07-06 08:04:35 +02:00
|
|
|
|
2020-07-07 10:15:56 +02:00
|
|
|
touch(static::TMP_MOCK_FILE);
|
|
|
|
|
2020-07-06 08:04:35 +02:00
|
|
|
$this->container->conf = $this->createMock(ConfigManager::class);
|
2020-07-07 10:15:56 +02:00
|
|
|
$this->container->conf->method('getConfigFileExt')->willReturn(static::TMP_MOCK_FILE);
|
|
|
|
|
2020-07-06 08:04:35 +02:00
|
|
|
$this->container->loginManager = $this->createMock(LoginManager::class);
|
|
|
|
|
2020-07-21 20:33:33 +02:00
|
|
|
$this->container->environment = ['REQUEST_URI' => 'http://shaarli/subfolder/path'];
|
|
|
|
|
2020-01-18 17:50:11 +01:00
|
|
|
$this->middleware = new ShaarliMiddleware($this->container);
|
|
|
|
}
|
|
|
|
|
2020-08-13 11:08:13 +02:00
|
|
|
public function tearDown(): void
|
2020-07-07 10:15:56 +02:00
|
|
|
{
|
|
|
|
unlink(static::TMP_MOCK_FILE);
|
|
|
|
}
|
|
|
|
|
2020-07-06 08:04:35 +02:00
|
|
|
/**
|
|
|
|
* Test middleware execution with valid controller call
|
|
|
|
*/
|
2020-01-18 17:50:11 +01:00
|
|
|
public function testMiddlewareExecution(): void
|
|
|
|
{
|
|
|
|
$request = $this->createMock(Request::class);
|
2020-06-13 11:22:14 +02:00
|
|
|
$request->method('getUri')->willReturnCallback(function (): Uri {
|
|
|
|
$uri = $this->createMock(Uri::class);
|
|
|
|
$uri->method('getBasePath')->willReturn('/subfolder');
|
|
|
|
|
|
|
|
return $uri;
|
|
|
|
});
|
|
|
|
|
2020-01-18 17:50:11 +01:00
|
|
|
$response = new Response();
|
|
|
|
$controller = function (Request $request, Response $response): Response {
|
|
|
|
return $response->withStatus(418); // I'm a tea pot
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @var Response $result */
|
|
|
|
$result = $this->middleware->__invoke($request, $response, $controller);
|
|
|
|
|
|
|
|
static::assertInstanceOf(Response::class, $result);
|
|
|
|
static::assertSame(418, $result->getStatusCode());
|
|
|
|
}
|
|
|
|
|
2020-07-06 08:04:35 +02:00
|
|
|
/**
|
2020-08-21 10:50:44 +02:00
|
|
|
* Test middleware execution with controller throwing a known front exception.
|
|
|
|
* The exception should be thrown to be later handled by the error handler.
|
2020-07-06 08:04:35 +02:00
|
|
|
*/
|
|
|
|
public function testMiddlewareExecutionWithFrontException(): void
|
2020-01-18 17:50:11 +01:00
|
|
|
{
|
|
|
|
$request = $this->createMock(Request::class);
|
2020-06-13 11:22:14 +02:00
|
|
|
$request->method('getUri')->willReturnCallback(function (): Uri {
|
|
|
|
$uri = $this->createMock(Uri::class);
|
|
|
|
$uri->method('getBasePath')->willReturn('/subfolder');
|
|
|
|
|
|
|
|
return $uri;
|
|
|
|
});
|
2020-07-06 08:04:35 +02:00
|
|
|
|
2020-01-18 17:50:11 +01:00
|
|
|
$response = new Response();
|
|
|
|
$controller = function (): void {
|
|
|
|
$exception = new LoginBannedException();
|
|
|
|
|
2021-04-05 09:39:34 +02:00
|
|
|
throw new $exception();
|
2020-01-18 17:50:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
$pageBuilder = $this->createMock(PageBuilder::class);
|
|
|
|
$pageBuilder->method('render')->willReturnCallback(function (string $message): string {
|
|
|
|
return $message;
|
|
|
|
});
|
|
|
|
$this->container->pageBuilder = $pageBuilder;
|
|
|
|
|
2020-08-21 10:50:44 +02:00
|
|
|
$this->expectException(LoginBannedException::class);
|
2020-01-18 17:50:11 +01:00
|
|
|
|
2020-08-21 10:50:44 +02:00
|
|
|
$this->middleware->__invoke($request, $response, $controller);
|
2020-01-18 17:50:11 +01:00
|
|
|
}
|
2020-07-06 08:04:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test middleware execution with controller throwing a not authorized exception
|
2020-08-21 10:50:44 +02:00
|
|
|
* The middle should send a redirection response to the login page.
|
2020-07-06 08:04:35 +02:00
|
|
|
*/
|
|
|
|
public function testMiddlewareExecutionWithUnauthorizedException(): void
|
|
|
|
{
|
|
|
|
$request = $this->createMock(Request::class);
|
|
|
|
$request->method('getUri')->willReturnCallback(function (): Uri {
|
|
|
|
$uri = $this->createMock(Uri::class);
|
|
|
|
$uri->method('getBasePath')->willReturn('/subfolder');
|
|
|
|
|
|
|
|
return $uri;
|
|
|
|
});
|
|
|
|
|
|
|
|
$response = new Response();
|
|
|
|
$controller = function (): void {
|
|
|
|
throw new UnauthorizedException();
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @var Response $result */
|
|
|
|
$result = $this->middleware->__invoke($request, $response, $controller);
|
|
|
|
|
|
|
|
static::assertSame(302, $result->getStatusCode());
|
2020-07-21 20:33:33 +02:00
|
|
|
static::assertSame(
|
|
|
|
'/subfolder/login?returnurl=' . urlencode('http://shaarli/subfolder/path'),
|
|
|
|
$result->getHeader('location')[0]
|
|
|
|
);
|
2020-07-06 08:04:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-21 10:50:44 +02:00
|
|
|
* Test middleware execution with controller throwing a not authorized exception.
|
|
|
|
* The exception should be thrown to be later handled by the error handler.
|
2020-07-06 08:04:35 +02:00
|
|
|
*/
|
2020-08-21 10:50:44 +02:00
|
|
|
public function testMiddlewareExecutionWithServerException(): void
|
2020-07-06 08:04:35 +02:00
|
|
|
{
|
|
|
|
$request = $this->createMock(Request::class);
|
|
|
|
$request->method('getUri')->willReturnCallback(function (): Uri {
|
|
|
|
$uri = $this->createMock(Uri::class);
|
|
|
|
$uri->method('getBasePath')->willReturn('/subfolder');
|
|
|
|
|
|
|
|
return $uri;
|
|
|
|
});
|
|
|
|
|
2021-04-05 09:39:34 +02:00
|
|
|
$dummyException = new class () extends \Exception {
|
|
|
|
};
|
2020-08-21 10:50:44 +02:00
|
|
|
|
2020-07-06 08:04:35 +02:00
|
|
|
$response = new Response();
|
2020-08-21 10:50:44 +02:00
|
|
|
$controller = function () use ($dummyException): void {
|
|
|
|
throw $dummyException;
|
2020-07-06 08:04:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
$parameters = [];
|
|
|
|
$this->container->pageBuilder = $this->createMock(PageBuilder::class);
|
|
|
|
$this->container->pageBuilder->method('render')->willReturnCallback(function (string $message): string {
|
|
|
|
return $message;
|
|
|
|
});
|
|
|
|
$this->container->pageBuilder
|
|
|
|
->method('assign')
|
|
|
|
->willReturnCallback(function (string $key, string $value) use (&$parameters): void {
|
|
|
|
$parameters[$key] = $value;
|
|
|
|
})
|
|
|
|
;
|
|
|
|
|
2020-08-21 10:50:44 +02:00
|
|
|
$this->expectException(get_class($dummyException));
|
2020-07-06 08:04:35 +02:00
|
|
|
|
2020-08-21 10:50:44 +02:00
|
|
|
$this->middleware->__invoke($request, $response, $controller);
|
2020-07-06 08:04:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMiddlewareExecutionWithUpdates(): void
|
|
|
|
{
|
|
|
|
$request = $this->createMock(Request::class);
|
|
|
|
$request->method('getUri')->willReturnCallback(function (): Uri {
|
|
|
|
$uri = $this->createMock(Uri::class);
|
|
|
|
$uri->method('getBasePath')->willReturn('/subfolder');
|
|
|
|
|
|
|
|
return $uri;
|
|
|
|
});
|
|
|
|
|
|
|
|
$response = new Response();
|
|
|
|
$controller = function (Request $request, Response $response): Response {
|
|
|
|
return $response->withStatus(418); // I'm a tea pot
|
|
|
|
};
|
|
|
|
|
|
|
|
$this->container->loginManager = $this->createMock(LoginManager::class);
|
|
|
|
$this->container->loginManager->method('isLoggedIn')->willReturn(true);
|
|
|
|
|
|
|
|
$this->container->conf = $this->createMock(ConfigManager::class);
|
|
|
|
$this->container->conf->method('get')->willReturnCallback(function (string $key): string {
|
|
|
|
return $key;
|
|
|
|
});
|
2020-07-07 10:15:56 +02:00
|
|
|
$this->container->conf->method('getConfigFileExt')->willReturn(static::TMP_MOCK_FILE);
|
2020-07-06 08:04:35 +02:00
|
|
|
|
|
|
|
$this->container->pageCacheManager = $this->createMock(PageCacheManager::class);
|
|
|
|
$this->container->pageCacheManager->expects(static::once())->method('invalidateCaches');
|
|
|
|
|
|
|
|
$this->container->updater = $this->createMock(Updater::class);
|
|
|
|
$this->container->updater
|
|
|
|
->expects(static::once())
|
|
|
|
->method('update')
|
|
|
|
->willReturn(['update123'])
|
|
|
|
;
|
|
|
|
$this->container->updater->method('getDoneUpdates')->willReturn($updates = ['update123', 'other']);
|
|
|
|
$this->container->updater
|
|
|
|
->expects(static::once())
|
|
|
|
->method('writeUpdates')
|
|
|
|
->with('resource.updates', $updates)
|
|
|
|
;
|
|
|
|
|
|
|
|
/** @var Response $result */
|
|
|
|
$result = $this->middleware->__invoke($request, $response, $controller);
|
|
|
|
|
|
|
|
static::assertInstanceOf(Response::class, $result);
|
|
|
|
static::assertSame(418, $result->getStatusCode());
|
|
|
|
}
|
2020-01-18 17:50:11 +01:00
|
|
|
}
|