|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +require 'oauth2/error' |
| 6 | + |
| 7 | +RSpec.describe 'Refreshing the auth token', type: :request do |
| 8 | + include ActiveSupport::Testing::TimeHelpers |
| 9 | + |
| 10 | + subject(:request) { get root_path } |
| 11 | + |
| 12 | + let(:logged_in_text) { 'Log out' } |
| 13 | + let(:stub_oauth_client) { instance_double(RpiAuth::OauthClient) } |
| 14 | + |
| 15 | + before do |
| 16 | + freeze_time |
| 17 | + allow(RpiAuth::OauthClient).to receive(:new).and_return(stub_oauth_client) |
| 18 | + allow(stub_oauth_client).to receive(:refresh_credentials).with(any_args) |
| 19 | + RpiAuth.configuration.user_model = 'User' |
| 20 | + end |
| 21 | + |
| 22 | + after do |
| 23 | + unfreeze_time |
| 24 | + end |
| 25 | + |
| 26 | + shared_examples 'there is no attempt to renew the token' do |
| 27 | + it 'calls refresh_credentials on the oauth client' do |
| 28 | + request |
| 29 | + expect(stub_oauth_client).not_to have_received(:refresh_credentials) |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + shared_examples 'there is an attempt to renew the token' do |
| 34 | + it 'does not call refresh_credentials on the oauth client' do |
| 35 | + request |
| 36 | + expect(stub_oauth_client).to have_received(:refresh_credentials) |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + shared_examples 'the user is logged in' do |
| 41 | + it do |
| 42 | + request |
| 43 | + expect(response.body).to include(logged_in_text) |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + shared_examples 'the user is logged out' do |
| 48 | + it do |
| 49 | + request |
| 50 | + expect(response.body).not_to include(logged_in_text) |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + context 'when not logged in' do |
| 55 | + it_behaves_like 'the user is logged out' |
| 56 | + it_behaves_like 'there is no attempt to renew the token' |
| 57 | + end |
| 58 | + |
| 59 | + context 'when logged in' do |
| 60 | + let(:user) { User.new(expires_at:) } |
| 61 | + |
| 62 | + before do |
| 63 | + log_in(user:) |
| 64 | + end |
| 65 | + |
| 66 | + context 'when the access token has not expired' do |
| 67 | + let(:expires_at) { 10.seconds.from_now } |
| 68 | + |
| 69 | + it_behaves_like 'the user is logged in' |
| 70 | + it_behaves_like 'there is no attempt to renew the token' |
| 71 | + end |
| 72 | + |
| 73 | + context 'when the access token has expired' do |
| 74 | + let(:expires_at) { 10.seconds.ago } |
| 75 | + |
| 76 | + before do |
| 77 | + allow(stub_oauth_client).to receive(:refresh_credentials).with(any_args).and_return({ access_token: 'foo', |
| 78 | + refresh_token: 'bar', |
| 79 | + expires_at: 10.minutes.from_now }) |
| 80 | + end |
| 81 | + |
| 82 | + it_behaves_like 'the user is logged in' |
| 83 | + it_behaves_like 'there is an attempt to renew the token' |
| 84 | + |
| 85 | + context 'when an OAuth error is raised' do |
| 86 | + before do |
| 87 | + allow(stub_oauth_client).to receive(:refresh_credentials).with(any_args).and_raise(OAuth2::Error.new('blargh')) |
| 88 | + end |
| 89 | + |
| 90 | + it_behaves_like 'the user is logged out' |
| 91 | + it_behaves_like 'there is an attempt to renew the token' |
| 92 | + end |
| 93 | + end |
| 94 | + end |
| 95 | +end |
0 commit comments