5
5
@copyright: 2008 MoinMoin:ThomasWaldmann
6
6
@license: GNU GPL, see COPYING for details.
10
from MoinMoin.web.request import TestRequest, evaluate_request
11
from MoinMoin import wsgiapp
12
from MoinMoin._tests import wikiconfig
9
import StringIO, urllib
11
from MoinMoin.server.server_wsgi import WsgiConfig
12
from MoinMoin.request import request_wsgi
16
16
""" test misc. auth methods """
17
17
PAGES = ['FrontPage', 'MoinMoin', 'HelpContents', 'WikiSandBox', ] # must all exist!
22
22
Some test needs specific config values, or they will fail.
24
config = WsgiConfig() # you MUST create an instance
25
26
def teardown_class(cls):
26
27
""" Stuff that should run to clean up the state of this test class
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)
39
class TestNoAuth(AuthTest):
32
def setup_env(self, **kw):
34
'SERVER_NAME': 'localhost',
39
'REQUEST_METHOD': 'GET',
40
'REMOTE_ADDR': '10.10.10.10',
41
'HTTP_HOST': 'localhost',
43
#'HTTP_ACCEPT_LANGUAGE': '',
46
env.update(default_environ)
48
if 'wsgi.input' not in env:
49
env['wsgi.input'] = StringIO.StringIO()
52
def process_request(self, environ):
53
request = request_wsgi.Request(environ)
55
return request # request.status, request.headers, request.output()
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)
45
63
assert not request.user.valid
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
68
for k, v in request.headers:
52
69
if k == 'Content-Type':
53
70
assert v.startswith('text/html')
67
84
#assert has_cc # cache anon user's content
68
assert '</html>' in ''.join(appiter)
70
class TestAnonSession(AuthTest):
71
class Config(wikiconfig.Config):
72
cookie_lifetime = 1, 12
85
output = request.output()
86
assert '</html>' in output
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)
77
92
trail_expected = []
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,
97
request = self.process_request(environ)
85
100
assert not request.user.valid
87
102
# Do we have a session?
88
assert request.session is not None
103
assert request.session
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
108
for k, v in request.headers:
95
109
if k == 'Content-Type':
96
110
assert v.startswith('text/html')
129
144
trail = request.session['trail']
130
145
assert trail == trail_expected
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)]
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)
144
153
trail_expected = []
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,
159
request = self.process_request(environ)
153
162
assert request.user.valid
154
163
assert request.user.name == username
156
165
# Do we have a session?
157
assert request.session is not None
166
assert request.session
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':
195
204
trail = request.session['trail']
196
205
assert trail == trail_expected
198
class TestMoinAuthSession(AuthTest):
199
class Config(wikiconfig.Config):
200
from MoinMoin.auth import MoinAuth
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ß'
208
214
User(self.request, name=username, password=password).save() # create user
209
215
trail_expected = []
211
217
for pagename in self.PAGES:
215
'password': password,
219
formdata = urllib.urlencode({
220
'name': username.encode('utf-8'),
221
'password': password.encode('utf-8'),
216
222
'login': 'login',
218
request = self.run_request(path='/%s' % pagename,
219
query_string='login=login',
220
method='POST', form_data=formdata)
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,
232
request = self.process_request(environ)
227
235
assert request.user.valid
228
236
assert request.user.name == username
230
238
# Do we have a session?
231
assert request.session is not None
239
assert request.session
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':