示例#1
0
    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
示例#2
0
    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
示例#3
0
def user_data():
    message = None
    if request.method == "POST":
        form = UserEditForm()
        if form.validate_on_submit():
            UserService.modify_user(form)
            return render_template("user_data.html", form=form)
        print(form.errors.items())
        return render_template("user_data.html", form=form, error="Error in the data")
    else:
        q = User.query.filter_by(id=current_user.id).first()
        if q is not None:
            form = UserForm(obj=q)
            return render_template("user_data.html", form=form)
示例#4
0
def update_book():
    if current_user is not None and hasattr(current_user, "id"):
        # the date and time come as string, so I have to parse them and transform them in python datetime
        #
        reservation_date = request.form.get("reservation_date")
        py_datetime = datetime.datetime.strptime(reservation_date,
                                                 "%d/%m/%Y %H:%M")
        #
        people_number = int(request.form.get("people_number"))
        #
        reservation_id = int(request.form.get("reservation_id"))

        new_book = BookingServices.update_book(
            reservation_id,
            current_user,
            py_datetime,
            people_number,
            request.form.get("friends"),
        )
        reservations_as_list = UserService.get_customer_reservation(
            None, None, current_user.id)

        form = ReservationForm()
        return render_template(
            "user_reservations.html",
            reservations_as_list=reservations_as_list,
            my_date_formatter=my_date_formatter,
            new_book=new_book,
            form=form,
        )
示例#5
0
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)
示例#6
0
    def test_reservation_as_list(self, client):
        """
        Test get reservation customer list
        """
        user = db.session.query(User).filter_by(
            email="*****@*****.**").first()
        raw_list = db.session.query(Reservation).filter_by(
            customer_id=user.id).all()
        reservations_as_list = UserService.get_customer_reservation(
            None, None, user.id)

        assert len(raw_list) == len(reservations_as_list)
def myreservation():

    # filter params
    fromDate = request.args.get("fromDate", type=str)
    toDate = request.args.get("toDate", type=str)

    reservations_as_list = UserService.get_customer_reservation(
        fromDate, toDate, current_user.id)
    form = ReservationForm()
    return render_template(
        "user_reservations.html",
        reservations_as_list=reservations_as_list,
        my_date_formatter=my_date_formatter,
        form=form,
    )
def delete_reservation(reservation_id):

    deleted = BookingServices.delete_book(reservation_id, current_user.id)

    reservations_as_list = UserService.get_customer_reservation(
        None, None, current_user.id)
    form = ReservationForm()
    return render_template(
        "user_reservations.html",
        reservations_as_list=reservations_as_list,
        my_date_formatter=my_date_formatter,
        deleted=deleted,
        _test="del_rest_test",
        form=form,
    )
示例#9
0
    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()
示例#10
0
def user_delete():
    UserService.delete_user(current_user.id)
    return redirect("/logout")