Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fix tests.

  • Loading branch information...
commit 4f88aabf58b68da483118dfd1a3ef874f0cc2097 1 parent fe6ef55
@makasim makasim authored
View
18 Controller/ImagineController.php
@@ -106,27 +106,33 @@ public function filterRuntimeAction(Request $request, $hash, $path, $filter)
{
try {
$filters = $request->query->get('filters', array());
- // Runtime config images have the trimmed hash prepended
+
if (true !== $this->signer->check($hash, $path, $filters)) {
- throw new BadRequestHttpException('Signed url does not pass the sign check. Maybe it was modified by someone.');
+ throw new BadRequestHttpException(sprintf(
+ 'Signed url does not pass the sign check for path "%s" and filter "%s" and runtime config %s',
+ $path,
+ $filter,
+ json_encode($filters)
+ ));
}
try {
$binary = $this->dataManager->find($filter, $path);
} catch (NotLoadableException $e) {
-
- throw new NotFoundHttpException('Source image could not be found', $e);
+ throw new NotFoundHttpException(sprintf('Source image could not be found for path "%s" and filter "%s"', $path, $filter), $e);
}
+ $cachePrefix = 'rc/'.$hash;
+
$this->cacheManager->store(
$this->filterManager->applyFilter($binary, $filter, array(
'filters' => $filters,
)),
- 'rc/'.$hash.'/'.$path,
+ $cachePrefix.'/'.$path,
$filter
);
- return new RedirectResponse($this->cacheManager->resolve('rc/'.$hash.'/'.$path, $filter), 301);
+ return new RedirectResponse($this->cacheManager->resolve($cachePrefix.'/'.$path, $filter), 301);
} catch (RuntimeException $e) {
throw new \RuntimeException(sprintf('Unable to create image for path "%s" and filter "%s". Message was "%s"', $hash.'/'.$path, $filter, $e->getMessage()), 0, $e);
}
View
6 Imagine/Cache/Signer.php
@@ -22,6 +22,12 @@ public function __construct($secret)
*/
public function sign($path, array $runtimeConfig = null)
{
+ if ($runtimeConfig) {
+ array_walk_recursive($runtimeConfig, function (&$value) {
+ $value = (string)$value;
+ });
+ }
+
return substr(preg_replace('/[^a-zA-Z0-9-_]/', '', base64_encode(hash_hmac('sha256', ltrim($path, '/') . (null === $runtimeConfig ?: serialize($runtimeConfig)), $this->secret, true))), 0, 8);
}
View
28 Tests/Functional/Controller/ImagineControllerTest.php
@@ -82,11 +82,11 @@ public function testShouldResolveFromCache()
/**
* @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
- * @expectedExceptionMessage Signed url does not pass the sign check. Maybe it was modified by someone.
+ * @expectedExceptionMessage Signed url does not pass the sign check for path "images/cats.jpeg" and filter "thumbnail_web_path" and runtime config {"thumbnail":{"size":["50","50"]}}
*/
public function testThrowBadRequestIfSignInvalidWhileUsingCustomFilters()
{
- $this->client->request('GET', '/media/cache/thumbnail_web_path/images/cats.jpeg?'.http_build_query(array(
+ $this->client->request('GET', '/media/cache/thumbnail_web_path/rc/invalidHash/images/cats.jpeg?'.http_build_query(array(
'filters' => array(
'thumbnail' => array('size' => array(50, 50))
),
@@ -114,13 +114,16 @@ public function testShouldResolveWithCustomFiltersPopulatingCacheFirst()
),
);
- $path = 'thumbnail_web_path/images/cats.jpeg';
- $expectedCachePath = 'thumbnail_web_path/'.$signer->sign($path, $params['filters']).'/images/cats.jpeg';
+ $path = 'images/cats.jpeg';
- $url = 'http://localhost/media/cache/'.$path.'?'.http_build_query($params);
+ $hash = $signer->sign($path, $params['filters']);
+
+ $expectedCachePath = 'thumbnail_web_path/rc/'.$hash.'/'.$path;
+
+ $url = 'http://localhost/media/cache/'.$expectedCachePath.'?'.http_build_query($params);
//guard
- $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg');
+ $this->assertFileNotExists($this->cacheRoot.'/'.$expectedCachePath);
$this->client->request('GET', $url);
@@ -144,16 +147,19 @@ public function testShouldResolveWithCustomFiltersFromCache()
),
);
- $path = 'thumbnail_web_path/images/cats.jpeg';
- $expectedCachePath = 'thumbnail_web_path/'.$signer->sign($path, $params['filters']).'/images/cats.jpeg';
+ $path = 'images/cats.jpeg';
+
+ $hash = $signer->sign($path, $params['filters']);
+
+ $expectedCachePath = 'thumbnail_web_path/rc/'.$hash.'/'.$path;
+
+ $url = 'http://localhost/media/cache/'.$expectedCachePath.'?'.http_build_query($params);
$this->filesystem->dumpFile(
- $this->cacheRoot.$expectedCachePath,
+ $this->cacheRoot.'/'.$expectedCachePath,
'anImageContent'
);
- $url = 'http://localhost/media/cache/'.$path.'?'.http_build_query($params);
-
$this->client->request('GET', $url);
$response = $this->client->getResponse();
View
64 Tests/Imagine/Cache/SignerTest.php
@@ -0,0 +1,64 @@
+<?php
+
+namespace Liip\ImagineBundle\Tests\Imagine\Cache;
+
+use Liip\ImagineBundle\Tests\AbstractTest;
+use Liip\ImagineBundle\Imagine\Cache\Signer;
+use Symfony\Component\HttpFoundation\Request;
+
+class SignerTest extends AbstractTest
+{
+ public function testImplementsSignerInterface()
+ {
+ $rc = new \ReflectionClass('Liip\ImagineBundle\Imagine\Cache\Signer');
+
+ $this->assertTrue($rc->implementsInterface('Liip\ImagineBundle\Imagine\Cache\SignerInterface'));
+ }
+
+ public function testCouldBeConstructedWithSecret()
+ {
+ new Signer('aSecret');
+ }
+
+ public function testShouldReturnShortHashOnSign()
+ {
+ $singer = new Signer('aSecret');
+
+ $this->assertEquals(8, strlen($singer->sign('aPath')));
+ }
+
+ public function testShouldSingAndSuccessfullyCheckPathWithoutRuntimeConfig()
+ {
+ $singer = new Signer('aSecret');
+
+ $this->assertTrue($singer->check($singer->sign('aPath'), 'aPath'));
+ }
+
+ public function testShouldSingAndSuccessfullyCheckPathWithRuntimeConfig()
+ {
+ $singer = new Signer('aSecret');
+
+ $this->assertTrue($singer->check($singer->sign('aPath', array('aConfig')), 'aPath', array('aConfig')));
+ }
+
+ public function testShouldConvertRecursivelyToStringAllRuntimeConfigParameters()
+ {
+ $singer = new Signer('aSecret');
+
+ $runtimeConfigInts = array(
+ 'foo' => 14,
+ 'bar' => array(
+ 'bar' => 15,
+ )
+ );
+
+ $runtimeConfigStrings = array(
+ 'foo' => "14",
+ 'bar' => array(
+ 'bar' => "15",
+ )
+ );
+
+ $this->assertTrue($singer->check($singer->sign('aPath', $runtimeConfigInts), 'aPath', $runtimeConfigStrings));
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.