Skip to content

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

Script v2: Identify detection attempt (#5818)

Artur Pataf01ce1b1b620
diff --git a/config/test.exs b/config/test.exs
index 15c5dc8e8c..afcffaf0a6 100644
--- a/config/test.exs
+++ b/config/test.exs
@@ -54,7 +54,7 @@ config :plausible, Plausible.HelpScout,
config :plausible, Plausible.InstallationSupport.Checks.Detection,
req_opts: [
- plug: {Req.Test, :global}
+ plug: {Req.Test, Plausible.InstallationSupport.Checks.Detection}
]
config :plausible, Plausible.InstallationSupport.Checks.InstallationV2,
diff --git a/extra/lib/plausible/installation_support/checks/detection.ex b/extra/lib/plausible/installation_support/checks/detection.ex
index 299904d530..f071affced 100644
--- a/extra/lib/plausible/installation_support/checks/detection.ex
+++ b/extra/lib/plausible/installation_support/checks/detection.ex
@@ -77,7 +77,7 @@ defmodule Plausible.InstallationSupport.Checks.Detection do
Jason.encode!(%{
code: @puppeteer_wrapper_code,
context: %{
- url: url,
+ url: Plausible.InstallationSupport.URL.bust_url(url),
userAgent: Plausible.InstallationSupport.user_agent(),
detectV1: detect_v1?,
timeoutMs: @plausible_window_check_timeout_ms,
diff --git a/test/plausible/installation_support/detection/checks_test.exs b/test/plausible/installation_support/detection/checks_test.exs
new file mode 100644
index 0000000000..2cfefedbc3
--- /dev/null
+++ b/test/plausible/installation_support/detection/checks_test.exs
@@ -0,0 +1,101 @@
+defmodule Plausible.InstallationSupport.Detection.ChecksTest do
+ use Plausible
+ use Plausible.DataCase, async: true
+
+ @moduletag :ee_only
+
+ on_ee do
+ import Plausible.AssertMatches
+ alias Plausible.InstallationSupport.Detection.{Checks}
+ alias Plausible.InstallationSupport.Result
+ use Plausible.Test.Support.DNS
+ import Plug.Conn
+ @moduletag :capture_log
+
+ describe "running detection" do
+ test "handles wordpress detection, retrying on 429" do
+ expected_domain = "example.com"
+ url_to_verify = nil
+ test = self()
+ stub_lookup_a_records(expected_domain)
+
+ get_context_from_body = fn conn ->
+ {:ok, body, _conn} = Plug.Conn.read_body(conn)
+ JSON.decode!(body)["context"]
+ end
+
+ detection_counter =
+ stub_detection_result(fn conn, request_i ->
+ send(test, get_context_from_body.(conn))
+
+ case request_i do
+ 1 ->
+ conn |> send_resp(429, "Too Many Requests")
+
+ _ ->
+ conn
+ |> put_resp_content_type("application/json")
+ |> send_resp(
+ 200,
+ JSON.encode!(%{
+ "data" => %{
+ "completed" => true,
+ "v1Detected" => nil,
+ "gtmLikely" => false,
+ "npm" => false,
+ "wordpressLikely" => true,
+ "wordpressPlugin" => false
+ }
+ })
+ )
+ end
+ end)
+
+ assert %Result{
+ ok?: true,
+ data: %{
+ v1_detected: nil,
+ wordpress_plugin: false,
+ npm: false,
+ suggested_technology: "wordpress"
+ }
+ } ==
+ Checks.run(url_to_verify, expected_domain,
+ report_to: nil,
+ async?: false,
+ slowdown: 0
+ )
+ |> Checks.interpret_diagnostics()
+
+ assert_receive context
+
+ assert_matches ^strict_map(%{
+ "debug" => false,
+ "detectV1" => false,
+ "timeoutMs" => 1500,
+ "url" =>
+ ^any(
+ :string,
+ ~r/https:\/\/#{expected_domain}\?plausible_verification=\d+$/
+ ),
+ "userAgent" =>
+ "Plausible Verification Agent - if abused, contact [email protected]"
+ }) = context
+
+ assert 2 == :atomics.get(detection_counter, 1)
+ end
+ end
+
+ defp stub_detection_result(handler) do
+ counter = :atomics.new(1, [])
+
+ Req.Test.stub(Plausible.InstallationSupport.Checks.Detection, fn conn ->
+ request_i = :atomics.add_get(counter, 1, 1)
+
+ handler.(conn, request_i)
+ end)
+
+ counter
+ end
+ end
+end
diff --git a/test/plausible_web/live/change_domain_v2_test.exs b/test/plausible_web/live/change_domain_v2_test.exs
index d8bca48a8f..4cbeccb0f8 100644
--- a/test/plausible_web/live/change_domain_v2_test.exs
+++ b/test/plausible_web/live/change_domain_v2_test.exs
@@ -364,7 +364,7 @@ defmodule PlausibleWeb.Live.ChangeDomainV2Test do
end
defp stub_detection_result(js_data) do
- Req.Test.stub(:global, fn conn ->
+ Req.Test.stub(Plausible.InstallationSupport.Checks.Detection, fn conn ->
conn
|> put_resp_content_type("application/json")
|> send_resp(200, Jason.encode!(%{"data" => Map.put(js_data, "completed", true)}))
@@ -372,7 +372,7 @@ defmodule PlausibleWeb.Live.ChangeDomainV2Test do
end
defp stub_detection_error do
- Req.Test.stub(:global, fn conn ->
+ Req.Test.stub(Plausible.InstallationSupport.Checks.Detection, fn conn ->
conn
|> put_resp_content_type("application/json")
|> send_resp(
diff --git a/test/plausible_web/live/installationv2_test.exs b/test/plausible_web/live/installationv2_test.exs
index 5055f95fa1..8d0fe28bad 100644
--- a/test/plausible_web/live/installationv2_test.exs
+++ b/test/plausible_web/live/installationv2_test.exs
@@ -687,7 +687,7 @@ defmodule PlausibleWeb.Live.InstallationV2Test do
end
defp stub_detection_result(js_data) do
- Req.Test.stub(:global, fn conn ->
+ Req.Test.stub(Plausible.InstallationSupport.Checks.Detection, fn conn ->
conn
|> put_resp_content_type("application/json")
|> send_resp(200, Jason.encode!(%{"data" => Map.put(js_data, "completed", true)}))
@@ -695,7 +695,7 @@ defmodule PlausibleWeb.Live.InstallationV2Test do
end
defp stub_detection_error do
- Req.Test.stub(:global, fn conn ->
+ Req.Test.stub(Plausible.InstallationSupport.Checks.Detection, fn conn ->
conn
|> put_resp_content_type("application/json")
|> send_resp(