-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix limited view issue #6161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Fix limited view issue #6161
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
137 changes: 137 additions & 0 deletions
137
test/plausible_web/controllers/api/stats_controller/query_controller_test.exs
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| defmodule PlausibleWeb.Api.InternalController.QueryTest do | ||
| use PlausibleWeb.ConnCase, async: false | ||
| use Plausible.Repo | ||
|
|
||
| describe "POST /api/:domain/query shared/public" do | ||
| test "returns aggregated metrics for public site", %{conn: conn} do | ||
| site = new_site(public: true) | ||
|
|
||
| populate_stats(site, [ | ||
| build(:pageview, user_id: 5, pathname: "/foo", timestamp: ~N[2021-01-01 00:00:00]) | ||
| ]) | ||
|
|
||
| conn = | ||
| post(conn, "/api/stats/#{URI.encode(site.domain)}/query", %{ | ||
| "metrics" => ["pageviews"], | ||
| "date_range" => "all", | ||
| "filters" => [["is", "event:page", ["/foo"]]] | ||
| }) | ||
|
|
||
| assert json_response(conn, 200)["results"] == [ | ||
| %{"metrics" => [1], "dimensions" => []} | ||
| ] | ||
| end | ||
|
|
||
| test "returns aggregated metrics with shared link auth", %{conn: conn} do | ||
| site = new_site() | ||
|
|
||
| populate_stats(site, [ | ||
| build(:pageview, user_id: 5, pathname: "/foo", timestamp: ~N[2021-01-01 00:00:00]), | ||
| build(:pageview, user_id: 5, timestamp: ~N[2021-01-01 00:25:00]), | ||
| build(:pageview, timestamp: ~N[2021-01-01 00:00:00]) | ||
| ]) | ||
|
|
||
| link = insert(:shared_link, site: site) | ||
|
|
||
| conn = | ||
| post(conn, "/api/stats/#{URI.encode(site.domain)}/query?auth=#{link.slug}", %{ | ||
| "metrics" => ["pageviews"], | ||
| "date_range" => "all", | ||
| "filters" => [["is", "event:page", ["/foo"]]] | ||
| }) | ||
|
|
||
| assert json_response(conn, 200)["results"] == [%{"metrics" => [1], "dimensions" => []}] | ||
| end | ||
|
|
||
| test "returns metrics with shared link auth that is limited to segment", %{conn: conn} do | ||
| site = new_site() | ||
|
|
||
| populate_stats(site, [ | ||
| build(:pageview, user_id: 5, pathname: "/foo", timestamp: ~N[2021-01-01 00:00:00]), | ||
| build(:pageview, user_id: 5, timestamp: ~N[2021-01-01 00:25:00]), | ||
| build(:pageview, timestamp: ~N[2021-01-01 00:00:00]) | ||
| ]) | ||
|
|
||
| segment = | ||
| insert(:segment, | ||
| site: site, | ||
| name: "Scandinavia", | ||
| type: :site, | ||
| segment_data: %{"filters" => [["is", "event:page", ["/foo"]]]} | ||
| ) | ||
|
|
||
| link = insert(:shared_link, segment_id: segment.id, site: site) | ||
|
|
||
| conn = | ||
| post(conn, "/api/stats/#{URI.encode(site.domain)}/query?auth=#{link.slug}", %{ | ||
| "metrics" => ["pageviews"], | ||
| "date_range" => "all", | ||
| "filters" => [["is", "segment", [segment.id]]] | ||
| }) | ||
|
|
||
| assert json_response(conn, 200)["results"] == [%{"metrics" => [1], "dimensions" => []}] | ||
| end | ||
|
|
||
| test "errors when expected segment filter not present for shared link auth that is limited to segment", | ||
| %{conn: conn} do | ||
| site = new_site() | ||
|
|
||
| segment = | ||
| insert(:segment, | ||
| site: site, | ||
| name: "Scandinavia", | ||
| type: :site, | ||
| segment_data: %{"filters" => [["is", "event:page", ["/foo"]]]} | ||
| ) | ||
|
|
||
| link = insert(:shared_link, segment_id: segment.id, site: site) | ||
|
|
||
| conn = | ||
| post(conn, "/api/stats/#{URI.encode(site.domain)}/query?auth=#{link.slug}", %{ | ||
| "metrics" => ["pageviews"], | ||
| "date_range" => "all", | ||
| "filters" => [] | ||
| }) | ||
|
|
||
| assert json_response(conn, 400) == %{ | ||
| "error" => "The first filter must be for the segment with id #{segment.id}" | ||
| } | ||
| end | ||
| end | ||
|
|
||
| describe "POST /api/:domain/query" do | ||
| setup [:create_user, :create_site, :log_in] | ||
|
|
||
| test "rejects when accessing any other site", %{conn: conn, site: site} do | ||
| conn = | ||
| post(conn, "/api/stats/any.other.site/query", %{ | ||
| # ignored | ||
| "site_id" => site.domain, | ||
| "metrics" => ["pageviews"], | ||
| "date_range" => "all", | ||
| "filters" => [] | ||
| }) | ||
|
|
||
| assert json_response(conn, 404) == %{ | ||
| "error" => "Site does not exist or user does not have sufficient access." | ||
| } | ||
| end | ||
|
|
||
| test "returns aggregated metrics", %{conn: conn, site: site} do | ||
| populate_stats(site, [ | ||
| build(:pageview, user_id: 5, timestamp: ~N[2021-01-01 00:00:00]), | ||
| build(:pageview, user_id: 5, timestamp: ~N[2021-01-01 00:25:00]), | ||
| build(:pageview, timestamp: ~N[2021-01-01 00:00:00]) | ||
| ]) | ||
|
|
||
| conn = | ||
| post(conn, "/api/stats/#{URI.encode(site.domain)}/query?site_id=ignored", %{ | ||
| "metrics" => ["pageviews"], | ||
| "date_range" => "all", | ||
| "filters" => [] | ||
| }) | ||
|
|
||
| assert json_response(conn, 200)["results"] == [%{"metrics" => [3], "dimensions" => []}] | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.