Skip to content
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

DEV: Promote AccessTokenInvalid to problem check #137

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/services/problem_check/access_token_invalid.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class ProblemCheck::AccessTokenInvalid < ::ProblemCheck::InlineProblemCheck
self.priority = "high"

private

def translation_key
"dashboard.patreon.access_token_invalid"
end
end
2 changes: 1 addition & 1 deletion config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ en:
errors:
patreon_creator_username_not_set: "You need to set the Patreon Creator Discourse Username setting before enabling Patreon Login."
dashboard:
patreon:
problem:
access_token_invalid: "Patreon Creator's access and refresh token values are incorrect. You must copy-paste new tokens from <a href='https://www.patreon.com/platform/documentation/clients'>Patreon website</a> to <a href='%{base_path}/admin/site_settings/category/all_results?filter=patreon'>site settings</a>."
patreon:
error:
Expand Down
5 changes: 1 addition & 4 deletions lib/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ def self.get(uri)
RateLimiter.new(nil, "patreon_api_hr", SiteSetting.max_patreon_api_reqs_per_hr, 1.hour)
limiter_day =
RateLimiter.new(nil, "patreon_api_day", SiteSetting.max_patreon_api_reqs_per_day, 1.day)
if AdminDashboardData.problem_message_check(ACCESS_TOKEN_INVALID)
AdminDashboardData.clear_problem_message(ACCESS_TOKEN_INVALID)
end

limiter_hr.performed! unless limiter_hr.can_perform?

Expand All @@ -44,7 +41,7 @@ def self.get(uri)
when 200
return JSON.parse response.body
when 401
AdminDashboardData.add_problem_message(ACCESS_TOKEN_INVALID, 7.hours)
ProblemCheckTracker[:access_token_invalid].problem!
else
e = ::Patreon::InvalidApiResponse.new(response.body.presence || "")
e.set_backtrace(caller)
Expand Down
5 changes: 3 additions & 2 deletions plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,14 @@ def self.all
require_relative "app/jobs/regular/sync_patron_groups"
require_relative "app/jobs/scheduled/patreon_sync_patrons_to_groups"
require_relative "app/jobs/scheduled/patreon_update_tokens"
require_relative "app/services/problem_check/access_token_invalid"
require_relative "lib/api"
require_relative "lib/seed"
require_relative "lib/campaign"
require_relative "lib/pledge"
require_relative "lib/patron"
require_relative "lib/tokens"

AdminDashboardData.problem_messages << ::Patreon::Api::ACCESS_TOKEN_INVALID

Patreon::Engine.routes.draw do
get "/rewards" => "patreon_admin#rewards", :constraints => AdminConstraint.new
get "/list" => "patreon_admin#list", :constraints => AdminConstraint.new
Expand Down Expand Up @@ -144,6 +143,8 @@ def self.all
add_to_serializer(:current_user, :show_donation_prompt?) do
Patreon.show_donation_prompt_to_user?(object)
end

register_problem_check ProblemCheck::AccessTokenInvalid
end

# Authentication with Patreon
Expand Down
22 changes: 11 additions & 11 deletions spec/lib/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ def stub(status)
stub_request(:get, url).to_return(content)
end

before { SiteSetting.stubs(patreon_enabled: true) }

it "should add admin warning message for invalid api response" do
stub(401)

expect(described_class.get(url)).to eq(error: I18n.t(described_class::INVALID_RESPONSE))
expect(
AdminDashboardData.problem_message_check(described_class::ACCESS_TOKEN_INVALID).sub(
"%{base_path}",
"",
),
).to eq(I18n.t(described_class::ACCESS_TOKEN_INVALID, base_path: ""))
described_class.get(url)

expect(ProblemCheckTracker[:access_token_invalid].blips).to eq(1)
expect(AdminNotice.find_by(identifier: :access_token_invalid).message).to eq(
I18n.t("dashboard.problem.access_token_invalid", base_path: Discourse.base_path),
)
end

it "should not add admin warning message for valid api response" do
stub(200)

expect(described_class.get(url)).to eq({})
expect(AdminDashboardData.problem_message_check(described_class::ACCESS_TOKEN_INVALID)).to eq(
nil,
)
expect(ProblemCheckTracker[:access_token_invalid].blips).to eq(0)
end

it "should add warning log" do
Expand Down
Loading