|
7 | 7 |
|
8 | 8 | subject { Class.new.send(:include, described_class).new } |
9 | 9 |
|
| 10 | + let(:token) { 'test-token' } |
| 11 | + let(:user) { double('user', id: 'user-id', email: 'test@example.com') } |
| 12 | + let(:auth_token) { double('auth_token', user: user) } |
| 13 | + |
10 | 14 | describe '#save_token / #fetch_token' do |
11 | 15 | it 'reads back a token it saved' do |
12 | 16 | subject.save_token('foo') |
|
38 | 42 | end |
39 | 43 | end |
40 | 44 | end |
| 45 | + |
| 46 | + describe '#current_token' do |
| 47 | + before do |
| 48 | + subject.save_token(token) |
| 49 | + end |
| 50 | + |
| 51 | + it 'returns the current auth token' do |
| 52 | + expect(Aptible::Auth::Token).to receive(:current_token) |
| 53 | + .with(token: token) |
| 54 | + .and_return(auth_token) |
| 55 | + |
| 56 | + expect(subject.current_token).to eq(auth_token) |
| 57 | + end |
| 58 | + |
| 59 | + it 'raises Thor::Error on 401 unauthorized' do |
| 60 | + response = Faraday::Response.new(status: 401) |
| 61 | + error = HyperResource::ClientError.new( |
| 62 | + '401 (invalid_token) Invalid Token', response: response |
| 63 | + ) |
| 64 | + expect(Aptible::Auth::Token).to receive(:current_token) |
| 65 | + .with(token: token) |
| 66 | + .and_raise(error) |
| 67 | + |
| 68 | + expect { subject.current_token } |
| 69 | + .to raise_error(Thor::Error, /Invalid Token/) |
| 70 | + end |
| 71 | + |
| 72 | + it 'raises Thor::Error on 403 forbidden' do |
| 73 | + response = Faraday::Response.new(status: 403) |
| 74 | + error = HyperResource::ClientError.new('403 (forbidden) Access denied', |
| 75 | + response: response) |
| 76 | + expect(Aptible::Auth::Token).to receive(:current_token) |
| 77 | + .with(token: token) |
| 78 | + .and_raise(error) |
| 79 | + |
| 80 | + expect { subject.current_token } |
| 81 | + .to raise_error(Thor::Error, /Access denied/) |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + describe '#whoami' do |
| 86 | + before do |
| 87 | + subject.save_token(token) |
| 88 | + end |
| 89 | + |
| 90 | + it 'returns the current user' do |
| 91 | + expect(Aptible::Auth::Token).to receive(:current_token) |
| 92 | + .with(token: token) |
| 93 | + .and_return(auth_token) |
| 94 | + |
| 95 | + expect(subject.whoami).to eq(user) |
| 96 | + end |
| 97 | + |
| 98 | + it 'raises Thor::Error on API error' do |
| 99 | + response = Faraday::Response.new(status: 401) |
| 100 | + error = HyperResource::ClientError.new( |
| 101 | + '401 (invalid_token) Invalid Token', response: response |
| 102 | + ) |
| 103 | + expect(Aptible::Auth::Token).to receive(:current_token) |
| 104 | + .with(token: token) |
| 105 | + .and_raise(error) |
| 106 | + |
| 107 | + expect { subject.whoami } |
| 108 | + .to raise_error(Thor::Error, /Invalid Token/) |
| 109 | + end |
| 110 | + end |
41 | 111 | end |
0 commit comments