1
0
Fork 0
forked from s3lph/matemat

test_facade: Added a login test case

This commit is contained in:
s3lph 2018-05-30 02:09:32 +02:00
parent 411372cc21
commit c238e8e9c8

View file

@ -1,6 +1,8 @@
import unittest import unittest
import bcrypt
from matemat.db import Database from matemat.db import Database
from matemat.exceptions import AuthenticationError, DatabaseConsistencyError from matemat.exceptions import AuthenticationError, DatabaseConsistencyError
@ -29,6 +31,36 @@ class DatabaseTest(unittest.TestCase):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
db.create_user('testuser', 'supersecurepassword2', 'testuser2@example.com') 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): def test_change_password(self):
with self.db as db: with self.db as db:
user = db.create_user('testuser', 'supersecurepassword', 'testuser@example.com') user = db.create_user('testuser', 'supersecurepassword', 'testuser@example.com')