def unconfirmed_user(): """ A pytest fixture that returns a non admin user """ user = User() user.email = "*****@*****.**" user.password = "******" user.is_email_confirmed = False return user
def add_admin(email, password, username): with app.app_context(): user = User() user.email = email user.username = username user.password = password user.is_admin = True user.is_email_confirmed = True user.email_confirm_date = datetime.datetime.now() user.save()
def test_valid_dashboard_login_is_case_insensitive(self, test_client): User.create(email="*****@*****.**", password="******") data = {"email": "*****@*****.**", "password": "******"} response = test_client.post( url_for("auth.login"), data=data, follow_redirects=True, ) assert response.status_code == 200 assert current_user.email.lower() == data["email"].lower() assert request.path == url_for("auth.unconfirmed")
def test_user_not_registered_on_invalid_form_submit(self, test_client): User.create(email="*****@*****.**", password="******") data = { "email": "*****@*****.**", "password": "******", "confirm": "password", } response = test_client.post( f"{module_info['url_prefix']}/register", data=data, follow_redirects=True, ) assert response.status_code == 200 assert request.path == url_for("auth.register")
def test_user_registration_is_case_insensitive(self, test_client): User.create(email="*****@*****.**", password="******") data = { "email": "*****@*****.**", "password": "******", "confirm": "password", } response = test_client.post( f"{module_info['url_prefix']}/register", data=data, follow_redirects=True, ) assert response.status_code == 200 assert request.path == url_for("auth.register")
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_password("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_password("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 test_send_email_with_no_default_sender(capfd, email_config): user = User.create(email="*****@*****.**", password="******") token = "sometoken" template = "auth/emails/activate_user" subject = "Please confirm your email" context = {"token": token, "user": user} send_async_email(user.email, subject, template, **context) captured = capfd.readouterr() assert "Shopyo Error: MAIL_DEFAULT_SENDER not configured" in captured.out
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_bridge).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 test_send_email_with_no_username_or_password_set(capfd, email_config): user = User.create(email="*****@*****.**", password="******") token = "sometoken" template = "auth/emails/activate_user" subject = "Please confirm your email" context = {"token": token, "user": user} thread = send_async_email(user.email, subject, template, **context) thread.join() captured = capfd.readouterr() assert ("Shopyo Error: MAIL_USERNAME, and/or MAIL_PASSWORD not configured" in captured.out)
def admin_user(): """ A pytest fixture that returns an admin user """ user = User() user.email = "*****@*****.**" user.password = "******" user.is_admin = True user.is_email_confirmed = True user.email_confirm_date = datetime.datetime.now() return user
def test_successful_login(self): user = User() user.username = test_user2["username"] user.set_hash(test_user2["password"]) user.admin_user = True user.insert() self.driver.get(self.url) self.driver.find_element_by_id("username").send_keys( test_user2["username"]) self.driver.find_element_by_id("password").send_keys( test_user2["password"]) self.driver.find_element_by_id("submit").click() time.sleep(3) self.assertEqual(self.driver.current_url, "http://localhost:8943/dashboard/")
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("appadmin.user_add")) flash(notify_warning("User with same email already exists")) context["roles"] = Role.query.all() return render_template("appadmin/add.html", **context)
def test_send_email_using_template_on_valid_credentials(capfd): user = User.create(email="*****@*****.**", password="******") token = "sometoken" template = "auth/emails/activate_user" subject = "Please confirm your email" from_email = "*****@*****.**" context = {"token": token, "user": user} thread = send_async_email(user.email, subject, template, from_email=from_email, **context) thread.join() captured = capfd.readouterr() assert "Please confirm your email" in captured.out assert "sometoken" in captured.out assert "*****@*****.**" in captured.out assert "*****@*****.**" in captured.out assert "Welcome to Shopyo" in captured.out assert "To confirm your account please click on" in captured.out assert "The Shopyo Team" in captured.out
def upload_default_users(): with app.app_context(): user = User() user.email = '*****@*****.**' user.password = '******' user.is_admin = True user.is_email_confirmed = True user.username = '******' user.email_confirm_date = datetime.datetime.now() user.like_list = LikeList() user.bookmark_list = BookmarkList() path = Path( title=data['title'], slug=data['slug'], ) for section in data['sections']: section = Section(title=section['title']) for url in section['links']: link = Link(url=url) section.links.append(link) path.sections.append(section) user.paths.append(path) user.save()