def get_user(user_id=None, username=None): user = None if user_id: query = "SELECT * FROM users WHERE id = %s" fetched_data = base_db.fetchone(query, user_id) if fetched_data: user = models.User(*fetched_data) if username: query = "SELECT * FROM users WHERE username = %s" fetched_data = base_db.fetchone(query, username) if fetched_data: user = models.User(*fetched_data) return user
def get_session(sessionid): raw_data = base_db.fetchone("SELECT * FROM sessions WHERE session_id = %s", sessionid) if raw_data: session = models.Session(*raw_data) return session
def test_returns_user_id(self): username = '******' password = '******' user_id = user_db.create_user(username, password) self.assertIsInstance(user_id, int) _, fetched_username, _, _ = base_db.fetchone("SELECT * FROM users WHERE id=%s", user_id) self.assertEqual(username, fetched_username)
def test_success_create(self): username = '******' password = '******' user_id = user_db.create_user(username, password) fetched_user_id, fetched_username, fetched_password, salt = base_db.fetchone("SELECT * FROM users") self.assertIsInstance(fetched_user_id, int) self.assertEqual(user_id, fetched_user_id) self.assertEqual(username, fetched_username) hash_password, _ = utils.get_hash_with_salt(password, salt=salt) self.assertEqual(hash_password, fetched_password)
def create_user(username, password): password, salt = utils.get_hash_with_salt(password) query = "INSERT INTO users(username, password, salt) VALUES (%s, %s, %s) RETURNING id;" return base_db.fetchone(query, username, password, salt)[0]