diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 7f479ca..b6e8ae9 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2024-12-01 16:21:41 UTC using RuboCop version 1.68.0. +# on 2024-12-07 14:38:33 UTC using RuboCop version 1.68.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -114,7 +114,7 @@ RSpec/EmptyExampleGroup: Exclude: - 'spec/models/athlete_spec.rb' -# Offense count: 167 +# Offense count: 168 # Configuration parameters: CountAsOne. RSpec/ExampleLength: Max: 36 @@ -155,13 +155,13 @@ RSpec/InstanceVariable: RSpec/LetSetup: Enabled: false -# Offense count: 95 +# Offense count: 96 # Configuration parameters: . # SupportedStyles: have_received, receive RSpec/MessageSpies: EnforcedStyle: receive -# Offense count: 207 +# Offense count: 208 RSpec/MultipleExpectations: Max: 11 @@ -195,7 +195,7 @@ RSpec/RepeatedExample: RSpec/SpecFilePathFormat: Enabled: false -# Offense count: 35 +# Offense count: 36 RSpec/StubbedMock: Exclude: - 'spec/api/endpoints/slack_endpoint_spec.rb' @@ -331,7 +331,7 @@ Style/StringConcatenation: - 'slack-strava/api/helpers/error_helpers.rb' - 'slack-strava/models/team.rb' -# Offense count: 233 +# Offense count: 235 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns. # URISchemes: http, https diff --git a/slack-strava/models/club.rb b/slack-strava/models/club.rb index dc16f11..249b1ee 100644 --- a/slack-strava/models/club.rb +++ b/slack-strava/models/club.rb @@ -189,9 +189,16 @@ def handle_slack_error(e) def handle_strava_error(e) logger.error e case e.message - when /Forbidden/, /Authorization Error/ + when 'Forbidden', 'Authorization Error' reset_access_tokens! dm! 'There was an authorization problem. Please reconnect the club via /slava clubs.' + when 'Bad Request' + error = e.errors&.first + code = error && error['code'] + if code == 'invalid' + reset_access_tokens! + dm! 'There was an authorization problem refreshing the club access token. Please reconnect the club via /slava clubs.' + end end raise e end diff --git a/spec/models/club_spec.rb b/spec/models/club_spec.rb index afd307e..249af61 100644 --- a/spec/models/club_spec.rb +++ b/spec/models/club_spec.rb @@ -119,6 +119,36 @@ expect(club.token_expires_at).to be_nil end end + it 'disconnects club on refresh token failure' do + allow(club.strava_client).to receive(:club_activities).and_raise( + Strava::Errors::Fault.new( + 401, + body: { + 'message' => 'Bad Request', + 'errors' => [ + { + 'resource' => 'RefreshToken', + 'field' => 'refresh_token', + 'code' => 'invalid' + } + ] + } + ) + ) + expect(club.team.slack_client).to receive(:chat_postMessage).with( + club.to_slack.merge( + text: 'There was an authorization problem refreshing the club access token. Please reconnect the club via /slava clubs.', + channel: club.channel_id, + as_user: true + ) + ).and_return('ts' => 1) + expect { club.sync_last_strava_activity! }.to raise_error Strava::Errors::Fault + expect(club.access_token).to be_nil + expect(club.token_type).to be_nil + expect(club.refresh_token).to be_nil + expect(club.token_expires_at).to be_nil + end + it 'disables sync on 404' do expect(club.sync_activities?).to be true allow(club.strava_client).to receive(:club_activities).and_raise(