changeset 4205:55e87dd68a22

Fix tests: failing auth-tests & Simplified: request setup code
author Florian Krupicka <florian.krupicka@googlemail.com>
date Wed, 09 Jul 2008 16:11:11 +0200
parents 424800cd9596
children 7c83580dba77
files MoinMoin/auth/_tests/test_auth.py
diffstat 1 files changed, 34 insertions(+), 62 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/auth/_tests/test_auth.py	Wed Jul 09 15:58:14 2008 +0200
+++ b/MoinMoin/auth/_tests/test_auth.py	Wed Jul 09 16:11:11 2008 +0200
@@ -6,11 +6,8 @@
     @license: GNU GPL, see COPYING for details.
 """
 
-import StringIO, urllib
-
-from MoinMoin.server.server_wsgi import WsgiConfig
-from MoinMoin.request import request_wsgi
-
+from MoinMoin.web.request import TestRequest
+from MoinMoin import wsgiapp
 
 class TestAuth:
     """ test misc. auth methods """
@@ -21,7 +18,8 @@
 
         Some test needs specific config values, or they will fail.
         """
-        config = WsgiConfig() # you MUST create an instance
+        # Why this?
+        # config = WsgiConfig() # you MUST create an instance
 
     def teardown_class(cls):
         """ Stuff that should run to clean up the state of this test class
@@ -29,35 +27,16 @@
         """
         pass
 
-    def setup_env(self, **kw):
-        default_environ = {
-            'SERVER_NAME': 'localhost',
-            'SERVER_PORT': '80',
-            'SCRIPT_NAME': '',
-            'PATH_INFO': '/',
-            'QUERY_STRING': '',
-            'REQUEST_METHOD': 'GET',
-            'REMOTE_ADDR': '10.10.10.10',
-            'HTTP_HOST': 'localhost',
-            #'HTTP_COOKIE': '',
-            #'HTTP_ACCEPT_LANGUAGE': '',
-        }
-        env = {}
-        env.update(default_environ)
-        env.update(kw)
-        if 'wsgi.input' not in env:
-            env['wsgi.input'] = StringIO.StringIO()
-        return env
-
-    def process_request(self, environ):
-        request = request_wsgi.Request(environ)
-        request.run()
-        return request # request.status, request.headers, request.output()
+    def run_request(self, **params):
+        if not 'REMOTE_ADDR' in params:
+            params['REMOTE_ADDR'] = '10.10.10.10'
+        request = TestRequest(**params)
+        request = wsgiapp.init(request)
+        return wsgiapp.run(request)
 
     def testNoAuth(self):
         """ run a simple request, no auth, just check if it succeeds """
-        environ = self.setup_env()
-        request = self.process_request(environ)
+        request = self.run_request()
 
         # anon user?
         assert not request.user.valid
@@ -82,8 +61,7 @@
         assert has_v
         # XXX BROKEN?:
         #assert has_cc # cache anon user's content
-        output = request.output()
-        assert '</html>' in output
+        assert '</html>' in request.output()
 
     def testAnonSession(self):
         """ run some requests, no auth, check if anon sessions work """
@@ -92,15 +70,14 @@
         trail_expected = []
         first = True
         for pagename in self.PAGES:
-            environ = self.setup_env(PATH_INFO='/%s' % pagename,
-                                     HTTP_COOKIE=cookie)
-            request = self.process_request(environ)
+            request = self.run_request(path='/%s' % pagename,
+                                       HTTP_COOKIE=cookie)
 
             # anon user?
             assert not request.user.valid
 
             # Do we have a session?
-            assert request.session
+            assert request.session is not None
 
             # check if the request resulted in normal status, result headers and content
             assert request.status == '200 OK'
@@ -123,8 +100,7 @@
             assert has_v
             # XX BROKEN
             #assert not has_cc # do not cache anon user's (with session!) content
-            output = request.output()
-            assert '</html>' in output
+            assert '</html>' in request.output()
 
             # The trail is only ever saved on the second page display
             # because otherwise anonymous sessions would be created
@@ -148,22 +124,23 @@
         """ run some requests with http auth, check whether session works """
         from MoinMoin.auth.http import HTTPAuth
         username = u'HttpAuthTestUser'
+        auth_info = u'%s:%s' % (username, u'testpass')
+        auth_header = 'Basic %s' % auth_info.encode('base64')
         self.config = self.TestConfig(auth=[HTTPAuth()], user_autocreate=True)
         cookie = ''
         trail_expected = []
         first = True
         for pagename in self.PAGES:
-            environ = self.setup_env(AUTH_TYPE='Basic', REMOTE_USER=str(username),
-                                     PATH_INFO='/%s' % pagename,
-                                     HTTP_COOKIE=cookie)
-            request = self.process_request(environ)
+            request = self.run_request(path='/%s' % pagename,
+                                       HTTP_COOKIE=cookie,
+                                       HTTP_AUTHORIZATION=auth_header)
 
             # Login worked?
             assert request.user.valid
             assert request.user.name == username
 
             # Do we have a session?
-            assert request.session
+            assert request.session is not None
 
             # check if the request resulted in normal status, result headers and content
             assert request.status == '200 OK'
@@ -185,8 +162,7 @@
             assert has_ct
             assert has_v
             assert has_cc # do not cache logged-in user's content
-            output = request.output()
-            assert '</html>' in output
+            assert '</html>' in request.output()
 
             # The trail is only ever saved on the second page display
             # because otherwise anonymous sessions would be created
@@ -216,27 +192,24 @@
         first = True
         for pagename in self.PAGES:
             if first:
-                formdata = urllib.urlencode({
-                    'name': username.encode('utf-8'),
-                    'password': password.encode('utf-8'),
+                formdata = {
+                    'name': username,
+                    'password': password,
                     'login': 'login',
-                })
-                environ = self.setup_env(PATH_INFO='/%s' % pagename,
-                                         HTTP_CONTENT_TYPE='application/x-www-form-urlencoded',
-                                         HTTP_CONTENT_LENGTH='%d' % len(formdata),
-                                         QUERY_STRING='action=login', REQUEST_METHOD='POST',
-                                         **{'wsgi.input': StringIO.StringIO(formdata)})
+                }
+                request = self.run_request(path='/%s' % pagename,
+                                           query_string='action=login',
+                                           method='POST', form_data=formdata)
             else: # not first page, use session cookie
-                environ = self.setup_env(PATH_INFO='/%s' % pagename,
-                                         HTTP_COOKIE=cookie)
-            request = self.process_request(environ)
+                request = self.run_request(path='/%s' % pagename,
+                                           HTTP_COOKIE=cookie)
 
             # Login worked?
             assert request.user.valid
             assert request.user.name == username
 
             # Do we have a session?
-            assert request.session
+            assert request.session is not None
 
             # check if the request resulted in normal status, result headers and content
             assert request.status == '200 OK'
@@ -258,8 +231,7 @@
             assert has_ct
             assert has_v
             assert has_cc # do not cache logged-in user's content
-            output = request.output()
-            assert '</html>' in output
+            assert '</html>' in request.output()
 
             # The trail is only ever saved on the second page display
             # because otherwise anonymous sessions would be created