Skip to content

Commit

Permalink
Merge pull request #35 from GawainLynch/hotfix/session
Browse files Browse the repository at this point in the history
Check if session is started before accessing
  • Loading branch information
CarsonF authored Dec 27, 2016
2 parents 3d307b7 + db6b2bd commit 9ec3f4d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
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

0 comments on commit 9ec3f4d

Please sign in to comment.