2019-09-02 14:29:19 +03:00
|
|
|
defmodule PlausibleWeb.StatsControllerTest do
|
|
|
|
use PlausibleWeb.ConnCase
|
|
|
|
use Plausible.Repo
|
|
|
|
import Plausible.TestUtils
|
|
|
|
|
2020-01-13 16:16:35 +03:00
|
|
|
describe "GET /:website - anonymous user" do
|
2019-09-02 14:29:19 +03:00
|
|
|
test "public site - shows site stats", %{conn: conn} do
|
|
|
|
insert(:site, domain: "public-site.io", public: true)
|
|
|
|
|
|
|
|
conn = get(conn, "/public-site.io")
|
2019-11-19 07:30:42 +03:00
|
|
|
assert html_response(conn, 200) =~ "stats-react-container"
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
|
2020-10-02 12:02:32 +03:00
|
|
|
test "public site - shows waiting for first pageview", %{conn: conn} do
|
|
|
|
insert(:site, domain: "some-other-public-site.io", public: true)
|
|
|
|
|
|
|
|
conn = get(conn, "/some-other-public-site.io")
|
|
|
|
assert html_response(conn, 200) =~ "Need to see the snippet again?"
|
|
|
|
end
|
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
test "can not view stats of a private website", %{conn: conn} do
|
2020-05-18 12:44:52 +03:00
|
|
|
conn = get(conn, "/test-site.com")
|
2019-09-02 14:29:19 +03:00
|
|
|
assert html_response(conn, 404) =~ "There's nothing here"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-13 16:16:35 +03:00
|
|
|
describe "GET /:website - as a logged in user" do
|
2019-09-02 14:29:19 +03:00
|
|
|
setup [:create_user, :log_in, :create_site]
|
|
|
|
|
|
|
|
test "can view stats of a website I've created", %{conn: conn, site: site} do
|
|
|
|
conn = get(conn, "/" <> site.domain)
|
2019-11-19 07:30:42 +03:00
|
|
|
assert html_response(conn, 200) =~ "stats-react-container"
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
test "can not view stats of someone else's website", %{conn: conn} do
|
|
|
|
conn = get(conn, "/some-other-site.com")
|
|
|
|
assert html_response(conn, 404) =~ "There's nothing here"
|
|
|
|
end
|
|
|
|
end
|
2020-01-13 16:16:35 +03:00
|
|
|
|
|
|
|
describe "GET /:website/visitors.csv" do
|
|
|
|
setup [:create_user, :log_in, :create_site]
|
|
|
|
|
|
|
|
test "exports graph as csv", %{conn: conn, site: site} do
|
|
|
|
today = Timex.today() |> Timex.format!("{ISOdate}")
|
|
|
|
|
|
|
|
conn = get(conn, "/" <> site.domain <> "/visitors.csv")
|
|
|
|
assert response(conn, 200) =~ "Date,Visitors"
|
2020-05-18 12:44:52 +03:00
|
|
|
assert response(conn, 200) =~ "#{today},3"
|
2020-01-13 16:16:35 +03:00
|
|
|
end
|
|
|
|
end
|
2020-01-29 12:29:11 +03:00
|
|
|
|
|
|
|
describe "GET /share/:slug" do
|
|
|
|
test "prompts a password for a password-protected link", %{conn: conn} do
|
|
|
|
site = insert(:site)
|
2020-06-08 10:35:13 +03:00
|
|
|
|
|
|
|
link =
|
|
|
|
insert(:shared_link, site: site, password_hash: Plausible.Auth.Password.hash("password"))
|
2020-01-29 12:29:11 +03:00
|
|
|
|
|
|
|
conn = get(conn, "/share/#{link.slug}")
|
|
|
|
assert response(conn, 200) =~ "Enter password"
|
|
|
|
end
|
|
|
|
|
2020-06-08 10:35:13 +03:00
|
|
|
test "logs anonymous user in straight away if the link is not password-protected", %{
|
|
|
|
conn: conn
|
|
|
|
} do
|
2020-05-18 12:44:52 +03:00
|
|
|
site = insert(:site, domain: "test-site.com")
|
2020-01-29 12:29:11 +03:00
|
|
|
link = insert(:shared_link, site: site)
|
|
|
|
|
|
|
|
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
|
2020-12-22 18:37:05 +03:00
|
|
|
|
|
|
|
test "encodes URI when redirecting", %{conn: conn} do
|
|
|
|
site = insert(:site, domain: "test-site.com/wat")
|
|
|
|
link = insert(:shared_link, site: site)
|
|
|
|
|
|
|
|
conn = get(conn, "/share/#{link.slug}")
|
|
|
|
assert redirected_to(conn, 302) == "/test-site.com%2Fwat"
|
|
|
|
end
|
2020-01-29 12:29:11 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "POST /share/:slug/authenticate" do
|
|
|
|
test "logs anonymous user in with correct password", %{conn: conn} do
|
2020-05-18 12:44:52 +03:00
|
|
|
site = insert(:site, domain: "test-site.com")
|
2020-06-08 10:35:13 +03:00
|
|
|
|
|
|
|
link =
|
|
|
|
insert(:shared_link, site: site, password_hash: Plausible.Auth.Password.hash("password"))
|
2020-01-29 12:29:11 +03:00
|
|
|
|
|
|
|
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
|
2020-05-18 12:44:52 +03:00
|
|
|
site = insert(:site, domain: "test-site.com")
|
2020-06-08 10:35:13 +03:00
|
|
|
|
|
|
|
link =
|
|
|
|
insert(:shared_link, site: site, password_hash: Plausible.Auth.Password.hash("password"))
|
2020-01-29 12:29:11 +03:00
|
|
|
|
|
|
|
conn = post(conn, "/share/#{link.slug}/authenticate", %{password: "WRONG!"})
|
|
|
|
assert html_response(conn, 200) =~ "Enter password"
|
|
|
|
end
|
|
|
|
end
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|