def GET(self, **kwargs): """logs the user in Note that this GET request doesn't take json like the other requests """ row = kwargs if row.has_key('username') and row.has_key('password'): conn, cur = databeast.connect() #does user exist? cur.execute("SELECT COUNT(*) FROM users WHERE username=%s", [row['username']]) count = cur.fetchall()[0][0] if count == 1: sql = "SELECT password FROM users WHERE username = %s" cur.execute(sql, [row['username']]) hashed_pw = cur.fetchall()[0][0] if bcrypt.hashpw(row['password'].encode('utf-8'), hashed_pw.encode('utf-8')) == hashed_pw: output = [True, "User %s logged in." % (row['username'])] #open a training session session.openSession(row['username']) else: output = [False, "Invalid login credentials."] else: output = [False, "User %s does not exist." % row['username']] return output else: raise cherrypy.HTTPError(400)
def test_open_session(self): """it should open a session""" result = session.openSession('unit_test_user_6') self.assertTrue(result[0] == True) #close the active session session.closeSession('unit_test_user_6') """it should increment the session # appropriately""" result = session.openSession('unit_test_user_6') r = requests.get(self.session_url, params = {'username':'******'}) json_data = json.loads(r.text) #print json_data self.assertTrue(json_data[0]==2)
def test_task_delivery(self): """sessions should close after all tasks completed""" tasks = ["task1", "task2", "task3"] session.openSession('unit_test_user_7') #override the task list for the purpose of this test conn, cur = databeast.connect() sql = "UPDATE sessions SET tasks_to_play = %s WHERE username = %s" cur.execute(sql, [tasks, 'unit_test_user_7']) conn.commit() #mark the first task as done payload = {'username': '******', 'task':'task1'} r = requests.put(self.session_url, json=payload) json_data = json.loads(r.text) self.assertTrue(json_data == ["task2", "task3"]) #mark the second task as done payload = {'username': '******', 'task':'task2'} r = requests.put(self.session_url, json=payload) json_data = json.loads(r.text) self.assertTrue(json_data == ["task3"]) #mark the third (and final) task as done payload = {'username': '******', 'task':'task3'} r = requests.put(self.session_url, json=payload) json_data = json.loads(r.text) self.assertTrue(json_data == []) #check that session is closed sql = "SELECT complete FROM sessions WHERE username = %s" cur.execute(sql, ['unit_test_user_7']) result = cur.fetchall() conn.close() self.assertTrue(result[0][0] == True)