Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: GannettDigital/Transmogrify
base: 6725084519
...
head fork: GannettDigital/Transmogrify
compare: 588db407c7
Checking mergeability… Don't worry, you can still create the pull request.
  • 3 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
Commits on Apr 10, 2012
Eric Moritz Changed settings so that the tests will run w/ modern Django caeb023
Eric Moritz Fixed the tests and added a test for utils.generate_url
The tests had a flawed security hash generator that ignored the secret key
24a90b2
Eric Moritz Added a generate_url function per Jonathan's request 588db40
Showing with 47 additions and 26 deletions.
  1. +13 −11 example/settings.py
  2. +28 −14 transmogrify/tests.py
  3. +6 −1 transmogrify/utils.py
View
24 example/settings.py
@@ -13,12 +13,17 @@
MANAGERS = ADMINS
-DATABASE_ENGINE = 'sqlite3' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
-DATABASE_NAME = 'dev.db' # Or path to database file if using sqlite3.
-DATABASE_USER = '' # Not used with sqlite3.
-DATABASE_PASSWORD = '' # Not used with sqlite3.
-DATABASE_HOST = '' # Set to empty string for localhost. Not used with sqlite3.
-DATABASE_PORT = '' # Set to empty string for default. Not used with sqlite3.
+DATABASES = {
+ 'default': {
+ 'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
+ 'NAME': 'dev.db', # Or path to database file if using sqlite3.
+ 'USER': '', # Not used with sqlite3.
+ 'PASSWORD': '', # Not used with sqlite3.
+ 'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
+ 'PORT': '', # Set to empty string for default. Not used with sqlite3.
+ }
+}
+
# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
@@ -76,10 +81,7 @@
)
INSTALLED_APPS = (
- 'django.contrib.admin',
- 'django.contrib.auth',
- 'django.contrib.contenttypes',
- 'django.contrib.sessions',
- 'django.contrib.sites',
'transmogrify',
)
+
+TRANSMOGRIFY_SECRET = "whatevs"
View
42 transmogrify/tests.py
@@ -1,8 +1,11 @@
import unittest
import os
-from django.template import Template
+from django.template import Template, Context
+from StringIO import StringIO
from transmogrify import Transmogrify
+import utils
+import settings
from PIL import Image
try:
from django.test import TestCase
@@ -10,6 +13,7 @@
except ImportError:
HAS_DJANGO = False
+
class TestTransmogrify(unittest.TestCase):
"""Testing the features of Transmogrify"""
def setUp(self):
@@ -103,7 +107,7 @@ class UrlProcessingTest(TestCase):
"""
def doShaHash(self, value):
import hashlib
- return hashlib.sha1(value).hexdigest()
+ return hashlib.sha1(value + settings.SECRET_KEY).hexdigest()
def testAliases(self):
import utils
@@ -151,6 +155,7 @@ def request(self, **request):
'SERVER_NAME': 'testserver',
'SERVER_PORT': 80,
'SERVER_PROTOCOL': 'HTTP/1.1',
+ 'wsgi.input': StringIO(),
}
environ.update(self.defaults)
environ.update(request)
@@ -159,40 +164,40 @@ def request(self, **request):
class TemplateTagTest(TestCase):
def doShaHash(self, value):
import hashlib
- return hashlib.sha1(value).hexdigest()
+ return hashlib.sha1(value + settings.SECRET_KEY).hexdigest()
def testResize(self):
t = Template("{% load transmogrifiers %}{% resize /test/picture.jpg 300 %}")
- self.assertEqual(t.render({}), '/test/picture_r300.jpg?%s' % self.doShaHash("_r300"))
+ self.assertEqual(t.render(Context({})), '/test/picture_r300.jpg?%s' % self.doShaHash("_r300"))
t = Template("{% load transmogrifiers %}{% resize /test/picture.jpg x300 %}")
- self.assertEqual(t.render({}), '/test/picture_rx300.jpg?%s' % self.doShaHash("_rx300"))
+ self.assertEqual(t.render(Context({})), '/test/picture_rx300.jpg?%s' % self.doShaHash("_rx300"))
t = Template("{% load transmogrifiers %}{% resize /test/picture.jpg 300x300 %}")
- self.assertEqual(t.render({}), '/test/picture_r300x300.jpg?%s' % self.doShaHash("_r300x300"))
+ self.assertEqual(t.render(Context({})), '/test/picture_r300x300.jpg?%s' % self.doShaHash("_r300x300"))
def testForceFit(self):
t = Template("{% load transmogrifiers %}{% forcefit /test/picture.jpg 300 %}")
- self.assertEqual(t.render({}), '/test/picture_s300.jpg?%s' % self.doShaHash("_s300"))
+ self.assertEqual(t.render(Context({})), '/test/picture_s300.jpg?%s' % self.doShaHash("_s300"))
t = Template("{% load transmogrifiers %}{% forcefit /test/picture.jpg x300 %}")
- self.assertEqual(t.render({}), '/test/picture_sx300.jpg?%s' % self.doShaHash("_sx300"))
+ self.assertEqual(t.render(Context({})), '/test/picture_sx300.jpg?%s' % self.doShaHash("_sx300"))
t = Template("{% load transmogrifiers %}{% forcefit /test/picture.jpg 300x300 %}")
- self.assertEqual(t.render({}), '/test/picture_s300x300.jpg?%s' % self.doShaHash("_s300x300"))
+ self.assertEqual(t.render(Context({})), '/test/picture_s300x300.jpg?%s' % self.doShaHash("_s300x300"))
def testCrop(self):
t = Template("{% load transmogrifiers %}{% crop /test/picture.jpg 300x300 %}")
- self.assertEqual(t.render({}), '/test/picture_c300x300.jpg?%s' % self.doShaHash("_c300x300"))
+ self.assertEqual(t.render(Context({})), '/test/picture_c300x300.jpg?%s' % self.doShaHash("_c300x300"))
def testLetterbox(self):
t = Template("{% load transmogrifiers %}{% letterbox /test/picture.jpg 300x300 #f8129b %}")
- self.assertEqual(t.render({}), '/test/picture_l300x300-f8129b.jpg?%s' % self.doShaHash("_l300x300-f8129b"))
+ self.assertEqual(t.render(Context({})), '/test/picture_l300x300-f8129b.jpg?%s' % self.doShaHash("_l300x300-f8129b"))
def testBorder(self):
t = Template("{% load transmogrifiers %}{% border /test/picture.jpg 1 #f8129b %}")
- self.assertEqual(t.render({}), '/test/picture_b1-f8129b.jpg?%s' % self.doShaHash("_b1-f8129b"))
+ self.assertEqual(t.render(Context({})), '/test/picture_b1-f8129b.jpg?%s' % self.doShaHash("_b1-f8129b"))
class ViewTest(TestCase):
def doShaHash(self, value):
import hashlib
- return hashlib.sha1(value).hexdigest()
+ return hashlib.sha1(value + settings.SECRET_KEY).hexdigest()
def testView(self):
rf = RequestFactory()
@@ -203,6 +208,15 @@ def testView(self):
response = views.transmogrify_serve(request, path, document_root)
self.assertEqual(response.status_code, 200)
+ class TestUtil(unittest.TestCase):
+ def testGenerateUrl(self):
+ expected = ("http://example.com/media/foo_r200.jpg"
+ "?d7a3f8c02c4ecb0c13aa024e1d80d1053ad1deec")
+
+ result = utils.generate_url("http://example.com/media/foo.jpg",
+ "_r200")
+ self.assertEqual(expected, result)
+
if __name__ == "__main__":
- unittest.main()
+ unittest.main()
View
7 transmogrify/utils.py
@@ -40,6 +40,12 @@ def create_securityhash(action_tuples):
security_hash = sha_constructor(action_string + SECRET_KEY).hexdigest()
return security_hash
+def generate_url(url, action_string):
+ security_hash = sha_constructor(action_string + SECRET_KEY).hexdigest()
+ base_url, ext = os.path.splitext(url)
+
+ return "%s%s%s?%s" % (base_url, action_string, ext, security_hash)
+
def is_valid_security(action_tuples, security_hash):
return create_securityhash(action_tuples) == security_hash
@@ -124,4 +130,3 @@ def process_url(url, server_name="", document_root=None):
'requested_file': requested_path,
'original_file': original_file
}
-

No commit comments for this range

Something went wrong with that request. Please try again.