Fix APP-46E: accept invitation 2nd click (#3120)

* Fix APP-46E: accept invitation 2nd click

* :LspStop

* Update lib/plausible_web/controllers/invitation_controller.ex

Co-authored-by: Vini Brasil <vini@hey.com>

---------

Co-authored-by: Vini Brasil <vini@hey.com>
This commit is contained in:
hq1 2023-07-11 10:52:09 +02:00 committed by GitHub
parent cf5cc8de94
commit 2be9020460
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 36 deletions

View File

@ -14,9 +14,10 @@ defmodule PlausibleWeb.InvitationController do
def accept_invitation(conn, %{"invitation_id" => invitation_id}) do def accept_invitation(conn, %{"invitation_id" => invitation_id}) do
invitation = invitation =
Repo.get_by!(Invitation, invitation_id: invitation_id) Repo.get_by(Invitation, invitation_id: invitation_id)
|> Repo.preload([:site, :inviter]) |> Repo.preload([:site, :inviter])
if invitation do
user = conn.assigns[:current_user] user = conn.assigns[:current_user]
existing_membership = Repo.get_by(Membership, user_id: user.id, site_id: invitation.site.id) existing_membership = Repo.get_by(Membership, user_id: user.id, site_id: invitation.site.id)
@ -49,11 +50,16 @@ defmodule PlausibleWeb.InvitationController do
|> put_flash(:success, "You now have access to #{invitation.site.domain}") |> put_flash(:success, "You now have access to #{invitation.site.domain}")
|> redirect(to: "/#{URI.encode_www_form(invitation.site.domain)}") |> redirect(to: "/#{URI.encode_www_form(invitation.site.domain)}")
{:error, _, _} -> {:error, _operation, _value, _changes} ->
conn conn
|> put_flash(:error, "Something went wrong, please try again") |> put_flash(:error, "Something went wrong, please try again")
|> redirect(to: "/sites") |> redirect(to: "/sites")
end end
else
conn
|> put_flash(:error, "Invitation missing or already accepted")
|> redirect(to: "/sites")
end
end end
defp downgrade_previous_owner(multi, site) do defp downgrade_previous_owner(multi, site) do

View File

@ -17,7 +17,12 @@ defmodule PlausibleWeb.Site.InvitationControllerTest do
role: :admin role: :admin
) )
post(conn, "/sites/invitations/#{invitation.invitation_id}/accept") conn = post(conn, "/sites/invitations/#{invitation.invitation_id}/accept")
assert Phoenix.Flash.get(conn.assigns.flash, :success) ==
"You now have access to #{site.domain}"
assert redirected_to(conn) == "/#{site.domain}"
refute Repo.exists?(from(i in Plausible.Auth.Invitation, where: i.email == ^user.email)) refute Repo.exists?(from(i in Plausible.Auth.Invitation, where: i.email == ^user.email))
@ -25,6 +30,30 @@ defmodule PlausibleWeb.Site.InvitationControllerTest do
assert membership.role == :admin assert membership.role == :admin
end end
test "does not crash if clicked for the 2nd time in another tab", %{conn: conn, user: user} do
site = insert(:site)
invitation =
insert(:invitation,
site_id: site.id,
inviter: build(:user),
email: user.email,
role: :admin
)
c1 = post(conn, "/sites/invitations/#{invitation.invitation_id}/accept")
assert redirected_to(c1) == "/#{site.domain}"
assert Phoenix.Flash.get(c1.assigns.flash, :success) ==
"You now have access to #{site.domain}"
c2 = post(conn, "/sites/invitations/#{invitation.invitation_id}/accept")
assert redirected_to(c2) == "/sites"
assert Phoenix.Flash.get(c2.assigns.flash, :error) ==
"Invitation missing or already accepted"
end
test "notifies the original inviter", %{conn: conn, user: user} do test "notifies the original inviter", %{conn: conn, user: user} do
inviter = insert(:user) inviter = insert(:user)
site = insert(:site) site = insert(:site)