def login(): form = LoginForm() if form.validate_on_submit(): username = form.username.data password = form.password.data if username and password: if username in list(LOGIN_.keys()): if LOGIN_[username] == password: global CURRENT_USER # AUTH if username == 'admin': CURRENT_USER = User(username, 'Admin') if username == 'employee': CURRENT_USER = User(username, 'Employee') if username == 'customer': CURRENT_USER = User(username, 'Customer') flash(f'Authenticated as {username}') return redirect(url_for('index')) flash('Invalid username or password') return render_template('login.html', form=form)
def test_get_user_by_id_should_be_success(self): roles = ["5ebfab44d751a6ed494c361e", "5ebfab44d751a6ed494c3612"] user_id = User.create_user("test-name", "*****@*****.**", roles, phone_number="phone-number") actual_user = User.get_user_by_id(user_id) self.assertTrue(len(actual_user) > 0) self.assertEqual(user_id, actual_user["id"]) self.assertEqual("test-name", actual_user["name"]) self.assertEqual("*****@*****.**", actual_user["email"]) self.assertEqual("phone-number", actual_user["phone_number"])
def get_user_specified(name, code): sql = "SELECT iduser, " \ "name, " \ "code " \ "FROM users " \ "WHERE name = %s AND code = %s" conn = SqlConnConcepts.get_connection() cursor = conn.cursor() resulting = None try: # Execute the SQL command cursor.execute(sql, (name, code,)) # Fetch all the rows in a list of lists. row = cursor.fetchone() id = row[0] name = row[1] code = row[2] resulting = User(id, name, code) except: print("Error User: unable to fetch data for name "+ name) conn.close() return resulting
def get_all_users(): sql = "SELECT iduser, " \ "name, " \ "code " \ "FROM users " \ conn = SqlConnConcepts.get_connection() cursor = conn.cursor() resulting = [] try: cursor.execute(sql) result = cursor.fetchall() for row in result: id = row[0] name = row[1] code = row[2] resulting.append(User(id, name, code)) except: print("Error User: unable to fetch all data") conn.close() return resulting
def get_user_by_id(id): sql = "SELECT iduser, " \ "name, " \ "code " \ "FROM users " \ "WHERE iduser = %d;" % id conn = SqlConnConcepts.get_connection() cursor = conn.cursor() resulting = None try: # Execute the SQL command cursor.execute(sql) # Fetch all the rows in a list of lists. row = cursor.fetchone() id = row[0] name = row[1] code = row[2] resulting = User(id, name, code) except: print("Error User: unable to fetch data of user #%d" % id) conn.close() return resulting
def test_get_all_user_should_be_success(self): roles = ["5ebfab44d751a6ed494c361e", "5ebfab44d751a6ed494c3612"] User.create_user("name1", "*****@*****.**", roles, phone_number="phone1") User.create_user("name2", "*****@*****.**", roles) User.create_user("name3", "*****@*****.**", roles) all_user = User.get_all_user() self.assertEqual(3, len(all_user)) first_user = all_user[0] third_user = all_user[2] self.assertEqual("name3", first_user["name"]) self.assertEqual("*****@*****.**", first_user["email"]) self.assertEqual("name1", third_user["name"]) self.assertEqual("*****@*****.**", third_user["email"])
def test_create_user_should_raise_exception_when_email_is_duplicate(self): roles = ["5ebfab44d751a6ed494c361e", "5ebfab44d751a6ed494c3612"] user_id = User.create_user("tes t-name", "*****@*****.**", roles, phone_number="test-phone") self.assertRaises(DuplicateDocument, User.create_user, "test-name", "*****@*****.**", roles)
def test_create_user_should_be_success(self): roles = ["5ebfab44d751a6ed494c361e", "5ebfab44d751a6ed494c3612"] user_id = User.create_user("test-name", "*****@*****.**", roles, phone_number="test-phone") self.assertEqual(24, len(user_id))