mirror of
https://github.com/plausible/analytics.git
synced 2024-12-26 11:02:52 +03:00
0fa6b688af
* Make TestUtils module available in all tests * Add macros patching the application env in tests Unfortunately a lot of existing functionality relies on certain application env setup. This isn't ideal because the app config is a shared state that prevents us from running the tests in parallel. Those macros encapsulate setting up new env for test purposes and make sure the changes are reverted when the test finishes. * Allow passing request opts to HTTPClient.post/4 We need this to swap custom request building in Google Analytics import. * Unify errors when listing sites * React: propagate backend error messages if available * React: catch API errors in Search Terms component * Propagate google API errors on referrer drilldown * Handle verified properties errors in SC settings * Add missing tests for SC settings controller * Unify errors for fetching search analytics queries (list stats) * Unify errors refreshing Google Auth Token * Test fetch_stats/3 errors and replace Double with Mox * Fixup makrup * s/class/className * Simplify Search Terms display in case of errors * Fix warnings
36 lines
1.6 KiB
Elixir
36 lines
1.6 KiB
Elixir
defmodule PlausibleWeb.Api.StatsController.RegionsTest do
|
|
use PlausibleWeb.ConnCase
|
|
|
|
describe "GET /api/stats/:domain/regions" do
|
|
defp seed(%{site: site}) do
|
|
populate_stats(site, [
|
|
build(:pageview, country_code: "EE", subdivision1_code: "EE-37", city_geoname_id: 588_409),
|
|
build(:pageview, country_code: "EE", subdivision1_code: "EE-37", city_geoname_id: 588_409),
|
|
build(:pageview, country_code: "EE", subdivision1_code: "EE-37", city_geoname_id: 588_409),
|
|
build(:pageview, country_code: "EE", subdivision1_code: "EE-39", city_geoname_id: 591_632),
|
|
build(:pageview, country_code: "EE", subdivision1_code: "EE-39", city_geoname_id: 591_632)
|
|
])
|
|
end
|
|
|
|
setup [:create_user, :log_in, :create_new_site, :add_imported_data, :seed]
|
|
|
|
test "returns top cities by new visitors", %{conn: conn, site: site} do
|
|
conn = get(conn, "/api/stats/#{site.domain}/regions?period=day")
|
|
|
|
assert json_response(conn, 200) == [
|
|
%{"code" => "EE-37", "country_flag" => "🇪🇪", "name" => "Harjumaa", "visitors" => 3},
|
|
%{"code" => "EE-39", "country_flag" => "🇪🇪", "name" => "Hiiumaa", "visitors" => 2}
|
|
]
|
|
end
|
|
|
|
test "when list is filtered returns one city only", %{conn: conn, site: site} do
|
|
filters = Jason.encode!(%{region: "EE-39"})
|
|
conn = get(conn, "/api/stats/#{site.domain}/regions?period=day&filters=#{filters}")
|
|
|
|
assert json_response(conn, 200) == [
|
|
%{"code" => "EE-39", "country_flag" => "🇪🇪", "name" => "Hiiumaa", "visitors" => 2}
|
|
]
|
|
end
|
|
end
|
|
end
|