I'm trying to make a function where that returns the name of the logged in user if one can be identified or None if not. i want to do this by finding the session id from the cookie in the Bottle request if present, and using it to look up the user in the sessions table.
My code so far:
def session_user(db):
"""try to
retrieve the user from the sessions table
return usernick or None if no valid session is present
"""
cursor = db.cursor()
sessionid = bottle.request.get_cookie(COOKIE_NAME)
usernick = None
if sessionid:
cursor.execute("SELECT usernick FROM sessions WHERE sessionid=?", (sessionid,))
usernick = cursor.fetchall()
return usernick
The table in database:
DROP TABLE IF EXISTS sessions;
CREATE TABLE sessions (
sessionid text unique primary key,
usernick text,
FOREIGN KEY(usernick) REFERENCES users(nick)
);
When i use the current code in my function i get a unit test error:
line 125, in test_session_user
self.assertEqual(nick_from_cookie, None, "Expected None in case with invalid session id, got %s" % str(nick_from_cookie))
AssertionError: [] != None : Expected None in case with invalid session id, got []
Aucun commentaire:
Enregistrer un commentaire