Skip to content

Commit

Permalink
Disable club sync on invalid access token.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Dec 7, 2024
1 parent b3f3d70 commit f5a5f00
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
12 changes: 6 additions & 6 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion slack-strava/models/club.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions spec/models/club_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit f5a5f00

Please sign in to comment.