add a test and fix rendering the dashboard link

This commit is contained in:
Robert 2022-11-03 11:20:29 +02:00
parent fc3be2a5b8
commit fbe3a4e6f1
3 changed files with 17 additions and 3 deletions

View File

@ -10,7 +10,7 @@ There are currently <%= @current_visitors %> visitors on <%= link(@site.domain,
<%= if @link do %>
<br /><br />
View dashboard: @link
View dashboard: <%= link(@link, to: @link) %>
<% end %>
<br /><br />
--

View File

@ -31,7 +31,7 @@ defmodule Plausible.Workers.SpikeNotifier do
:ok
end
def notify(notification, current_visitors, sources) do
defp notify(notification, current_visitors, sources) do
for recipient <- notification.recipients do
send_notification(recipient, notification.site, current_visitors, sources)
end
@ -45,7 +45,7 @@ defmodule Plausible.Workers.SpikeNotifier do
site = Repo.preload(site, :members)
dashboard_link =
if Enum.member?(site.members, recipient) do
if Enum.any?(site.members, &(&1.email == recipient)) do
PlausibleWeb.Endpoint.url() <> "/" <> URI.encode_www_form(site.domain)
end

View File

@ -85,4 +85,18 @@ defmodule Plausible.Workers.SpikeNotifierTest do
assert_no_emails_delivered()
end
test "adds a dashboard link if recipient has access to the site" do
user = insert(:user, email: "robert@example.com")
site = insert(:site, domain: "example.com", members: [user])
insert(:spike_notification, site: site, threshold: 10, recipients: ["robert@example.com"])
clickhouse_stub =
stub(Plausible.Stats.Clickhouse, :current_visitors, fn _site, _query -> 10 end)
|> stub(:top_sources, fn _site, _query, _limit, _page, _show_noref -> [] end)
SpikeNotifier.perform(nil, clickhouse_stub)
assert_email_delivered_with(html_body: ~r/View dashboard: <a href=\"http.+\/example.com/)
end
end