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
|
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012,2013 Steffen Hoffmann <hoff.st@web.de>
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.
#
# Author: Steffen Hoffmann <hoff.st@web.de>
import shutil
import tempfile
import unittest
from trac.core import TracError
from trac.perm import PermissionCache, PermissionSystem
from trac.test import EnvironmentStub, MockRequest
from trac.web.session import Session
from ..api import AccountManager
from ..db import SessionStore
from . import makeSuite
class _BaseTestCase(unittest.TestCase):
def setUp(self):
self.env = EnvironmentStub(default_data=True,
enable=['trac.*', 'acct_mgr.api.*',
'acct_mgr.db.SessionStore',
'acct_mgr.pwhash.*',
'acct_mgr.htfile.HtDigestStore']
)
self.env.path = tempfile.mkdtemp()
self.perm = PermissionSystem(self.env)
def tearDown(self):
# Really close db connections.
self.env.shutdown()
shutil.rmtree(self.env.path)
class AccountManagerTestCase(_BaseTestCase):
def setUp(self):
_BaseTestCase.setUp(self)
self.mgr = AccountManager(self.env)
self.store = SessionStore(self.env)
self.store.set_password('user', 'passwd')
args = dict(username='user', name='', email='')
self.req = MockRequest(self.env, authname='user1', args=args)
def test_set_password(self):
# Can't work without at least one password store.
self.assertRaises(TracError, self.mgr.set_password, 'user', 'passwd')
self.env.config.set(
'account-manager', 'password_store', 'SessionStore')
self.mgr.set_password('user', 'passwd')
# Refuse to overwrite existing credentials, if requested.
self.assertRaises(TracError, self.mgr.set_password, 'user', 'passwd',
overwrite=False)
def test_approval_admin_keep_perm(self):
self.perm.grant_permission('admin', 'ACCTMGR_ADMIN')
# Some elevated permission action.
action = 'USER_VIEW'
self.assertFalse(action in PermissionCache(self.env))
req = self.req
req.perm = PermissionCache(self.env, 'admin')
req.session = Session(self.env, req)
req.session.save()
self.mgr.pre_process_request(req, None)
self.assertTrue(action in req.perm)
# Mock an authenticated request with account approval pending.
req.session['approval'] = 'pending'
req.session.save()
# Don't touch admin user requests.
self.mgr.pre_process_request(req, None)
self.assertTrue(action in req.perm)
def test_approval_user_strip_perm(self):
# Some elevated permission action.
action = 'USER_VIEW'
self.assertFalse(action in PermissionCache(self.env))
self.perm.grant_permission('user', action)
req = self.req
req.perm = PermissionCache(self.env, 'user')
req.session = Session(self.env, req)
req.session.save()
self.mgr.pre_process_request(req, None)
self.assertTrue(action in req.perm)
# Mock an authenticated request with account approval pending.
req.session['approval'] = 'pending'
req.session.save()
# Remove elevated permission, if account approval is pending.
self.mgr.pre_process_request(req, None)
self.assertFalse(action in req.perm)
def test_maybe_update_hash(self):
# Configure another, primary password store.
self.env.config.set('account-manager', 'password_store',
'HtDigestStore, SessionStore')
self.env.config.set('account-manager', 'htdigest_file', '.htdigest')
self.env.db_transaction("""
INSERT INTO session_attribute (sid,authenticated,name,value)
VALUES (%s,%s,%s,%s)
""", ('user', 1, 'password_refreshed', '1'))
# Refresh not happening due to 'password_refreshed' attribute.
self.mgr._maybe_update_hash('user', 'passwd')
for _, in self.env.db_query("""
SELECT value FROM session_attribute
WHERE sid='user'
AND authenticated=1
AND name='password'
"""):
break
else:
self.fail("Session attribute 'password' not found.")
self.env.db_transaction("""
DELETE FROM session_attribute
WHERE sid='user'
AND authenticated=1
AND name='password_refreshed'
""")
# Refresh (and effectively migrate) user credentials.
self.mgr._maybe_update_hash('user', 'passwd')
for _, in self.env.db_query("""
SELECT value
FROM session_attribute
WHERE sid='user'
AND authenticated=1
AND name='password'
"""):
self.fail("Session attribute 'password' should not be found.")
for value, in self.env.db_query("""
SELECT value
FROM session_attribute
WHERE sid='user'
AND authenticated=1
AND name='password_refreshed'
"""):
self.assertEqual('1', value)
break
else:
self.fail("Session attribute 'password_refreshed' not found.")
class PermissionTestCase(_BaseTestCase):
def setUp(self):
_BaseTestCase.setUp(self)
self.req = MockRequest(self.env)
self.actions = ['ACCTMGR_ADMIN', 'ACCTMGR_CONFIG_ADMIN',
'ACCTMGR_USER_ADMIN', 'EMAIL_VIEW', 'USER_VIEW']
# Tests
def test_available_actions(self):
for action in self.actions:
self.assertFalse(action not in self.perm.get_actions())
def test_available_actions_no_perms(self):
for action in self.actions:
self.assertFalse(self.perm.check_permission(action, 'anonymous'))
def test_available_actions_config_admin(self):
user = 'config_admin'
self.perm.grant_permission(user, 'ACCTMGR_CONFIG_ADMIN')
actions = [self.actions[0]] + self.actions[2:]
for action in actions:
self.assertFalse(self.perm.check_permission(action, user))
def test_available_actions_user_admin(self):
user = 'user_admin'
self.perm.grant_permission(user, 'ACCTMGR_USER_ADMIN')
for action in self.actions[2:]:
self.assertTrue(self.perm.check_permission(action, user))
for action in self.actions[:2] + ['TRAC_ADMIN']:
self.assertFalse(self.perm.check_permission(action, user))
def test_available_actions_full_perms(self):
perm_map = dict(acctmgr_admin='ACCTMGR_ADMIN',
trac_admin='TRAC_ADMIN')
for user in perm_map:
self.perm.grant_permission(user, perm_map[user])
for action in self.actions:
self.assertTrue(self.perm.check_permission(action, user))
if user != 'trac_admin':
self.assertFalse(self.perm.check_permission('TRAC_ADMIN',
user))
def test_suite():
suite = unittest.TestSuite()
suite.addTest(makeSuite(AccountManagerTestCase))
suite.addTest(makeSuite(PermissionTestCase))
return suite
if __name__ == '__main__':
unittest.main(defaultTest='test_suite')
|