def test_delete_user(self): UserController.add_user(self.user_1) self.assertEqual(UserController.delete_user('nickname_1', 'email_1'), 'SUCCESS') self.assertEqual( UserController.delete_user('fake_nickname_1', 'fake_email_1'), 'SUCCESS')
def update_profile(): global current_user if not current_user: add_output("You have to login first! \n") return temp = UserController.retrieve_user(UserFields.email.name, current_user.email) if temp == Errors.MISSING.name: add_output("No user with such credentials exists. \n") return elif temp == Errors.FAILURE.name: return_failure() return user_id = current_user.uid current_user = read_user() current_user.uid = user_id result = UserController.edit_user(current_user) if result == Errors.MISSING.name: add_output("No user with such credentials exists. \n") current_user = temp elif result == Errors.FAILURE.name: return_failure() current_user = temp elif result == Errors.DUPLICATE.name: add_output("A user with the same credentials already exists! \n") current_user = temp else: add_output("User updated. Email now at: " + current_user.email + ". \n") current_user = UserController.retrieve_user(UserFields.userid.name, result) UserController.print_user(current_user) return
def update_profile(data): user = UserModel(0, data['name'], data['nickname'], data['gender'], data['email'], data['location'], stringToEnum(data['tags']), data['description'], [], [], data['image'], data['google_id']) result = UserController.edit_user(user) if result == Errors.MISSING.name: return result, None elif result == Errors.FAILURE.name: return result, None else: user = UserController.retrieve_user(UserFields.googleid.name, data['google_id']) data = { 'id': user.uid, 'name': user.name, 'nickname': user.nickname, 'email': user.email, 'location': user.location, 'description': user.description, 'gender': user.gender, 'image': user.image, 'tags': user.tags, 'google_id': user.google_id } return 'OK', data
def test_retrieve_hit(): user_one = UserModel('0', 'chengyun yu', 'yuche', 'male', '*****@*****.**', '0,0', 'sports, dining', 'i am a sports guy who likes to eat. ' ) user_two = UserModel('0', 'chengyun yu', 'yuche', 'male', '*****@*****.**', '0,0', 'sports, dining', 'this is my new description. ' ) user_three = UserModel('0', 'non existent', 'noext', 'male', '*****@*****.**', '0.0', 'sports, dining', 'i am a sports guy who does not exist. ' ) print("Insert New: " + UserController.add_user(user_one)) print("Insert Conflict: " + UserController.add_user(user_two)) print("Update: " + UserController.edit_user(user_two)) print("Retrieve Hit: " + UserController.retrieve_user(UserFields.email.name, user_two.email)) print("Retrieve Miss: " + UserController.retrieve_user(UserFields.email.name, user_three.email)) return 'Tests Finished'
def test_retrieve_user(self): UserController.delete_user('nickname_1', 'email_1') UserController.delete_user('nickname_2', 'email_2') UserController.add_user(self.user_1) self.assertIsInstance(UserController.retrieve_user('email', 'email_1'), UserModel) self.assertEqual(UserController.retrieve_user('email', 'email_2'), 'MISSING') UserController.delete_user('nickname_1', 'email_1')
def test_host_event(self): UserController.add_user(self.user_1) user_2 = UserController.retrieve_user('email', 'email_1') event_id = EventController.add_event(self.event_1) self.event_1.eventid = event_id self.assertIsNotNone(EventController.host_event(user_2, self.event_1)) UserController.delete_user('nickname_1', 'email_1')
def group_email(): global current_user global current_event if not current_user: add_output('You have to log in first. \n') event_id = event_id_input.get() current_event = EventController.retrieve_event(event_id) if current_event == Errors.MISSING.name: add_output('No such event. \n') current_event = None return elif current_event == Errors.FAILURE.name: return_failure() current_event = None return if current_user.uid != current_event.hosts: add_output('You have to be the host to send a group email. \n') current_event = None return message = email_message_input.get() user_list = current_event.attendees EventController.print_event(current_event) for user_id in user_list: sent = False try: temp_user = UserController.retrieve_user(UserFields.userid.name, user_id) send_email(message, temp_user.email) sent = True finally: if not sent: add_output('Unable to send email to ' + temp_user.email + '. \n') return
def register(): global current_user current_user = read_user() result = UserController.add_user(current_user) if result == Errors.DUPLICATE.name: add_output("A user with the same credentials already exists! \n") current_user = None elif result == Errors.FAILURE.name: return_failure() current_user = None else: add_output("User registered JoinMe with email " + current_user.email + ". \n") current_user = UserController.retrieve_user(UserFields.email.name, current_user.email) UserController.print_user(current_user) return
def login(): global current_user email = user_email_input.get() result = UserController.retrieve_user(UserFields.email.name, email) if result == Errors.MISSING.name: add_output("No user with such credential exists. \n") UserController.print_user(current_user) return elif result == Errors.FAILURE.name: add_output("Failed to login. Please try again. \n") UserController.print_user(current_user) return add_output("You logged in with email " + email + ". \n") current_user = UserController.retrieve_user(UserFields.email.name, email) UserController.print_user(current_user) return
def addRoutes(api): api.add_route('/ttt/play', GameController.PlayResource()) api.add_route('/adduser', UserController.addUser()) api.add_route('/verify', UserController.verifyUser()) api.add_route('/verify/{email}/{key}', UserController.verifyUser()) api.add_route('/getcookie', UserController.cookieTest()) api.add_route('/login', UserController.login()) api.add_route('/logout', UserController.logout()) api.add_route('/listgames', GameController.listgames()) api.add_route('/getgame', GameController.getgame()) api.add_route('/getgameboard', GameController.GameBoard()) api.add_route('/getscore', GameController.getscore()) return api
def verifylogin(self, email, password): if email == "": print(f"{bcolors.FAIL}É necessario inserir um email!") if password == "": print(f"{bcolors.FAIL}É necessario inserir uma senha!") from Controller import UserController user = UserController.UserControl().get_by_email(email, "Model/Users.txt") if user == []: print(f"{bcolors.FAIL}O email ou a senha estão incorretos!{bcolors.ENDC}") HomeView.main().showlogin() if not str(user["email"]) == email and not str(user["pasword"]) == password: print(f"{bcolors.FAIL}O email ou a senha estão incorretos!{bcolors.ENDC}") HomeView.main().showlogin() if user["admin"] == True: pass #pagina de admin else: from View import UserView UserView.main().showuserhome(user)
def contact_friend(): global current_user if not current_user: add_output('You have to log in first. \n') message = email_message_input.get() nickname = email_nickname_input.get() sent = False try: temp_user = UserController.retrieve_user(UserFields.nickname.name, nickname) if temp_user == Errors.MISSING.name: add_output('No user with nickname ' + nickname + '. Please check again. \n') else: send_email(message, temp_user.email) sent = True finally: if not sent: add_output('Unable to send email to ' + temp_user.email + '. \n') return
) user_two = UserModel('0', 'chengyun yu', 'yuche', 'male', '*****@*****.**', '0,0', 'sports, dining', 'this is my new description. ' ) user_three = UserModel('0', 'non existent', 'noext', 'male', '*****@*****.**', '0.0', 'sports, dining', 'i am a sports guy who does not exist. ' ) print("Insert New: " + UserController.add_user(user_one)) print("Insert Conflict: " + UserController.add_user(user_two)) print("Update: " + UserController.edit_user(user_two)) print("Retrieve Hit: " + UserController.retrieve_user(UserFields.email.name, user_two.email)) print("Retrieve Miss: " + UserController.retrieve_user(UserFields.email.name, user_three.email)) return 'Tests Finished' decoded: str = '(\'Chengyun Yu\', \'yuche\', \'Male\', \'New York\', \'[email protected]\', \'sports\', \"I\'m a sports guy. \", 64)' print(UserController.decode_string(decoded))
def test_edit_user_1(self): UserController.delete_user('nickname_3', 'email_2') UserController.delete_user('nickname_2', 'email_2') UserController.add_user(self.user_2) user_3 = UserController.retrieve_user('email', 'email_2') user_3.nickname = 'nickname_3' self.assertIsNot(UserController.edit_user(user_3), 'MISSING' or 'DUPLICATE' or 'FAILURE') self.assertEqual(UserController.edit_user(user_3), 'MISSING') self.assertEqual( UserController.retrieve_user('email', 'email_2').nickname, 'nickname_3') UserController.delete_user('nickname_3', 'email_2')
def test_add_user(self): UserController.delete_user('nickname_1', 'email_1') self.assertEqual(UserController.add_user(self.user_1), 'SUCCESS') self.assertEqual(UserController.add_user(self.user_1), 'DUPLICATE') UserController.delete_user('nickname_1', 'email_1')
def register(self, name, birth, cpf, email, password): from View import HomeView if name == "": print(f"{bcolors.FAIL}Seu nome não pode ser vazio!{bcolors.ENDC}") HomeView.main().showregister() if not " " in name: print(f"{bcolors.WARNING}Você deve inserir seu nome completo!{bcolors.ENDC}") HomeView.main().showregister() if len(name) <= 2: print(f"{bcolors.FAIL}Seu nome é muito pequeno!{bcolors.ENDC}") HomeView.main().showregister() for i in range(len(name)): if name[i] in string.digits: print(f"{bcolors.FAIL}Seu nome não pode possuir números!{bcolors.ENDC}") HomeView.main().showregister() elif not name[i] in string.ascii_letters and not name[i] == " ": print(f"{bcolors.FAIL}Seu nome não pode possuir caracteres especiais{bcolors.ENDC}") HomeView.main().showregister() if birth == "": print(f"{bcolors.FAIL}Sua data de nascimento não pode ser vazia!{bcolors.ENDC}") HomeView.main().showregister() if not "/" in birth: print(f"{bcolors.FAIL}Sua data de nascimento precisa estar no formato (DD/MM/AAAA){bcolors.ENDC}") HomeView.main().showregister() for i in range(len(birth)): if birth[i] in string.ascii_letters: print(f"{bcolors.FAIL}Sua data de nascimento não pode conter letras!{bcolors.ENDC}") HomeView.main().showregister() elif not birth[i] in string.ascii_letters and not birth[i] == "/": pass if " " in birth: print( f"{bcolors.FAIL}Sua data de nascimento não pode conter espaços, formato correto (DD/MM/AAAA){bcolors.ENDC}") HomeView.main().showregister() sbirth = birth.split("/") if len(sbirth) > 3: print(f"{bcolors.FAIL}Sua data de nascimento precisa estar no formato (DD/MM/AAAA){bcolors.ENDC}") HomeView.main().showregister() for i in range(3): if i == 0: if not len(sbirth[i]) == 2: print(f"{bcolors.FAIL}Sua data de nascimento precisa estar no formato (DD/MM/AAAA){bcolors.ENDC}") HomeView.main().showregister() if not int(sbirth[i]) >= 0 or not int(sbirth[i]) <= 31: print(f"{bcolors.FAIL}Sua data de nascimento precisa estar entre 01 a 31 dias{bcolors.ENDC}") HomeView.main().showregister() if i == 1: if not len(sbirth[i]) == 2: print(f"{bcolors.FAIL}Sua data de nascimento precisa estar no formato (DD/MM/AAAA){bcolors.ENDC}") HomeView.main().showregister() elif not int(sbirth[i]) >= 0 or not int(sbirth[i]) <= 12: print(f"{bcolors.FAIL}Sua data de nascimento precisa estar entre 01 a 12 meses{bcolors.ENDC}") HomeView.main().showregister() if i == 2: if not len(sbirth[i]) == 4: print(f"{bcolors.FAIL}Sua data de nascimento precisa estar no formato (DD/MM/AAAA){bcolors.ENDC}") HomeView.main().showregister() date = datetime.datetime.now() date = date.strftime('%Y') if not int(sbirth[i]) >= 1900 or not (int(sbirth[i]) <= int(date)): print(f"{bcolors.FAIL}Sua data de nascimento precisa estar entre 1900 a {date} meses{bcolors.ENDC}") HomeView.main().showregister() if cpf == "": print(f"{bcolors.FAIL}CPF inválido!{bcolors.ENDC}") HomeView.main().showregister() cpf = cpf.replace(".", "") cpf = cpf.replace("-", "") if not len(cpf) == 11: print(f"{bcolors.FAIL}CPF inválido!{bcolors.ENDC}") HomeView.main().showregister() isOnlySame = True for i in range(len(cpf)): if not i == 0: if not cpf[i] == cpf[i - 1]: isOnlySame = False if not cpf[i] in string.digits: print(f"{bcolors.FAIL}CPF inválido!{bcolors.ENDC}") HomeView.main().showregister() if isOnlySame: print(f"{bcolors.FAIL}CPF inválido!{bcolors.ENDC}") HomeView.main().showregister() numbers = [int(digit) for digit in cpf if digit.isdigit()] sum_of_products = sum(a * b for a, b in zip(numbers[0:9], range(10, 1, -1))) expected_digit = (sum_of_products * 10 % 11) % 10 if numbers[9] != expected_digit: print(f"{bcolors.FAIL}CPF inválido!{bcolors.ENDC}") HomeView.main().showregister() sum_of_products = sum(a * b for a, b in zip(numbers[0:10], range(11, 1, -1))) expected_digit = (sum_of_products * 10 % 11) % 10 if numbers[10] != expected_digit: print(f"{bcolors.FAIL}CPF inválido!{bcolors.ENDC}") HomeView.main().showregister() if email == "": print(f"{bcolors.FAIL}Você precisa inserir um endereço de email!{bcolors.ENDC}") HomeView.main().showregister() if not "@" in email: print(f"{bcolors.FAIL}O email inserido não é um email real!{bcolors.ENDC}") HomeView.main().showregister() if not "." in email: print(f"{bcolors.FAIL}O email insiredo não é um email real!{bcolors.ENDC}") HomeView.main().showregister() if " " in email: print(f"{bcolors.FAIL}O email não pode possuir espaços!{bcolors.ENDC}") HomeView.main().showregister() if password == "": print(f"{bcolors.FAIL}Você precisa inserir uma senha!{bcolors.ENDC}") HomeView.main().showregister() if " " in password: print(f"{bcolors.FAIL}A sua senha não pode possuir espaços!{bcolors.ENDC}") HomeView.main().showregister() if len(password) < 5: print(f"{bcolors.FAIL}A sua senha deve possuir pelo menos 5 caracteres!{bcolors.ENDC}") HomeView.main().showregister() from Model import UserModal from Controller import UserController if not UserController.UserControl().get_by_email(email, "Model/Users.txt") == []: print(f"{bcolors.FAIL}Já existe um usuario com esse email!{bcolors.ENDC}") HomeView.main().showregister() if not UserController.UserControl().get_by_cpf(cpf, "Model/Users.txt") == []: print(f"{bcolors.FAIL}Já existe um usuario com esse cpf!{bcolors.ENDC}") HomeView.main().showregister() try: UserController.UserControl().add_user(UserModal.User("0", name, birth, cpf, email, password, False), "Model/Users.txt") print(f"{bcolors.OKGREEN}Usuário cadastrado com sucesso!{bcolors.ENDC}") from View import HomeView HomeView.main().showlogin(self) except: print(f"{bcolors.WARNING}Não foi possível realizar o cadastro, tente novamente em alguns anos.{bcolors.ENDC}")
def login(email): result = UserController.retrieve_user(UserFields.googleid.name, email) return result