From 2e6f13ba67a419add61e8401c0f84071762eb106 Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Tue, 12 Oct 2021 10:28:32 +0200 Subject: [PATCH] Make sure ownership transfer sets trial_expiry_date --- .../controllers/invitation_controller.ex | 2 +- .../invitation_controller_test.exs | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/plausible_web/controllers/invitation_controller.ex b/lib/plausible_web/controllers/invitation_controller.ex index 51de2fefd..94cbef54d 100644 --- a/lib/plausible_web/controllers/invitation_controller.ex +++ b/lib/plausible_web/controllers/invitation_controller.ex @@ -65,7 +65,7 @@ defmodule PlausibleWeb.InvitationController do end defp end_trial_of_new_owner(multi, new_owner) do - if Plausible.Billing.on_trial?(new_owner) do + if Plausible.Billing.on_trial?(new_owner) || is_nil(new_owner.trial_expiry_date) do Ecto.Multi.update(multi, :user, Plausible.Auth.User.end_trial(new_owner)) else multi diff --git a/test/plausible_web/controllers/invitation_controller_test.exs b/test/plausible_web/controllers/invitation_controller_test.exs index 9c10e672f..fe7da24c5 100644 --- a/test/plausible_web/controllers/invitation_controller_test.exs +++ b/test/plausible_web/controllers/invitation_controller_test.exs @@ -121,6 +121,27 @@ defmodule PlausibleWeb.Site.InvitationControllerTest do assert Timex.before?(Repo.reload!(user).trial_expiry_date, Timex.today()) assert Repo.reload!(site).locked end + + test "ownership transfer - if new owner does not have a trial - will set trial_expiry_date to yesterday", + %{ + conn: conn, + user: user + } do + Repo.update_all(from(u in Plausible.Auth.User, where: u.id == ^user.id), + set: [trial_expiry_date: nil] + ) + + inviter = insert(:user) + site = insert(:site, locked: false) + + invitation = + insert(:invitation, site_id: site.id, inviter: inviter, email: user.email, role: :owner) + + post(conn, "/sites/invitations/#{invitation.invitation_id}/accept") + + assert Timex.before?(Repo.reload!(user).trial_expiry_date, Timex.today()) + assert Repo.reload!(site).locked + end end describe "POST /sites/invitations/:invitation_id/reject" do