diff --git a/matemat/db/test/test_facade.py b/matemat/db/test/test_facade.py index 049edcb..859ea37 100644 --- a/matemat/db/test/test_facade.py +++ b/matemat/db/test/test_facade.py @@ -1,6 +1,8 @@ import unittest +import bcrypt + from matemat.db import Database from matemat.exceptions import AuthenticationError, DatabaseConsistencyError @@ -29,6 +31,36 @@ class DatabaseTest(unittest.TestCase): with self.assertRaises(ValueError): db.create_user('testuser', 'supersecurepassword2', 'testuser2@example.com') + def test_login(self): + with self.db as db: + with db.transaction() as c: + u = db.create_user('testuser', 'supersecurepassword', 'testuser@example.com') + # Add a touchkey without using the provided function + c.execute('''UPDATE users SET touchkey = :tkhash WHERE user_id = :user_id''', { + 'tkhash': bcrypt.hashpw(b'0123', bcrypt.gensalt(12)), + 'user_id': u.id + }) + user = db.login('testuser', 'supersecurepassword') + self.assertEqual(u.id, user.id) + user = db.login('testuser', touchkey='0123') + self.assertEqual(u.id, user.id) + with self.assertRaises(AuthenticationError): + # Inexistent user should fail + db.login('nooone', 'supersecurepassword') + with self.assertRaises(AuthenticationError): + # Wrong password should fail + db.login('testuser', 'anothersecurepassword') + with self.assertRaises(AuthenticationError): + # Wrong touchkey should fail + db.login('testuser', touchkey='0124') + with self.assertRaises(ValueError): + # No password or touchkey should fail + db.login('testuser') + with self.assertRaises(ValueError): + # Both password and touchkey should fail + db.login('testuser', password='supersecurepassword', touchkey='0123') + + def test_change_password(self): with self.db as db: user = db.create_user('testuser', 'supersecurepassword', 'testuser@example.com')