Permalink
Browse files

Fix-up request object. Added tests.

  • Loading branch information...
1 parent e96b51b commit 4d3348d49f7c8b7ae20274dbd49ff588385e4e2b @tomatohater committed Aug 11, 2011
@@ -0,0 +1 @@
+# Nothing to see here
@@ -13,21 +13,20 @@
def obfuscate(value, juice=None):
"""
Template filter that obfuscates whatever text it is applied to. The text is
- supposed to be a URL, but it will obfuscate any text.
+ supposed to be a URL, but it will obfuscate anything.
Usage:
Extremely unfriendly URL:
- {{ "/my-site-path/"|obfuscate }}
+ {{ "/my-secret-path/"|obfuscate }}
Include some SEO juice:
- {{ "/my-site-path/"|obfuscate:"some SEO friendly text" }}
+ {{ "/my-secret-path/"|obfuscate:"some SEO friendly text" }}
"""
obfuscator = Obfuscator(settings.UNFRIENDLY_SECRET)
kwargs = {
'key': obfuscator.obfuscate(*[value]),
}
-
if juice:
kwargs['juice'] = slugify(juice)
@@ -0,0 +1,86 @@
+import datetime
+
+from django.test import TestCase
+from django.core.urlresolvers import reverse
+from django.template.defaultfilters import slugify
+
+from unfriendly import settings
+from unfriendly.utils import Obfuscator
+from unfriendly.templatetags.unfriendly_tags import obfuscate
+
+
+class UnfriendlyTests(TestCase):
+ urls = 'unfriendly.tests.urls'
+
+ def setUp(self):
+ self.obfuscator = Obfuscator(settings.UNFRIENDLY_SECRET)
+ self.juice = 'Lorem ipsum dolor sit amet'
+
+ def test_obfuscator(self):
+ """
+ Test the Obfuscator.
+ """
+ original = self.juice
+
+ obfuscated = self.obfuscator.obfuscate(original)
+ self.assertNotEqual(original, obfuscated)
+
+ deobfuscated = self.obfuscator.deobfuscate(obfuscated)
+ self.assertEqual(original, deobfuscated)
+
+ def test_obfuscate_filter(self):
+ """
+ Test the obfuscate filter.
+ """
+ test_url = reverse('unfriendly-test')
+ obfuscated_url = obfuscate(test_url)
+ view_url = reverse('unfriendly-deobfuscate', kwargs={
+ 'key': self.obfuscator.obfuscate(test_url),
+ })
+ self.assertEqual(view_url, obfuscated_url)
+
+ def test_obfuscate_filter_with_juice(self):
+ """
+ Test the obfuscate filter.
+ """
+ test_url = reverse('unfriendly-test')
+ obfuscated_url = obfuscate(test_url, self.juice)
+ view_url = reverse('unfriendly-deobfuscate', kwargs={
+ 'juice': slugify(self.juice),
+ 'key': self.obfuscator.obfuscate(test_url),
+ })
+ self.assertEqual(view_url, obfuscated_url)
+
+ def test_deobfuscate_view(self):
+ """
+ Test the deobfuscate view.
+ """
+ test_url = reverse('unfriendly-test')
+ obfuscated_url = obfuscate(test_url)
+
+ test_response = self.client.get(test_url)
+ obfuscated_response = self.client.get(obfuscated_url)
+
+ self.assertEqual(test_response.content, obfuscated_response.content)
+
+ def test_deobfuscate_view_with_juice(self):
+ """
+ Test the deobfuscate view with seo juice.
+ """
+ test_url = reverse('unfriendly-test')
+ obfuscated_url = obfuscate(test_url, self.juice)
+
+ test_response = self.client.get(test_url)
+ obfuscated_response = self.client.get(obfuscated_url)
+
+ self.assertEqual(test_response.content, obfuscated_response.content)
+
+ def test_deobfuscate_view_404(self):
+ """
+ Test deobfuscate view with hacked url.
+ """
+ view_url = reverse('unfriendly-deobfuscate', kwargs={
+ 'key': 'hacked-key',
+ })
+ response = self.client.get(view_url)
+ self.assertEqual(response.status_code, 404)
@@ -0,0 +1,10 @@
+from django.conf.urls.defaults import patterns, url, include
+
+
+urlpatterns = patterns('unfriendly.tests.views',
+ url(r'^test-view/$', 'test_view', name='unfriendly-test'),
+)
+
+urlpatterns += patterns('',
+ url(r'^', include('unfriendly.urls')),
+)
@@ -0,0 +1,5 @@
+from django.http import HttpResponse
+
+
+def test_view(request):
+ return HttpResponse('this is a test')
View
@@ -1,4 +1,4 @@
-from django.conf.urls.defaults import *
+from django.conf.urls.defaults import patterns, url
urlpatterns = patterns('unfriendly.views',
# Mostly unfriendly URL (but with SEO juice).
View
@@ -9,14 +9,16 @@
def deobfuscate(request, key, juice=None):
"""
- Returns HttpResponse from original obfuscated view.
- SEO juice is ignored since it is only for URL display purposes.
+ Deobfuscates the URL and returns HttpResponse from source view.
+ SEO juice is mostly ignored as it is intended for display purposes only.
"""
obfuscator = Obfuscator(settings.UNFRIENDLY_SECRET)
+
try:
url = obfuscator.deobfuscate(str(key))
except:
return HttpResponseNotFound()
+
url_parts = urlparse(url)
path = url_parts.path
query = url_parts.query
@@ -26,7 +28,17 @@ def deobfuscate(request, key, juice=None):
except Resolver404:
return HttpResponseNotFound()
- if query:
- request.GET = QueryDict(query)
+ # fix-up the request object
+ request.path = path
+ request.path_info = path
+ request.GET = QueryDict(query)
+ request.META['QUERY_STRING'] = query
+ request.META['PATH_INFO'] = path
+
+ response = view(request, *args, **kwargs)
+
+ # offer up a friendlier juice-powered filename if downloaded
+ if juice and not response.has_header('Content-Disposition'):
+ response['Content-Disposition'] = 'inline; filename=%s' % juice
- return view(request, *args, **kwargs)
+ return response

0 comments on commit 4d3348d

Please sign in to comment.