def test_modify_user_role_id(self, client): """ With this code is tested the services to perform the user modification with service and have the result on db Test flow - Create user - Modify user - check user - delete user to clean the database """ form = UserForm() form.firstname.data = "Vincenzo" form.lastname.data = "Palazzo" form.password = "******" form.phone.data = "12345" form.dateofbirth = "12/12/2020" form.email.data = "*****@*****.**" user = User() form.populate_obj(user) user = UserService.create_user(user, form.password, 2) assert user is not None assert user.role_id is 2 response = login(client, form.email.data, form.password) assert response.status_code == 200 assert "logged_test" in response.data.decode("utf-8") formTest = UserForm(obj=user) user_modified = UserService.modify_user(formTest, 3) assert user is not None assert user.role_id is not 2 UserService.delete_user(user_modified.id) user_modified = get_user_with_email(user_modified.email) assert user_modified is None
def test_delete_user_with_email(self): """ This test cases test if the user service are able to remove correctly the user inside the DB Test flow - Create a new user with the service - delete a new user with service with user pass - check on db if this user is gone """ form = UserForm() form.firstname.data = "Vincenzo" form.lastname.data = "Palazzo" form.password = "******" form.phone.data = "12345" form.dateofbirth = "12/12/2020" form.email.data = "*****@*****.**" user = User() form.populate_obj(user) user = UserService.create_user(user, form.password, 2) assert user is not None assert user.role_id is 2 UserService.delete_user(email=user.email) user = db.session.query(User).filter_by(email=user.email).first() assert user is None
def test_create_user(self): """ test create user :return: """ form = UserForm() form.firstname.data = "Vincenzo" form.lastname.data = "Palazzo" form.password = "******" form.phone.data = "12345" form.dateofbirth = "12/12/2020" form.email.data = "*****@*****.**" user = User() form.populate_obj(user) user = UserService.create_user(user, form.password) assert user is not None assert user.role_id is 3 db.session.query(User).filter_by(id=user.id).delete() db.session.commit()
def _create_generic_user(role_id: int = 3, name_on_page: str = "customer"): """ This method contains the logic to create a new user with a different role :param role_id: role id on database this mean that is possible :param name_on_page: name to customize the page inside the template :return: response template """ form = UserForm() if request.method == "POST": if form.validate_on_submit(): q_user_email = UserService.user_is_present(email=form.email.data) q_user_phone = UserService.user_is_present(phone=form.phone.data) current_app.logger.error( "user with email is null? ".format(q_user_email is None) ) current_app.logger.error( "user with phone is null? ".format(q_user_phone is None) ) if (q_user_email is not None) or (q_user_phone is not None): return render_template( "create_user.html", form=form, message="Email {} and/or number {} already registered".format( form.email.data, form.phone.data ), type=name_on_page, ) user = User() form.populate_obj(user) user = UserService.create_user(user, form.password.data, role_id) if user is not None and user.authenticate(form.password.data): login_user(user) DispatcherMessage.send_message( REGISTRATION_EMAIL, [user.email, user.lastname, "112344"], ) new_role = UserService.get_user_role(role_id) if new_role is not None: session["ROLE"] = new_role.value return redirect("/") return render_template("create_user.html", form=form, type=name_on_page)