diff --git a/config/test.exs b/config/test.exs index a57ff19a3..c311c15a9 100644 --- a/config/test.exs +++ b/config/test.exs @@ -18,6 +18,10 @@ config :plausible, paddle_api: Plausible.PaddleApi.Mock, google_api: Plausible.Google.Api.Mock +config :plausible, :google, + client_id: "fake_client_id", + client_secret: "fake_client_secret" + config :bamboo, :refute_timeout, 10 geolix_sample_lookup = %{ diff --git a/fixture/vcr_cassettes/google_analytics_auth#invalid_grant.json b/fixture/vcr_cassettes/google_analytics_auth#invalid_grant.json new file mode 100644 index 000000000..5a39b986e --- /dev/null +++ b/fixture/vcr_cassettes/google_analytics_auth#invalid_grant.json @@ -0,0 +1,35 @@ +[ + { + "request": { + "body": "client_id=fake_client_id&client_secret=fake_client_secret&refresh_token=*****&grant_type=refresh_token&redirect_uri=http://localhost:8000/auth/google/callback", + "headers": { + "Content-Type": "application/x-www-form-urlencoded" + }, + "method": "post", + "options": [], + "request_body": "", + "url": "https://www.googleapis.com/oauth2/v4/token" + }, + "response": { + "binary": false, + "body": "{\n \"error\": \"invalid_grant\",\n \"error_description\": \"Bad Request\"\n}", + "headers": { + "date": "Fri, 12 Aug 2022 16:43:57 GMT", + "pragma": "no-cache", + "cache-control": "no-cache, no-store, max-age=0, must-revalidate", + "expires": "Mon, 01 Jan 1990 00:00:00 GMT", + "content-type": "application/json; charset=utf-8", + "vary": "X-Origin", + "server": "scaffolding on HTTPServer2", + "x-xss-protection": "0", + "x-frame-options": "SAMEORIGIN", + "x-content-type-options": "nosniff", + "alt-svc": "h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000,h3-Q050=\":443\"; ma=2592000,h3-Q046=\":443\"; ma=2592000,h3-Q043=\":443\"; ma=2592000,quic=\":443\"; ma=2592000; v=\"46,43\"", + "accept-ranges": "none", + "transfer-encoding": "chunked" + }, + "status_code": 400, + "type": "ok" + } + } +] diff --git a/lib/plausible/google/http.ex b/lib/plausible/google/http.ex index 7d38d81eb..22b796a2e 100644 --- a/lib/plausible/google/http.ex +++ b/lib/plausible/google/http.ex @@ -161,11 +161,15 @@ defmodule Plausible.Google.HTTP do {:ok, %Finch.Response{body: body, status: 200}} -> {:ok, Jason.decode!(body)} - {:error, %Finch.Response{body: body}} -> + {:ok, %Finch.Response{body: body, status: _non_http_200}} -> body - |> Jason.decode!(body) + |> Jason.decode!() |> Map.get("error") |> then(&{:error, &1}) + + {:error, %Finch.Response{body: body}} -> + Sentry.capture_message("Error fetching Google queries", extra: Jason.decode!(body)) + {:error, :unknown} end end diff --git a/lib/plausible_web/templates/site/settings_search_console.html.eex b/lib/plausible_web/templates/site/settings_search_console.html.eex index a4267f078..fb5502ede 100644 --- a/lib/plausible_web/templates/site/settings_search_console.html.eex +++ b/lib/plausible_web/templates/site/settings_search_console.html.eex @@ -37,7 +37,12 @@ <% end %> <% {:error, error} -> %>
The following error happened when fetching your Google Search Console domains.
-<%= error %>
+ + <%= if error == "invalid_grant" do %> +Invalid Grant error returned from Google. See here on how to fix it.
+ <% else %> +<%= error %>
+ <% end %> <% end %> <% else %> <%= button("Continue with Google", to: Plausible.Google.Api.authorize_url(@site.id, "search-console"), class: "button mt-8") %> diff --git a/test/plausible/google/api_test.exs b/test/plausible/google/api_test.exs index d4ee6ccba..8d699114d 100644 --- a/test/plausible/google/api_test.exs +++ b/test/plausible/google/api_test.exs @@ -146,5 +146,22 @@ defmodule Plausible.Google.ApiTest do ]} = Plausible.Google.Api.fetch_stats(site, query, 5) end end + + test "returns error when token refresh fails", %{user: user, site: site} do + use_cassette "google_analytics_auth#invalid_grant" do + insert(:google_auth, + user: user, + site: site, + property: "sc-domain:dummy.test", + access_token: "*****", + refresh_token: "*****", + expires: NaiveDateTime.add(NaiveDateTime.utc_now(), -3600) + ) + + query = %Plausible.Stats.Query{date_range: Date.range(~D[2022-01-01], ~D[2022-01-05])} + + assert {:error, "invalid_grant"} = Plausible.Google.Api.fetch_stats(site, query, 5) + end + end end end diff --git a/test/plausible/google/buffer_test.exs b/test/plausible/google/buffer_test.exs index a76fb170f..2bcd483f7 100644 --- a/test/plausible/google/buffer_test.exs +++ b/test/plausible/google/buffer_test.exs @@ -1,5 +1,5 @@ defmodule Plausible.Google.BufferTest do - use Plausible.DataCase, async: true + use Plausible.DataCase, async: false import Plausible.TestUtils import Ecto.Query alias Plausible.Google.Buffer @@ -7,7 +7,11 @@ defmodule Plausible.Google.BufferTest do setup [:create_user, :create_new_site, :set_buffer_size] defp set_buffer_size(_setup_args) do - Application.put_env(:plausible, :google, max_buffer_size: 10) + google_setting = Application.get_env(:plausible, :google) + on_exit(fn -> Application.put_env(:plausible, :google, google_setting) end) + test_setting = Keyword.put(google_setting, :max_buffer_size, 10) + Application.put_env(:plausible, :google, test_setting) + :ok end