Skip to content

Commit

Permalink
skip verification in ce (#4604)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslandoga authored Sep 23, 2024
1 parent 1b91224 commit 71fa387
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 26 deletions.
98 changes: 72 additions & 26 deletions lib/plausible_web/live/verification.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
defmodule PlausibleWeb.Live.Verification do
@moduledoc """
LiveView coordinating the site verification process.
Onboarding new sites, renders a standalone component.
Onboarding new sites, renders a standalone component.
Embedded modal variant is available for general site settings.
"""
use Plausible
use PlausibleWeb, :live_view
use Phoenix.HTML

Expand All @@ -28,13 +29,14 @@ defmodule PlausibleWeb.Live.Verification do
private = Map.get(socket.private.connect_info, :private, %{})

super_admin? = Plausible.Auth.is_super_admin?(socket.assigns.current_user)
has_pageviews? = has_pageviews?(site)

socket =
assign(socket,
site: site,
super_admin?: super_admin?,
domain: domain,
has_pageviews?: has_pageviews?(site),
has_pageviews?: has_pageviews?,
component: @component,
installation_type: params["installation_type"],
report_to: self(),
Expand All @@ -45,28 +47,65 @@ defmodule PlausibleWeb.Live.Verification do
attempts: 0
)

if connected?(socket) do
launch_delayed(socket)
on_ee do
if connected?(socket) do
launch_delayed(socket)
end
end

on_ee do
{:ok, socket}
else
# on CE we skip the verification process and instead,
# we just wait for the first pageview to be recorded
socket =
if has_pageviews? do
redirect_to_stats(socket)
else
schedule_pageviews_check(socket)
end

{:ok, socket}
end
end

{:ok, socket}
on_ee do
def render(assigns) do
~H"""
<PlausibleWeb.Components.FlowProgress.render flow={@flow} current_step="Verify installation" />
<.live_component
module={@component}
installation_type={@installation_type}
domain={@domain}
id="verification-standalone"
attempts={@attempts}
flow={@flow}
awaiting_first_pageview?={not @has_pageviews?}
super_admin?={@super_admin?}
/>
"""
end
else
def render(assigns) do
~H"""
<PlausibleWeb.Components.FlowProgress.render flow={@flow} current_step="Verify installation" />
<.awaiting_pageviews />
"""
end
end

def render(assigns) do
~H"""
<PlausibleWeb.Components.FlowProgress.render flow={@flow} current_step="Verify installation" />
<.live_component
module={@component}
installation_type={@installation_type}
domain={@domain}
id="verification-standalone"
attempts={@attempts}
flow={@flow}
awaiting_first_pageview?={not @has_pageviews?}
super_admin?={@super_admin?}
/>
"""
on_ce do
defp awaiting_pageviews(assigns) do
~H"""
<PlausibleWeb.Components.Generic.focus_box>
<div class="flex items-center">
<div class="block pulsating-circle"></div>
<p class="ml-8">Awaiting your first pageview …</p>
</div>
</PlausibleWeb.Components.Generic.focus_box>
"""
end
end

def handle_event("launch-verification", _, socket) do
Expand Down Expand Up @@ -116,7 +155,7 @@ defmodule PlausibleWeb.Live.Verification do
interpretation = Checks.interpret_diagnostics(state)

if not socket.assigns.has_pageviews? do
Process.send_after(self(), :check_pageviews, socket.assigns.delay * 2)
schedule_pageviews_check(socket)
end

update_component(socket,
Expand All @@ -132,17 +171,24 @@ defmodule PlausibleWeb.Live.Verification do
def handle_info(:check_pageviews, socket) do
socket =
if has_pageviews?(socket.assigns.site) do
redirect(socket,
external: Routes.stats_url(PlausibleWeb.Endpoint, :stats, socket.assigns.domain, [])
)
redirect_to_stats(socket)
else
Process.send_after(self(), :check_pageviews, socket.assigns.delay * 2)
socket
schedule_pageviews_check(socket)
end

{:noreply, socket}
end

defp schedule_pageviews_check(socket) do
Process.send_after(self(), :check_pageviews, socket.assigns.delay * 2)
socket
end

defp redirect_to_stats(socket) do
stats_url = Routes.stats_url(PlausibleWeb.Endpoint, :stats, socket.assigns.domain, [])
redirect(socket, external: stats_url)
end

defp reset_component(socket) do
update_component(socket,
message: "We're visiting your site to ensure that everything is working",
Expand Down
32 changes: 32 additions & 0 deletions test/plausible_web/live/verification_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ defmodule PlausibleWeb.Live.VerificationTest do
@heading ~s|#progress-indicator h3|

describe "GET /:domain" do
@tag :ee_only
test "static verification screen renders", %{conn: conn, site: site} do
resp =
get(conn, conn |> no_slowdown() |> get("/#{site.domain}") |> redirected_to)
Expand All @@ -23,9 +24,19 @@ defmodule PlausibleWeb.Live.VerificationTest do

assert resp =~ "Verifying your installation"
end

@tag :ce_build_only
test "static verification screen renders (ce)", %{conn: conn, site: site} do
resp =
get(conn, conn |> no_slowdown() |> get("/#{site.domain}") |> redirected_to)
|> html_response(200)

assert resp =~ "Awaiting your first pageview …"
end
end

describe "LiveView" do
@tag :ee_only
test "LiveView mounts", %{conn: conn, site: site} do
stub_fetch_body(200, "")
stub_installation()
Expand All @@ -38,6 +49,13 @@ defmodule PlausibleWeb.Live.VerificationTest do
"We're visiting your site to ensure that everything is working"
end

@tag :ce_build_only
test "LiveView mounts (ce)", %{conn: conn, site: site} do
{_, html} = get_lv(conn, site)
assert html =~ "Awaiting your first pageview …"
end

@tag :ee_only
test "eventually verifies installation", %{conn: conn, site: site} do
stub_fetch_body(200, source(site.domain))
stub_installation()
Expand All @@ -59,6 +77,7 @@ defmodule PlausibleWeb.Live.VerificationTest do
assert html =~ "Awaiting your first pageview"
end

@tag :ee_only
test "won't await first pageview if site has pageviews", %{conn: conn, site: site} do
populate_stats(site, [
build(:pageview)
Expand Down Expand Up @@ -106,6 +125,19 @@ defmodule PlausibleWeb.Live.VerificationTest do
assert_redirect(lv, "http://localhost:8000/#{URI.encode_www_form(site.domain)}/")
end

@tag :ce_build_only
test "will redirect when first pageview arrives (ce)", %{conn: conn, site: site} do
{:ok, lv} = kick_off_live_verification(conn, site)

html = render(lv)
assert text(html) =~ "Awaiting your first pageview …"

populate_stats(site, [build(:pageview)])

assert_redirect(lv, "http://localhost:8000/#{URI.encode_www_form(site.domain)}/")
end

@tag :ee_only
test "eventually fails to verify installation", %{conn: conn, site: site} do
stub_fetch_body(200, "")
stub_installation(200, plausible_installed(false))
Expand Down

0 comments on commit 71fa387

Please sign in to comment.