File: test.py

package info (click to toggle)
python-authkit 0.4.1~r143-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 740 kB
  • ctags: 703
  • sloc: python: 4,643; makefile: 39; sh: 33
file content (454 lines) | stat: -rw-r--r-- 15,190 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
"""
Very basic tests which so no more than check each of the authentication
methods to ensure that an unprotected page is accessible and that a 
protected page triggers the a sign in.

Note: Should the Form and Forward methods return 401 or 200 when they
generate an HTML page for the user to sign in?
"""

import sys
import os
import paste.lint

from authkit.authenticate import middleware, sample_app
from paste.fixture import *

sys.path.insert(0, os.getcwd()+'/examples/docs')

from form import app as form_app
from basic import app as basic_app
from digest import app as digest_app
from forward import app as forward_app
from open_id import app as openid_app
from redirect import app as redirect_app

# Add the paste validation middleware
form_app = paste.lint.middleware(form_app)
basic_app = paste.lint.middleware(basic_app)
digest_app = paste.lint.middleware(digest_app)
forward_app = paste.lint.middleware(forward_app)
openid_app = paste.lint.middleware(openid_app)
redirect_app = paste.lint.middleware(redirect_app)

sys.path.insert(0, os.getcwd()+'/examples/config')
from digest import app as config_app

def assertEqual(a,b):
    if a != b:
        raise AssertionError('%s != %s'%(a,b))

def assertAllEqual(*args):
    if not len(args)>2:
        raise Exception("Need two arguments")
    a = args[0]
    for b in args[1:]:
        if a != b:
            raise AssertionError('%s != %s'%(a,b))

apps = [
    form_app, 
    basic_app, 
    digest_app, 
    forward_app,
    openid_app, 
    redirect_app,
    config_app,
]

def test_ok():
    for app in apps:
        if app == forward_app:
            res = TestApp(app).get('')
            assertEqual(res.header('content-type'), 'text/plain')
            assertEqual(res.full_status, '200 OK')
            assert 'You Have Access To This Page.' in res
        else:
            res = TestApp(app).get('')
            assertEqual(res.header('content-type'), 'text/plain; charset=UTF-8')
            assertEqual(res.full_status, '200 OK')
            assert 'You Have Access To This Page.' in res

def test_intercept():
    # XXX Note, these tests don't test when the inclusion of a username and only test form
    # should also test all the other methods too for correct behaviour
    def sample_app(environ, start_response):
        if environ.get('PATH_INFO') == '/403':
            start_response('403 Forbidden', [('Content-type', 'text/plain')])
            return ['Access denied']
        elif environ.get('PATH_INFO') == '/401':
            start_response('401 Unauth', [('Content-type', 'text/plain')])
            return ['Not Authed']
        elif environ.get('PATH_INFO') == '/702':
            start_response('702 Doesnt exist', [('Content-type', 'text/plain')])
            return ['Access denied']
        elif environ.get('PATH_INFO') == '/500':
            start_response('500 Error', [('Content-type', 'text/plain')])
            return ['Error']

    app = middleware(
        sample_app,
        setup_method='digest',
        digest_realm='test',
        digest_authenticate_user_data = """
            Username1:password1
            username2:password2
        """,
        cookie_signoutpath = '/signout',
        setup_intercept = "403, 702",
    )
    res = TestApp(app).get('/403', status=401)
    assertEqual(res.header('content-type'), 'text/plain')
    # XXX Should this keep the original status code or not?
    assertEqual(res.full_status, '401 Unauthorized')
    assert 'This server could not verify that you are authorized' in res

    res = TestApp(app).get('/702', status=401)
    assertEqual(res.header('content-type'), 'text/plain')
    # XXX Should this keep the original status code or not?
    assertEqual(res.full_status, '401 Unauthorized')
    assert 'This server could not verify that you are authorized' in res

    res = TestApp(app).get('/500', status=500)
    assertEqual(res.header('content-type'), 'text/plain')
    assertEqual(res.full_status, '500 Error')
    assert 'Error' in res
    
    res = TestApp(app).get('/401', status=401)
    assertEqual(res.header('content-type'), 'text/plain')
    assertEqual(res.full_status, '401 Unauth')
    assert 'Not Authed' in res
    
def test_fail():
    for app in [basic_app, digest_app, config_app]:
        res = TestApp(app).get('/private', status=401)
        assertEqual(res.header('content-type'),'text/plain')
        assertEqual(res.full_status, '401 Unauthorized')
        #raise Exception(res)
        assert 'This server could not verify that you are' in res

def test_form_fail():
    res = TestApp(form_app).get('/private', status=200)
    assertEqual(res.header('content-type'),'text/html; charset=UTF-8')
    assertEqual(res.full_status, '200 OK')
    assert 'Please Sign In' in res

def test_forward_fail():
    res = TestApp(forward_app).get('/private')
    assertEqual(res.header('content-type'),'text/html')
    # XXX Not sure about this but using a 401 triggers an infinite loop
    # of redirects.
    assertEqual(res.full_status, '200 Sign in required')
    assert 'Please Sign In' in res

def test_openid_fail():
    res = TestApp(openid_app).get('/private')
    assertEqual(res.header('content-type'),'text/html; charset=UTF-8')
    assertEqual(res.full_status, '200 OK')
    assert 'Please Sign In' in res

def test_redirect_fail():
    res = TestApp(redirect_app).get('/private', status=302)
    assertEqual(res.header('Location'),'http://3aims.com')
    assertEqual(res.full_status, '302 Found')

def test_users_api_database():
    try: 
        from authkit.users.sqlalchemy_04_driver import UsersFromDatabase, setup_model
    except ImportError:
        raise Exception("Could not run the SQLAlchemy tests, not installed")
    try: 
        from sqlalchemymanager import SQLAlchemyManager
    except ImportError:
        raise Exception("Could not run the SQLAlchemy tests, SQLAlchemyManager is not installed")
    if os.path.exists("mydb.db"):
        os.remove("mydb.db")

    app = SQLAlchemyManager(
        None, 
        {'sqlalchemy.url':'sqlite:///mydb.db'}, 
        [setup_model]
    )
    app.create_all()
    connection = app.engine.connect()
    session = app.session_maker(bind=connection)
    try:
        environ = {}
        environ['sqlalchemy.session'] = session
        environ['sqlalchemy.model'] = app.model
        d = UsersFromDatabase(environ)
        d.role_create("wiki")
        d.role_create("adMin")
        d.role_create("editor")
        d.group_create("pyLOns")
        d.group_create("dJAngo")
        d.user_create("jaMEs", "passWOrd1", "pyLoNs")
        d.user_create("ben", "password2")
        d.user_create("Simon", "password3")
        d.user_create("ian", "paSsword4")
        assertEqual(d.list_roles(),["admin", "editor", "wiki"])
        assertEqual(d.list_groups(),["django", "pylons"])
        assertEqual(d.list_users(),['ben', 'ian', 'james', 'simon'])
        assertEqual(d.user_has_password("james", "passWOrd1"), True)
        assertEqual(d.user_has_password("james", "password1"), False)
        
        d.role_create("test_role")
        d.group_create("test_group")
        d.user_create("test_user", "password")
        assertEqual(d.list_roles(),["admin", "editor", "test_role", "wiki"])
        assertEqual(d.list_groups(),["django", "pylons", "test_group"])
        assertEqual(d.list_users(),['ben', 'ian', 'james', 'simon', "test_user"])
        d.role_delete("test_role")
        d.group_delete("test_group")
        d.user_delete("test_user")
        assertEqual(d.list_roles(),["admin", "editor", "wiki"])
        assertEqual(d.list_groups(),["django", "pylons"])
        assertEqual(d.list_users(),['ben', 'ian', 'james', 'simon'])
    
        assertEqual(d.user_has_role("james", "admin"), False)
        d.user_add_role("james", "admin")
        assertEqual(d.user_has_role("james", "admin"), True)
        d.user_remove_role("james", "admin")
        assertEqual(d.user_has_role("james", "admin"), False)
    
        d.user_add_role("james", "wiki")
        d.user_add_role("simon", "wiki")
        d.user_add_role("james", "admin")
        #d.user_add_role("james", "editor")
        d.user_add_role("ben", "editor")
        
        assertEqual(d.user_has_group("james", "pylons"), True)
        assertEqual(d.user_has_group("simon", None), True)
        assertEqual(d.user_has_group("simon", "django"), False)
        d.user_set_group("simon", "dJangO")
        assertEqual(d.user_has_group("simon", None), False)
        d.user_set_group("bEn", "PyLONS")
        assertEqual(d.user_has_group("simon", "django"), True)
        assertEqual(d.user_has_group("bEn", "pYlons"), True)
        d.user_remove_group("bEn")
        assertEqual(d.user_has_group("bEn", "pYlons"), False)
        d.user_set_group("bEn", "PyLONS")
        assertEqual(d.user_has_group("bEn", "pYlons"), True)
        
        assertEqual(d.list_users(),['ben', 'ian', 'james', 'simon'])
        d.user_set_username("james", "jim")
        assertEqual(d.list_users(),['ben', 'ian', 'jim', 'simon'])
        d.user_set_username("jim", "james")
        
        from authkit.users import UsersFromFile, UsersFromString, AuthKitNoSuchUserError, AuthKitNoSuchGroupError,AuthKitNoSuchRoleError
        string_data = """jaMEs:passWOrd1:pyLOns wiki adMin
        ben:password2:pylons admin editor
        simon:password3:dJAngo
        ian:paSsword4 wiki
        """
        filename = 'test/user_file_data.txt'
        
        s = UsersFromString(string_data)
        f = UsersFromFile(filename)
    
        # Test Parsing
        assertAllEqual(
            s.passwords,
            f.passwords,
            {
                'james':'passWOrd1',
                'ben':'password2',
                'simon':'password3',
                'ian':'paSsword4',
            },
        )
        assertAllEqual(
            s.roles, 
            f.roles,
            {
                'james':['admin', 'wiki'],
                'ben':['admin','editor'],
                'ian':['wiki'],
                'simon':[],
            },
        )
        assertAllEqual(
            s.groups, 
            f.groups,
            {
                'james':'pylons',
                'ben':'pylons',
                'ian': None,
                'simon':'django',
            },
        )
        assertAllEqual(
            s.usernames, 
            f.usernames,
            ['ben', 'ian', 'james', 'simon'],
        )
    
        # Test list functions
        assertAllEqual(
            s.list_users(),
            f.list_users(),
            d.list_users(),
            ['ben', 'ian', 'james', 'simon'],
        )
        assertAllEqual(
            s.list_roles(), 
            f.list_roles(),
            d.list_roles(),
            ['admin', 'editor', 'wiki'],
        )
        assertAllEqual(
            s.list_groups(), 
            f.list_groups(),
            d.list_groups(),
            ['django','pylons'],
        )
    
        # Test user has functions
        assertAllEqual(
            s.user_has_role('jAMes','WiKi'), 
            f.user_has_role('jAMes','WiKi'), 
            d.user_has_role('jAMes','WiKi'), 
            True
        )
        assertAllEqual(
            s.user_has_role('jAMes','editOr'), 
            f.user_has_role('jAMes','editOr'), 
            d.user_has_role('jAMes','editOr'), 
            False
        )
        
        assertAllEqual(
            s.user_has_group('jAMeS','PyLons'), 
            f.user_has_group('jAMes','pylOns'), 
            d.user_has_group('jAMes','pylOns'), 
            True
        )
        assertAllEqual(
            s.user_has_group('jameS','djaNgo'), 
            f.user_has_group('JAMes','djAngo'), 
            d.user_has_group('JAMes','djAngo'), 
            False
        )
    
        assertAllEqual(
            s.user_has_password('jAMeS','passWOrd1'), 
            f.user_has_password('jAMes','passWOrd1'), 
            d.user_has_password('jAMes','passWOrd1'), 
            True
        )
        assertAllEqual(
            s.user_has_password('jameS','PASSWORD1'), 
            f.user_has_password('JAMes','PASSWORD1'), 
            d.user_has_password('JAMes','PASSWORD1'), 
            False
        )
    
        # Existence Methods
        assertAllEqual(
            s.user_exists('jAMeS'), 
            f.user_exists('jAMes'), 
            d.user_exists('jAMes'), 
            True
        )
        assertAllEqual(
            s.user_exists('nobody'), 
            f.user_exists('nobody'), 
            d.user_exists('nobody'), 
            False
        )
        
        # Existence Methods
        assertAllEqual(
            s.role_exists('wiKi'), 
            f.role_exists('Wiki'), 
            d.role_exists('Wiki'), 
            True
        )
        assertAllEqual(
            s.role_exists('norole'), 
            f.role_exists('norole'), 
            d.role_exists('norole'), 
            False
        )
        
        assertAllEqual(
            s.group_exists('pyLons'), 
            f.group_exists('PYlons'), 
            d.group_exists('PYlons'), 
            True
        )
        assertAllEqual(
            s.group_exists('nogroup'), 
            f.group_exists('nogroup'), 
            d.group_exists('nogroup'), 
            False
        )
    
    
        # User Methods
        
        assertAllEqual(
            s.user('James'), 
            f.user('James'),
            d.user('James'),
            {
                'username': 'james',
                'group':    'pylons',
                'password': 'passWOrd1',
                'roles':    ['admin','wiki'],
            }
        )
        
        # Test all user methods raise:
        for plugin in [s,f,d]:
            for func in [
                'user',
                'user_roles',
                'user_group',
                'user_password',
            ]:
                try:
                    getattr(plugin, func)('nouser')
                except AuthKitNoSuchUserError, e:
                    pass
                else:
                    raise AssertionError("Failed to throw a no user error")
        for plugin in [s,f,d]:
            for func in [
                'user_has_password',
                'user_has_role',
                'user_has_group',
            ]:
                try:
                    getattr(plugin, func)('nouser','somevar')
                except AuthKitNoSuchUserError, e:
                    pass
                else:
                    raise AssertionError("Failed to throw a no user error")
    
        assertAllEqual(
            s.user_roles('James'), 
            f.user_roles('James'),
            d.user_roles('James'),
            ['admin','wiki']
        )
        assertAllEqual(
            s.user_group('James'), 
            f.user_group('James'),
            d.user_group('James'),
            'pylons'
        )
        assertAllEqual(
            s.user_password('James'), 
            f.user_password('James'),
            d.user_password('James'),
            'passWOrd1'
        )
        
        session.flush()
        session.commit()
    finally:
        session.close()
        connection.close()