|
7 | 7 | expect(client).to be_an_instance_of Net::HTTP::Post |
8 | 8 | end |
9 | 9 | end |
| 10 | + |
| 11 | + describe '.get_available_items(diaper_bank_id)' do |
| 12 | + subject { described_class.get_available_items(diaper_bank_id) } |
| 13 | + let(:diaper_bank_id) { Faker::Number.number } |
| 14 | + let(:fake_routes) { instance_double('Routes.instances') } |
| 15 | + let(:fake_route) { instance_double('route') } |
| 16 | + |
| 17 | + before do |
| 18 | + allow(described_class).to receive(:routes).and_return(fake_routes) |
| 19 | + allow(fake_routes).to receive(:partner_requests).with(diaper_bank_id).and_return(fake_route) |
| 20 | + end |
| 21 | + |
| 22 | + context 'when the response code is 200' do |
| 23 | + let(:fake_response) { instance_double('response', code: '200', body: { status: Faker::Number.number }.to_json) } |
| 24 | + before do |
| 25 | + allow(described_class).to receive(:diaper_get_request).with(fake_route).and_return(fake_response) |
| 26 | + end |
| 27 | + |
| 28 | + it 'should return the parsed response body' do |
| 29 | + expect(subject).to eq(JSON.parse(fake_response.body)) |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + context 'when the response code is not 200' do |
| 34 | + let(:fake_response) { instance_double('response', code: '400') } |
| 35 | + |
| 36 | + before do |
| 37 | + allow(described_class).to receive(:diaper_get_request).with(fake_route).and_return(fake_response) |
| 38 | + end |
| 39 | + |
| 40 | + it 'should return a empty array' do |
| 41 | + expect(subject).to eq([]) |
| 42 | + end |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + describe '.diaper_get_request(uri)' do |
| 47 | + subject { described_class.diaper_get_request(uri) } |
| 48 | + let(:uri) { 'fake-uri' } |
| 49 | + let(:fake_request) { {} } |
| 50 | + let(:fake_https_client) { instance_double(Net::HTTP) } |
| 51 | + let(:fake_response) { { status: 'fake' } } |
| 52 | + |
| 53 | + before do |
| 54 | + fake_x_api_key = Faker::Number.number |
| 55 | + allow(ENV).to receive(:[]).with('DIAPERBANK_KEY').and_return(fake_x_api_key) |
| 56 | + allow(Net::HTTP::Get).to receive(:new).with(uri).and_return(fake_request) |
| 57 | + allow(described_class).to receive(:https).with(uri).and_return(fake_https_client) |
| 58 | + allow(fake_https_client).to receive(:request).with({ |
| 59 | + "Content-Type" => "application/json", |
| 60 | + "X-Api-Key" => fake_x_api_key |
| 61 | + }).and_return(fake_response) |
| 62 | + end |
| 63 | + |
| 64 | + it 'should return the https response' do |
| 65 | + expect(subject).to eq(fake_response) |
| 66 | + end |
| 67 | + end |
10 | 68 | end |
0 commit comments