Child: [f64061] (diff)

Download this file

test_auth.py    103 lines (93 with data), 3.7 kB

  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
# -*- coding: utf-8 -*-
"""
Model tests for auth
"""
import mock
from nose.tools import with_setup
from pylons import c, g, request
from webob import Request
from ming.orm.ormsession import ThreadLocalORMSession
import allura.model.auth
from allura.lib.app_globals import Globals
from allura import model as M
from allura.lib import plugin
from allura.tests import helpers
def setUp():
helpers.setup_basic_test()
ThreadLocalORMSession.close_all()
helpers.setup_global_objects()
@with_setup(setUp)
def test_password_encoder():
# Verify salt
ep = plugin.LocalAuthenticationProvider(Request.blank('/'))._encode_password
assert ep('test_pass') != ep('test_pass')
assert ep('test_pass', '0000') == ep('test_pass', '0000')
@with_setup(setUp)
def test_email_address():
addr = M.EmailAddress(_id='test_admin@sf.net', claimed_by_user_id=c.user._id)
ThreadLocalORMSession.flush_all()
assert addr.claimed_by_user() == c.user
addr2 = M.EmailAddress.upsert('test@sf.net')
addr3 = M.EmailAddress.upsert('test_admin@sf.net')
assert addr3 is addr
assert addr2 is not addr
assert addr2
addr4 = M.EmailAddress.upsert('test@SF.NET')
assert addr4 is addr2
addr.send_verification_link()
assert addr is c.user.address_object('test_admin@sf.net')
c.user.claim_address('test@SF.NET')
assert 'test@sf.net' in c.user.email_addresses
@with_setup(setUp)
def test_openid():
oid = M.OpenId.upsert('http://google.com/accounts/1', 'My Google OID')
oid.claimed_by_user_id = c.user._id
ThreadLocalORMSession.flush_all()
assert oid.claimed_by_user() is c.user
assert M.OpenId.upsert('http://google.com/accounts/1', 'My Google OID') is oid
ThreadLocalORMSession.flush_all()
assert oid is c.user.openid_object(oid._id)
c.user.claim_openid('http://google.com/accounts/2')
oid2 = M.OpenId.upsert('http://google.com/accounts/2', 'My Google OID')
assert oid2._id in c.user.open_ids
ThreadLocalORMSession.flush_all()
@with_setup(setUp)
def test_user():
assert c.user.url() .endswith('/u/test-admin/')
assert c.user.script_name .endswith('/u/test-admin/')
assert len(list(c.user.my_projects())) == 0
assert M.User.anonymous().project_role().name == '*anonymous'
u = M.User.register(dict(
username='nosetest-user'))
ThreadLocalORMSession.flush_all()
assert u.private_project().shortname == 'u/nosetest-user'
roles = list(u.role_iter())
assert len(roles) == 3, roles
u.set_password('foo')
provider = plugin.LocalAuthenticationProvider(Request.blank('/'))
assert provider._validate_password(u, 'foo')
assert not provider._validate_password(u, 'foobar')
u.set_password('foobar')
assert provider._validate_password(u, 'foobar')
assert not provider._validate_password(u, 'foo')
@with_setup(setUp)
def test_project_role():
role = M.ProjectRole(name='test_role')
c.user.project_role().roles.append(role._id)
ThreadLocalORMSession.flush_all()
for pr in c.user.role_iter():
assert pr.display()
pr.special
assert pr.user in (c.user, M.User.anonymous())
list(pr.role_iter())
@with_setup(setUp)
def test_default_project_roles():
roles = dict((pr.name, pr)
for pr in M.ProjectRole.query.find().all()
if pr.name)
assert len(roles) == M.ProjectRole.query.find().count()-1
assert 'Admin' in roles.keys(), roles.keys()
assert 'Developer' in roles.keys(), roles.keys()
assert 'Member' in roles.keys(), roles.keys()
assert roles['Developer']._id in roles['Admin'].roles
assert roles['Member']._id in roles['Developer'].roles