|
1 | 1 | RSpec.describe CirroIO::Client::Base do
|
2 |
| - describe 'jwt_authentication' do |
3 |
| - before do |
4 |
| - configure_api_client |
5 |
| - end |
| 2 | + let :request_headers do |
| 3 | + { |
| 4 | + 'Accept' => 'application/vnd.api+json', |
| 5 | + 'Accept-Encoding' => 'gzip,deflate', |
| 6 | + 'Content-Type' => 'application/vnd.api+json', |
| 7 | + 'User-Agent' => 'Faraday v1.1.0', |
| 8 | + 'Authorization' => 'Bearer jwt-token', |
| 9 | + } |
| 10 | + end |
6 | 11 |
|
7 |
| - it 'sends correct token' do |
8 |
| - allow(JWT).to receive(:encode).and_return('jwt-token') |
| 12 | + let :response_headers do |
| 13 | + { |
| 14 | + 'Content-Type' => 'application/json', |
| 15 | + } |
| 16 | + end |
9 | 17 |
|
| 18 | + before do |
| 19 | + configure_api_client |
| 20 | + allow(JWT).to receive(:encode).and_return('jwt-token') |
| 21 | + end |
| 22 | + |
| 23 | + describe 'jwt_authentication' do |
| 24 | + it 'sends correct token' do |
10 | 25 | stub_request(:get, "#{test_site}/v1/app-workers/1")
|
11 |
| - .with(headers: { |
12 |
| - 'Accept' => 'application/vnd.api+json', |
13 |
| - 'Accept-Encoding' => 'gzip,deflate', |
14 |
| - 'Content-Type' => 'application/vnd.api+json', |
15 |
| - 'User-Agent' => 'Faraday v1.1.0', |
16 |
| - 'Authorization' => 'Bearer jwt-token', |
17 |
| - }) |
18 |
| - .to_return(body: File.read('./spec/fixtures/app_worker.json'), headers: { 'Content-Type' => 'application/json' }) |
| 26 | + .with(headers: request_headers) |
| 27 | + .to_return(body: File.read('./spec/fixtures/app_worker.json'), headers: response_headers) |
19 | 28 |
|
20 | 29 | app_worker = CirroIO::Client::AppWorker.find(1).first
|
21 | 30 |
|
22 | 31 | expect(app_worker.id).to eq('1')
|
23 | 32 | end
|
24 | 33 |
|
25 | 34 | it 'sends token correctly for custom requests as well' do
|
26 |
| - allow(JWT).to receive(:encode).and_return('jwt-token') |
27 |
| - |
28 | 35 | stub_request(:post, "#{test_site}/v1/bulk/custom-endpoint")
|
29 | 36 | .with(headers: {
|
30 | 37 | 'Accept' => '*/*',
|
|
38 | 45 | described_class.custom_post('bulk/custom-endpoint', { a: :b })
|
39 | 46 | end
|
40 | 47 | end
|
| 48 | + |
| 49 | + describe 'configuration' do |
| 50 | + let(:other_site) { 'https://api.other.cirro.io' } |
| 51 | + let(:other_version) { 'vXXX' } |
| 52 | + |
| 53 | + before do |
| 54 | + CirroIO::Client::AppWorker.site = "#{other_site}/#{other_version}" |
| 55 | + end |
| 56 | + |
| 57 | + it 'supports multiple backends' do |
| 58 | + stub_request(:get, "#{other_site}/#{other_version}/app-workers/1") |
| 59 | + .with(headers: request_headers) |
| 60 | + .to_return(body: File.read('spec/fixtures/app_worker.json'), headers: response_headers) |
| 61 | + |
| 62 | + expect(CirroIO::Client::AppWorker.find(1).first.id).to eq('1') |
| 63 | + |
| 64 | + stub_request(:get, "#{test_site}/v1/app-users/3") |
| 65 | + .with(headers: request_headers) |
| 66 | + .to_return(body: File.read('spec/fixtures/app_user.json'), headers: response_headers) |
| 67 | + |
| 68 | + expect(CirroIO::Client::AppUser.find(3).first.id).to eq('3') |
| 69 | + end |
| 70 | + end |
41 | 71 | end
|
0 commit comments