Skip to content

Instantly share code, notes, and snippets.

@antulik antulik/spec.rb
Last active Jun 8, 2019

Embed
What would you like to do?
# OLD
context 'when token is expired' do
let!(:user) { create :user, discord_token_expired: true }
context 'when token refresh successful' do
let(:response) { build(:discord_refresh_token_response) }
before do
stub_request(:post, "https://discordapp.com/api/v6/oauth2/token")
.to_return(body: response.to_json)
end
it 'updates user token' do
expect { subject }.to(
change { user.reload.oauth_discord['credentials']['token'] }.to(response['access_token'])
)
end
it 'keep new token when transaction failed' do
ActiveRecord::Base.transaction do
subject
raise ActiveRecord::Rollback
end
expect(user.reload.oauth_discord['credentials']['token']).to eq response['access_token']
end
end
end
# NEW
shared_context 'user with expired token' do
let!(:user) { create :user, discord_token_expired: true }
end
shared_context 'token refresh successful' do
let(:response) { build(:discord_refresh_token_response) }
before do
stub_request(:post, "https://discordapp.com/api/v6/oauth2/token")
.to_return(body: response.to_json)
end
end
context do
when 'user with expired token'
when 'token refresh successful'
it 'updates user token' do
expect { subject }.to(
change { user.reload.oauth_discord['credentials']['token'] }.to(response['access_token'])
)
end
it 'keeps new token when transaction failed' do
ActiveRecord::Base.transaction do
subject
raise ActiveRecord::Rollback
end
expect(user.reload.oauth_discord['credentials']['token']).to eq response['access_token']
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.