Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if session is started before accessing #35

Merged
merged 2 commits into from
Dec 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ protected function serve(Application $app, Request $request, $file, $action, $wi
protected function isRestricted(Application $app, Request $request)
{
$session = $request->getSession();
$auth = (isset($session)) ? $session->get('authentication') : null;
$auth = $session && $session->isStarted() ? $session->get('authentication') : null;

if ($auth && ($auth->getUser()->getEnabled())) {
if ($auth && $auth->getUser()->getEnabled()) {
return false;
}

Expand Down
43 changes: 33 additions & 10 deletions tests/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
use Bolt\Filesystem\Handler\Image;
use Bolt\Filesystem\Handler\Image\Dimensions;
use Bolt\Thumbs\Controller;
use Bolt\Thumbs;
use Bolt\Thumbs\Thumbnail;
use Bolt\Thumbs\Transaction;
use Silex\Application;
use Silex\Provider\ServiceControllerServiceProvider;
use Silex\WebTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpKernel\Exception\HttpException;

class ControllerTest extends WebTestCase
{
Expand Down Expand Up @@ -52,10 +55,10 @@ public function testIsRestricted()
$app['thumbnails.only_aliases'] = false;
$controller = new Controller();
$request = Request::create('/thumbs/123x456c/herp/derp.png');
$this->assertInstanceOf('Bolt\Thumbs\Response', $controller->thumbnail($app, $request, 'herp/derp.png', 'c', 123, 456));
$this->assertInstanceOf(Thumbs\Response::class, $controller->thumbnail($app, $request, 'herp/derp.png', 'c', 123, 456));

$app['thumbnails.only_aliases'] = true;
$this->setExpectedException('Symfony\Component\HttpKernel\Exception\HttpException');
$this->setExpectedException(HttpException::class);
$controller->thumbnail($app, $request, 'herp/derp.png', 'c', 123, 456);
}

Expand All @@ -65,23 +68,39 @@ public function testNotIsRestrictedWhenLoggedIn()
$controller = new Controller();
$request = Request::create('/thumbs/123x456c/herp/derp.png');

$session = $this->getMock('Symfony\Component\HttpFoundation\Session\Session');
$user = $this->getMock('stdClass', ['getEnabled']);
$user = $this->getMockBuilder('stdClass')
->setMethods(['getEnabled'])
->getMock()
;
$user->expects($this->any())
->method('getEnabled')
->willReturn(true);
$auth = $this->getMock('stdClass', ['getUser']);
->willReturn(true)
;

$auth = $this->getMockBuilder('stdClass')
->setMethods(['getUser'])
->getMock()
;
$auth->expects($this->any())
->method('getUser')
->willReturn($user);
->willReturn($user)
;

$session = $this->getMockBuilder(Session::class)->getMock();
$session->expects($this->any())
->method('get')
->with('authentication')
->willReturn($auth);
->willReturn($auth)
;
$session->expects($this->atLeastOnce())
->method('isStarted')
->willReturn(true)
;
/** @var Session $session */
$request->setSession($session);

$app['thumbnails.only_aliases'] = true;
$this->assertInstanceOf('Bolt\Thumbs\Response', $controller->thumbnail($app, $request, 'herp/derp.png', 'c', 123, 456));
$this->assertInstanceOf(Thumbs\Response::class, $controller->thumbnail($app, $request, 'herp/derp.png', 'c', 123, 456));
}

/**
Expand All @@ -94,7 +113,11 @@ public function createApplication()
$app->mount('/thumbs', $app['controller.thumbnails']);
$app->register(new ServiceControllerServiceProvider());

$mock = $this->getMock('Bolt\Thumbs\ThumbnailResponder', ['respond'], [], '', false);
$mock = $this->getMockBuilder(Thumbs\Responder::class)
->setMethods(['respond'])
->disableOriginalConstructor()
->getMock()
;
$mock
->expects($this->any())
->method('respond')
Expand Down
3 changes: 2 additions & 1 deletion tests/ResponderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Bolt\Filesystem;
use Bolt\Filesystem\Adapter\Local;
use Bolt\Filesystem\Handler\Image;
use Bolt\Thumbs\Creator;
use Bolt\Thumbs\CreatorInterface;
use Bolt\Thumbs\FinderInterface;
use Bolt\Thumbs\Responder;
Expand Down Expand Up @@ -70,7 +71,7 @@ public function testSrcImage()

public function testCaching()
{
$this->creator = $this->getMock('\Bolt\Thumbs\CreatorInterface');
$this->creator = $this->getMockBuilder(Creator::class)->getMock();
$this->creator->expects($this->once())
->method('create')
->willReturnCallback(
Expand Down