def new_user(): """ A pytest fixture that returns a user model object """ user = User(email="*****@*****.**", password="******") user.first_name = "New" user.last_name = "User" return user
def add_admin(email, password): with app.app_context(): user = User() user.email = email user.password = password user.is_admin = True user.email_confirmed = True user.email_confirm_date = datetime.datetime.now() user.save()
def test_admin_update_user_adding_new_roles_to_user(self, test_client): user = User.create(email="*****@*****.**", password="******") role1 = Role.create(name="test1-role") role2 = Role.create(name="test2-role") data = { "id": str(user.id), "email": "*****@*****.**", "password": "******", "first_name": "Test", "last_name": "User", "is_admin": "", f"role_{role1.id}": "", f"role_{role2.id}": "", } response = test_client.post( f"{module_info['url_prefix']}/update", data=data, follow_redirects=True, ) assert response.status_code == 200 assert user.email == "*****@*****.**" assert user.check_hash("newpass") assert user.first_name == "Test" assert user.last_name == "User" assert len(user.roles) == 2 assert role1.users[0].email == "*****@*****.**" assert role2.users[0].email == "*****@*****.**"
def test_admin_update_user_remove_old_roles_from_user(self, test_client): user = User(email="*****@*****.**", password="******", is_admin=True) user.is_admin = True role1 = Role(name="test1-role") role2 = Role(name="test2-role") user.roles = [role1, role2] user.save() data = { "id": str(user.id), "email": "*****@*****.**", "first_name": "Test", "last_name": "User", "password": "******", "is_admin": None, } response = test_client.post( f"{module_info['url_prefix']}/update", data=data, follow_redirects=True, ) assert response.status_code == 200 assert user.email == "*****@*****.**" assert user.check_hash("pass") assert len(user.roles) == 0 assert len(role1.users) == 0 assert len(role2.users) == 0
def test_admin_edit_existing_user_get(self, test_client): user = User.create(email="*****@*****.**", password="******") response = test_client.get( f"{module_info['url_prefix']}/edit/{user.id}", ) assert response.status_code == 200 assert b"*****@*****.**" in response.data assert b"Edit User" in response.data
def register(): context = {} reg_form = RegistrationForm() context["form"] = reg_form if request.method == "POST": if reg_form.validate_on_submit(): email = reg_form.email.data password = reg_form.password.data # add the user to the db User.create(email=email, password=password) flash(notify_success("Registered successfully! Please Log In")) return redirect(url_for("auth.login")) return render_template("auth/register.html", **context)
def test_admin_delete_existing_user_get(self, test_client): user = User(email="*****@*****.**", password="******") role1 = Role(name="test1-role") role2 = Role(name="test2-role") user.roles = [role1, role2] user.save() response = test_client.get( f"{module_info['url_prefix']}/delete/{user.id}", follow_redirects=True, ) test_user = User.query.filter(User.email == user.email).scalar() test_roles = Role.query.count() user_role = (User.query.join(role_user_link).join(Role).filter( User.id == user.id).scalar()) assert response.status_code == 200 assert test_user is None assert user_role is None assert test_roles == 2
def test_admin_add_existing_user_post(self, test_client): User.create(email="*****@*****.**", password="******") data = { "email": "*****@*****.**", "password": "******", "first_name": "Test", "last_name": "User", "is_admin": "", } response = test_client.post( f"{module_info['url_prefix']}/add", data=data, follow_redirects=True, ) test_users = User.query.filter(User.email == "*****@*****.**").count() assert response.status_code == 200 assert b"User with same email already exists" in response.data assert test_users == 1
def user_add(): """ **Adds a User** adds a user to database. """ context = {} if request.method == "POST": email = request.form["email"] password = request.form["password"] first_name = request.form["first_name"] last_name = request.form["last_name"] admin_user = request.form.get("is_admin") if admin_user == "True": is_admin = True else: is_admin = False has_user = db.session.query( exists().where(User.email == email)).scalar() if not has_user: new_user = User() new_user.email = email new_user.is_admin = is_admin new_user.first_name = first_name new_user.last_name = last_name new_user.password = password for key in request.form: if key.startswith("role_"): role_id = key.split("_")[1] role = Role.get_by_id(role_id) new_user.roles.append(role) new_user.save() return redirect(url_for("admin.user_add")) flash(notify_warning("User with same email already exists")) context["roles"] = Role.query.all() return render_template("admin/add.html", **context)
def register(): if request.method == 'POST': form = RegisterCustomerForm() if not form.validate_on_submit(): flash_errors(form) user = User() if User.query.filter(User.email == form.email.data).first(): flash(notify_warning("Email exists")) return mhelp.redirect_url('shop.homepage') user.email = form.email.data password1 = form.password.data password2 = form.reconfirm_password.data if not password1 == password2: flash(notify_warning("Passwords don't match")) return mhelp.redirect_url('shop.homepage') user.password = password1 user.is_customer = True print(user.email, password1) user.save() flash(notify_success('Successfully registered, please log in!')) return mhelp.redirect_url('shop.homepage')
def admin_user(): """ A pytest fixture that returns an admin user """ user = User(email="*****@*****.**", is_admin=True, password="******") return user
def non_admin_user(): """ A pytest fixture that returns a non admin user """ user = User(email="*****@*****.**", password="******") return user
def checkout_process(): if request.method == "POST": cart_info = get_cart_data() if len(cart_info["cart_data"]) == 0: flash(notify_warning("Cart cannot be empty!")) return mhelp.redirect_url("shop.checkout") form = CheckoutForm() with open( os.path.join( current_app.config["BASE_DIR"], "modules", "box__ecommerce", "shopman", "data", "country.json", )) as f: countries = json.load(f) # country_choices = [(c["name"], c["name"]) for c in countries] # form.default_country.choices = country_choices # form.diff_country.choices = country_choices country_choices = [('mauritius', 'Mauritius')] form.default_country.choices = country_choices form.diff_country.choices = country_choices # print(dir(form)) # ordered dict print(form._fields[0][0]) # print(form._fields['default_first_name'].data) checkout_data = {} for key in form._fields: checkout_data[key] = form._fields[key].data session["checkout_data"][0] = checkout_data print(request.form["paymentoption"]) if form.validate_on_submit(): if not form.diffAddress.data: first_name = form.default_first_name.data last_name = form.default_last_name.data country = form.default_country.data street = form.default_street.data town_city = form.default_town_city.data phone = form.default_phone.data email = form.default_email.data order_notes = form.default_order_notes.data elif form.diffAddress.data: first_name = form.diff_first_name.data last_name = form.diff_last_name.data country = form.diff_country.data street = form.diff_street.data town_city = form.diff_town_city.data phone = form.diff_phone.data email = form.diff_email.data order_notes = form.dif_order_notes.data billing_detail = BillingDetail() billing_detail.first_name = first_name billing_detail.last_name = last_name billing_detail.country = country billing_detail.street = street billing_detail.town_city = town_city billing_detail.phone = phone billing_detail.email = email billing_detail.order_notes = order_notes if form.createAccount.data: if not User.query.filter((User.email == email)).first(): user = User() user.first_name = first_name user.last_name = last_name user.email = email user.password = form.passoword.data user.email_confirmed = True user.is_customer = True user.email_confirm_date = datetime.now() order = Order() order.billing_detail = billing_detail shipping_option = DeliveryOption.query.get( request.form["deliveryoption"]) order.shipping_option = shipping_option payment_option = PaymentOption.query.get( request.form["paymentoption"]) order.payment_option = payment_option if current_user.is_authenticated: order.logged_in_customer_email = current_user.email if form.applyCoupon.data: coupon = Coupon.query.filter( Coupon.string == form.coupon.data).first() if coupon: order.coupon = coupon else: flash(notify_warning("Invalid Coupon")) cart_info = get_cart_data() cart_data = cart_info["cart_data"] for barcode in Cart.data()['items']: for item in Cart.data()['items'][barcode]: order_item = OrderItem() product = Product.query.filter_by(barcode=barcode).first() order_item.barcode = barcode order_item.quantity = int(item['quantity']) order_item.size = item['size'] order_item.color = item['color'] order.order_items.append(order_item) template = "shop/emails/order_info" subject = "FreaksBoutique - Order Details" context = {} context.update({'order': order, 'int': int, 'sum': sum}) send_async_email(email, subject, template, **context) order.insert() flash(notify_success("Great!")) context = mhelp.context() Cart.reset() return render_template("shop/order_complete.html", **context) else: flash_errors(form) return mhelp.redirect_url("shop.checkout")