def register(): """ This methods gets the json data from user and uses them to register user with the register_user method """ user = User() username = request.json['username'] email = request.json['email'] password = request.json['password'] password_confirmation = request.json['password_confirmation'] if not username or len(username.strip()) == 0: return jsonify({"message": "Username cannot be blank"}),401 elif not email: return jsonify({"message": "Email cannot be blank"}),401 elif not password: return jsonify({"message": "Password cannot be blank"}), 401 elif password != password_confirmation: return jsonify({"message": "Password does not match"}), 401 elif not re.match(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)", email): return jsonify({"message": "Input a valid email"}), 401 elif len(password) < 5: return jsonify({"message": "Password too short"}), 401 elif [u for u in user.users if u['email']== email]: return jsonify({"message": "User already exists"}), 401 user.register_user(username, email, password, password_confirmation) return jsonify({"message": "Registration successful"}), 201
def login(): """ This method checks if user exists in the users list then checks if email and password passed matches the ones for the list user then logs the user in and creates a session """ user_obj = User() email = request.json['email'] password = request.json['password'] user = [u for u in user_obj.users if email == u['email'] and password == u['password']] if not user: return jsonify({"message": "Invalid email/password combination"}), 401 user_obj.login_user(email, password) session['email'] = email return jsonify({"message": "Login successful"}), 200
class TestUserModel(TestCase): def setUp(self) -> None: self.usr_obj = User() def test_set_user(self): actual = self.usr_obj.set_user("guest") expected = "guest" self.assertEqual(expected, actual)
def login(): """ Checks if user exits in users then checks for matching password and username from users list hence logs in a user and creates a session """ user_obj = User() email = request.json['email'] password = request.json['password'] user = [ i for i in user_obj.users if email == i['email'] and password == i['password'] ] if not user: return jsonify({"message": "Invalid email/password combination"}), 401 user_obj.login_user(email, password) session['email'] = email return jsonify({"message": "Login successful"}), 200
def reset_password(): """ This method checks if user first exists then resets the old password and returns a success message """ user = User() email = request.json['email'] password = request.json['password'] password_confirmation = request.json['password_confirmation'] db_user = [u for u in user.users if email == u['email']] if not db_user: return jsonify({"message": "Email not found"}), 401 elif password != password_confirmation: return jsonify({"message": "Passwords don't match"}), 401 elif not password: return jsonify({"message": "Password cannot be blank"}), 401 elif len(password) < 5: return jsonify({"message": "Password too short"}), 401 user.reset_password(email, password, password_confirmation) user.user_info['password'] = password return jsonify({"message": "Password successfully reset"}), 200
def get_users(): user = User() return jsonify({'users': user.users})
def test_user_can_edit_recipe(category, desc, name, new_name): user = User("a","b","c","d") user.create_category(category, desc) user.create_recipe(category, name, desc) user.edit_recipe_name(name, new_name) assert new_name in user.recipes
def user_agent(): user = User("Ben","Ben12","ben0","password") user.create_category("Samosa","They look like triangles") user.create_recipe("Samosa", "sangala", "agian, like triangles") return user
def test_edit_category(new, old, desc): user = User("a","b","c","d") a = user.create_category(new,desc) b = user.edit_category(new, old) assert old in user.categories
def test_user_args_are_strings(): with pytest.raises(TypeError): user = User(1,3,"ui",[7.0,9])
def test_user_cant_miss_args(): with pytest.raises(TypeError): user = User("a")
def setUp(self) -> None: self.usr_obj = User()