Skip to content

Commit

Permalink
Merge pull request #22 from Recruitee/conditionally-support-traffic-f…
Browse files Browse the repository at this point in the history
…rom-headless-agents

Conditionally support traffic from headless agents
  • Loading branch information
rdk08 authored Apr 18, 2023
2 parents 22af8e5 + b74f1c1 commit 22961e9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
1 change: 1 addition & 0 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ config :plausible,
admin_email: admin_email,
admin_pwd: admin_pwd,
environment: env,
system_environment: env,
mailer_email: mailer_email,
super_admin_user_ids: super_admin_user_ids,
site_limit: site_limit,
Expand Down
5 changes: 3 additions & 2 deletions lib/plausible_web/controllers/api/external_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,9 @@ defmodule PlausibleWeb.Api.ExternalController do

defp is_bot?(%UAInspector.Result.Bot{}), do: true

defp is_bot?(%UAInspector.Result{client: %UAInspector.Result.Client{name: "Headless Chrome"}}),
do: true
defp is_bot?(%UAInspector.Result{client: %UAInspector.Result.Client{name: "Headless Chrome"}}) do
Application.get_env(:plausible, :system_environment) not in ["rc", "staging"]
end

defp is_bot?(_), do: false

Expand Down
53 changes: 36 additions & 17 deletions test/plausible_web/controllers/api/external_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,29 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
)
|> post("/api/event", params)

assert get_event("headless-chrome-test.com") == nil
refute get_event("headless-chrome-test.com")
end

test "Headless Chrome is not ignored on staging environment", %{conn: conn} do
initial_env = Application.get_env(:plausible, :system_environment)
Application.put_env(:plausible, :system_environment, "staging")

params = %{
name: "pageview",
url: "http://www.example.com/",
domain: "headless-chrome-stg-test.com"
}

conn
|> put_req_header(
"user-agent",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/85.0.4183.83 Safari/537.36"
)
|> post("/api/event", params)

assert get_event("headless-chrome-stg-test.com")

Application.put_env(:plausible, :system_environment, initial_env)
end

test "parses user_agent", %{conn: conn} do
Expand Down Expand Up @@ -476,11 +498,11 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
url: "http://gigride.live/",
domain: "special-props-test.com",
props: %{
campaign_id: 8,
campaign_id: "vendor-8",
company_id: 10,
job_id: 12,
page_id: 15,
product_id: 11,
product_id: "vendor-11",
site_id: 9,
careers_application_form_uuid: "313a26c2-741c-421c-9a6b-f39c02c8d35c"
}
Expand All @@ -491,8 +513,8 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do

event = get_event("special-props-test.com")

assert Map.get(event, :campaign_id) == 8
assert Map.get(event, :product_id) == 11
assert Map.get(event, :campaign_id) == "vendor-8"
assert Map.get(event, :product_id) == "vendor-11"
assert Map.get(event, :company_id) == 10
assert Map.get(event, :job_id) == 12
assert Map.get(event, :page_id) == 15
Expand Down Expand Up @@ -878,9 +900,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
|> post("/api/event", params)

assert json_response(conn, 400) == %{
"errors" => %{
"hostname" => ["can't be blank"]
}
"errors" => %{"request" => "Unable to process request"}
}
end

Expand All @@ -897,9 +917,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
|> post("/api/event", params)

assert json_response(conn, 400) == %{
"errors" => %{
"domain" => ["can't be blank"]
}
"errors" => %{"request" => "Unable to process request"}
}
end
end
Expand Down Expand Up @@ -1077,11 +1095,12 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
assert pageview.hostname == "liipgellkffekalgefpjolodblggkmjg"
end

describe "GET /api/health" do
test "returns 200 OK", %{conn: conn} do
conn = get(conn, "/api/health")
# /api/health path is disabled in PlausibleWeb.Router module
# describe "GET /api/health" do
# test "returns 200 OK", %{conn: conn} do
# conn = get(conn, "/api/health")

assert conn.status == 200
end
end
# assert conn.status == 200
# end
# end
end

0 comments on commit 22961e9

Please sign in to comment.