Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

UT improved

  • Loading branch information...
commit 875058cd9bca895a71c366b63e7aa8bdedcb8f93 1 parent b27c11d
@FedericoCeratto authored
Showing with 77 additions and 10 deletions.
  1. +6 −3 src/cork.py
  2. +71 −7 test/test.py
View
9 src/cork.py
@@ -82,6 +82,8 @@ def __init__(self, directory, email_sender=None, smtp_server=None,
self._roles = {}
self._roles_fname = roles_fname
self._mtimes = {}
+ self._pending_reg_fname = pending_reg_fname
+ self._pending_registrations = {}
self.mailer = Mailer(email_sender, smtp_server)
self._refresh() # load users and roles
@@ -218,7 +220,7 @@ def delete_role(self, role):
if role not in self._roles:
raise AAAException("Nonexistent role.")
self._roles.pop(role)
- self._savejson('roles', self._roles)
+ self._savejson(self._roles_fname, self._roles)
def create_user(self, username, role, password, email_addr=None,
description=None):
@@ -290,7 +292,7 @@ def user(self, username):
return None
def register(self, username, password, email_addr, role='user',
- max_level=50, email_template='registration_email', description=None):
+ max_level=50, email_template='view/registration_email', description=None):
"""Register a new user account. An email with a registration validation
is sent to the user.
WARNING: this method is available to unauthenticated users
@@ -328,7 +330,7 @@ def register(self, username, password, email_addr, role='user',
'desc': description,
'creation_date': creation_date
}
- self._save_pending_registrations()
+ self._savejson(self._roles_fname, self._roles)
# send registration email
email_text = bottle.template(email_template,
@@ -353,6 +355,7 @@ def _refresh(self):
"""Load users and roles from JSON files, if needed"""
self._loadjson(self._users_fname, self._users)
self._loadjson(self._roles_fname, self._roles)
+ self._loadjson(self._pending_reg_fname, self._pending_registrations)
def _loadjson(self, fname, dest):
"""Load JSON file located under self._directory, if needed
View
78 test/test.py
@@ -1,5 +1,5 @@
from nose.tools import assert_raises, with_setup
-from os import listdir, mkdir
+import os
from tempfile import mkdtemp
from time import time
import mock
@@ -20,7 +20,16 @@ def _beaker_session_username(self):
def _setup_cookie(self, username):
global cookie_name
- print 'setting cookie'
+ cookie_name = username
+
+class MockedUnauthenticatedCork(Cork):
+ """Mocked module where the current user is always 'admin'"""
+ @property
+ def _beaker_session_username(self):
+ return None
+
+ def _setup_cookie(self, username):
+ global cookie_name
cookie_name = username
def setup_dir():
@@ -28,21 +37,32 @@ def setup_dir():
global testdir
tstamp = "%f" % time()
testdir = "/dev/shm/fl_%s" % tstamp
- mkdir(testdir)
+ os.mkdir(testdir)
+ os.mkdir(testdir + '/view')
with open("%s/users.json" % testdir, 'w') as f:
f.write("""{"admin": {"email_addr": null, "desc": null, "role": "admin", "hash": "69f75f38ac3bfd6ac813794f3d8c47acc867adb10b806e8979316ddbf6113999b6052efe4ba95c0fa9f6a568bddf60e8e5572d9254dbf3d533085e9153265623", "creation_date": "2012-04-09 14:22:27.075596"}}""")
with open("%s/roles.json" % testdir, 'w') as f:
- f.write("""{"admin": 100}""")
+ f.write("""{"special": 200, "admin": 100, "user": 50}""")
+ with open("%s/register.json" % testdir, 'w') as f:
+ f.write("""{}""")
+ with open("%s/view/registration_email.tpl" % testdir, 'w') as f:
+ f.write(""" """)
print "setup done in %s" % testdir
-
+
def setup_mockedadmin():
"""Setup test directory and a MockedAdminCork instance"""
global aaa
global cookie_name
setup_dir()
aaa = MockedAdminCork(testdir, smtp_server='localhost')
- aaa._users['admin'] = {'role': 'admin', 'email': 'foo@foo.org'}
- aaa._roles = {'special': 200, 'admin': 100, 'user': 50}
+ cookie_name = None
+
+def setup_mocked_unauthenticated():
+ """Setup test directory and a MockedAdminCork instance"""
+ global aaa
+ global cookie_name
+ setup_dir()
+ aaa = MockedUnauthenticatedCork(testdir)
cookie_name = None
def teardown_dir():
@@ -72,6 +92,10 @@ def test_create_existing_role():
assert_raises(AAAException, aaa.create_role, 'user', 33)
@with_setup(setup_mockedadmin, teardown_dir)
+def test_create_role_with_incorrect_level():
+ assert_raises(AAAException, aaa.create_role, 'user', 'not_a_number')
+
+@with_setup(setup_mockedadmin, teardown_dir)
def test_create_role():
assert len(aaa._roles) == 3, repr(aaa._roles)
aaa.create_role('user33', 33)
@@ -208,6 +232,46 @@ def test_update_email():
assert aaa._users['admin']['email'] == 'foo'
+@with_setup(setup_mocked_unauthenticated, teardown_dir)
+def test_get_current_user_unauth():
+ def get_user():
+ print aaa.current_user.username
+ assert_raises(AAAException, get_user)
+
+@with_setup(setup_mockedadmin, teardown_dir)
+def test_register_no_user():
+ assert_raises(AssertionError, aaa.register, None, 'pwd', 'a@a.a')
+
+@with_setup(setup_mockedadmin, teardown_dir)
+def test_register_no_pwd():
+ assert_raises(AssertionError, aaa.register, 'foo', None, 'a@a.a')
+
+@with_setup(setup_mockedadmin, teardown_dir)
+def test_register_no_email():
+ assert_raises(AssertionError, aaa.register, 'foo', 'pwd', None)
+
+@with_setup(setup_mockedadmin, teardown_dir)
+def test_register_already_existing():
+ assert_raises(AAAException, aaa.register, 'admin', 'pwd', 'a@a.a')
+
+@with_setup(setup_mockedadmin, teardown_dir)
+def test_register_no_role():
+ assert_raises(AAAException, aaa.register, 'foo', 'pwd', 'a@a.a', role='clown')
+
+@with_setup(setup_mockedadmin, teardown_dir)
+def test_register_role_too_high():
+ assert_raises(AAAException, aaa.register, 'foo', 'pwd', 'a@a.a', role='admin')
+
+# Patch the mailer _send() method to prevent network interactions
+@with_setup(setup_mockedadmin, teardown_dir)
+@mock.patch.object(Mailer, '_send')
+def test_register(mocked):
+ old_dir = os.getcwd()
+ os.chdir(testdir)
+ aaa.register('foo', 'pwd', 'a@a.a')
+ os.chdir(old_dir)
+
+
# Patch the mailer _send() method to prevent network interactions
@with_setup(setup_mockedadmin, teardown_dir)
@mock.patch.object(Mailer, '_send')
Please sign in to comment.
Something went wrong with that request. Please try again.