Rest of the fsckn owl

pull/3559/head
Adam Rutkowski 3 years ago
parent 667fe1d2bb
commit eacb5f6ce4

@ -209,13 +209,7 @@ defmodule Plausible.Billing.Quota do
stats_api_usage = from a in Plausible.Auth.ApiKey, where: a.user_id == ^user.id
queries =
if Plausible.Release.selfhost?() do
[
{Props, props_usage_query},
{RevenueGoals, revenue_goals_usage},
{StatsAPI, stats_api_usage}
]
else
on_full_build do
funnels_usage_query =
from f in "funnels",
inner_join: os in subquery(owned_sites_query(user)),
@ -227,6 +221,12 @@ defmodule Plausible.Billing.Quota do
{RevenueGoals, revenue_goals_usage},
{StatsAPI, stats_api_usage}
]
else
[
{Props, props_usage_query},
{RevenueGoals, revenue_goals_usage},
{StatsAPI, stats_api_usage}
]
end
Enum.reduce(queries, [], fn {feature, query}, acc ->

@ -10,6 +10,7 @@ defmodule Plausible.Plugins.API.Token do
It is prefixed with a plain text identifier allowing source scanning
for leaked secrets.
"""
use Plausible
use Ecto.Schema
import Ecto.Changeset
@ -63,10 +64,15 @@ defmodule Plausible.Plugins.API.Token do
can scan repositories for accidental secret commits.
"""
def prefix() do
case {Plausible.Release.selfhost?(), Application.get_env(:plausible, :environment)} do
{true, _} -> "plausible-plugin-selfhost"
{false, "prod"} -> "plausible-plugin"
{false, env} -> "plausible-plugin-#{env}"
on_full_build do
env = Application.get_env(:plausible, :environment)
case env do
"prod" -> "plausible-plugin"
env -> "plausible-plugin-#{env}"
end
else
"plausible-plugin-selfhost"
end
end

@ -50,7 +50,7 @@ defmodule Plausible.Site.Memberships.AcceptInvitation do
@spec accept_invitation(String.t(), Auth.User.t(), Keyword.t()) ::
{:ok, Site.Membership.t()} | {:error, :invitation_not_found | Ecto.Changeset.t()}
def accept_invitation(invitation_id, user, opts \\ []) do
selfhost? = Keyword.get(opts, :selfhost?, Plausible.Release.selfhost?())
selfhost? = Keyword.get(opts, :selfhost?, small_build?())
with {:ok, invitation} <- Invitations.find_for_user(invitation_id, user) do
membership = get_or_create_membership(invitation, user)

@ -1,4 +1,5 @@
defmodule Plausible.Release do
use Plausible
use Plausible.Repo
require Logger
@ -10,13 +11,12 @@ defmodule Plausible.Release do
:ecto
]
@spec selfhost? :: boolean
def selfhost? do
Application.fetch_env!(@app, :is_selfhost)
end
def should_be_first_launch? do
selfhost?() and not (_has_users? = Repo.exists?(Plausible.Auth.User))
on_full_build do
false
else
not (_has_users? = Repo.exists?(Plausible.Auth.User))
end
end
def migrate do

@ -10,7 +10,7 @@ defmodule PlausibleWeb.Router do
plug :fetch_live_flash
plug :put_secure_browser_headers
plug PlausibleWeb.Plugs.NoRobots
plug PlausibleWeb.FirstLaunchPlug, redirect_to: "/register"
on_full_build(do: nil, else: plug(PlausibleWeb.FirstLaunchPlug, redirect_to: "/register"))
plug PlausibleWeb.SessionTimeoutPlug, timeout_after_seconds: @two_weeks_in_seconds
plug PlausibleWeb.AuthPlug
plug PlausibleWeb.LastSeenPlug

@ -3,9 +3,9 @@
<%= form_for @conn, "/activate", [class: "w-full max-w-lg mx-auto bg-white dark:bg-gray-800 shadow-md rounded px-8 py-6 mb-4 mt-8"], fn f -> %>
<h2 class="text-xl font-black dark:text-gray-100">
<%= if @has_any_memberships? do %>
Verify your email address
Verify your email address
<% else %>
Activate your account
Activate your account
<% end %>
</h2>
@ -15,7 +15,17 @@
<div class="mt-12 flex items-stretch flex-grow">
<div>
<%= text_input f, :code, class: "tracking-widest font-medium shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-36 px-8 border-gray-300 dark:border-gray-500 rounded-l-md dark:text-gray-200 dark:bg-gray-900", oninput: "this.value=this.value.replace(/[^0-9]/g, ''); if (this.value.length >= 4) document.getElementById('submit').focus()", onclick: "this.select();", maxlength: "4", placeholder: "••••", style: "letter-spacing: 10px;", required: "required" %>
<%= text_input(f, :code,
class:
"tracking-widest font-medium shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-36 px-8 border-gray-300 dark:border-gray-500 rounded-l-md dark:text-gray-200 dark:bg-gray-900",
oninput:
"this.value=this.value.replace(/[^0-9]/g, ''); if (this.value.length >= 4) document.getElementById('submit').focus()",
onclick: "this.select();",
maxlength: "4",
placeholder: "••••",
style: "letter-spacing: 10px;",
required: "required"
) %>
</div>
<button id="submit" class="button rounded-l-none">Activate &rarr;</button>
</div>
@ -26,11 +36,27 @@
</div>
<ol class="list-disc text-xs text-gray-500 leading-tight space-y-1 mt-1">
<li>Check your spam folder</li>
<li><%= link("Send a new code", class: "underline text-indigo-600", to: "/activate/request-code", method: :post) %> to <%= @conn.assigns[:current_user].email %></li>
<li>
<%= link("Send a new code",
class: "underline text-indigo-600",
to: "/activate/request-code",
method: :post
) %> to <%= @conn.assigns[:current_user].email %>
</li>
<%= if full_build?() do %>
<li><a class="underline text-indigo-600" href="https://plausible.io/contact">Contact us</a> if the problem persists</li>
<li>
<a class="underline text-indigo-600" href="https://plausible.io/contact">
Contact us
</a>
if the problem persists
</li>
<% else %>
<li>Ask on our <%= link("community-supported forum", to: "https://github.com/plausible/analytics/discussions", class: "text-indigo-600 underline" ) %></li>
<li>
Ask on our <%= link("community-supported forum",
to: "https://github.com/plausible/analytics/discussions",
class: "text-indigo-600 underline"
) %>
</li>
<% end %>
</ol>
<div class="mt-4 text-sm dark:text-gray-100">
@ -38,13 +64,22 @@
</div>
<ul class="list-disc text-xs text-gray-500 leading-tight mt-1">
<%= if @has_any_memberships? do %>
<li>
<%= link("Change email back to", class: "underline text-indigo-600", to: "/settings/email/cancel", method: "post") %> to <%= @conn.assigns[:current_user].previous_email %>
</li>
<li>
<%= link("Change email back to",
class: "underline text-indigo-600",
to: "/settings/email/cancel",
method: "post"
) %> to <%= @conn.assigns[:current_user].previous_email %>
</li>
<% else %>
<li>
<%= link("Delete this account", class: "underline text-indigo-600", to: "/me?redirect=/register", method: "delete", data: [confirm: "Deleting your account cannot be reversed. Are you sure?"]) %> and start over
</li>
<li>
<%= link("Delete this account",
class: "underline text-indigo-600",
to: "/me?redirect=/register",
method: "delete",
data: [confirm: "Deleting your account cannot be reversed. Are you sure?"]
) %> and start over
</li>
<% end %>
</ul>
<% end %>
@ -58,8 +93,11 @@
<%= error_tag(assigns, :error) %>
<%= button("Request activation code", to: "/activate/request-code", method: :post, class: "button mt-12") %>
<%= button("Request activation code",
to: "/activate/request-code",
method: :post,
class: "button mt-12"
) %>
</div>
<% end %>

@ -1,19 +1,16 @@
<%= if @success do %>
Your Google Analytics import has completed successfully. The Plausible dashboard for <%= @site.domain %> now contains historical imported data from <%= date_format(@site.imported_data.start_date) %> to <%= date_format(@site.imported_data.end_date) %>
<br /><br />
<%= link("Click here", to: @link) %> to view your dashboard.
Your Google Analytics import has completed successfully. The Plausible dashboard for <%= @site.domain %> now contains historical imported data from <%= date_format(
@site.imported_data.start_date
) %> to <%= date_format(@site.imported_data.end_date) %>
<br /><br />
<%= link("Click here", to: @link) %> to view your dashboard.
<% else %>
Unfortunately, your Google Analytics import for <%= @site.domain %> did not complete successfully. Sorry about that!
<br /> <br />
This may be due to the user metric setting in your Google Analytics account. We found that changing the user metric setting is necessary to get the correct data out of the Google API.
<br /> <br />
Please log into your Google Analytics account and go to Admin -> Property Settings -> User Analysis. Then make sure that "Enable User Metric in Reporting" is set to OFF. Then try importing again. You can switch the setting back after the import.
<br /> <br />
If you've already changed that setting, then it is something else:
<br />
<br /> <br /> If you've already changed that setting, then it is something else: <br />
<ul>
<li>
@ -24,9 +21,7 @@
</li>
</ul>
<br />
If all of the above is fine, please try to do the import once again. Sometimes the Google Analytics API just randomly returns empty data. It's intermittent and random. Trying to do the import again may return what you need.
<%= if full_build?() do %>
<br /> <br />
Please reply to this email to let us know if you're still experiencing issues with the import.

@ -12,14 +12,14 @@
<div class="ml-3">
<h3 class="text-sm font-medium text-yellow-800 dark:text-yellow-400">
There has been a server error.
<%= if !assigns[:selfhosted?] do %>
<%= if full_build?() do %>
But don't worry, we're on it!
<% end %>
</h3>
<div class="mt-2 text-sm text-yellow-700 dark:text-yellow-300">
<p>
<%= if assigns[:trace_id] && !assigns[:selfhosted?] do %>
<%= if assigns[:trace_id] && full_build?() do %>
If you would like to help, tell us what you were trying to do. Our development team will receive your report.
<% else %>
We have been notified.
@ -30,7 +30,7 @@
</div>
</div>
<%= if !assigns[:selfhosted?] do %>
<%= if full_build?() do %>
<%= if assigns[:trace_id] do %>
<div class="my-6">
<%= form_for :error, Routes.error_report_path(PlausibleWeb.Endpoint, :submit_error_report), fn f -> %>

@ -1,9 +1,10 @@
defmodule PlausibleWeb.ErrorView do
use Plausible
use PlausibleWeb, :view
def render("500.json", %{conn: %{private: %{PlausibleWeb.Plugins.API.Router => _}}}) do
contact_support_note =
if not Plausible.Release.selfhost?() do
on_full_build do
"If the problem persists please contact support@plausible.io"
end
@ -32,23 +33,21 @@ defmodule PlausibleWeb.ErrorView do
def render(<<"5", _error_5xx::binary-size(2), ".html">>, assigns) do
current_user = assigns[:current_user]
selfhosted? = Plausible.Release.selfhost?()
last_event = Sentry.get_last_event_id_and_source()
case {selfhosted?, current_user, last_event} do
{false, current_user, {event_id, :plug}}
case {current_user, last_event} do
{current_user, {event_id, :plug}}
when is_binary(event_id) and not is_nil(current_user) ->
opts = %{
trace_id: event_id,
user_name: current_user.name,
user_email: current_user.email,
selfhosted?: selfhosted?
user_email: current_user.email
}
render("server_error.html", Map.merge(opts, assigns))
_ ->
render("server_error.html", Map.put(assigns, :selfhosted?, selfhosted?))
render("server_error.html", assigns)
end
end

@ -3,6 +3,7 @@ defmodule Plausible.Plugins.API.TokenTest do
alias Plausible.Plugins.API.Token
@tag :full_build_only
test "basic token properties" do
t1 = Token.generate()
t2 = Token.generate()
@ -24,20 +25,23 @@ defmodule Plausible.Plugins.API.TokenTest do
end
describe "prefix/0" do
test "default prefix" do
@tag :full_build_only
test "default prefix - full build" do
assert Token.prefix() == "plausible-plugin-test"
end
@tag :small_build_only
test "selfhosted prefix" do
patch_env(:is_selfhost, true)
assert Token.prefix() == "plausible-plugin-selfhost"
end
@tag :full_build_only
test "prod prefix" do
patch_env(:environment, "prod")
assert Token.prefix() == "plausible-plugin"
end
@tag :full_build_only
test "staging prefix" do
patch_env(:environment, "staging")
assert Token.prefix() == "plausible-plugin-staging"

@ -9,7 +9,6 @@ defmodule Plausible.Plugins.API.TokensTest do
test "generates and stores the token" do
site = insert(:site)
assert {:ok, %Token{} = token, raw} = Tokens.create(site, "My test token")
assert <<"plausible-plugin-test-", _::binary-size(40)>> = raw
from_db = Repo.get(Token, token.id)

@ -1,30 +1,30 @@
defmodule Plausible.ReleaseTest do
use Plausible.DataCase
use Plausible.DataCase, async: true
alias Plausible.{Release, Auth}
import ExUnit.CaptureIO
describe "should_be_first_launch?/0" do
@tag :small_build_only
test "returns true when self-hosted and no users" do
patch_env(:is_selfhost, true)
refute Repo.exists?(Auth.User)
assert Release.should_be_first_launch?()
end
@tag :full_build_only
test "returns false when not self-hosted and has no users" do
patch_env(:is_selfhost, false)
refute Repo.exists?(Auth.User)
refute Release.should_be_first_launch?()
end
@tag :full_build_only
test "returns false when not self-hosted and has users" do
insert(:user)
patch_env(:is_selfhost, false)
refute Release.should_be_first_launch?()
end
@tag :small_build_only
test "returns false when self-hosted and has users" do
insert(:user)
patch_env(:is_selfhost, true)
refute Release.should_be_first_launch?()
end
end

@ -367,6 +367,7 @@ defmodule Plausible.Site.Memberships.AcceptInvitationTest do
refute Repo.reload(invitation)
end
@tag :full_build_only
test "locks the site if the new owner has no active subscription or trial" do
existing_owner = insert(:user)
@ -447,6 +448,7 @@ defmodule Plausible.Site.Memberships.AcceptInvitationTest do
assert Repo.reload!(site).locked
end
@tag :full_build_only
test "sets user's trial expiry date to yesterday if they don't have one" do
existing_owner = insert(:user)
@ -473,6 +475,7 @@ defmodule Plausible.Site.Memberships.AcceptInvitationTest do
assert Repo.reload!(site).locked
end
@tag :full_build_only
test "ends grace period and sends an email about it if new owner is past grace period" do
existing_owner = insert(:user)

@ -2,9 +2,8 @@ defmodule PlausibleWeb.AdminAuthControllerTest do
use PlausibleWeb.ConnCase
alias Plausible.Release
setup_patch_env(:is_selfhost, true)
describe "GET /" do
@describetag :small_build_only
test "disable registration", %{conn: conn} do
prevent_first_launch()
patch_config(disable_registration: true)

@ -2,6 +2,14 @@ defmodule PlausibleWeb.AuthController.LogsTest do
use PlausibleWeb.ConnCase
import ExUnit.CaptureLog
if small_build?() do
setup do
# insert some user to prevent first launch redirect
insert(:user)
:ok
end
end
describe "POST /login" do
setup do
patch_env(:log_failed_login_attempts, true)

@ -15,6 +15,14 @@ defmodule PlausibleWeb.AuthControllerTest do
setup :verify_on_exit!
if small_build?() do
setup do
# insert some user to prevent first launch redirect
insert(:user)
:ok
end
end
@v3_plan_id "749355"
@v4_plan_id "857097"
@configured_enterprise_plan_paddle_plan_id "123"

@ -4,6 +4,14 @@ defmodule PlausibleWeb.AvatarControllerTest do
import Mox
setup :verify_on_exit!
if small_build?() do
setup do
# insert some user to prevent first launch redirect
insert(:user)
:ok
end
end
describe "GET /avatar/:hash" do
test "proxies the request to gravatar", %{conn: conn} do
expect(

@ -1,5 +1,7 @@
defmodule PlausibleWeb.ErrorReportControllerTest do
use PlausibleWeb.ConnCase, async: true
@moduletag :full_build_only
use Bamboo.Test
import Phoenix.View

@ -1,6 +1,14 @@
defmodule PlausibleWeb.PageControllerTest do
use PlausibleWeb.ConnCase, async: true
if small_build?() do
setup do
# insert some user to prevent first launch redirect
insert(:user)
:ok
end
end
describe "GET /" do
test "shows landing page when user not authenticated", %{conn: conn} do
assert conn |> get("/") |> html_response(200) =~ "Welcome to Plausible Analytics!"

@ -44,6 +44,7 @@ defmodule PlausibleWeb.StatsControllerTest do
end
test "can not view stats of a private website", %{conn: conn} do
_ = insert(:user)
conn = get(conn, "/test-site.com")
assert html_response(conn, 404) =~ "There's nothing here"
end

@ -2,6 +2,14 @@ defmodule PlausibleWeb.UnsubscribeControllerTest do
use PlausibleWeb.ConnCase, async: true
use Plausible.Repo
if small_build?() do
setup do
# insert some user to prevent first launch redirect
insert(:user)
:ok
end
end
describe "GET /sites/:website/weekly-report/unsubscribe" do
test "removes a recipient from the weekly report without them having to log in", %{conn: conn} do
site = insert(:site)

@ -1,5 +1,6 @@
defmodule PlausibleWeb.FirstLaunchPlugTest do
use PlausibleWeb.ConnCase
@moduletag :small_build_only
import Plug.Test
alias PlausibleWeb.FirstLaunchPlug
@ -25,7 +26,6 @@ defmodule PlausibleWeb.FirstLaunchPlugTest do
refute conn.halted
# even when it's the first launch
patch_env(:is_selfhost, true)
assert Release.should_be_first_launch?()
conn = conn("GET", "/register")
@ -34,6 +34,7 @@ defmodule PlausibleWeb.FirstLaunchPlugTest do
end
test "no-op when not first launch" do
insert(:user)
refute Release.should_be_first_launch?()
conn = conn("GET", "/sites")
conn = FirstLaunchPlug.call(conn, @opts)
@ -41,7 +42,6 @@ defmodule PlausibleWeb.FirstLaunchPlugTest do
end
test "redirects to :redirect_to when first launch" do
patch_env(:is_selfhost, true)
assert Release.should_be_first_launch?()
conn = conn("GET", "/sites")
@ -53,21 +53,10 @@ defmodule PlausibleWeb.FirstLaunchPlugTest do
describe "first launch plug in :browser pipeline" do
test "redirects to /register on first launch", %{conn: conn} do
patch_env(:is_selfhost, true)
assert Release.should_be_first_launch?()
conn = get(conn, "/")
assert redirected_to(conn) == "/register"
end
test "no-op when not first launch", %{conn: conn} do
patch_env(:is_selfhost, false)
refute Release.should_be_first_launch?()
# gets redirected to login by auth plugs
# "first launch" doesn't interfere
conn = get(conn, "/sites")
assert redirected_to(conn) == "/login"
end
end
end

@ -19,6 +19,7 @@ defmodule PlausibleWeb.ConnCase do
quote do
# Import conveniences for testing with connections
use Plausible.TestUtils
use Plausible
import Plug.Conn
import Phoenix.ConnTest
alias PlausibleWeb.Router.Helpers, as: Routes

@ -6,7 +6,9 @@ FunWithFlags.enable(:window_time_on_page)
Ecto.Adapters.SQL.Sandbox.mode(Plausible.Repo, :manual)
if Mix.env() == :small_test do
IO.puts("Test mode: SMALL")
ExUnit.configure(exclude: [:slow, :full_build_only])
else
IO.puts("Test mode: FULL")
ExUnit.configure(exclude: [:slow, :small_build_only])
end

Loading…
Cancel
Save