Fix and test Google Analytics token refresh (#2381)
This commit fixes a bug where Google Analytics import tokens were not being refreshed properly because the function was not returning the expected tuple. Thanks to @aerosol we can nicely test this now.
This commit is contained in:
parent
8e75f2fc07
commit
73444ddfb6
|
|
@ -0,0 +1,7 @@
|
||||||
|
{
|
||||||
|
"access_token": "1/fFAGRNJru1FTz70BzhT3Zg",
|
||||||
|
"expires_in": 3920,
|
||||||
|
"token_type": "Bearer",
|
||||||
|
"scope": "email https://www.googleapis.com/auth/analytics.readonly",
|
||||||
|
"refresh_token": "1//xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI"
|
||||||
|
}
|
||||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
|
@ -219,15 +219,13 @@ defmodule Plausible.Google.Api do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp maybe_refresh_token({access_token, nil, nil}) do
|
|
||||||
{:ok, access_token}
|
|
||||||
end
|
|
||||||
|
|
||||||
defp maybe_refresh_token({access_token, refresh_token, expires_at}) do
|
defp maybe_refresh_token({access_token, refresh_token, expires_at}) do
|
||||||
if needs_to_refresh_token?(expires_at) do
|
with true <- needs_to_refresh_token?(expires_at),
|
||||||
do_refresh_token(refresh_token)
|
{:ok, {new_access_token, _expires_at}} <- do_refresh_token(refresh_token) do
|
||||||
|
{:ok, new_access_token}
|
||||||
else
|
else
|
||||||
{:ok, access_token}
|
false -> {:ok, access_token}
|
||||||
|
{:error, cause} -> {:error, cause}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -203,7 +203,7 @@ defmodule Plausible.Google.HTTP do
|
||||||
redirect_uri: redirect_uri()
|
redirect_uri: redirect_uri()
|
||||||
}
|
}
|
||||||
|
|
||||||
case HTTPClient.post(url, headers, params) do
|
case HTTPClient.impl().post(url, headers, params) do
|
||||||
{:ok, %Finch.Response{body: body, status: 200}} ->
|
{:ok, %Finch.Response{body: body, status: 200}} ->
|
||||||
{:ok, body}
|
{:ok, body}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,51 @@ defmodule Plausible.Google.ApiTest do
|
||||||
|
|
||||||
setup [:create_user, :create_new_site]
|
setup [:create_user, :create_new_site]
|
||||||
|
|
||||||
|
@refresh_token_body Jason.decode!(File.read!("fixture/ga_refresh_token.json"))
|
||||||
|
|
||||||
|
@full_report_mock [
|
||||||
|
"ga_report_imported_visitors.json",
|
||||||
|
"ga_report_imported_sources.json",
|
||||||
|
"ga_report_imported_pages.json",
|
||||||
|
"ga_report_imported_entry_pages.json",
|
||||||
|
"ga_report_imported_exit_pages.json",
|
||||||
|
"ga_report_imported_locations.json",
|
||||||
|
"ga_report_imported_devices.json",
|
||||||
|
"ga_report_imported_browsers.json",
|
||||||
|
"ga_report_imported_operating_systems.json"
|
||||||
|
]
|
||||||
|
|> Enum.map(&File.read!/1)
|
||||||
|
|> Enum.map(&Jason.decode!/1)
|
||||||
|
|
||||||
|
test "import_analytics/4 refreshes OAuth token when needed", %{site: site} do
|
||||||
|
past = DateTime.add(DateTime.utc_now(), -3600, :second)
|
||||||
|
auth = {"redacted_access_token", "redacted_refresh_token", DateTime.to_iso8601(past)}
|
||||||
|
range = Date.range(~D[2020-01-01], ~D[2020-02-02])
|
||||||
|
|
||||||
|
expect(Plausible.HTTPClient.Mock, :post, fn "https://www.googleapis.com/oauth2/v4/token",
|
||||||
|
headers,
|
||||||
|
body ->
|
||||||
|
assert [{"content-type", "application/x-www-form-urlencoded"}] == headers
|
||||||
|
|
||||||
|
assert %{
|
||||||
|
grant_type: :refresh_token,
|
||||||
|
redirect_uri: "http://localhost:8000/auth/google/callback",
|
||||||
|
refresh_token: "redacted_refresh_token"
|
||||||
|
} = body
|
||||||
|
|
||||||
|
{:ok, %Finch.Response{status: 200, body: @refresh_token_body}}
|
||||||
|
end)
|
||||||
|
|
||||||
|
for report <- @full_report_mock do
|
||||||
|
expect(Plausible.HTTPClient.Mock, :post, fn _url, headers, _body, _opts ->
|
||||||
|
assert [{"Authorization", "Bearer 1/fFAGRNJru1FTz70BzhT3Zg"}] == headers
|
||||||
|
{:ok, %Finch.Response{status: 200, body: report}}
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
assert :ok == Plausible.Google.Api.import_analytics(site, range, "123551", auth)
|
||||||
|
end
|
||||||
|
|
||||||
describe "fetch_and_persist/4" do
|
describe "fetch_and_persist/4" do
|
||||||
@ok_response Jason.decode!(File.read!("fixture/ga_batch_report.json"))
|
@ok_response Jason.decode!(File.read!("fixture/ga_batch_report.json"))
|
||||||
@no_report_response Jason.decode!(File.read!("fixture/ga_report_empty_rows.json"))
|
@no_report_response Jason.decode!(File.read!("fixture/ga_report_empty_rows.json"))
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue