Extract non-auth logic from auth controller
parent
ad04c5f6c6
commit
df1a55fb86
@ -0,0 +1,75 @@
|
||||
defmodule PlausibleWeb.GoogleAuthController do
|
||||
use PlausibleWeb, :controller
|
||||
use Plausible.Repo
|
||||
|
||||
def google_auth_callback(conn, %{"error" => error, "state" => state} = params) do
|
||||
[site_id, _redirect_to] = Jason.decode!(state)
|
||||
site = Repo.get(Plausible.Site, site_id)
|
||||
|
||||
case error do
|
||||
"access_denied" ->
|
||||
conn
|
||||
|> put_flash(
|
||||
:error,
|
||||
"We were unable to authenticate your Google Analytics account. Please check that you have granted us permission to 'See and download your Google Analytics data' and try again."
|
||||
)
|
||||
|> redirect(to: Routes.site_path(conn, :settings_general, site.domain))
|
||||
|
||||
message when message in ["server_error", "temporarily_unavailable"] ->
|
||||
conn
|
||||
|> put_flash(
|
||||
:error,
|
||||
"We are unable to authenticate your Google Analytics account because Google's authentication service is temporarily unavailable. Please try again in a few moments."
|
||||
)
|
||||
|> redirect(to: Routes.site_path(conn, :settings_general, site.domain))
|
||||
|
||||
_any ->
|
||||
Sentry.capture_message("Google OAuth callback failed. Reason: #{inspect(params)}")
|
||||
|
||||
conn
|
||||
|> put_flash(
|
||||
:error,
|
||||
"We were unable to authenticate your Google Analytics account. If the problem persists, please contact support for assistance."
|
||||
)
|
||||
|> redirect(to: Routes.site_path(conn, :settings_general, site.domain))
|
||||
end
|
||||
end
|
||||
|
||||
def google_auth_callback(conn, %{"code" => code, "state" => state}) do
|
||||
res = Plausible.Google.HTTP.fetch_access_token(code)
|
||||
[site_id, redirect_to] = Jason.decode!(state)
|
||||
site = Repo.get(Plausible.Site, site_id)
|
||||
expires_at = NaiveDateTime.add(NaiveDateTime.utc_now(), res["expires_in"])
|
||||
|
||||
case redirect_to do
|
||||
"import" ->
|
||||
redirect(conn,
|
||||
to:
|
||||
Routes.site_path(conn, :import_from_google_view_id_form, site.domain,
|
||||
access_token: res["access_token"],
|
||||
refresh_token: res["refresh_token"],
|
||||
expires_at: NaiveDateTime.to_iso8601(expires_at)
|
||||
)
|
||||
)
|
||||
|
||||
_ ->
|
||||
id_token = res["id_token"]
|
||||
[_, body, _] = String.split(id_token, ".")
|
||||
id = body |> Base.decode64!(padding: false) |> Jason.decode!()
|
||||
|
||||
Plausible.Site.GoogleAuth.changeset(%Plausible.Site.GoogleAuth{}, %{
|
||||
email: id["email"],
|
||||
refresh_token: res["refresh_token"],
|
||||
access_token: res["access_token"],
|
||||
expires: expires_at,
|
||||
user_id: conn.assigns[:current_user].id,
|
||||
site_id: site_id
|
||||
})
|
||||
|> Repo.insert!()
|
||||
|
||||
site = Repo.get(Plausible.Site, site_id)
|
||||
|
||||
redirect(conn, to: "/#{URI.encode_www_form(site.domain)}/settings/integrations")
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,296 @@
|
||||
defmodule PlausibleWeb.UserController do
|
||||
use PlausibleWeb, :controller
|
||||
use Plausible.Repo
|
||||
|
||||
alias Plausible.Auth
|
||||
alias PlausibleWeb.TwoFactor
|
||||
|
||||
plug(PlausibleWeb.RequireAccountPlug)
|
||||
|
||||
plug(:assign_is_selfhost)
|
||||
|
||||
defp assign_is_selfhost(conn, _opts) do
|
||||
assign(conn, :is_selfhost, Plausible.Release.selfhost?())
|
||||
end
|
||||
|
||||
def user_settings(conn, _params) do
|
||||
user = conn.assigns.current_user
|
||||
settings_changeset = Auth.User.settings_changeset(user)
|
||||
email_changeset = Auth.User.settings_changeset(user)
|
||||
|
||||
render_settings(conn,
|
||||
settings_changeset: settings_changeset,
|
||||
email_changeset: email_changeset
|
||||
)
|
||||
end
|
||||
|
||||
def activate_form(conn, _params) do
|
||||
user = conn.assigns.current_user
|
||||
|
||||
render(conn, "activate.html",
|
||||
has_email_code?: Plausible.Users.has_email_code?(user),
|
||||
has_any_invitations?: Plausible.Site.Memberships.pending?(user.email),
|
||||
has_any_memberships?: Plausible.Site.Memberships.any?(user),
|
||||
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
||||
)
|
||||
end
|
||||
|
||||
def activate(conn, %{"code" => code}) do
|
||||
user = conn.assigns.current_user
|
||||
|
||||
has_any_invitations? = Plausible.Site.Memberships.pending?(user.email)
|
||||
has_any_memberships? = Plausible.Site.Memberships.any?(user)
|
||||
|
||||
case Auth.EmailVerification.verify_code(user, code) do
|
||||
:ok ->
|
||||
cond do
|
||||
has_any_memberships? ->
|
||||
handle_email_updated(conn)
|
||||
|
||||
has_any_invitations? ->
|
||||
redirect(conn, to: Routes.site_path(conn, :index))
|
||||
|
||||
true ->
|
||||
redirect(conn, to: Routes.site_path(conn, :new))
|
||||
end
|
||||
|
||||
{:error, :incorrect} ->
|
||||
render(conn, "activate.html",
|
||||
error: "Incorrect activation code",
|
||||
has_email_code?: true,
|
||||
has_any_invitations?: has_any_invitations?,
|
||||
has_any_memberships?: has_any_memberships?,
|
||||
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
||||
)
|
||||
|
||||
{:error, :expired} ->
|
||||
render(conn, "activate.html",
|
||||
error: "Code is expired, please request another one",
|
||||
has_email_code?: false,
|
||||
has_any_invitations?: has_any_invitations?,
|
||||
has_any_memberships?: has_any_memberships?,
|
||||
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
def request_activation_code(conn, _params) do
|
||||
user = conn.assigns.current_user
|
||||
Auth.EmailVerification.issue_code(user)
|
||||
|
||||
conn
|
||||
|> put_flash(:success, "Activation code was sent to #{user.email}")
|
||||
|> redirect(to: Routes.user_path(conn, :activate_form))
|
||||
end
|
||||
|
||||
def initiate_2fa_setup(conn, _params) do
|
||||
case Auth.TOTP.initiate(conn.assigns.current_user) do
|
||||
{:ok, user, %{totp_uri: totp_uri, secret: secret}} ->
|
||||
render(conn, "initiate_2fa_setup.html", user: user, totp_uri: totp_uri, secret: secret)
|
||||
|
||||
{:error, :already_setup} ->
|
||||
conn
|
||||
|> put_flash(:error, "Two-factor authentication is already setup for this account.")
|
||||
|> redirect(to: Routes.user_path(conn, :user_settings) <> "#setup-2fa")
|
||||
end
|
||||
end
|
||||
|
||||
def verify_2fa_setup_form(conn, _params) do
|
||||
if Auth.TOTP.initiated?(conn.assigns.current_user) do
|
||||
render(conn, "verify_2fa_setup.html")
|
||||
else
|
||||
redirect(conn, to: Routes.user_path(conn, :user_settings) <> "#setup-2fa")
|
||||
end
|
||||
end
|
||||
|
||||
def verify_2fa_setup(conn, %{"code" => code}) do
|
||||
case Auth.TOTP.enable(conn.assigns.current_user, code) do
|
||||
{:ok, _, %{recovery_codes: codes}} ->
|
||||
conn
|
||||
|> put_flash(:success, "Two-factor authentication is fully enabled now")
|
||||
|> render("generate_2fa_recovery_codes.html", recovery_codes: codes, from_setup: true)
|
||||
|
||||
{:error, :invalid_code} ->
|
||||
conn
|
||||
|> put_flash(:error, "The provided code is invalid. Please try again")
|
||||
|> render("verify_2fa_setup.html")
|
||||
|
||||
{:error, :not_initiated} ->
|
||||
conn
|
||||
|> put_flash(:error, "Please enable two-factor authentication for this account first.")
|
||||
|> redirect(to: Routes.user_path(conn, :user_settings) <> "#setup-2fa")
|
||||
end
|
||||
end
|
||||
|
||||
def disable_2fa(conn, %{"password" => password}) do
|
||||
case Auth.TOTP.disable(conn.assigns.current_user, password) do
|
||||
{:ok, _} ->
|
||||
conn
|
||||
|> TwoFactor.clear_remember_2fa()
|
||||
|> put_flash(:success, "Two-factor authentication is disabled")
|
||||
|> redirect(to: Routes.user_path(conn, :user_settings) <> "#setup-2fa")
|
||||
|
||||
{:error, :invalid_password} ->
|
||||
conn
|
||||
|> put_flash(:error, "Incorrect password provided")
|
||||
|> redirect(to: Routes.user_path(conn, :user_settings) <> "#setup-2fa")
|
||||
end
|
||||
end
|
||||
|
||||
def generate_2fa_recovery_codes(conn, %{"password" => password}) do
|
||||
case Auth.TOTP.generate_recovery_codes(conn.assigns.current_user, password) do
|
||||
{:ok, codes} ->
|
||||
conn
|
||||
|> put_flash(:success, "New recovery codes generated")
|
||||
|> render("generate_2fa_recovery_codes.html", recovery_codes: codes, from_setup: false)
|
||||
|
||||
{:error, :invalid_password} ->
|
||||
conn
|
||||
|> put_flash(:error, "Incorrect password provided")
|
||||
|> redirect(to: Routes.user_path(conn, :user_settings) <> "#setup-2fa")
|
||||
|
||||
{:error, :not_enabled} ->
|
||||
conn
|
||||
|> put_flash(:error, "Please enable two-factor authentication for this account first.")
|
||||
|> redirect(to: Routes.user_path(conn, :user_settings) <> "#setup-2fa")
|
||||
end
|
||||
end
|
||||
|
||||
def save_settings(conn, %{"user" => user_params}) do
|
||||
user = conn.assigns.current_user
|
||||
changes = Auth.User.settings_changeset(user, user_params)
|
||||
|
||||
case Repo.update(changes) do
|
||||
{:ok, _user} ->
|
||||
conn
|
||||
|> put_flash(:success, "Account settings saved successfully")
|
||||
|> redirect(to: Routes.user_path(conn, :user_settings))
|
||||
|
||||
{:error, changeset} ->
|
||||
email_changeset = Auth.User.settings_changeset(user)
|
||||
|
||||
render_settings(conn,
|
||||
settings_changeset: changeset,
|
||||
email_changeset: email_changeset
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
def update_email(conn, %{"user" => user_params}) do
|
||||
user = conn.assigns.current_user
|
||||
changes = Auth.User.email_changeset(user, user_params)
|
||||
|
||||
case Repo.update(changes) do
|
||||
{:ok, user} ->
|
||||
if user.email_verified do
|
||||
handle_email_updated(conn)
|
||||
else
|
||||
Auth.EmailVerification.issue_code(user)
|
||||
redirect(conn, to: Routes.user_path(conn, :activate_form))
|
||||
end
|
||||
|
||||
{:error, changeset} ->
|
||||
settings_changeset = Auth.User.settings_changeset(user)
|
||||
|
||||
render_settings(conn,
|
||||
settings_changeset: settings_changeset,
|
||||
email_changeset: changeset
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
def cancel_update_email(conn, _params) do
|
||||
changeset = Auth.User.cancel_email_changeset(conn.assigns.current_user)
|
||||
|
||||
case Repo.update(changeset) do
|
||||
{:ok, user} ->
|
||||
conn
|
||||
|> put_flash(:success, "Email changed back to #{user.email}")
|
||||
|> redirect(to: Routes.user_path(conn, :user_settings) <> "#change-email-address")
|
||||
|
||||
{:error, _} ->
|
||||
conn
|
||||
|> put_flash(
|
||||
:error,
|
||||
"Could not cancel email update because previous email has already been taken"
|
||||
)
|
||||
|> redirect(to: Routes.user_path(conn, :activate_form))
|
||||
end
|
||||
end
|
||||
|
||||
defp handle_email_updated(conn) do
|
||||
conn
|
||||
|> put_flash(:success, "Email updated successfully")
|
||||
|> redirect(to: Routes.user_path(conn, :user_settings) <> "#change-email-address")
|
||||
end
|
||||
|
||||
defp render_settings(conn, opts) do
|
||||
settings_changeset = Keyword.fetch!(opts, :settings_changeset)
|
||||
email_changeset = Keyword.fetch!(opts, :email_changeset)
|
||||
|
||||
user = Plausible.Users.with_subscription(conn.assigns.current_user)
|
||||
{pageview_usage, custom_event_usage} = Plausible.Billing.usage_breakdown(user)
|
||||
|
||||
render(conn, "user_settings.html",
|
||||
user: user |> Repo.preload(:api_keys),
|
||||
settings_changeset: settings_changeset,
|
||||
email_changeset: email_changeset,
|
||||
subscription: user.subscription,
|
||||
invoices: Plausible.Billing.paddle_api().get_invoices(user.subscription),
|
||||
theme: user.theme || "system",
|
||||
team_member_limit: Plausible.Billing.Quota.team_member_limit(user),
|
||||
team_member_usage: Plausible.Billing.Quota.team_member_usage(user),
|
||||
site_limit: Plausible.Billing.Quota.site_limit(user),
|
||||
site_usage: Plausible.Billing.Quota.site_usage(user),
|
||||
total_pageview_limit: Plausible.Billing.Quota.monthly_pageview_limit(user.subscription),
|
||||
total_pageview_usage: pageview_usage + custom_event_usage,
|
||||
totp_enabled?: Auth.TOTP.enabled?(user),
|
||||
custom_event_usage: custom_event_usage,
|
||||
pageview_usage: pageview_usage
|
||||
)
|
||||
end
|
||||
|
||||
def new_api_key(conn, _params) do
|
||||
changeset = Auth.ApiKey.changeset(%Auth.ApiKey{})
|
||||
|
||||
render(conn, "new_api_key.html",
|
||||
changeset: changeset,
|
||||
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
||||
)
|
||||
end
|
||||
|
||||
def create_api_key(conn, %{"api_key" => %{"name" => name, "key" => key}}) do
|
||||
case Auth.create_api_key(conn.assigns.current_user, name, key) do
|
||||
{:ok, _api_key} ->
|
||||
conn
|
||||
|> put_flash(:success, "API key created successfully")
|
||||
|> redirect(to: "/settings#api-keys")
|
||||
|
||||
{:error, changeset} ->
|
||||
render(conn, "new_api_key.html",
|
||||
changeset: changeset,
|
||||
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
def delete_api_key(conn, %{"id" => id}) do
|
||||
case Auth.delete_api_key(conn.assigns.current_user, id) do
|
||||
:ok ->
|
||||
conn
|
||||
|> put_flash(:success, "API key revoked successfully")
|
||||
|> redirect(to: "/settings#api-keys")
|
||||
|
||||
{:error, :not_found} ->
|
||||
conn
|
||||
|> put_flash(:error, "Could not find API Key to delete")
|
||||
|> redirect(to: "/settings#api-keys")
|
||||
end
|
||||
end
|
||||
|
||||
def delete_me(conn, _params) do
|
||||
Plausible.Auth.delete_user(conn.assigns.current_user)
|
||||
|
||||
redirect(conn, to: Routes.auth_path(conn, :logout))
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,78 @@
|
||||
defmodule PlausibleWeb.TwoFactor do
|
||||
@moduledoc """
|
||||
Functions for managing session data related to two-factor
|
||||
authentication.
|
||||
"""
|
||||
|
||||
import Plug.Conn
|
||||
|
||||
alias Plausible.Auth
|
||||
|
||||
@remember_2fa_cookie "remember_2fa"
|
||||
@remember_2fa_days 30
|
||||
@remember_2fa_seconds @remember_2fa_days * 24 * 60 * 60
|
||||
|
||||
@session_2fa_cookie "session_2fa"
|
||||
@session_2fa_seconds 5 * 60
|
||||
|
||||
@spec set_2fa_user(Plug.Conn.t(), Auth.User.t()) :: Plug.Conn.t()
|
||||
def set_2fa_user(conn, %Auth.User{} = user) do
|
||||
put_resp_cookie(conn, @session_2fa_cookie, %{current_2fa_user_id: user.id},
|
||||
encrypt: true,
|
||||
max_age: @session_2fa_seconds,
|
||||
same_site: "Lax"
|
||||
)
|
||||
end
|
||||
|
||||
@spec get_2fa_user(Plug.Conn.t()) :: {:ok, Auth.User.t()} | {:error, :not_found}
|
||||
def get_2fa_user(conn) do
|
||||
conn = fetch_cookies(conn, encrypted: [@session_2fa_cookie])
|
||||
session_2fa = conn.cookies[@session_2fa_cookie]
|
||||
|
||||
with id when is_integer(id) <- session_2fa[:current_2fa_user_id],
|
||||
%Auth.User{} = user <- Plausible.Users.with_subscription(id) do
|
||||
{:ok, user}
|
||||
else
|
||||
_ -> {:error, :not_found}
|
||||
end
|
||||
end
|
||||
|
||||
@spec clear_2fa_user(Plug.Conn.t()) :: Plug.Conn.t()
|
||||
def clear_2fa_user(conn) do
|
||||
delete_resp_cookie(conn, @session_2fa_cookie,
|
||||
encrypt: true,
|
||||
max_age: @session_2fa_seconds,
|
||||
same_site: "Lax"
|
||||
)
|
||||
end
|
||||
|
||||
@spec remember_2fa_days() :: non_neg_integer()
|
||||
def remember_2fa_days(), do: @remember_2fa_days
|
||||
|
||||
@spec remember_2fa?(Plug.Conn.t()) :: boolean()
|
||||
def remember_2fa?(conn) do
|
||||
conn = fetch_cookies(conn, signed: [@remember_2fa_cookie])
|
||||
|
||||
conn.cookies[@remember_2fa_cookie] == "true"
|
||||
end
|
||||
|
||||
@spec maybe_set_remember_2fa(Plug.Conn.t(), String.t() | nil) :: Plug.Conn.t()
|
||||
def maybe_set_remember_2fa(conn, "true") do
|
||||
put_resp_cookie(conn, @remember_2fa_cookie, "true",
|
||||
sign: true,
|
||||
max_age: @remember_2fa_seconds,
|
||||
same_site: "Lax"
|
||||
)
|
||||
end
|
||||
|
||||
def maybe_set_remember_2fa(conn, _), do: conn
|
||||
|
||||
@spec clear_remember_2fa(Plug.Conn.t()) :: Plug.Conn.t()
|
||||
def clear_remember_2fa(conn) do
|
||||
delete_resp_cookie(conn, @remember_2fa_cookie,
|
||||
sign: true,
|
||||
max_age: @remember_2fa_seconds,
|
||||
same_site: "Lax"
|
||||
)
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,73 @@
|
||||
defmodule PlausibleWeb.UserView do
|
||||
use PlausibleWeb, :view
|
||||
require Plausible.Billing.Subscription.Status
|
||||
alias Plausible.Billing.{Plans, Subscription}
|
||||
|
||||
def base_domain do
|
||||
PlausibleWeb.Endpoint.host()
|
||||
end
|
||||
|
||||
def plausible_url do
|
||||
PlausibleWeb.Endpoint.url()
|
||||
end
|
||||
|
||||
def subscription_quota(subscription, options \\ [])
|
||||
|
||||
def subscription_quota(nil, _options), do: "Free trial"
|
||||
|
||||
def subscription_quota(subscription, options) do
|
||||
subscription
|
||||
|> Plausible.Billing.Quota.monthly_pageview_limit()
|
||||
|> PlausibleWeb.StatsView.large_number_format()
|
||||
|> then(fn quota ->
|
||||
if Keyword.get(options, :format) == :long,
|
||||
do: "#{quota} pageviews",
|
||||
else: quota
|
||||
end)
|
||||
end
|
||||
|
||||
def subscription_interval(subscription) do
|
||||
Plans.subscription_interval(subscription)
|
||||
end
|
||||
|
||||
def format_invoices(invoice_list) do
|
||||
Enum.map(invoice_list, fn invoice ->
|
||||
%{
|
||||
date:
|
||||
invoice["payout_date"] |> Date.from_iso8601!() |> Timex.format!("{Mshort} {D}, {YYYY}"),
|
||||
amount: (invoice["amount"] / 1) |> :erlang.float_to_binary(decimals: 2),
|
||||
currency: invoice["currency"] |> PlausibleWeb.BillingView.present_currency(),
|
||||
url: invoice["receipt_url"]
|
||||
}
|
||||
end)
|
||||
end
|
||||
|
||||
def delimit_integer(number) do
|
||||
Integer.to_charlist(number)
|
||||
|> :lists.reverse()
|
||||
|> delimit_integer([])
|
||||
|> String.Chars.to_string()
|
||||
end
|
||||
|
||||
defp delimit_integer([a, b, c, d | tail], acc) do
|
||||
delimit_integer([d | tail], [",", c, b, a | acc])
|
||||
end
|
||||
|
||||
defp delimit_integer(list, acc) do
|
||||
:lists.reverse(list) ++ acc
|
||||
end
|
||||
|
||||
@spec present_subscription_status(Subscription.Status.status()) :: String.t()
|
||||
def present_subscription_status(Subscription.Status.active()), do: "Active"
|
||||
def present_subscription_status(Subscription.Status.past_due()), do: "Past due"
|
||||
def present_subscription_status(Subscription.Status.deleted()), do: "Cancelled"
|
||||
def present_subscription_status(Subscription.Status.paused()), do: "Paused"
|
||||
def present_subscription_status(status), do: status
|
||||
|
||||
@spec subscription_colors(Subscription.Status.status()) :: String.t()
|
||||
def subscription_colors(Subscription.Status.active()), do: "bg-green-100 text-green-800"
|
||||
def subscription_colors(Subscription.Status.past_due()), do: "bg-yellow-100 text-yellow-800"
|
||||
def subscription_colors(Subscription.Status.paused()), do: "bg-red-100 text-red-800"
|
||||
def subscription_colors(Subscription.Status.deleted()), do: "bg-red-100 text-red-800"
|
||||
def subscription_colors(_), do: ""
|
||||
end
|
||||
@ -0,0 +1,16 @@
|
||||
defmodule PlausibleWeb.GoogleAuthControllerTest do
|
||||
use PlausibleWeb.ConnCase, async: true
|
||||
|
||||
describe "GET /auth/google/callback" do
|
||||
test "shows error and redirects back to settings when authentication fails", %{conn: conn} do
|
||||
site = insert(:site)
|
||||
callback_params = %{"error" => "access_denied", "state" => "[#{site.id},\"import\"]"}
|
||||
conn = get(conn, Routes.google_auth_path(conn, :google_auth_callback), callback_params)
|
||||
|
||||
assert redirected_to(conn, 302) == Routes.site_path(conn, :settings_general, site.domain)
|
||||
|
||||
assert Phoenix.Flash.get(conn.assigns.flash, :error) =~
|
||||
"unable to authenticate your Google Analytics"
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,772 @@
|
||||
defmodule PlausibleWeb.UserControllerTest do
|
||||
use PlausibleWeb.ConnCase, async: true
|
||||
use Bamboo.Test
|
||||
use Plausible.Repo
|
||||
|
||||
import Plausible.Test.Support.HTML
|
||||
|
||||
alias Plausible.Auth
|
||||
alias Plausible.Billing.Subscription
|
||||
|
||||
require Logger
|
||||
require Plausible.Billing.Subscription.Status
|
||||
|
||||
@v3_plan_id "749355"
|
||||
@v4_plan_id "857097"
|
||||
@configured_enterprise_plan_paddle_plan_id "123"
|
||||
|
||||
describe "GET /activate" do
|
||||
setup [:create_user, :log_in]
|
||||
|
||||
test "if user does not have a code: prompts user to request activation code", %{conn: conn} do
|
||||
conn = get(conn, "/activate")
|
||||
|
||||
assert html_response(conn, 200) =~ "Request activation code"
|
||||
end
|
||||
|
||||
test "if user does have a code: prompts user to enter the activation code from their email",
|
||||
%{conn: conn} do
|
||||
conn =
|
||||
post(conn, "/activate/request-code")
|
||||
|> get("/activate")
|
||||
|
||||
assert html_response(conn, 200) =~ "Please enter the 4-digit code we sent to"
|
||||
end
|
||||
end
|
||||
|
||||
describe "POST /activate/request-code" do
|
||||
setup [:create_user, :log_in]
|
||||
|
||||
test "generates an activation pin for user account", %{conn: conn, user: user} do
|
||||
post(conn, "/activate/request-code")
|
||||
|
||||
assert code = Repo.get_by(Auth.EmailActivationCode, user_id: user.id)
|
||||
|
||||
assert code.user_id == user.id
|
||||
refute Auth.EmailVerification.expired?(code)
|
||||
end
|
||||
|
||||
test "regenerates an activation pin even if there's one already", %{conn: conn, user: user} do
|
||||
five_minutes_ago =
|
||||
NaiveDateTime.utc_now()
|
||||
|> Timex.shift(minutes: -5)
|
||||
|> NaiveDateTime.truncate(:second)
|
||||
|
||||
{:ok, verification} = Auth.EmailVerification.issue_code(user, five_minutes_ago)
|
||||
|
||||
post(conn, "/activate/request-code")
|
||||
|
||||
assert new_verification = Repo.get_by(Auth.EmailActivationCode, user_id: user.id)
|
||||
|
||||
assert verification.id == new_verification.id
|
||||
assert verification.user_id == new_verification.user_id
|
||||
# this actually has a chance to fail 1 in 8999 runs
|
||||
# but at the same time it's good to have a confirmation
|
||||
# that it indeed generates a new code
|
||||
if verification.code == new_verification.code do
|
||||
Logger.warning(
|
||||
"Congratulations! You you have hit 1 in 8999 chance of the same " <>
|
||||
"email verification code repeating twice in a row!"
|
||||
)
|
||||
end
|
||||
|
||||
assert NaiveDateTime.compare(verification.issued_at, new_verification.issued_at) == :lt
|
||||
end
|
||||
|
||||
test "sends activation email to user", %{conn: conn, user: user} do
|
||||
post(conn, "/activate/request-code")
|
||||
|
||||
assert_delivered_email_matches(%{to: [{_, user_email}], subject: subject})
|
||||
assert user_email == user.email
|
||||
assert subject =~ "is your Plausible email verification code"
|
||||
end
|
||||
|
||||
test "redirects user to /activate", %{conn: conn} do
|
||||
conn = post(conn, "/activate/request-code")
|
||||
|
||||
assert redirected_to(conn, 302) == "/activate"
|
||||
end
|
||||
end
|
||||
|
||||
describe "POST /activate" do
|
||||
setup [:create_user, :log_in]
|
||||
|
||||
test "with wrong pin - reloads the form with error", %{conn: conn} do
|
||||
conn = post(conn, "/activate", %{code: "1234"})
|
||||
|
||||
assert html_response(conn, 200) =~ "Incorrect activation code"
|
||||
end
|
||||
|
||||
test "with expired pin - reloads the form with error", %{conn: conn, user: user} do
|
||||
one_day_ago =
|
||||
NaiveDateTime.utc_now()
|
||||
|> Timex.shift(days: -1)
|
||||
|> NaiveDateTime.truncate(:second)
|
||||
|
||||
{:ok, verification} = Auth.EmailVerification.issue_code(user, one_day_ago)
|
||||
|
||||
conn = post(conn, "/activate", %{code: verification.code})
|
||||
|
||||
assert html_response(conn, 200) =~ "Code is expired, please request another one"
|
||||
end
|
||||
|
||||
test "marks the user account as active", %{conn: conn, user: user} do
|
||||
Repo.update!(Auth.User.changeset(user, %{email_verified: false}))
|
||||
post(conn, "/activate/request-code")
|
||||
|
||||
verification = Repo.get_by!(Auth.EmailActivationCode, user_id: user.id)
|
||||
|
||||
conn = post(conn, "/activate", %{code: verification.code})
|
||||
user = Repo.get_by(Auth.User, id: user.id)
|
||||
|
||||
assert user.email_verified
|
||||
assert redirected_to(conn) == "/sites/new"
|
||||
end
|
||||
|
||||
test "redirects to /sites if user has invitation", %{conn: conn, user: user} do
|
||||
site = insert(:site)
|
||||
insert(:invitation, inviter: build(:user), site: site, email: user.email)
|
||||
Repo.update!(Auth.User.changeset(user, %{email_verified: false}))
|
||||
post(conn, "/activate/request-code")
|
||||
|
||||
verification = Repo.get_by!(Auth.EmailActivationCode, user_id: user.id)
|
||||
|
||||
conn = post(conn, "/activate", %{code: verification.code})
|
||||
|
||||
assert redirected_to(conn) == "/sites"
|
||||
end
|
||||
|
||||
test "removes used up verification code", %{conn: conn, user: user} do
|
||||
Repo.update!(Auth.User.changeset(user, %{email_verified: false}))
|
||||
post(conn, "/activate/request-code")
|
||||
|
||||
verification = Repo.get_by!(Auth.EmailActivationCode, user_id: user.id)
|
||||
|
||||
post(conn, "/activate", %{code: verification.code})
|
||||
|
||||
refute Repo.get_by(Auth.EmailActivationCode, user_id: user.id)
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET /settings" do
|
||||
setup [:create_user, :log_in]
|
||||
|
||||
test "shows the form", %{conn: conn} do
|
||||
conn = get(conn, "/settings")
|
||||
assert resp = html_response(conn, 200)
|
||||
assert resp =~ "Change account name"
|
||||
assert resp =~ "Change email address"
|
||||
end
|
||||
|
||||
test "shows subscription", %{conn: conn, user: user} do
|
||||
insert(:subscription, paddle_plan_id: "558018", user: user)
|
||||
conn = get(conn, "/settings")
|
||||
assert html_response(conn, 200) =~ "10k pageviews"
|
||||
assert html_response(conn, 200) =~ "monthly billing"
|
||||
end
|
||||
|
||||
test "shows yearly subscription", %{conn: conn, user: user} do
|
||||
insert(:subscription, paddle_plan_id: "590752", user: user)
|
||||
conn = get(conn, "/settings")
|
||||
assert html_response(conn, 200) =~ "100k pageviews"
|
||||
assert html_response(conn, 200) =~ "yearly billing"
|
||||
end
|
||||
|
||||
test "shows free subscription", %{conn: conn, user: user} do
|
||||
insert(:subscription, paddle_plan_id: "free_10k", user: user)
|
||||
conn = get(conn, "/settings")
|
||||
assert html_response(conn, 200) =~ "10k pageviews"
|
||||
assert html_response(conn, 200) =~ "N/A billing"
|
||||
end
|
||||
|
||||
test "shows enterprise plan subscription", %{conn: conn, user: user} do
|
||||
insert(:subscription, paddle_plan_id: "123", user: user)
|
||||
|
||||
configure_enterprise_plan(user)
|
||||
|
||||
conn = get(conn, "/settings")
|
||||
assert html_response(conn, 200) =~ "20M pageviews"
|
||||
assert html_response(conn, 200) =~ "yearly billing"
|
||||
end
|
||||
|
||||
test "shows current enterprise plan subscription when user has a new one to upgrade to", %{
|
||||
conn: conn,
|
||||
user: user
|
||||
} do
|
||||
insert(:subscription,
|
||||
paddle_plan_id: @configured_enterprise_plan_paddle_plan_id,
|
||||
user: user
|
||||
)
|
||||
|
||||
insert(:enterprise_plan,
|
||||
paddle_plan_id: "1234",
|
||||
user: user,
|
||||
monthly_pageview_limit: 10_000_000,
|
||||
billing_interval: :yearly
|
||||
)
|
||||
|
||||
configure_enterprise_plan(user)
|
||||
|
||||
conn = get(conn, "/settings")
|
||||
assert html_response(conn, 200) =~ "20M pageviews"
|
||||
assert html_response(conn, 200) =~ "yearly billing"
|
||||
end
|
||||
|
||||
test "renders two links to '/billing/choose-plan` with the text 'Upgrade'", %{conn: conn} do
|
||||
doc =
|
||||
get(conn, "/settings")
|
||||
|> html_response(200)
|
||||
|
||||
upgrade_link_1 = find(doc, "#monthly-quota-box a")
|
||||
upgrade_link_2 = find(doc, "#upgrade-link-2")
|
||||
|
||||
assert text(upgrade_link_1) == "Upgrade"
|
||||
assert text_of_attr(upgrade_link_1, "href") == Routes.billing_path(conn, :choose_plan)
|
||||
|
||||
assert text(upgrade_link_2) == "Upgrade"
|
||||
assert text_of_attr(upgrade_link_2, "href") == Routes.billing_path(conn, :choose_plan)
|
||||
end
|
||||
|
||||
test "renders a link to '/billing/choose-plan' with the text 'Change plan' + cancel link", %{
|
||||
conn: conn,
|
||||
user: user
|
||||
} do
|
||||
insert(:subscription, paddle_plan_id: @v3_plan_id, user: user)
|
||||
|
||||
doc =
|
||||
get(conn, "/settings")
|
||||
|> html_response(200)
|
||||
|
||||
refute element_exists?(doc, "#upgrade-link-2")
|
||||
assert doc =~ "Cancel my subscription"
|
||||
|
||||
change_plan_link = find(doc, "#monthly-quota-box a")
|
||||
|
||||
assert text(change_plan_link) == "Change plan"
|
||||
assert text_of_attr(change_plan_link, "href") == Routes.billing_path(conn, :choose_plan)
|
||||
end
|
||||
|
||||
test "/billing/choose-plan link does not show up when enterprise subscription is past_due", %{
|
||||
conn: conn,
|
||||
user: user
|
||||
} do
|
||||
configure_enterprise_plan(user)
|
||||
|
||||
insert(:subscription,
|
||||
user: user,
|
||||
status: Subscription.Status.past_due(),
|
||||
paddle_plan_id: @configured_enterprise_plan_paddle_plan_id
|
||||
)
|
||||
|
||||
doc =
|
||||
conn
|
||||
|> get(Routes.user_path(conn, :user_settings))
|
||||
|> html_response(200)
|
||||
|
||||
refute element_exists?(doc, "#upgrade-or-change-plan-link")
|
||||
end
|
||||
|
||||
test "/billing/choose-plan link does not show up when enterprise subscription is paused", %{
|
||||
conn: conn,
|
||||
user: user
|
||||
} do
|
||||
configure_enterprise_plan(user)
|
||||
|
||||
insert(:subscription,
|
||||
user: user,
|
||||
status: Subscription.Status.paused(),
|
||||
paddle_plan_id: @configured_enterprise_plan_paddle_plan_id
|
||||
)
|
||||
|
||||
doc =
|
||||
conn
|
||||
|> get(Routes.user_path(conn, :user_settings))
|
||||
|> html_response(200)
|
||||
|
||||
refute element_exists?(doc, "#upgrade-or-change-plan-link")
|
||||
end
|
||||
|
||||
test "renders two links to '/billing/choose-plan' with the text 'Upgrade' for a configured enterprise plan",
|
||||
%{conn: conn, user: user} do
|
||||
configure_enterprise_plan(user)
|
||||
|
||||
doc =
|
||||
get(conn, "/settings")
|
||||
|> html_response(200)
|
||||
|
||||
upgrade_link_1 = find(doc, "#monthly-quota-box a")
|
||||
upgrade_link_2 = find(doc, "#upgrade-link-2")
|
||||
|
||||
assert text(upgrade_link_1) == "Upgrade"
|
||||
|
||||
assert text_of_attr(upgrade_link_1, "href") ==
|
||||
Routes.billing_path(conn, :choose_plan)
|
||||
|
||||
assert text(upgrade_link_2) == "Upgrade"
|
||||
|
||||
assert text_of_attr(upgrade_link_2, "href") ==
|
||||
Routes.billing_path(conn, :choose_plan)
|
||||
end
|
||||
|
||||
test "links to '/billing/choose-plan' with the text 'Change plan' for a configured enterprise plan with an existing subscription + renders cancel button",
|
||||
%{conn: conn, user: user} do
|
||||
insert(:subscription, paddle_plan_id: @v3_plan_id, user: user)
|
||||
|
||||
configure_enterprise_plan(user)
|
||||
|
||||
doc =
|
||||
get(conn, "/settings")
|
||||
|> html_response(200)
|
||||
|
||||
refute element_exists?(doc, "#upgrade-link-2")
|
||||
assert doc =~ "Cancel my subscription"
|
||||
|
||||
change_plan_link = find(doc, "#monthly-quota-box a")
|
||||
|
||||
assert text(change_plan_link) == "Change plan"
|
||||
|
||||
assert text_of_attr(change_plan_link, "href") ==
|
||||
Routes.billing_path(conn, :choose_plan)
|
||||
end
|
||||
|
||||
test "renders cancelled subscription notice", %{conn: conn, user: user} do
|
||||
insert(:subscription,
|
||||
paddle_plan_id: @v4_plan_id,
|
||||
user: user,
|
||||
status: :deleted,
|
||||
next_bill_date: ~D[2023-01-01]
|
||||
)
|
||||
|
||||
notice_text =
|
||||
get(conn, "/settings")
|
||||
|> html_response(200)
|
||||
|> text_of_element("#global-subscription-cancelled-notice")
|
||||
|
||||
assert notice_text =~ "Subscription cancelled"
|
||||
assert notice_text =~ "Upgrade your subscription to get access to your stats again"
|
||||
end
|
||||
|
||||
test "renders cancelled subscription notice with some subscription days still left", %{
|
||||
conn: conn,
|
||||
user: user
|
||||
} do
|
||||
insert(:subscription,
|
||||
paddle_plan_id: @v4_plan_id,
|
||||
user: user,
|
||||
status: :deleted,
|
||||
next_bill_date: Timex.shift(Timex.today(), days: 10)
|
||||
)
|
||||
|
||||
notice_text =
|
||||
get(conn, "/settings")
|
||||
|> html_response(200)
|
||||
|> text_of_element("#global-subscription-cancelled-notice")
|
||||
|
||||
assert notice_text =~ "Subscription cancelled"
|
||||
assert notice_text =~ "You have access to your stats until"
|
||||
assert notice_text =~ "Upgrade your subscription to make sure you don't lose access"
|
||||
end
|
||||
|
||||
test "renders cancelled subscription notice with a warning about losing grandfathering", %{
|
||||
conn: conn,
|
||||
user: user
|
||||
} do
|
||||
insert(:subscription,
|
||||
paddle_plan_id: @v3_plan_id,
|
||||
user: user,
|
||||
status: :deleted,
|
||||
next_bill_date: Timex.shift(Timex.today(), days: 10)
|
||||
)
|
||||
|
||||
notice_text =
|
||||
get(conn, "/settings")
|
||||
|> html_response(200)
|
||||
|> text_of_element("#global-subscription-cancelled-notice")
|
||||
|
||||
assert notice_text =~ "Subscription cancelled"
|
||||
assert notice_text =~ "You have access to your stats until"
|
||||
|
||||
assert notice_text =~
|
||||
"by letting your subscription expire, you lose access to our grandfathered terms"
|
||||
end
|
||||
|
||||
test "shows invoices for subscribed user", %{conn: conn, user: user} do
|
||||
insert(:subscription,
|
||||
paddle_plan_id: "558018",
|
||||
paddle_subscription_id: "redundant",
|
||||
user: user
|
||||
)
|
||||
|
||||
conn = get(conn, "/settings")
|
||||
assert html_response(conn, 200) =~ "Dec 24, 2020"
|
||||
assert html_response(conn, 200) =~ "€11.11"
|
||||
assert html_response(conn, 200) =~ "Nov 24, 2020"
|
||||
assert html_response(conn, 200) =~ "$22.00"
|
||||
end
|
||||
|
||||
test "shows 'something went wrong' on failed invoice request'", %{conn: conn, user: user} do
|
||||
insert(:subscription,
|
||||
paddle_plan_id: "558018",
|
||||
paddle_subscription_id: "invalid_subscription_id",
|
||||
user: user
|
||||
)
|
||||
|
||||
conn = get(conn, "/settings")
|
||||
assert html_response(conn, 200) =~ "Invoices"
|
||||
assert html_response(conn, 200) =~ "Something went wrong"
|
||||
end
|
||||
|
||||
test "does not show invoice section for a user with no subscription", %{conn: conn} do
|
||||
conn = get(conn, "/settings")
|
||||
refute html_response(conn, 200) =~ "Invoices"
|
||||
end
|
||||
|
||||
test "does not show invoice section for a free subscription", %{conn: conn, user: user} do
|
||||
Plausible.Billing.Subscription.free(%{user_id: user.id, currency_code: "EUR"})
|
||||
|> Repo.insert!()
|
||||
|
||||
conn = get(conn, "/settings")
|
||||
refute html_response(conn, 200) =~ "Invoices"
|
||||
end
|
||||
end
|
||||
|
||||
describe "PUT /settings" do
|
||||
setup [:create_user, :log_in]
|
||||
|
||||
test "updates user record", %{conn: conn, user: user} do
|
||||
put(conn, "/settings", %{"user" => %{"name" => "New name"}})
|
||||
|
||||
user = Plausible.Repo.get(Auth.User, user.id)
|
||||
assert user.name == "New name"
|
||||
end
|
||||
|
||||
test "does not allow setting non-profile fields", %{conn: conn, user: user} do
|
||||
expiry_date = user.trial_expiry_date
|
||||
|
||||
assert %Date{} = expiry_date
|
||||
|
||||
put(conn, "/settings", %{
|
||||
"user" => %{"name" => "New name", "trial_expiry_date" => "2023-07-14"}
|
||||
})
|
||||
|
||||
assert Repo.reload!(user).trial_expiry_date == expiry_date
|
||||
end
|
||||
|
||||
test "redirects user to /settings", %{conn: conn} do
|
||||
conn = put(conn, "/settings", %{"user" => %{"name" => "New name"}})
|
||||
|
||||
assert redirected_to(conn, 302) == "/settings"
|
||||
end
|
||||
|
||||
test "renders form with error if form validations fail", %{conn: conn} do
|
||||
conn = put(conn, "/settings", %{"user" => %{"name" => ""}})
|
||||
|
||||
assert html_response(conn, 200) =~ "can't be blank"
|
||||
end
|
||||
end
|
||||
|
||||
describe "PUT /settings/email" do
|
||||
setup [:create_user, :log_in]
|
||||
|
||||
test "updates email and forces reverification", %{conn: conn, user: user} do
|
||||
password = "very-long-very-secret-123"
|
||||
|
||||
user
|
||||
|> Auth.User.set_password(password)
|
||||
|> Repo.update!()
|
||||
|
||||
assert user.email_verified
|
||||
|
||||
conn =
|
||||
put(conn, "/settings/email", %{
|
||||
"user" => %{"email" => "new" <> user.email, "password" => password}
|
||||
})
|
||||
|
||||
assert redirected_to(conn, 302) == Routes.user_path(conn, :activate)
|
||||
|
||||
updated_user = Repo.reload!(user)
|
||||
|
||||
assert updated_user.email == "new" <> user.email
|
||||
assert updated_user.previous_email == user.email
|
||||
refute updated_user.email_verified
|
||||
|
||||
assert_delivered_email_matches(%{to: [{_, user_email}], subject: subject})
|
||||
assert user_email == updated_user.email
|
||||
assert subject =~ "is your Plausible email verification code"
|
||||
end
|
||||
|
||||
test "renders form with error on no fields filled", %{conn: conn} do
|
||||
conn = put(conn, "/settings/email", %{"user" => %{}})
|
||||
|
||||
assert html_response(conn, 200) =~ "can't be blank"
|
||||
end
|
||||
|
||||
test "renders form with error on invalid password", %{conn: conn, user: user} do
|
||||
conn =
|
||||
put(conn, "/settings/email", %{
|
||||
"user" => %{"password" => "invalid", "email" => "new" <> user.email}
|
||||
})
|
||||
|
||||
assert html_response(conn, 200) =~ "is invalid"
|
||||
end
|
||||
|
||||
test "renders form with error on already taken email", %{conn: conn, user: user} do
|
||||
other_user = insert(:user)
|
||||
|
||||
password = "very-long-very-secret-123"
|
||||
|
||||
user
|
||||
|> Auth.User.set_password(password)
|
||||
|> Repo.update!()
|
||||
|
||||
conn =
|
||||
put(conn, "/settings/email", %{
|
||||
"user" => %{"password" => password, "email" => other_user.email}
|
||||
})
|
||||
|
||||
assert html_response(conn, 200) =~ "has already been taken"
|
||||
end
|
||||
|
||||
test "renders form with error when email is identical with the current one", %{
|
||||
conn: conn,
|
||||
user: user
|
||||
} do
|
||||
password = "very-long-very-secret-123"
|
||||
|
||||
user
|
||||
|> Auth.User.set_password(password)
|
||||
|> Repo.update!()
|
||||
|
||||
conn =
|
||||
put(conn, "/settings/email", %{
|
||||
"user" => %{"password" => password, "email" => user.email}
|
||||
})
|
||||
|
||||
assert html_response(conn, 200) =~ "can't be the same"
|
||||
end
|
||||
end
|
||||
|
||||
describe "POST /settings/email/cancel" do
|
||||
setup [:create_user, :log_in]
|
||||
|
||||
test "cancels email reverification in progress", %{conn: conn, user: user} do
|
||||
user =
|
||||
user
|
||||
|> Ecto.Changeset.change(
|
||||
email_verified: false,
|
||||
email: "new" <> user.email,
|
||||
previous_email: user.email
|
||||
)
|
||||
|> Repo.update!()
|
||||
|
||||
conn = post(conn, "/settings/email/cancel")
|
||||
|
||||
assert redirected_to(conn, 302) ==
|
||||
Routes.user_path(conn, :user_settings) <> "#change-email-address"
|
||||
|
||||
updated_user = Repo.reload!(user)
|
||||
|
||||
assert updated_user.email_verified
|
||||
assert updated_user.email == user.previous_email
|
||||
refute updated_user.previous_email
|
||||
end
|
||||
|
||||
test "fails to cancel reverification when previous email is already retaken", %{
|
||||
conn: conn,
|
||||
user: user
|
||||
} do
|
||||
user =
|
||||
user
|
||||
|> Ecto.Changeset.change(
|
||||
email_verified: false,
|
||||
email: "new" <> user.email,
|
||||
previous_email: user.email
|
||||
)
|
||||
|> Repo.update!()
|
||||
|
||||
_other_user = insert(:user, email: user.previous_email)
|
||||
|
||||
conn = post(conn, "/settings/email/cancel")
|
||||
|
||||
assert redirected_to(conn, 302) == Routes.user_path(conn, :activate_form)
|
||||
|
||||
assert Phoenix.Flash.get(conn.assigns.flash, :error) =~
|
||||
"Could not cancel email update"
|
||||
end
|
||||
|
||||
test "crashes when previous email is empty on cancel (should not happen)", %{
|
||||
conn: conn,
|
||||
user: user
|
||||
} do
|
||||
user
|
||||
|> Ecto.Changeset.change(
|
||||
email_verified: false,
|
||||
email: "new" <> user.email,
|
||||
previous_email: nil
|
||||
)
|
||||
|> Repo.update!()
|
||||
|
||||
assert_raise RuntimeError, ~r/Previous email is empty for user/, fn ->
|
||||
post(conn, "/settings/email/cancel")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "DELETE /me" do
|
||||
setup [:create_user, :log_in, :create_new_site]
|
||||
use Plausible.Repo
|
||||
|
||||
test "deletes the user", %{conn: conn, user: user, site: site} do
|
||||
Repo.insert_all("intro_emails", [
|
||||
%{
|
||||
user_id: user.id,
|
||||
timestamp: NaiveDateTime.utc_now()
|
||||
}
|
||||
])
|
||||
|
||||
Repo.insert_all("feedback_emails", [
|
||||
%{
|
||||
user_id: user.id,
|
||||
timestamp: NaiveDateTime.utc_now()
|
||||
}
|
||||
])
|
||||
|
||||
Repo.insert_all("create_site_emails", [
|
||||
%{
|
||||
user_id: user.id,
|
||||
timestamp: NaiveDateTime.utc_now()
|
||||
}
|
||||
])
|
||||
|
||||
Repo.insert_all("check_stats_emails", [
|
||||
%{
|
||||
user_id: user.id,
|
||||
timestamp: NaiveDateTime.utc_now()
|
||||
}
|
||||
])
|
||||
|
||||
Repo.insert_all("sent_renewal_notifications", [
|
||||
%{
|
||||
user_id: user.id,
|
||||
timestamp: NaiveDateTime.utc_now()
|
||||
}
|
||||
])
|
||||
|
||||
insert(:google_auth, site: site, user: user)
|
||||
insert(:subscription, user: user, status: Subscription.Status.deleted())
|
||||
insert(:subscription, user: user, status: Subscription.Status.active())
|
||||
|
||||
conn = delete(conn, "/me")
|
||||
assert redirected_to(conn) == "/logout"
|
||||
assert Repo.reload(site) == nil
|
||||
assert Repo.reload(user) == nil
|
||||
assert Repo.all(Plausible.Billing.Subscription) == []
|
||||
end
|
||||
|
||||
test "deletes sites that the user owns", %{conn: conn, user: user, site: owner_site} do
|
||||
viewer_site = insert(:site)
|
||||
insert(:site_membership, site: viewer_site, user: user, role: "viewer")
|
||||
|
||||
delete(conn, "/me")
|
||||
|
||||
assert Repo.get(Plausible.Site, viewer_site.id)
|
||||
refute Repo.get(Plausible.Site, owner_site.id)
|
||||
end
|
||||
end
|
||||
|
||||
describe "POST /settings/api-keys" do
|
||||
setup [:create_user, :log_in]
|
||||
import Ecto.Query
|
||||
|
||||
test "can create an API key", %{conn: conn, user: user} do
|
||||
insert(:site, memberships: [build(:site_membership, user: user, role: "owner")])
|
||||
|
||||
conn =
|
||||
post(conn, "/settings/api-keys", %{
|
||||
"api_key" => %{
|
||||
"user_id" => user.id,
|
||||
"name" => "all your code are belong to us",
|
||||
"key" => "swordfish"
|
||||
}
|
||||
})
|
||||
|
||||
key = Auth.ApiKey |> where(user_id: ^user.id) |> Repo.one()
|
||||
assert conn.status == 302
|
||||
assert key.name == "all your code are belong to us"
|
||||
end
|
||||
|
||||
test "cannot create a duplicate API key", %{conn: conn, user: user} do
|
||||
insert(:site, memberships: [build(:site_membership, user: user, role: "owner")])
|
||||
|
||||
conn =
|
||||
post(conn, "/settings/api-keys", %{
|
||||
"api_key" => %{
|
||||
"user_id" => user.id,
|
||||
"name" => "all your code are belong to us",
|
||||
"key" => "swordfish"
|
||||
}
|
||||
})
|
||||
|
||||
conn2 =
|
||||
post(conn, "/settings/api-keys", %{
|
||||
"api_key" => %{
|
||||
"user_id" => user.id,
|
||||
"name" => "all your code are belong to us",
|
||||
"key" => "swordfish"
|
||||
}
|
||||
})
|
||||
|
||||
assert html_response(conn2, 200) =~ "has already been taken"
|
||||
end
|
||||
|
||||
test "can't create api key into another site", %{conn: conn, user: me} do
|
||||
_my_site = insert(:site, memberships: [build(:site_membership, user: me, role: "owner")])
|
||||
|
||||
other_user = insert(:user)
|
||||
|
||||
_other_site =
|
||||
insert(:site, memberships: [build(:site_membership, user: other_user, role: "owner")])
|
||||
|
||||
conn =
|
||||
post(conn, "/settings/api-keys", %{
|
||||
"api_key" => %{
|
||||
"user_id" => other_user.id,
|
||||
"name" => "all your code are belong to us",
|
||||
"key" => "swordfish"
|
||||
}
|
||||
})
|
||||
|
||||
assert conn.status == 302
|
||||
|
||||
refute Auth.ApiKey |> where(user_id: ^other_user.id) |> Repo.one()
|
||||
end
|
||||
end
|
||||
|
||||
describe "DELETE /settings/api-keys/:id" do
|
||||
setup [:create_user, :log_in]
|
||||
alias Auth.ApiKey
|
||||
|
||||
test "can't delete api key that doesn't belong to me", %{conn: conn} do
|
||||
other_user = insert(:user)
|
||||
insert(:site, memberships: [build(:site_membership, user: other_user, role: "owner")])
|
||||
|
||||
assert {:ok, %ApiKey{} = api_key} =
|
||||
%ApiKey{user_id: other_user.id}
|
||||
|> ApiKey.changeset(%{"name" => "other user's key"})
|
||||
|> Repo.insert()
|
||||
|
||||
conn = delete(conn, "/settings/api-keys/#{api_key.id}")
|
||||
assert Phoenix.Flash.get(conn.assigns.flash, :error) == "Could not find API Key to delete"
|
||||
assert Repo.get(ApiKey, api_key.id)
|
||||
end
|
||||
end
|
||||
|
||||
defp configure_enterprise_plan(user) do
|
||||
insert(:enterprise_plan,
|
||||
paddle_plan_id: @configured_enterprise_plan_paddle_plan_id,
|
||||
user: user,
|
||||
monthly_pageview_limit: 20_000_000,
|
||||
billing_interval: :yearly
|
||||
)
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue