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 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 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 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()