Commits

Jesper Nøhr  committed aec0852

test for logged in users, now knows whether it's behind nginx and will ignore any in-django ssi processing

  • Participants
  • Parent commits 5e70069

Comments (0)

Files changed (5)

File fakesi/middleware.py

 import re
+import urlparse
 
-from django.core.urlresolvers import get_urlconf, get_resolver, Resolver404
-from django.core.urlresolvers import resolve
+from django.core.urlresolvers import resolve, Resolver404
 
 INCLUDE_TAG = re.compile(r'<!--#[\s.]+include[\s.]+virtual=["\'](?P<path>.+)["\'][\s.]+-->')
 
+def nginx_ssi(request):
+    return request.META.get('HTTP_NGINX_SSI', 'off') == 'on'
+
 class FakesiMiddleware(object):
     def process_response(self, request, response):
-        def ssi_include(match):
-            func, args, kwargs = resolve(match.group('path'))
+        if nginx_ssi(request):
+            return response
+        else:
+            def ssi_include(match):
+                func, args, kwargs = resolve(match.group('path'))
+                up = urlparse.urlparse(match.group('path'))
+
+                request.META['PATH_INFO'] = up.path
+                request.META['QUERY_STRING'] = up.query
 
-            # TODO: Insert fancy response when path doesn't resolve.
+                # TODO: Insert fancy response when path doesn't resolve.
 
-            return func(request).content
+                return func(request).content
 
-        response.content = re.sub(INCLUDE_TAG, ssi_include, response.content)
-        response['Content-Length'] = len(response.content)
+            response.content = re.sub(INCLUDE_TAG, ssi_include, response.content)
+            response['Content-Length'] = len(response.content)
 
-        return response
+            return response

File fakesi/tests/runtests.py

 
 if not settings.configured:
     settings.configure(
-        DATABASE_ENGINE='sqlite3',
+        DATABASES={
+            'default':{
+                'ENGINE': 'sqlite3',
+                'NAME': 'fakesi_tests.db',
+            }
+        },
         INSTALLED_APPS=[
+            'django.contrib.contenttypes',
+            'django.contrib.auth',
+            'django.contrib.sessions',
             'fakesi',
             'fakesi.tests',
         ],
+        TEMPLATE_CONTEXT_PROCESSORS=[
+            'django.core.context_processors.request'
+        ],
         TEMPLATE_DIRS=[
             'fakesi/tests/',
         ],
         ROOT_URLCONF='fakesi.tests.urls',
         MIDDLEWARE_CLASSES=[
+            'django.contrib.sessions.middleware.SessionMiddleware',
+            'django.contrib.auth.middleware.AuthenticationMiddleware',
             'fakesi.middleware.FakesiMiddleware',
         ]
     )
 
 from django.test.simple import run_tests
 
-
 def runtests(*test_args):
     if not test_args:
         test_args = ['tests']
     failures = run_tests(test_args, verbosity=1, interactive=True)
     sys.exit(failures)
 
-
 if __name__ == '__main__':
     runtests(*sys.argv[1:])

File fakesi/tests/templates/root.html

 </head>
 <body>
 	<h1>This is the body of the template</h1>
-	<p>I'm about to do an SSI include!</p>
+	<p>This is '{{ request.path }}'.</p>
+	<p>I'm about to do an SSI include! In the root, you are '{{ request.user }}'</p>
 	<!--# include virtual="/ssi?with=getvars" -->
 </body>
 </html>

File fakesi/tests/tests.py

 from django.test import TestCase
 
+from django.contrib.auth.models import User
+
 from django.test.client import Client
 
 class LolTest(TestCase):
 	def test_me(self):
 		c = Client()
 
+		print c.get('/')
+
+class TestLoggedIn(TestCase):
+	def test_login(self):
+		c = Client()
+
+		# make a user
+		User.objects.create_user('admin', 'admin@admin.ca', 'admin')
+
+		assert c.login(username='admin', password='admin') == True, "couldn't log in"
+
 		print c.get('/')

File fakesi/tests/views.py

         {}, RequestContext(request))
 
 def ssi(request):
-    print "MOTHERFUCKING SSI CALLED"
-    return HttpResponse("hey "+repr(request))
+    print "THIS IS SSI", request
+    return HttpResponse("hey "+repr(request.user))