|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | +require 'pega_api/client' |
| 5 | + |
| 6 | +RSpec.describe IvcChampva::PegaApi::Client do |
| 7 | + subject { described_class.new } |
| 8 | + |
| 9 | + describe 'get_report' do |
| 10 | + let(:body200and200) do # pega api response with HTTP status 200 and alternate status 200 |
| 11 | + fixture_path = Rails.root.join('modules', 'ivc_champva', 'spec', 'fixtures', 'pega_api_json', |
| 12 | + 'report_response_200_200.json') |
| 13 | + fixture_path.read |
| 14 | + end |
| 15 | + |
| 16 | + let(:body200and500) do # pega api response with HTTP status 200 and alternate status 500 |
| 17 | + fixture_path = Rails.root.join('modules', 'ivc_champva', 'spec', 'fixtures', 'pega_api_json', |
| 18 | + 'report_response_200_500.json') |
| 19 | + fixture_path.read |
| 20 | + end |
| 21 | + |
| 22 | + let(:body403) do # pega api response with HTTP status 403 forbidden |
| 23 | + fixture_path = Rails.root.join('modules', 'ivc_champva', 'spec', 'fixtures', 'pega_api_json', |
| 24 | + 'report_response_403.json') |
| 25 | + fixture_path.read |
| 26 | + end |
| 27 | + |
| 28 | + context 'successful response from pega' do |
| 29 | + let(:faraday_response) { double('Faraday::Response', status: 200, body: body200and200) } |
| 30 | + |
| 31 | + before do |
| 32 | + allow_any_instance_of(Faraday::Connection).to receive(:post).with(anything).and_return(faraday_response) |
| 33 | + end |
| 34 | + |
| 35 | + it 'returns the body as an array of hashes' do |
| 36 | + result = subject.get_report(Date.new(2024, 11, 1), Date.new(2024, 12, 31)) |
| 37 | + |
| 38 | + expect(result[0]['Creation Date']).to eq('2024-11-27T08:42:11.372000') |
| 39 | + expect(result[0]['PEGA Case ID']).to eq('D-55824') |
| 40 | + expect(result[0]['Status']).to eq('Open') |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + context 'unsuccessful pega response with bad HTTP status' do |
| 45 | + let(:faraday_response) { double('Faraday::Response', status: 403, body: body403) } |
| 46 | + |
| 47 | + before do |
| 48 | + allow_any_instance_of(Faraday::Connection).to receive(:post).with(anything).and_return(faraday_response) |
| 49 | + end |
| 50 | + |
| 51 | + it 'raises error when response is 404' do |
| 52 | + expect { subject.get_report(nil, nil) }.to raise_error(IvcChampva::PegaApi::PegaApiError) |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + context 'unsuccessful pega response with bad alternate status' do |
| 57 | + let(:faraday_response) { double('Faraday::Response', status: 200, body: body200and500) } |
| 58 | + |
| 59 | + before do |
| 60 | + allow_any_instance_of(Faraday::Connection).to receive(:post).with(anything).and_return(faraday_response) |
| 61 | + end |
| 62 | + |
| 63 | + it 'raises error when alternate status is 500' do |
| 64 | + expect { subject.get_report(nil, nil) }.to raise_error(IvcChampva::PegaApi::PegaApiError) |
| 65 | + end |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + describe 'headers' do |
| 70 | + it 'returns the right headers' do |
| 71 | + result = subject.headers(Date.new(2024, 11, 1), Date.new(2024, 12, 31)) |
| 72 | + |
| 73 | + expect(result[:content_type]).to eq('application/json') |
| 74 | + expect(result['x-api-key']).to eq('fake_api_key') |
| 75 | + expect(result['date_start']).to eq('2024-11-01') |
| 76 | + expect(result['date_end']).to eq('2024-12-31') |
| 77 | + expect(result['case_id']).to eq('') |
| 78 | + end |
| 79 | + |
| 80 | + it 'returns the right headers with nil dates' do |
| 81 | + result = subject.headers(nil, nil) |
| 82 | + |
| 83 | + expect(result[:content_type]).to eq('application/json') |
| 84 | + expect(result['x-api-key']).to eq('fake_api_key') |
| 85 | + expect(result['date_start']).to eq('') |
| 86 | + expect(result['date_end']).to eq('') |
| 87 | + expect(result['case_id']).to eq('') |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + # Temporary, delete me |
| 92 | + # This test is used to hit the production endpoint when running locally. |
| 93 | + # It can be removed once we have some real code that uses the Pega API client. |
| 94 | + describe 'hit the production endpoint', skip: 'this is useful as a way to hit the API during local development' do |
| 95 | + let(:forced_headers) do |
| 96 | + { |
| 97 | + :content_type => 'application/json', |
| 98 | + # use the following line when running locally tp pull the key from an environment variable |
| 99 | + 'x-api-key' => ENV.fetch('PEGA_API_KEY'), # to set: export PEGA_API_KEY=insert1the2api3key4here |
| 100 | + 'date_start' => '', # '2024-11-01', # '11/01/2024', |
| 101 | + 'date_end' => '', # '2024-12-31', # '12/07/2024', |
| 102 | + 'case_id' => '' |
| 103 | + } |
| 104 | + end |
| 105 | + |
| 106 | + before do |
| 107 | + allow_any_instance_of(IvcChampva::PegaApi::Client).to receive(:headers).with(anything, anything) |
| 108 | + .and_return(forced_headers) |
| 109 | + end |
| 110 | + |
| 111 | + it 'returns report data' do |
| 112 | + VCR.configure do |c| |
| 113 | + c.allow_http_connections_when_no_cassette = true |
| 114 | + end |
| 115 | + |
| 116 | + result = subject.get_report(Date.new(2024, 11, 1), Date.new(2024, 12, 31)) |
| 117 | + expect(result.count).to be_positive |
| 118 | + |
| 119 | + # byebug # in byebug, type 'p result' to view the response |
| 120 | + end |
| 121 | + end |
| 122 | +end |
0 commit comments