|
| 1 | +defmodule PlausibleWeb.Api.StatsController.DebugMetadataTest do |
| 2 | + alias Plausible.{IngestRepo, ClickhouseRepo} |
| 3 | + use PlausibleWeb.ConnCase, async: true |
| 4 | + |
| 5 | + describe "Debug metadata for logged in requests" do |
| 6 | + setup [:create_user, :log_in] |
| 7 | + |
| 8 | + test "for main-graph", %{conn: conn, user: user} do |
| 9 | + domain = :rand.bytes(20) |> Base.url_encode64() |
| 10 | + site = new_site(domain: domain, owner: user) |
| 11 | + conn = get(conn, "/api/stats/#{site.domain}/main-graph") |
| 12 | + |
| 13 | + assert json_response(conn, 200) |
| 14 | + |
| 15 | + IngestRepo.query!("SYSTEM FLUSH LOGS") |
| 16 | + |
| 17 | + %{rows: [r1, r2]} = |
| 18 | + ClickhouseRepo.query!( |
| 19 | + "FROM system.query_log SELECT log_comment WHERE JSONExtractString(log_comment, 'site_domain') = {$0:String}", |
| 20 | + [site.domain] |
| 21 | + ) |
| 22 | + |
| 23 | + for [unparsed_log_comment] <- [r1, r2] do |
| 24 | + decoded = Jason.decode!(unparsed_log_comment) |
| 25 | + |
| 26 | + assert_matches ^strict_map(%{ |
| 27 | + "params" => ^strict_map(%{"domain" => ^site.domain}), |
| 28 | + "phoenix_action" => "main_graph", |
| 29 | + "phoenix_controller" => "Elixir.PlausibleWeb.Api.StatsController", |
| 30 | + "request_method" => "GET", |
| 31 | + "request_path" => ^"/api/stats/#{site.domain}/main-graph", |
| 32 | + "site_domain" => ^site.domain, |
| 33 | + "site_id" => ^site.id, |
| 34 | + "team_id" => ^team_of(user).id, |
| 35 | + "user_id" => ^user.id |
| 36 | + }) = decoded |
| 37 | + end |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + defp setup_dashboard_case(domain, type) do |
| 42 | + site_owner = new_user() |
| 43 | + |
| 44 | + case type do |
| 45 | + "public" -> |
| 46 | + site = new_site(domain: domain, owner: site_owner, public: true) |
| 47 | + {site, "", %{"domain" => domain}} |
| 48 | + |
| 49 | + "shared" -> |
| 50 | + site = new_site(domain: domain, owner: site_owner) |
| 51 | + link = insert(:shared_link, site: site) |
| 52 | + |
| 53 | + {site, "?auth=#{link.slug}", %{"domain" => domain, "auth" => link.slug}} |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + describe "Debug metadata for non-private dashboard requests" do |
| 58 | + setup [:create_user, :log_in] |
| 59 | + |
| 60 | + for type <- ["public", "shared"] do |
| 61 | + test "for /pages request (#{type})", %{ |
| 62 | + conn: conn, |
| 63 | + user: user |
| 64 | + } do |
| 65 | + domain = :rand.bytes(20) |> Base.url_encode64() |
| 66 | + {site, query_string, expected_params} = setup_dashboard_case(domain, unquote(type)) |
| 67 | + conn = get(conn, "/api/stats/#{site.domain}/pages#{query_string}") |
| 68 | + |
| 69 | + assert json_response(conn, 200) |
| 70 | + |
| 71 | + IngestRepo.query!("SYSTEM FLUSH LOGS") |
| 72 | + |
| 73 | + %{rows: [r1, r2]} = |
| 74 | + ClickhouseRepo.query!( |
| 75 | + "FROM system.query_log SELECT log_comment WHERE JSONExtractString(log_comment, 'site_domain') = {$0:String}", |
| 76 | + [site.domain] |
| 77 | + ) |
| 78 | + |
| 79 | + for [unparsed_log_comment] <- [r1, r2] do |
| 80 | + decoded = Jason.decode!(unparsed_log_comment) |
| 81 | + |
| 82 | + assert_matches ^strict_map(%{ |
| 83 | + # params are asserted below |
| 84 | + "params" => %{}, |
| 85 | + "phoenix_action" => "pages", |
| 86 | + "phoenix_controller" => "Elixir.PlausibleWeb.Api.StatsController", |
| 87 | + "request_method" => "GET", |
| 88 | + "request_path" => ^"/api/stats/#{site.domain}/pages", |
| 89 | + "site_domain" => ^site.domain, |
| 90 | + "site_id" => ^site.id, |
| 91 | + # nil team_id because viewing a public/shared dashboard |
| 92 | + "team_id" => nil, |
| 93 | + # the logged in user ID is included even when viewing a random public dashboard |
| 94 | + "user_id" => ^user.id |
| 95 | + }) = decoded |
| 96 | + |
| 97 | + assert decoded["params"] == expected_params |
| 98 | + end |
| 99 | + end |
| 100 | + end |
| 101 | + end |
| 102 | +end |
0 commit comments