Changeset 152

Show
Ignore:
Timestamp:
05/13/08 16:00:56
Author:
thejimmyg
Message:

Added a series of updates to SQLAlchemy code plus a fix to the OpenID support and an encoding problem with form.py

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • AuthKit/trunk/CHANGELOG.txt

    r151 r152  
    440.4.1 (**svn**) 
    55 
     6* Changed the import of the openid.sreg module to openid.extensions.sreg 
     7* Fixed the encoding of the form.py file 
     8* Updated the examples to use the latest syntax 
     9* Updated the tests for the new SQLAlchemy drivers 
     10* Added Daniel Pronych's SQLAlchemy drivers but with significant changes 
    611* Fixed a bug due to a change in the latest version of Python OpenID 
    712  so that AuthKit OpenID works with Yahoo sign-ins. Phil Kershaw #50 
  • AuthKit/trunk/authkit/authenticate/form.py

    r148 r152  
     1# -*- coding: utf-8 -*- 
    12"""Form and cookie based authentication middleware 
    23 
  • AuthKit/trunk/authkit/authenticate/open_id.py

    r151 r152  
    6464from paste.request import construct_url 
    6565from openid.consumer import consumer 
    66 from openid import sreg 
     66from openid.extensions import sreg 
    6767from openid.cryptutil import randomString 
    6868from authkit.authenticate import get_template, valid_password, \ 
  • AuthKit/trunk/authkit/users/postgresql_driver.py

    r133 r152  
     1"""\ 
     2An SQLAlchemy driver for Pylons when used directly with PostgreSQL. 
     3""" 
    14import datetime 
    25from authkit.users import * 
  • AuthKit/trunk/authkit/users/sqlalchemy_04_driver.py

    r137 r152  
     1"""\ 
     2This module provides an implementation of an SQLAlchemy 0.4 driver for use with  
     3the SQLAlchemyManager middleware. 
     4 
     5Ordinarily you will probably want to use the ``authkit.users.sqlalchemy_driver``  
     6module which is designed to be used with Pylons 0.9.7 and automatically uses the  
     7correct driver for the version of SQLAlchemy you are using. 
     8""" 
     9 
    110from paste.util.import_string import eval_import 
    211from authkit.users import * 
  • AuthKit/trunk/examples/docs/forward.py

    r61 r152  
    6666    sample_app, 
    6767    setup_method='forward,cookie', 
    68     forward_internalpath = '/signin', 
     68    forward_signinpath = '/signin', 
    6969    cookie_signoutpath = '/signout', 
    7070    cookie_secret = 'somesecret', 
  • AuthKit/trunk/test/test.py

    r141 r152  
    453453 
    454454         
     455 
     456def test_users_model_api_database(): 
     457    sys.path.insert(0, os.getcwd()+'/examples/user/database-model') 
     458    try:  
     459        from authkit.users.sqlalchemy_driver import UsersFromDatabase 
     460    except ImportError: 
     461        raise Exception("Could not run the SQLAlchemy tests, not installed") 
     462    if os.path.exists("test.db"): 
     463        os.remove("test.db") 
     464    import model as test_model 
     465 
     466    # Setup SQLAlchemy database engine 
     467    from sqlalchemy import engine_from_config 
     468    engine = engine_from_config({'sqlalchemy.url':'sqlite:///test.db'}, 'sqlalchemy.') 
     469    test_model.init_model(engine) 
     470    test_model.engine = engine 
     471  
     472    d = UsersFromDatabase(test_model) 
     473     
     474    test_model.meta.metadata.create_all(test_model.engine) 
     475     
     476    d.role_create("wiki") 
     477    d.role_create("adMin") 
     478    d.role_create("editor") 
     479    d.group_create("pyLOns") 
     480    d.group_create("dJAngo") 
     481    d.user_create("jaMEs", "passWOrd1", "pyLoNs") 
     482    d.user_create("ben", "password2") 
     483    d.user_create("Simon", "password3") 
     484    d.user_create("ian", "paSsword4") 
     485    assertEqual(d.list_roles(),["admin", "editor", "wiki"]) 
     486    assertEqual(d.list_groups(),["django", "pylons"]) 
     487    assertEqual(d.list_users(),['ben', 'ian', 'james', 'simon']) 
     488    assertEqual(d.user_has_password("james", "passWOrd1"), True) 
     489    assertEqual(d.user_has_password("james", "password1"), False) 
     490     
     491    d.role_create("test_role") 
     492    d.group_create("test_group") 
     493    d.user_create("test_user", "password") 
     494    assertEqual(d.list_roles(),["admin", "editor", "test_role", "wiki"]) 
     495    assertEqual(d.list_groups(),["django", "pylons", "test_group"]) 
     496    assertEqual(d.list_users(),['ben', 'ian', 'james', 'simon', "test_user"]) 
     497    d.role_delete("test_role") 
     498    d.group_delete("test_group") 
     499    d.user_delete("test_user") 
     500    assertEqual(d.list_roles(),["admin", "editor", "wiki"]) 
     501    assertEqual(d.list_groups(),["django", "pylons"]) 
     502    assertEqual(d.list_users(),['ben', 'ian', 'james', 'simon']) 
     503 
     504    assertEqual(d.user_has_role("james", "admin"), False) 
     505    d.user_add_role("james", "admin") 
     506    assertEqual(d.user_has_role("james", "admin"), True) 
     507    d.user_remove_role("james", "admin") 
     508    assertEqual(d.user_has_role("james", "admin"), False) 
     509 
     510    d.user_add_role("james", "wiki") 
     511    d.user_add_role("simon", "wiki") 
     512    d.user_add_role("james", "admin") 
     513    #d.user_add_role("james", "editor") 
     514    d.user_add_role("ben", "editor") 
     515     
     516    assertEqual(d.user_has_group("james", "pylons"), True) 
     517    assertEqual(d.user_has_group("simon", None), True) 
     518    assertEqual(d.user_has_group("simon", "django"), False) 
     519    d.user_set_group("simon", "dJangO") 
     520    assertEqual(d.user_has_group("simon", None), False) 
     521    d.user_set_group("bEn", "PyLONS") 
     522    assertEqual(d.user_has_group("simon", "django"), True) 
     523    assertEqual(d.user_has_group("bEn", "pYlons"), True) 
     524    d.user_remove_group("bEn") 
     525    assertEqual(d.user_has_group("bEn", "pYlons"), False) 
     526    d.user_set_group("bEn", "PyLONS") 
     527    assertEqual(d.user_has_group("bEn", "pYlons"), True) 
     528     
     529    assertEqual(d.list_users(),['ben', 'ian', 'james', 'simon']) 
     530    d.user_set_username("james", "jim") 
     531    assertEqual(d.list_users(),['ben', 'ian', 'jim', 'simon']) 
     532    d.user_set_username("jim", "james") 
     533     
     534    from authkit.users import UsersFromFile, UsersFromString, AuthKitNoSuchUserError, AuthKitNoSuchGroupError,AuthKitNoSuchRoleError 
     535    string_data = """jaMEs:passWOrd1:pyLOns wiki adMin 
     536    ben:password2:pylons admin editor 
     537    simon:password3:dJAngo 
     538    ian:paSsword4 wiki 
     539    """ 
     540    filename = 'test/user_file_data.txt' 
     541     
     542    s = UsersFromString(string_data) 
     543    f = UsersFromFile(filename) 
     544 
     545    # Test Parsing 
     546    assertAllEqual( 
     547        s.passwords, 
     548        f.passwords, 
     549        { 
     550            'james':'passWOrd1', 
     551            'ben':'password2', 
     552            'simon':'password3', 
     553            'ian':'paSsword4', 
     554        }, 
     555    ) 
     556    assertAllEqual( 
     557        s.roles,  
     558        f.roles, 
     559        { 
     560            'james':['admin', 'wiki'], 
     561            'ben':['admin','editor'], 
     562            'ian':['wiki'], 
     563            'simon':[], 
     564        }, 
     565    ) 
     566    assertAllEqual( 
     567        s.groups,  
     568        f.groups, 
     569        { 
     570            'james':'pylons', 
     571            'ben':'pylons', 
     572            'ian': None, 
     573            'simon':'django', 
     574        }, 
     575    ) 
     576    assertAllEqual( 
     577        s.usernames,  
     578        f.usernames, 
     579        ['ben', 'ian', 'james', 'simon'], 
     580    ) 
     581 
     582    # Test list functions 
     583    assertAllEqual( 
     584        s.list_users(), 
     585        f.list_users(), 
     586        d.list_users(), 
     587        ['ben', 'ian', 'james', 'simon'], 
     588    ) 
     589    assertAllEqual( 
     590        s.list_roles(),  
     591        f.list_roles(), 
     592        d.list_roles(), 
     593        ['admin', 'editor', 'wiki'], 
     594    ) 
     595    assertAllEqual( 
     596        s.list_groups(),  
     597        f.list_groups(), 
     598        d.list_groups(), 
     599        ['django','pylons'], 
     600    ) 
     601 
     602    # Test user has functions 
     603    assertAllEqual( 
     604        s.user_has_role('jAMes','WiKi'),  
     605        f.user_has_role('jAMes','WiKi'),  
     606        d.user_has_role('jAMes','WiKi'),  
     607        True 
     608    ) 
     609    assertAllEqual( 
     610        s.user_has_role('jAMes','editOr'),  
     611        f.user_has_role('jAMes','editOr'),  
     612        d.user_has_role('jAMes','editOr'),  
     613        False 
     614    ) 
     615     
     616    assertAllEqual( 
     617        s.user_has_group('jAMeS','PyLons'),  
     618        f.user_has_group('jAMes','pylOns'),  
     619        d.user_has_group('jAMes','pylOns'),  
     620        True 
     621    ) 
     622    assertAllEqual( 
     623        s.user_has_group('jameS','djaNgo'),  
     624        f.user_has_group('JAMes','djAngo'),  
     625        d.user_has_group('JAMes','djAngo'),  
     626        False 
     627    ) 
     628 
     629    assertAllEqual( 
     630        s.user_has_password('jAMeS','passWOrd1'),  
     631        f.user_has_password('jAMes','passWOrd1'),  
     632        d.user_has_password('jAMes','passWOrd1'),  
     633        True 
     634    ) 
     635    assertAllEqual( 
     636        s.user_has_password('jameS','PASSWORD1'),  
     637        f.user_has_password('JAMes','PASSWORD1'),  
     638        d.user_has_password('JAMes','PASSWORD1'),  
     639        False 
     640    ) 
     641 
     642    # Existence Methods 
     643    assertAllEqual( 
     644        s.user_exists('jAMeS'),  
     645        f.user_exists('jAMes'),  
     646        d.user_exists('jAMes'),  
     647        True 
     648    ) 
     649    assertAllEqual( 
     650        s.user_exists('nobody'),  
     651        f.user_exists('nobody'),  
     652        d.user_exists('nobody'),  
     653        False 
     654    ) 
     655     
     656    # Existence Methods 
     657    assertAllEqual( 
     658        s.role_exists('wiKi'),  
     659        f.role_exists('Wiki'),  
     660        d.role_exists('Wiki'),  
     661        True 
     662    ) 
     663    assertAllEqual( 
     664        s.role_exists('norole'),  
     665        f.role_exists('norole'),  
     666        d.role_exists('norole'),  
     667        False 
     668    ) 
     669     
     670    assertAllEqual( 
     671        s.group_exists('pyLons'),  
     672        f.group_exists('PYlons'),  
     673        d.group_exists('PYlons'),  
     674        True 
     675    ) 
     676    assertAllEqual( 
     677        s.group_exists('nogroup'),  
     678        f.group_exists('nogroup'),  
     679        d.group_exists('nogroup'),  
     680        False 
     681    ) 
     682 
     683 
     684    # User Methods 
     685     
     686    assertAllEqual( 
     687        s.user('James'),  
     688        f.user('James'), 
     689        d.user('James'), 
     690        { 
     691            'username': 'james', 
     692            'group':    'pylons', 
     693            'password': 'passWOrd1', 
     694            'roles':    ['admin','wiki'], 
     695        } 
     696    ) 
     697     
     698    # Test all user methods raise: 
     699    for plugin in [s,f,d]: 
     700        for func in [ 
     701            'user', 
     702            'user_roles', 
     703            'user_group', 
     704            'user_password', 
     705        ]: 
     706            try: 
     707                getattr(plugin, func)('nouser') 
     708            except AuthKitNoSuchUserError, e: 
     709                pass 
     710            else: 
     711                raise AssertionError("Failed to throw a no user error") 
     712    for plugin in [s,f,d]: 
     713        for func in [ 
     714            'user_has_password', 
     715            'user_has_role', 
     716            'user_has_group', 
     717        ]: 
     718            try: 
     719                getattr(plugin, func)('nouser','somevar') 
     720            except AuthKitNoSuchUserError, e: 
     721                pass 
     722            else: 
     723                raise AssertionError("Failed to throw a no user error") 
     724 
     725    assertAllEqual( 
     726        s.user_roles('James'),  
     727        f.user_roles('James'), 
     728        d.user_roles('James'), 
     729        ['admin','wiki'] 
     730    ) 
     731    assertAllEqual( 
     732        s.user_group('James'),  
     733        f.user_group('James'), 
     734        d.user_group('James'), 
     735        'pylons' 
     736    ) 
     737    assertAllEqual( 
     738        s.user_password('James'),  
     739        f.user_password('James'), 
     740        d.user_password('James'), 
     741        'passWOrd1' 
     742    ) 
     743         
     744         
     745