Skip to content

Commit

Permalink
DEV: Promote AccessTokenInvalid to problem check
Browse files Browse the repository at this point in the history
  • Loading branch information
Drenmi committed Apr 23, 2024
1 parent 2b95a18 commit 6ceb08d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 18 deletions.
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

0 comments on commit 6ceb08d

Please sign in to comment.