Added unit testing support + moved make_unique_username() to module level

Also cleaned up make_unique_username() code to no longer be recursive,
and to correctly handle numerical suffixes ≥ 10.
This commit is contained in:
2016-03-04 14:08:10 +01:00
parent 260cfdbcd5
commit 1e141492a3
3 changed files with 74 additions and 19 deletions

0
pillar/tests/__init__.py Normal file
View File

50
pillar/tests/test_auth.py Normal file
View File

@@ -0,0 +1,50 @@
import unittest
import os
TEST_EMAIL_USER = 'koro'
TEST_EMAIL_ADDRESS = '%s@testing.blender.org' % TEST_EMAIL_USER
os.environ['MONGO_DBNAME'] = 'unittest'
os.environ['EVE_SETTINGS'] = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'settings.py')
from application import app
class FlaskrTestCase(unittest.TestCase):
def setUp(self):
self.app = app.test_client()
def tearDown(self):
pass
def test_make_unique_username(self):
from application.utils.authentication import make_unique_username
with app.test_request_context():
# Delete the user we want to test for
users = app.data.driver.db['users']
users.delete_many({'username': TEST_EMAIL_USER})
# This user shouldn't exist yet.
self.assertEqual(TEST_EMAIL_USER, make_unique_username(TEST_EMAIL_ADDRESS))
# Add a user, then test again.
user_data = {
'full_name': 'Coro the Llama',
'username': TEST_EMAIL_USER,
'email': TEST_EMAIL_ADDRESS,
'auth': [{
'provider': 'unit-test',
'user_id': 'test123',
'token': ''}],
'settings': {
'email_communications': 0
}
}
users.insert_one(user_data)
try:
self.assertIsNotNone(users.find_one({'username': TEST_EMAIL_USER}))
self.assertEqual('%s1' % TEST_EMAIL_USER, make_unique_username(TEST_EMAIL_ADDRESS))
finally:
users.delete_many({'username': TEST_EMAIL_USER})