From 700a65c98a112a201788b941d74cf05511776168 Mon Sep 17 00:00:00 2001
From: Uku Taht
- If you accept the ownership transfer, you will be responsible for billing.
+ If you accept the ownership transfer, you will be responsible for billing going forward.
+ <%= if is_nil(@current_user.trial_expiry_date) && is_nil(@current_user.subscription) do %>
+
+ You will have to enter your card details immediately with no 30-day trial.
+ <% end %>
+ <%= if Plausible.Billing.on_trial?(@current_user) do %>
+
+ Your 30-day free trial will end immediately and you will have to enter your card details to keep using Plausible.
+ <% end %>
+ When you create your first site, your account will enter a 30 day free trial. +
+Just the naked domain or subdomain without 'www'
diff --git a/priv/repo/migrations/20210908081119_allow_trial_expiry_to_be_null.exs b/priv/repo/migrations/20210908081119_allow_trial_expiry_to_be_null.exs new file mode 100644 index 000000000..21e935703 --- /dev/null +++ b/priv/repo/migrations/20210908081119_allow_trial_expiry_to_be_null.exs @@ -0,0 +1,9 @@ +defmodule Plausible.Repo.Migrations.AllowTrialExpiryToBeNull do + use Ecto.Migration + + def change do + alter table(:users) do + modify :trial_expiry_date, :date, null: true + end + end +end diff --git a/test/plausible_web/controllers/auth_controller_test.exs b/test/plausible_web/controllers/auth_controller_test.exs index cb44b32b9..b6e3f3849 100644 --- a/test/plausible_web/controllers/auth_controller_test.exs +++ b/test/plausible_web/controllers/auth_controller_test.exs @@ -71,6 +71,116 @@ defmodule PlausibleWeb.AuthControllerTest do end end + describe "GET /register/invitations/:invitation_id" do + test "shows the register form", %{conn: conn} do + inviter = insert(:user) + site = insert(:site, members: [inviter]) + + invitation = + insert(:invitation, + site_id: site.id, + inviter: inviter, + email: "user@email.co", + role: :admin + ) + + conn = get(conn, "/register/invitation/#{invitation.invitation_id}") + + assert html_response(conn, 200) =~ "Enter your details" + end + end + + describe "POST /register/invitation/:invitation_id" do + setup do + inviter = insert(:user) + site = insert(:site, members: [inviter]) + + invitation = + insert(:invitation, + site_id: site.id, + inviter: inviter, + email: "user@email.co", + role: :admin + ) + + {:ok, %{site: site, invitation: invitation}} + end + + test "registering sends an activation link", %{conn: conn, invitation: invitation} do + post(conn, "/register/invitation/#{invitation.invitation_id}", + user: %{ + name: "Jane Doe", + email: "user@example.com", + password: "very-secret", + password_confirmation: "very-secret" + } + ) + + assert_delivered_email_matches(%{to: [{_, user_email}], subject: subject}) + assert user_email == "user@example.com" + assert subject =~ "is your Plausible email verification code" + end + + test "creates user record", %{conn: conn, invitation: invitation} do + post(conn, "/register/invitation/#{invitation.invitation_id}", + user: %{ + name: "Jane Doe", + email: "user@example.com", + password: "very-secret", + password_confirmation: "very-secret" + } + ) + + user = Repo.get_by(Plausible.Auth.User, email: "user@example.com") + assert user.name == "Jane Doe" + end + + test "leaves trial_expiry_date null when invitation role is not :owner", %{ + conn: conn, + invitation: invitation + } do + post(conn, "/register/invitation/#{invitation.invitation_id}", + user: %{ + name: "Jane Doe", + email: "user@example.com", + password: "very-secret", + password_confirmation: "very-secret" + } + ) + + user = Repo.get_by(Plausible.Auth.User, email: "user@example.com") + assert is_nil(user.trial_expiry_date) + end + + test "logs the user in", %{conn: conn, invitation: invitation} do + conn = + post(conn, "/register/invitation/#{invitation.invitation_id}", + user: %{ + name: "Jane Doe", + email: "user@example.com", + password: "very-secret", + password_confirmation: "very-secret" + } + ) + + assert get_session(conn, :current_user_id) + end + + test "user is redirected to activation after registration", %{conn: conn} do + conn = + post(conn, "/register", + user: %{ + name: "Jane Doe", + email: "user@example.com", + password: "very-secret", + password_confirmation: "very-secret" + } + ) + + assert redirected_to(conn) == "/activate" + end + end + describe "GET /activate" do setup [:create_user, :log_in] diff --git a/test/plausible_web/controllers/invitation_controller_test.exs b/test/plausible_web/controllers/invitation_controller_test.exs index 4e9671a5d..9c10e672f 100644 --- a/test/plausible_web/controllers/invitation_controller_test.exs +++ b/test/plausible_web/controllers/invitation_controller_test.exs @@ -101,6 +101,26 @@ defmodule PlausibleWeb.Site.InvitationControllerTest do assert Repo.reload!(site).locked end + + test "ownership transfer - will end the trial of the new owner immediately", %{ + conn: conn, + user: user + } do + Repo.update_all(from(u in Plausible.Auth.User, where: u.id == ^user.id), + set: [trial_expiry_date: Timex.today() |> Timex.shift(days: 7)] + ) + + 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 diff --git a/test/plausible_web/controllers/site_controller_test.exs b/test/plausible_web/controllers/site_controller_test.exs index 3a6dc0bd5..9794854d5 100644 --- a/test/plausible_web/controllers/site_controller_test.exs +++ b/test/plausible_web/controllers/site_controller_test.exs @@ -104,6 +104,19 @@ defmodule PlausibleWeb.SiteControllerTest do assert Repo.exists?(Plausible.Site, domain: "example.com") end + test "starts trial if user does not have trial yet", %{conn: conn, user: user} do + Plausible.Auth.User.remove_trial_expiry(user) |> Repo.update!() + + post(conn, "/sites", %{ + "site" => %{ + "domain" => "example.com", + "timezone" => "Europe/London" + } + }) + + assert Repo.reload!(user).trial_expiry_date + end + test "sends welcome email if this is the user's first site", %{conn: conn} do post(conn, "/sites", %{ "site" => %{