-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
155 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1229,109 +1229,147 @@ defmodule PlausibleWeb.SiteControllerTest do | |
end | ||
|
||
for type <- [:spike, :drop] do | ||
describe "POST /sites/:website/traffic-change-notification/#{type}/enable" do | ||
setup [:create_user, :log_in, :create_site] | ||
describe "POST /sites/:website/traffic-change-notification/#{type}/enable" do | ||
setup [:create_user, :log_in, :create_site] | ||
|
||
test "creates a #{type} notification record with the user email", %{ | ||
conn: conn, | ||
site: site, | ||
user: user | ||
} do | ||
post(conn, "/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/enable") | ||
test "creates a #{type} notification record with the user email", %{ | ||
conn: conn, | ||
site: site, | ||
user: user | ||
} do | ||
post(conn, "/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/enable") | ||
|
||
notification = Repo.get_by(Plausible.Site.TrafficChangeNotification, site_id: site.id, type: unquote(type)) | ||
assert notification.recipients == [user.email] | ||
end | ||
notification = | ||
Repo.get_by(Plausible.Site.TrafficChangeNotification, | ||
site_id: site.id, | ||
type: unquote(type) | ||
) | ||
|
||
test "does not allow duplicate #{type} notification to be created", %{ | ||
conn: conn, | ||
site: site | ||
} do | ||
post(conn, "/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/enable") | ||
post(conn, "/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/enable") | ||
assert notification.recipients == [user.email] | ||
end | ||
|
||
assert Repo.aggregate( | ||
from(s in Plausible.Site.TrafficChangeNotification, where: s.site_id == ^site.id and s.type == ^unquote(type)), | ||
:count | ||
) == 1 | ||
test "does not allow duplicate #{type} notification to be created", %{ | ||
conn: conn, | ||
site: site | ||
} do | ||
post(conn, "/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/enable") | ||
post(conn, "/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/enable") | ||
|
||
assert Repo.aggregate( | ||
from(s in Plausible.Site.TrafficChangeNotification, | ||
where: s.site_id == ^site.id and s.type == ^unquote(type) | ||
), | ||
:count | ||
) == 1 | ||
end | ||
end | ||
end | ||
|
||
describe "POST /sites/:website/traffic-change-notification/#{type}/disable" do | ||
setup [:create_user, :log_in, :create_site] | ||
describe "POST /sites/:website/traffic-change-notification/#{type}/disable" do | ||
setup [:create_user, :log_in, :create_site] | ||
|
||
test "deletes the #{type} notification record", %{conn: conn, site: site} do | ||
insert(:"#{unquote(type)}_notification", site: site) | ||
test "deletes the #{type} notification record", %{conn: conn, site: site} do | ||
insert(:"#{unquote(type)}_notification", site: site) | ||
|
||
post(conn, "/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/disable") | ||
post(conn, "/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/disable") | ||
|
||
refute Repo.get_by(Plausible.Site.TrafficChangeNotification, site_id: site.id) | ||
refute Repo.get_by(Plausible.Site.TrafficChangeNotification, site_id: site.id) | ||
end | ||
end | ||
end | ||
|
||
describe "PUT /sites/:website/traffic-change-notification/#{type}" do | ||
setup [:create_user, :log_in, :create_site] | ||
describe "PUT /sites/:website/traffic-change-notification/#{type}" do | ||
setup [:create_user, :log_in, :create_site] | ||
|
||
test "updates #{type} notification threshold", %{conn: conn, site: site} do | ||
insert(:"#{unquote(type)}_notification", site: site, threshold: 10) | ||
test "updates #{type} notification threshold", %{conn: conn, site: site} do | ||
insert(:"#{unquote(type)}_notification", site: site, threshold: 10) | ||
|
||
put(conn, "/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}", %{ | ||
"traffic_change_notification" => %{"threshold" => "15"} | ||
}) | ||
put(conn, "/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}", %{ | ||
"traffic_change_notification" => %{"threshold" => "15"} | ||
}) | ||
|
||
notification = | ||
Repo.get_by(Plausible.Site.TrafficChangeNotification, | ||
site_id: site.id, | ||
type: unquote(type) | ||
) | ||
|
||
notification = Repo.get_by(Plausible.Site.TrafficChangeNotification, site_id: site.id, type: unquote(type)) | ||
assert notification.threshold == 15 | ||
assert notification.threshold == 15 | ||
end | ||
end | ||
end | ||
|
||
describe "POST /sites/:website/traffic-change-notification/#{type}/recipients" do | ||
setup [:create_user, :log_in, :create_site] | ||
describe "POST /sites/:website/traffic-change-notification/#{type}/recipients" do | ||
setup [:create_user, :log_in, :create_site] | ||
|
||
test "adds a recipient to the #{type} notification", %{conn: conn, site: site} do | ||
insert(:"#{unquote(type)}_notification", site: site) | ||
test "adds a recipient to the #{type} notification", %{conn: conn, site: site} do | ||
insert(:"#{unquote(type)}_notification", site: site) | ||
|
||
post(conn, "/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/recipients", | ||
recipient: "[email protected]" | ||
) | ||
post( | ||
conn, | ||
"/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/recipients", | ||
recipient: "[email protected]" | ||
) | ||
|
||
report = Repo.get_by(Plausible.Site.TrafficChangeNotification, site_id: site.id, type: unquote(type)) | ||
assert report.recipients == ["[email protected]"] | ||
report = | ||
Repo.get_by(Plausible.Site.TrafficChangeNotification, | ||
site_id: site.id, | ||
type: unquote(type) | ||
) | ||
|
||
assert report.recipients == ["[email protected]"] | ||
end | ||
end | ||
end | ||
|
||
describe "DELETE /sites/:website/traffic-change-notification/#{type}/recipients/:recipient" do | ||
setup [:create_user, :log_in, :create_site] | ||
describe "DELETE /sites/:website/traffic-change-notification/#{type}/recipients/:recipient" do | ||
setup [:create_user, :log_in, :create_site] | ||
|
||
test "removes a recipient from the #{type} notification", %{conn: conn, site: site} do | ||
insert(:"#{unquote(type)}_notification", site: site, recipients: ["[email protected]"]) | ||
test "removes a recipient from the #{type} notification", %{conn: conn, site: site} do | ||
insert(:"#{unquote(type)}_notification", site: site, recipients: ["[email protected]"]) | ||
|
||
delete(conn, "/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/recipients/[email protected]") | ||
delete( | ||
conn, | ||
"/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/recipients/[email protected]" | ||
) | ||
|
||
report = Repo.get_by(Plausible.Site.TrafficChangeNotification, site_id: site.id, type: unquote(type)) | ||
assert report.recipients == [] | ||
end | ||
report = | ||
Repo.get_by(Plausible.Site.TrafficChangeNotification, | ||
site_id: site.id, | ||
type: unquote(type) | ||
) | ||
|
||
test "fails to remove a recipient from the #{type} notification in a foreign website", %{ | ||
conn: conn | ||
} do | ||
site = insert(:site) | ||
insert(:"#{unquote(type)}_notification", site: site, recipients: ["[email protected]"]) | ||
assert report.recipients == [] | ||
end | ||
|
||
conn = | ||
delete(conn, "/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/recipients/[email protected]") | ||
test "fails to remove a recipient from the #{type} notification in a foreign website", %{ | ||
conn: conn | ||
} do | ||
site = insert(:site) | ||
insert(:"#{unquote(type)}_notification", site: site, recipients: ["[email protected]"]) | ||
|
||
assert conn.status == 404 | ||
conn = | ||
delete( | ||
conn, | ||
"/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/recipients/[email protected]" | ||
) | ||
|
||
conn = | ||
delete(conn, "/sites/#{site.domain}/traffic-change-notification/recipients/#{unquote(type)}/recipient%40email.com") | ||
assert conn.status == 404 | ||
|
||
assert conn.status == 404 | ||
conn = | ||
delete( | ||
conn, | ||
"/sites/#{site.domain}/traffic-change-notification/recipients/#{unquote(type)}/recipient%40email.com" | ||
) | ||
|
||
report = Repo.get_by(Plausible.Site.TrafficChangeNotification, site_id: site.id, type: unquote(type)) | ||
assert [_] = report.recipients | ||
assert conn.status == 404 | ||
|
||
report = | ||
Repo.get_by(Plausible.Site.TrafficChangeNotification, | ||
site_id: site.id, | ||
type: unquote(type) | ||
) | ||
|
||
assert [_] = report.recipients | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe "GET /sites/:website/shared-links/new" do | ||
setup [:create_user, :log_in, :create_site] | ||
|