示例#1
0
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
示例#2
0
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
示例#3
0
    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)
示例#4
0
    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)
示例#5
0
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]