~ubuntu-branches/ubuntu/natty/moin/natty-updates

« back to all changes in this revision

Viewing changes to MoinMoin/auth/_tests/test_auth.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-06-22 21:17:13 UTC
  • mto: This revision was merged to the branch mainline in revision 18.
  • Revision ID: james.westby@ubuntu.com-20080622211713-inlv5k4eifxckelr
Import upstream version 1.7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
    @copyright: 2008 MoinMoin:ThomasWaldmann
6
6
    @license: GNU GPL, see COPYING for details.
7
7
"""
8
 
import py.test
9
 
 
10
 
from MoinMoin.web.request import TestRequest, evaluate_request
11
 
from MoinMoin import wsgiapp
12
 
from MoinMoin._tests import wikiconfig
13
 
 
14
 
 
15
 
class AuthTest:
 
8
 
 
9
import StringIO, urllib
 
10
 
 
11
from MoinMoin.server.server_wsgi import WsgiConfig
 
12
from MoinMoin.request import request_wsgi
 
13
 
 
14
 
 
15
class TestAuth:
16
16
    """ test misc. auth methods """
17
17
    PAGES = ['FrontPage', 'MoinMoin', 'HelpContents', 'WikiSandBox', ] # must all exist!
18
18
 
21
21
 
22
22
        Some test needs specific config values, or they will fail.
23
23
        """
 
24
        config = WsgiConfig() # you MUST create an instance
24
25
 
25
26
    def teardown_class(cls):
26
27
        """ Stuff that should run to clean up the state of this test class
27
28
 
28
29
        """
29
 
 
30
 
    def run_request(self, **params):
31
 
        request = TestRequest(**params)
32
 
        #XXX: config passing hack
33
 
        request.given_config = getattr(self, 'Config', wikiconfig.Config)
34
 
        context = wsgiapp.init(request)
35
 
        wsgiapp.run(context)
36
 
        return context
37
 
 
38
 
 
39
 
class TestNoAuth(AuthTest):
 
30
        pass
 
31
 
 
32
    def setup_env(self, **kw):
 
33
        default_environ = {
 
34
            'SERVER_NAME': 'localhost',
 
35
            'SERVER_PORT': '80',
 
36
            'SCRIPT_NAME': '',
 
37
            'PATH_INFO': '/',
 
38
            'QUERY_STRING': '',
 
39
            'REQUEST_METHOD': 'GET',
 
40
            'REMOTE_ADDR': '10.10.10.10',
 
41
            'HTTP_HOST': 'localhost',
 
42
            #'HTTP_COOKIE': '',
 
43
            #'HTTP_ACCEPT_LANGUAGE': '',
 
44
        }
 
45
        env = {}
 
46
        env.update(default_environ)
 
47
        env.update(kw)
 
48
        if 'wsgi.input' not in env:
 
49
            env['wsgi.input'] = StringIO.StringIO()
 
50
        return env
 
51
 
 
52
    def process_request(self, environ):
 
53
        request = request_wsgi.Request(environ)
 
54
        request.run()
 
55
        return request # request.status, request.headers, request.output()
 
56
 
40
57
    def testNoAuth(self):
41
58
        """ run a simple request, no auth, just check if it succeeds """
42
 
        request = self.run_request()
 
59
        environ = self.setup_env()
 
60
        request = self.process_request(environ)
43
61
 
44
62
        # anon user?
45
63
        assert not request.user.valid
46
64
 
47
 
        appiter, status, headers = evaluate_request(request.request)
48
65
        # check if the request resulted in normal status, result headers and content
49
 
        assert status[:3] == '200'
 
66
        assert request.status == '200 OK'
50
67
        has_ct = has_v = has_cc = False
51
 
        for k, v in headers:
 
68
        for k, v in request.headers:
52
69
            if k == 'Content-Type':
53
70
                assert v.startswith('text/html')
54
71
                has_ct = True
65
82
        assert has_v
66
83
        # XXX BROKEN?:
67
84
        #assert has_cc # cache anon user's content
68
 
        assert '</html>' in ''.join(appiter)
69
 
 
70
 
class TestAnonSession(AuthTest):
71
 
    class Config(wikiconfig.Config):
72
 
        cookie_lifetime = 1, 12
 
85
        output = request.output()
 
86
        assert '</html>' in output
73
87
 
74
88
    def testAnonSession(self):
75
89
        """ run some requests, no auth, check if anon sessions work """
 
90
        self.config = self.TestConfig(anonymous_session_lifetime=1)
76
91
        cookie = ''
77
92
        trail_expected = []
78
93
        first = True
79
94
        for pagename in self.PAGES:
80
 
            environ_overrides = {'HTTP_COOKIE': cookie}
81
 
            request = self.run_request(path='/%s' % pagename,
82
 
                                       environ_overrides=environ_overrides)
 
95
            environ = self.setup_env(PATH_INFO='/%s' % pagename,
 
96
                                     HTTP_COOKIE=cookie)
 
97
            request = self.process_request(environ)
83
98
 
84
99
            # anon user?
85
100
            assert not request.user.valid
86
101
 
87
102
            # Do we have a session?
88
 
            assert request.session is not None
 
103
            assert request.session
89
104
 
90
 
            appiter, status, headers = evaluate_request(request.request)
91
105
            # check if the request resulted in normal status, result headers and content
92
 
            assert status[:3] == '200'
 
106
            assert request.status == '200 OK'
93
107
            has_ct = has_v = has_cc = False
94
 
            for k, v in headers:
 
108
            for k, v in request.headers:
95
109
                if k == 'Content-Type':
96
110
                    assert v.startswith('text/html')
97
111
                    has_ct = True
109
123
            assert has_v
110
124
            # XX BROKEN
111
125
            #assert not has_cc # do not cache anon user's (with session!) content
112
 
            assert '</html>' in ''.join(appiter)
 
126
            output = request.output()
 
127
            assert '</html>' in output
113
128
 
114
129
            # The trail is only ever saved on the second page display
115
130
            # because otherwise anonymous sessions would be created
129
144
            trail = request.session['trail']
130
145
            assert trail == trail_expected
131
146
 
132
 
class TestHttpAuthSession(AuthTest):
133
 
    py.test.skip("We currently have no http auth code in moin. GivenAuth relies on the web server doing the http auth check.")
134
 
    class Config(wikiconfig.Config):
135
 
        from MoinMoin.auth.http import HttpAuth # does not exist (yet?)
136
 
        auth = [HttpAuth(autocreate=True)]
137
 
 
138
147
    def testHttpAuthSession(self):
139
148
        """ run some requests with http auth, check whether session works """
 
149
        from MoinMoin.auth.http import HTTPAuth
140
150
        username = u'HttpAuthTestUser'
141
 
        auth_info = u'%s:%s' % (username, u'testpass')
142
 
        auth_header = 'Basic %s' % auth_info.encode('base64')
 
151
        self.config = self.TestConfig(auth=[HTTPAuth()], user_autocreate=True)
143
152
        cookie = ''
144
153
        trail_expected = []
145
154
        first = True
146
155
        for pagename in self.PAGES:
147
 
            environ_overrides = {'HTTP_COOKIE': cookie,
148
 
                                 'HTTP_AUTHORIZATION': auth_header}
149
 
            request = self.run_request(path='/%s' % pagename,
150
 
                                       environ_overrides=environ_overrides)
 
156
            environ = self.setup_env(AUTH_TYPE='Basic', REMOTE_USER=str(username),
 
157
                                     PATH_INFO='/%s' % pagename,
 
158
                                     HTTP_COOKIE=cookie)
 
159
            request = self.process_request(environ)
151
160
 
152
161
            # Login worked?
153
162
            assert request.user.valid
154
163
            assert request.user.name == username
155
164
 
156
165
            # Do we have a session?
157
 
            assert request.session is not None
 
166
            assert request.session
158
167
 
159
 
            appiter, status, headers = evaluate_request(request.request)
160
168
            # check if the request resulted in normal status, result headers and content
161
 
            assert status[:3] == '200'
 
169
            assert request.status == '200 OK'
162
170
            has_ct = has_v = has_cc = False
163
171
            for k, v in request.headers:
164
172
                if k == 'Content-Type':
177
185
            assert has_ct
178
186
            assert has_v
179
187
            assert has_cc # do not cache logged-in user's content
180
 
            assert '</html>' in ''.join(appiter)
 
188
            output = request.output()
 
189
            assert '</html>' in output
181
190
 
182
191
            # The trail is only ever saved on the second page display
183
192
            # because otherwise anonymous sessions would be created
195
204
            trail = request.session['trail']
196
205
            assert trail == trail_expected
197
206
 
198
 
class TestMoinAuthSession(AuthTest):
199
 
    class Config(wikiconfig.Config):
200
 
        from MoinMoin.auth import MoinAuth
201
 
        auth = [MoinAuth()]
202
 
 
203
207
    def testMoinAuthSession(self):
204
208
        """ run some requests with MoinAuth, check whether session works """
 
209
        from MoinMoin.auth import MoinAuth
205
210
        from MoinMoin.user import User
 
211
        self.config = self.TestConfig(auth=[MoinAuth()])
206
212
        username = u'MoinAuthTestUser'
207
 
        password = u'ßecretß'
 
213
        password = u'secret'
208
214
        User(self.request, name=username, password=password).save() # create user
209
215
        trail_expected = []
210
216
        first = True
211
217
        for pagename in self.PAGES:
212
218
            if first:
213
 
                formdata = {
214
 
                    'name': username,
215
 
                    'password': password,
 
219
                formdata = urllib.urlencode({
 
220
                    'name': username.encode('utf-8'),
 
221
                    'password': password.encode('utf-8'),
216
222
                    'login': 'login',
217
 
                }
218
 
                request = self.run_request(path='/%s' % pagename,
219
 
                                           query_string='login=login',
220
 
                                           method='POST', form_data=formdata)
 
223
                })
 
224
                environ = self.setup_env(PATH_INFO='/%s' % pagename,
 
225
                                         HTTP_CONTENT_TYPE='application/x-www-form-urlencoded',
 
226
                                         HTTP_CONTENT_LENGTH='%d' % len(formdata),
 
227
                                         QUERY_STRING='action=login', REQUEST_METHOD='POST',
 
228
                                         **{'wsgi.input': StringIO.StringIO(formdata)})
221
229
            else: # not first page, use session cookie
222
 
                environ_overrides = {'HTTP_COOKIE': cookie}
223
 
                request = self.run_request(path='/%s' % pagename,
224
 
                                           environ_overrides=environ_overrides)
 
230
                environ = self.setup_env(PATH_INFO='/%s' % pagename,
 
231
                                         HTTP_COOKIE=cookie)
 
232
            request = self.process_request(environ)
225
233
 
226
234
            # Login worked?
227
235
            assert request.user.valid
228
236
            assert request.user.name == username
229
237
 
230
238
            # Do we have a session?
231
 
            assert request.session is not None
 
239
            assert request.session
232
240
 
233
 
            appiter, status, headers = evaluate_request(request.request)
234
241
            # check if the request resulted in normal status, result headers and content
235
 
            assert status[:3] == '200'
 
242
            assert request.status == '200 OK'
236
243
            has_ct = has_v = has_cc = False
237
244
            for k, v in request.headers:
238
245
                if k == 'Content-Type':
251
258
            assert has_ct
252
259
            assert has_v
253
260
            assert has_cc # do not cache logged-in user's content
254
 
            assert '</html>' in ''.join(appiter)
 
261
            output = request.output()
 
262
            assert '</html>' in output
255
263
 
256
264
            # The trail is only ever saved on the second page display
257
265
            # because otherwise anonymous sessions would be created