def test_new_anonymous_invite(app): test_invite = UserInvitation.new_invite() assert test_invite.token in UserInvitation.deliver_invite(test_invite) assert not test_invite.is_expired assert not test_invite.is_admin assert UserInvitation.get_invite(test_invite.token) test_invite.accept_invite() assert test_invite.accepted_date assert test_invite.is_expired db.session.rollback()
def new_user(email, admin): ensure_server_name() if email and User.exists(email): raise click.BadParameter(f"{email} already exists") invite = UserInvitation.new_invite(email=email, is_admin=admin) msg = UserInvitation.deliver_invite(invite) db.session.commit() print(msg)
def test_double_invite(app): email = "*****@*****.**" invite_1 = UserInvitation.new_invite(email) old_token = invite_1.token # invite_2 should be an instance of UserInvitation(id=1) with a new token invite_2 = UserInvitation.new_invite(email) assert old_token != invite_2.token assert invite_1.token == invite_2.token assert not UserInvitation.get_invite(old_token) assert UserInvitation.get_invite(invite_1.token) db.session.rollback()
def test_new_email_invite(app): email = "*****@*****.**" test_invite = UserInvitation.new_invite(email) with mail.record_messages() as outbox: UserInvitation.deliver_invite(test_invite) assert test_invite.email in outbox[0].recipients assert test_invite.token in outbox[0].body assert UserInvitation.get_invite(test_invite.token) test_invite.accept_invite() assert test_invite.accepted_date assert test_invite.is_expired db.session.rollback()
def invite_user(): url_token = request.args.get("token", None) invite = UserInvitation.get_invite(url_token) if not invite: flash("Invite token is invalid or has expired", "danger") return redirect(url_for("auth.login")) supported_forms = { "invite": { "form": AcceptInviteForm, "template": "auth/accept_invite.html" }, "register": { "form": RegistrationForm, "template": "auth/register.html" }, } form_type = "invite" if invite.email else "register" form = supported_forms[form_type]["form"]() if form.validate_on_submit(): email = invite.email if invite.email else form.email.data new_user = User.new_user_from_invite(invite, form.password.data, email=email) db.session.commit() login_user(new_user) flash("Your password has been set.", "success") return redirect(url_for("main.browse")) return render_template(supported_forms[form_type]["template"], title="Accept Invitation", form=form)
def users(): users = User.query.all() inviteForm = forms.InviteUserForm() if inviteForm.validate_on_submit(): invitation = UserInvitation.new_invite(inviteForm.email.data) msg = UserInvitation.deliver_invite(invitation) flash(msg, "success") db.session.commit() return redirect(url_for("admin.users")) return render_template( "admin/users.html", users=users, delForm=forms.UserDeleteForm(), editForm=forms.UserEditForm(), inviteForm=inviteForm, )
def test_inviting_existing_user(app): email = "*****@*****.**" u = User(email=email) db.session.add(u) assert not UserInvitation.new_invite(email) db.session.rollback()
def test_double_anonymous_deliver(app): test_invite = UserInvitation.new_invite() assert test_invite.token in UserInvitation.deliver_invite(test_invite) assert test_invite.token in UserInvitation.deliver_invite(test_invite) db.session.rollback()
def new_invite(email=None, admin=False): invite = UserInvitation.new_invite(email=email, is_admin=admin) msg = UserInvitation.deliver_invite(invite) print(msg)