Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Shared links (#29)

Uku Taht5f62025176a4
diff --git a/assets/css/app.css b/assets/css/app.css
index c3d92b8374..0ecc15b446 100644
--- a/assets/css/app.css
+++ b/assets/css/app.css
@@ -217,6 +217,14 @@ a {
height: 1em;
width: 1em;
transform: translateY(0.15em);
+ overflow: visible;
+}
+
+.feather-sm {
+ height: 1em;
+ width: 1em;
+ transform: translateY(0.05em);
+ overflow: visible;
}
.table-striped tbody tr:nth-child(odd) {
diff --git a/lib/plausible/site/shared_link.ex b/lib/plausible/site/shared_link.ex
new file mode 100644
index 0000000000..c8985aad5a
--- /dev/null
+++ b/lib/plausible/site/shared_link.ex
@@ -0,0 +1,30 @@
+defmodule Plausible.Site.SharedLink do
+ use Ecto.Schema
+ import Ecto.Changeset
+
+ schema "shared_links" do
+ belongs_to :site, Plausible.Site
+ field :slug, :string
+ field :password_hash, :string
+ field :password, :string, virtual: true
+
+ timestamps()
+ end
+
+ def changeset(link, attrs \\ %{}) do
+ link
+ |> cast(attrs, [:slug, :password])
+ |> validate_required([:slug])
+ |> unique_constraint(:slug)
+ |> hash_password()
+ end
+
+ defp hash_password(link) do
+ case link.changes[:password] do
+ nil -> link
+ password ->
+ hash = Plausible.Auth.Password.hash(password)
+ change(link, password_hash: hash)
+ end
+ end
+end
diff --git a/lib/plausible_web/controllers/site_controller.ex b/lib/plausible_web/controllers/site_controller.ex
index 8b50e033cb..caa81e0491 100644
--- a/lib/plausible_web/controllers/site_controller.ex
+++ b/lib/plausible_web/controllers/site_controller.ex
@@ -83,6 +83,7 @@ defmodule PlausibleWeb.SiteController do
weekly_report = Repo.get_by(Plausible.Site.WeeklyReport, site_id: site.id)
monthly_report = Repo.get_by(Plausible.Site.MonthlyReport, site_id: site.id)
goals = Goals.for_site(site.domain)
+ shared_links = Repo.all(from l in Plausible.Site.SharedLink, where: l.site_id == ^site.id)
conn
|> assign(:skip_plausible_tracking, true)
@@ -92,6 +93,7 @@ defmodule PlausibleWeb.SiteController do
monthly_report: monthly_report,
search_console_domains: search_console_domains,
goals: goals,
+ shared_links: shared_links,
changeset: Plausible.Site.changeset(site, %{})
)
end
@@ -258,6 +260,35 @@ defmodule PlausibleWeb.SiteController do
|> redirect(to: "/#{site.domain}/settings#email-reports")
end
+ def new_shared_link(conn, %{"website" => website}) do
+ site = Sites.get_for_user!(conn.assigns[:current_user].id, website)
+ changeset = Plausible.Site.SharedLink.changeset(%Plausible.Site.SharedLink{}, %{})
+
+ render(conn, "new_shared_link.html", site: site, changeset: changeset, layout: {PlausibleWeb.LayoutView, "focus.html"})
+ end
+
+ def create_shared_link(conn, %{"website" => website, "shared_link" => link}) do
+ site = Sites.get_for_user!(conn.assigns[:current_user].id, website)
+ changes = Plausible.Site.SharedLink.changeset(%Plausible.Site.SharedLink{
+ site_id: site.id,
+ slug: Nanoid.generate()
+ }, link)
+
+ case Repo.insert(changes) do
+ {:ok, _created} ->
+ redirect(conn, to: "/#{site.domain}/settings#visibility")
+ {:error, changeset} ->
+ render(conn, "new_shared_link.html", site: site, changeset: changeset, layout: {PlausibleWeb.LayoutView, "focus.html"})
+ end
+ end
+
+ def delete_shared_link(conn, %{"website" => website, "slug" => slug}) do
+ Repo.get_by(Plausible.Site.SharedLink, slug: slug)
+ |> Repo.delete!
+
+ redirect(conn, to: "/#{website}/settings#visibility")
+ end
+
defp insert_site(user_id, params) do
site_changeset = Plausible.Site.changeset(%Plausible.Site{}, params)
diff --git a/lib/plausible_web/controllers/stats_controller.ex b/lib/plausible_web/controllers/stats_controller.ex
index 9f98c6541b..d73b885945 100644
--- a/lib/plausible_web/controllers/stats_controller.ex
+++ b/lib/plausible_web/controllers/stats_controller.ex
@@ -59,11 +59,62 @@ defmodule PlausibleWeb.StatsController do
end
end
+ def shared_link(conn, %{"slug" => slug}) do
+ shared_link = Repo.get_by(Plausible.Site.SharedLink, slug: slug)
+ |> Repo.preload(:site)
+
+ if shared_link do
+ if shared_link.password_hash do
+ render(conn, "shared_link_password.html", link: shared_link, layout: {PlausibleWeb.LayoutView, "focus.html"})
+ else
+ shared_link_auth_success(conn, shared_link)
+ end
+ else
+ render_error(conn, 404)
+ end
+ end
+
+ def authenticate_shared_link(conn, %{"slug" => slug, "password" => password}) do
+ shared_link = Repo.get_by(Plausible.Site.SharedLink, slug: slug)
+ |> Repo.preload(:site)
+
+ if shared_link do
+ if Plausible.Auth.Password.match?(password, shared_link.password_hash) do
+ shared_link_auth_success(conn, shared_link)
+ else
+ render(conn, "shared_link_password.html", link: shared_link, error: "Incorrect password. Please try again.", layout: {PlausibleWeb.LayoutView, "focus.html"})
+ end
+ else
+ render_error(conn, 404)
+ end
+ end
+
+ defp shared_link_auth_success(conn, shared_link) do
+ site_session_key = "authorized_site__" <> shared_link.site.domain
+
+ conn
+ |> put_session(site_session_key, %{
+ id: shared_link.site.id,
+ domain: shared_link.site.domain,
+ timezone: shared_link.site.timezone,
+ valid_until: Timex.now() |> Timex.shift(minutes: 30) |> DateTime.to_unix()
+ })
+ |> redirect(to: "/#{shared_link.site.domain}")
+ end
+
defp current_user_can_access?(_conn, %Plausible.Site{public: true}) do
true
end
defp current_user_can_access?(conn, site) do
+ site_session_key = "authorized_site__" <> site.domain
+ site_session = get_session(conn, site_session_key)
+ valid_site_session = site_session && site_session[:valid_until] > DateTime.to_unix(Timex.now())
+
+ valid_site_session || current_user_is_owner?(conn, site)
+ end
+
+ defp current_user_is_owner?(conn, site) do
case conn.assigns[:current_user] do
nil -> false
user -> Plausible.Sites.is_owner?(user.id, site)
diff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.ex
index 3d5806d816..cb73832ecd 100644
--- a/lib/plausible_web/router.ex
+++ b/lib/plausible_web/router.ex
@@ -109,9 +109,14 @@ defmodule PlausibleWeb.Router do
post "/sites/:website/monthly-report/recipients", SiteController, :add_monthly_report_recipient
delete "/sites/:website/monthly-report/recipients/:recipient", SiteController, :remove_monthly_report_recipient
+ get "/sites/:website/shared-links/new", SiteController, :new_shared_link
+ post "/sites/:website/shared-links", SiteController, :create_shared_link
+ delete "/sites/:website/shared-links/:slug", SiteController, :delete_shared_link
+
get "/sites/:website/weekly-report/unsubscribe", UnsubscribeController, :weekly_report
get "/sites/:website/monthly-report/unsubscribe", UnsubscribeController, :monthly_report
+
get "/:website/snippet", SiteController, :add_snippet
get "/:website/settings", SiteController, :settings
get "/:website/goals", SiteController, :goals
@@ -122,6 +127,8 @@ defmodule PlausibleWeb.Router do
put "/:website/settings/google", SiteController, :update_google_auth
delete "/:website", SiteController, :delete_site
+ get "/share/:slug", StatsController, :shared_link
+ post "/share/:slug/authenticate", StatsController, :authenticate_shared_link
get "/:website/visitors.csv", StatsController, :csv_export
get "/:website/*path", StatsController, :stats
end
diff --git a/lib/plausible_web/templates/layout/_svg_icons.html.eex b/lib/plausible_web/templates/layout/_svg_icons.html.eex
index 51cbfbe86d..2f0c42eabd 100644
--- a/lib/plausible_web/templates/layout/_svg_icons.html.eex
+++ b/lib/plausible_web/templates/layout/_svg_icons.html.eex
@@ -18,12 +18,14 @@
<symbol id="feather-chevron-right" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-chevron-right"><polyline points="9 18 15 12 9 6"></polyline></symbol>
- <symbol id="feather-x-circle" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"></circle><line x1="15" y1="9" x2="9" y2="15"></line><line x1="9" y1="9" x2="15" y2="15"></line></symbol>
-
<symbol id="feather-maximize" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M8 3H5a2 2 0 0 0-2 2v3m18 0V5a2 2 0 0 0-2-2h-3m0 18h3a2 2 0 0 0 2-2v-3M3 16v3a2 2 0 0 0 2 2h3"/></symbol>
<symbol id="feather-download" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-download"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path><polyline points="7 10 12 15 17 10"></polyline><line x1="12" y1="15" x2="12" y2="3"></line></symbol>
<symbol id="feather-mail" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-mail"><path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"></path><polyline points="22,6 12,13 2,6"></polyline></symbol>
+
+ <symbol id="feather-copy" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-copy"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></symbol>
+
+ <symbol id="feather-trash-2" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-trash-2"><polyline points="3 6 5 6 21 6"></polyline><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path><line x1="10" y1="11" x2="10" y2="17"></line><line x1="14" y1="11" x2="14" y2="17"></line></symbol>
</defs>
</svg>
diff --git a/lib/plausible_web/templates/site/new_shared_link.html.eex b/lib/plausible_web/templates/site/new_shared_link.html.eex
new file mode 100644
index 0000000000..f9146fd670
--- /dev/null
+++ b/lib/plausible_web/templates/site/new_shared_link.html.eex
@@ -0,0 +1,14 @@
+<%= form_for @changeset, "/sites/#{@site.domain}/shared-links", [class: "max-w-sm w-full mx-auto bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4 mt-8"], fn f -> %>
+ <h2>New shared link</h2>
+ <div class="my-6">
+ Add a password or leave it blank so anyone with the link can see the stats.
+ Once the link is created, we cannot reveal the password. Please make sure you save it in a secure place.
+ </div>
+ <div class="my-6">
+ <%= label f, :password, "Password (optional)", class: "block text-sm font-bold" %>
+ <%= password_input f, :password, class: "transition mt-3 bg-grey-lighter appearance-none border border-transparent rounded w-full p-2 text-grey-darker leading-normal appearance-none focus:outline-none focus:bg-white focus:border-grey-light" %>
+ <%= error_tag f, :password %>
+ </div>
+
+ <%= submit "Create shared link", class: "button mt-4 w-full" %>
+<% end %>
diff --git a/lib/plausible_web/templates/site/settings.html.eex b/lib/plausible_web/templates/site/settings.html.eex
index 67b6ec31bd..fdf2d3d166 100644
--- a/lib/plausible_web/templates/site/settings.html.eex
+++ b/lib/plausible_web/templates/site/settings.html.eex
@@ -28,7 +28,7 @@
<% end %>
</div>
-<div class="max-w-md mx-auto bg-white shadow-md rounded rounded-t-none border-t-2 border-indigo-lightest px-8 pt-6 pb-8 mt-10">
+<div class="max-w-md mx-auto bg-white shadow-md rounded rounded-t-none border-t-2 border-indigo-lightest px-8 pt-6 pb-8 mt-10" id="visibility">
<div class="flex items-center justify-between">
<h2>Visibility</h2>
</div>
@@ -36,10 +36,12 @@
<div class="my-4 border-b border-grey-light"></div>
<%= if @site.public do %>
Stats for <%= @site.domain %> are currently <b>public</b>. Anyone with the following link can view the stats:
- <div class="w-full md:w-2/3">
- <input type="text" id="public-link" value="https://plausible.io/<%= @site.domain %>" class="transition bg-grey-lighter appearance-none border border-transparent rounded w-full p-2 text-grey-darker leading-normal appearance-none focus:outline-none focus:border-grey-light mt-4" />
- <a onclick="var input = document.getElementById('public-link'); input.focus(); input.select(); document.execCommand('copy');" href="javascript:void(0)" class="no-underline text-blue text-sm hover:underline">Copy to clipboard</a>
- </div>
+ <div class="relative text-sm mt-4">
+ <input type="text" id="public-link" value="https://plausible.io/<%= @site.domain %>" class="transition bg-grey-lighter appearance-none border border-transparent rounded w-full p-2 pr-16 text-grey-darker appearance-none focus:outline-none focus:border-grey-light" />
+ <a onclick="var input = document.getElementById('public-link'); input.focus(); input.select(); document.execCommand('copy');" href="javascript:void(0)" class="absolute pin-r text-indigo-darker font-bold p-2">
+ <svg class="feather-sm"><use xlink:href="#feather-copy" /></svg>
+ </a>
+ </div>
<%= button("Make stats private", to: "/sites/#{@site.domain}/make-private", method: "POST", class: "button mt-8") %>
<% else %>
<div class="text-grey-darker">
@@ -48,6 +50,31 @@
</div>
<%= button("Make stats public", to: "/sites/#{@site.domain}/make-public", method: "POST", class: "button mt-8") %>
<% end %>
+
+ <div class="mt-16">
+ <h2>Shared links</h2>
+
+ <div class="my-4 border-b border-grey-light"></div>
+
+ <div class="my-4">
+ You can share your stats privately by generating a shared link. The links are impossible to guess and
+ you can add password protection for extra security.
+ </div>
+
+ <%= for link <- @shared_links do %>
+ <div class="flex relative w-full mt-2 text-sm">
+ <input type="text" id="<%= link.slug %>" readonly="readonly" value="<%= shared_link_dest(link) %>" class="transition bg-grey-lighter appearance-none border border-transparent rounded rounded-r-none w-full p-2 text-grey-darker appearance-none focus:outline-none focus:border-grey-light" />
+ <button onclick="var input = document.getElementById('<%= link.slug %>'); input.focus(); input.select(); document.execCommand('copy');" href="javascript:void(0)" class="py-2 px-4 bg-grey-lighter text-indigo-darker rounded-none border-r border-grey-light">
+ <svg class="feather-sm"><use xlink:href="#feather-copy" /></svg>
+ </button>
+ <%= button(to: "/sites/#{@site.domain}/shared-links/#{link.slug}", method: :delete, class: "py-2 px-4 bg-grey-lighter text-red-dark rounded-l-none", data: [confirm: "Are you sure you want to delete this shared link? The stats will not be accessible with this link anymore."]) do %>
+ <svg class="feather-sm"><use xlink:href="#feather-trash-2" /></svg>
+ <% end %>
+ </div>
+ <% end %>
+ </div>
+
+ <%= link("+ New link", to: "/sites/#{@site.domain}/shared-links/new", class: "button mt-4") %>
</div>
<div class="max-w-md mx-auto bg-white shadow-md rounded rounded-t-none border-t-2 border-indigo-lightest px-8 pt-6 pb-8 mt-10">
diff --git a/lib/plausible_web/templates/stats/shared_link_password.html.eex b/lib/plausible_web/templates/stats/shared_link_password.html.eex
new file mode 100644
index 0000000000..eba0778e28
--- /dev/null
+++ b/lib/plausible_web/templates/stats/shared_link_password.html.eex
@@ -0,0 +1,17 @@
+<%= form_for @conn, "/share/#{@link.slug}/authenticate", [class: "max-w-sm w-full mx-auto bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4 mt-8"], fn f -> %>
+ <h2>Enter password</h2>
+ <div class="my-6">
+ This link is password-protected. Please enter the password to continue to the dashboard.
+ </div>
+
+ <div class="my-6">
+ <%= label f, :password, "Password", class: "block text-sm font-bold" %>
+ <%= password_input f, :password, class: "transition mt-3 bg-grey-lighter appearance-none border border-transparent rounded w-full p-2 text-grey-darker leading-normal appearance-none focus:outline-none focus:bg-white focus:border-grey-light" %>
+
+ <%= if @conn.assigns[:error] do %>
+ <div class="text-red text-xs italic mt-4"><%= @conn.assigns[:error] %></div>
+ <% end %>
+ </div>
+
+ <%= submit "Continue", class: "button mt-4 w-full" %>
+<% end %>
diff --git a/lib/plausible_web/views/site_view.ex b/lib/plausible_web/views/site_view.ex
index be6c4258bf..b00243fbf3 100644
--- a/lib/plausible_web/views/site_view.ex
+++ b/lib/plausible_web/views/site_view.ex
@@ -9,6 +9,10 @@ defmodule PlausibleWeb.SiteView do
name
end
+ def shared_link_dest(link) do
+ PlausibleWeb.Endpoint.url() <> "/share/" <> link.slug
+ end
+
def snippet() do
"""
<script>
diff --git a/mix.exs b/mix.exs
index f2889af83d..ff48c3159f 100644
--- a/mix.exs
+++ b/mix.exs
@@ -60,7 +60,8 @@ defmodule Plausible.MixProject do
{:joken, "~> 2.0"},
{:php_serializer, "~> 0.9.0"},
{:csv, "~> 2.3"},
- {:oauther, "~> 1.1"}
+ {:oauther, "~> 1.1"},
+ {:nanoid, "~> 2.0.2"}
]
end
diff --git a/mix.lock b/mix.lock
index 0d06ce4ee3..0f7a023e33 100644
--- a/mix.lock
+++ b/mix.lock
@@ -32,6 +32,7 @@
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm"},
"mime": {:hex, :mime, "1.3.1", "30ce04ab3175b6ad0bdce0035cba77bba68b813d523d1aac73d9781b4d193cf8", [:mix], [], "hexpm"},
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm"},
+ "nanoid": {:hex, :nanoid, "2.0.2", "f3f7b4bf103ab6667f22beb00b6315825ee3f30100dd2c93d534e5c02164e857", [:mix], [], "hexpm"},
"oauther": {:hex, :oauther, "1.1.1", "7d8b16167bb587ecbcddd3f8792beb9ec3e7b65c1f8ebd86b8dd25318d535752", [:mix], [], "hexpm"},
"parallel_stream": {:hex, :parallel_stream, "1.0.6", "b967be2b23f0f6787fab7ed681b4c45a215a81481fb62b01a5b750fa8f30f76c", [:mix], [], "hexpm"},
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm"},
diff --git a/priv/repo/migrations/20200122150130_add_shared_links.exs b/priv/repo/migrations/20200122150130_add_shared_links.exs
new file mode 100644
index 0000000000..a63bc6064e
--- /dev/null
+++ b/priv/repo/migrations/20200122150130_add_shared_links.exs
@@ -0,0 +1,13 @@
+defmodule Plausible.Repo.Migrations.AddSharedLinks do
+ use Ecto.Migration
+
+ def change do
+ create table(:shared_links) do
+ add :site_id, references(:sites, on_delete: :delete_all), null: false
+ add :slug, :string, null: false
+ add :password_hash, :string
+
+ timestamps()
+ end
+ end
+end
diff --git a/test/plausible_web/controllers/site_controller_test.exs b/test/plausible_web/controllers/site_controller_test.exs
index 8b72569cf9..f4a0b933e1 100644
--- a/test/plausible_web/controllers/site_controller_test.exs
+++ b/test/plausible_web/controllers/site_controller_test.exs
@@ -285,4 +285,51 @@ defmodule PlausibleWeb.SiteControllerTest do
assert report.recipients == []
end
end
+
+ describe "GET /sites/:website/shared-links/new" do
+ setup [:create_user, :log_in, :create_site]
+
+ test "shows form for new shared link", %{conn: conn, site: site} do
+ conn = get(conn, "/sites/#{site.domain}/shared-links/new")
+
+ assert html_response(conn, 200) =~ "New shared link"
+ end
+ end
+
+ describe "POSt /sites/:website/shared-links" do
+ setup [:create_user, :log_in, :create_site]
+
+ test "creates shared link without password", %{conn: conn, site: site} do
+ post(conn, "/sites/#{site.domain}/shared-links", %{"shared_link" => %{}})
+
+ link = Repo.one(Plausible.Site.SharedLink)
+
+ refute is_nil(link.slug)
+ assert is_nil(link.password_hash)
+ end
+
+ test "creates shared link with password", %{conn: conn, site: site} do
+ post(conn, "/sites/#{site.domain}/shared-links", %{
+ "shared_link" => %{"password" => "password"}
+ })
+
+ link = Repo.one(Plausible.Site.SharedLink)
+
+ refute is_nil(link.slug)
+ refute is_nil(link.password_hash)
+ end
+ end
+
+ describe "DELETE /sites/:website/shared-links/:slug" do
+ setup [:create_user, :log_in, :create_site]
+
+ test "shows form for new shared link", %{conn: conn, site: site} do
+ link = insert(:shared_link, site: site)
+
+ conn = delete(conn, "/sites/#{site.domain}/shared-links/#{link.slug}")
+
+ refute Repo.one(Plausible.Site.SharedLink)
+ assert redirected_to(conn, 302) =~ "/#{site.domain}/settings"
+ end
+ end
end
diff --git a/test/plausible_web/controllers/stats_controller_test.exs b/test/plausible_web/controllers/stats_controller_test.exs
index 372d74ab8b..df02f20b10 100644
--- a/test/plausible_web/controllers/stats_controller_test.exs
+++ b/test/plausible_web/controllers/stats_controller_test.exs
@@ -50,4 +50,48 @@ defmodule PlausibleWeb.StatsControllerTest do
assert response(conn, 200) =~ "#{today},1"
end
end
+
+ describe "GET /share/:slug" do
+ test "prompts a password for a password-protected link", %{conn: conn} do
+ site = insert(:site)
+ link = insert(:shared_link, site: site, password_hash: Plausible.Auth.Password.hash("password"))
+
+ conn = get(conn, "/share/#{link.slug}")
+ assert response(conn, 200) =~ "Enter password"
+ end
+
+ test "logs anonymous user in straight away if the link is not password-protected", %{conn: conn} do
+ site = insert(:site)
+ link = insert(:shared_link, site: site)
+ insert(:pageview, hostname: site.domain)
+
+ conn = get(conn, "/share/#{link.slug}")
+ assert redirected_to(conn, 302) == "/#{site.domain}"
+
+ conn = get(conn, "/#{site.domain}")
+ assert html_response(conn, 200) =~ "stats-react-container"
+ end
+ end
+
+ describe "POST /share/:slug/authenticate" do
+ test "logs anonymous user in with correct password", %{conn: conn} do
+ site = insert(:site)
+ link = insert(:shared_link, site: site, password_hash: Plausible.Auth.Password.hash("password"))
+ insert(:pageview, hostname: site.domain)
+
+ conn = post(conn, "/share/#{link.slug}/authenticate", %{password: "password"})
+ assert redirected_to(conn, 302) == "/#{site.domain}"
+
+ conn = get(conn, "/#{site.domain}")
+ assert html_response(conn, 200) =~ "stats-react-container"
+ end
+
+ test "shows form again with wrong password", %{conn: conn} do
+ site = insert(:site)
+ link = insert(:shared_link, site: site, password_hash: Plausible.Auth.Password.hash("password"))
+
+ conn = post(conn, "/share/#{link.slug}/authenticate", %{password: "WRONG!"})
+ assert html_response(conn, 200) =~ "Enter password"
+ end
+ end
end
diff --git a/test/support/factory.ex b/test/support/factory.ex
index 8519f5adc0..ca67567d6e 100644
--- a/test/support/factory.ex
+++ b/test/support/factory.ex
@@ -97,4 +97,10 @@ defmodule Plausible.Factory do
def monthly_report_factory do
%Plausible.Site.MonthlyReport{}
end
+
+ def shared_link_factory do
+ %Plausible.Site.SharedLink{
+ slug: Nanoid.generate()
+ }
+ end
end