|
| 1 | +require 'rails_helper' |
| 2 | +require 'support/iiif_check_helper' |
| 3 | + |
| 4 | +RSpec.describe HealthChecks::IIIFItemCheck do |
| 5 | + subject(:check) { described_class.new } |
| 6 | + |
| 7 | + def run_check |
| 8 | + check.run |
| 9 | + check |
| 10 | + end |
| 11 | + |
| 12 | + describe '#check' do |
| 13 | + |
| 14 | + context 'with a valid IIIF base URI' do |
| 15 | + include_context 'IIIF item checks' |
| 16 | + |
| 17 | + it 'fails and sets message when the GET health check response is not successful' do |
| 18 | + response = instance_double('Faraday::Response', |
| 19 | + success?: false, |
| 20 | + status: 503, |
| 21 | + headers: {}) |
| 22 | + |
| 23 | + allow(connection).to receive(:get).with(test_uri).and_return(response) |
| 24 | + |
| 25 | + run_check |
| 26 | + |
| 27 | + expect(check.message).to match(/returned status 503/) |
| 28 | + |
| 29 | + if check.respond_to?(:failure?) |
| 30 | + expect(check.failure?).to be(true) |
| 31 | + else |
| 32 | + expect(check.instance_variable_get(:@failure_occurred)).to be(true) |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + it 'fails and sets message when Access-Control-Allow-Origin header is missing/blank' do |
| 37 | + response = instance_double('Faraday::Response', |
| 38 | + success?: true, |
| 39 | + status: 200, |
| 40 | + headers: { 'Access-Control-Allow-Origin' => '' }) |
| 41 | + |
| 42 | + allow(connection).to receive(:get).with(test_uri).and_return(response) |
| 43 | + |
| 44 | + run_check |
| 45 | + |
| 46 | + expect(check.message).to eq( |
| 47 | + "GET #{test_uri} missing Access-Control-Allow-Origin header" |
| 48 | + ) |
| 49 | + |
| 50 | + if check.respond_to?(:failure?) |
| 51 | + expect(check.failure?).to be(true) |
| 52 | + else |
| 53 | + expect(check.instance_variable_get(:@failure_occurred)).to be(true) |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + it 'does not fail when reachable and ACAO header present' do |
| 58 | + response = instance_double('Faraday::Response', |
| 59 | + success?: true, |
| 60 | + status: 200, |
| 61 | + headers: { 'Access-Control-Allow-Origin' => '*' }) |
| 62 | + |
| 63 | + allow(connection).to receive(:get).with(test_uri).and_return(response) |
| 64 | + |
| 65 | + run_check |
| 66 | + |
| 67 | + expect(check.message).to eq('HTTP check successful') |
| 68 | + |
| 69 | + if check.respond_to?(:failure?) |
| 70 | + expect(check.failure?).to be(false) |
| 71 | + else |
| 72 | + expect(check.instance_variable_get(:@failure_occurred)).not_to be(true) |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + it 'fails and sets message when an exception is raised' do |
| 77 | + allow(connection).to receive(:get).with(test_uri).and_raise(StandardError, 'boom') |
| 78 | + |
| 79 | + run_check |
| 80 | + |
| 81 | + expect(check.message).to match('StandardError') |
| 82 | + |
| 83 | + if check.respond_to?(:failure?) |
| 84 | + expect(check.failure?).to be(true) |
| 85 | + else |
| 86 | + expect(check.instance_variable_get(:@failure_occurred)).to be(true) |
| 87 | + end |
| 88 | + end |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + describe 'private helpers' do |
| 93 | + describe '#iiif_connection' do |
| 94 | + it 'configures Faraday timeouts' do |
| 95 | + conn = check.send(:iiif_connection) |
| 96 | + |
| 97 | + expect(conn.options.open_timeout).to eq(2) |
| 98 | + expect(conn.options.timeout).to eq(3) |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + describe '#iiif_base_uri' do |
| 103 | + it 'returns nil when iiif_base_uri is nil' do |
| 104 | + allow(Lending::Config).to receive(:iiif_base_uri).and_return(nil) |
| 105 | + |
| 106 | + expect(check.send(:iiif_base_uri)).to be_nil |
| 107 | + end |
| 108 | + |
| 109 | + it 'returns the Lending::Config value when set' do |
| 110 | + allow(Lending::Config).to receive(:iiif_base_uri).and_return('http://example.test/iiif') |
| 111 | + expect(check.send(:iiif_base_uri)).to eq('http://example.test/iiif') |
| 112 | + end |
| 113 | + end |
| 114 | + |
| 115 | + describe '#iiif_test_uri' do |
| 116 | + it 'returns nil when iiif_base_uri is nil' do |
| 117 | + allow(Lending::Config).to receive(:iiif_base_uri).and_return(nil) |
| 118 | + |
| 119 | + expect(check.send(:iiif_test_uri)).to be_nil |
| 120 | + end |
| 121 | + |
| 122 | + context 'with IIIF item checks' do |
| 123 | + include_context 'IIIF item checks' |
| 124 | + |
| 125 | + it 'returns nil when no Item exists' do |
| 126 | + allow(Lending::Config).to receive(:iiif_base_uri).and_return(base_uri) |
| 127 | + stub_items(active_first: nil, inactive_first: nil) |
| 128 | + |
| 129 | + expect(check.send(:iiif_test_uri)).to be_nil |
| 130 | + end |
| 131 | + |
| 132 | + it 'builds a test uri from an active item (or inactive fallback)' do |
| 133 | + base_uri = URI('http://example.test/iiif/') |
| 134 | + allow(Lending::Config).to receive(:iiif_base_uri).and_return(base_uri) |
| 135 | + |
| 136 | + iiif_dir = instance_double('IiifDirectory', first_image_url_path: 'some/path') |
| 137 | + item = instance_double('Item', iiif_directory: iiif_dir) |
| 138 | + stub_items(active_first: item, inactive_first: nil) |
| 139 | + |
| 140 | + expect(BerkeleyLibrary::Util::URIs).to receive(:append) |
| 141 | + .with(base_uri, 'some/path', 'info.json') |
| 142 | + .and_return('http://example.test/iiif/some/path/info.json') |
| 143 | + |
| 144 | + expect(check.send(:iiif_test_uri)).to eq('http://example.test/iiif/some/path/info.json') |
| 145 | + end |
| 146 | + end |
| 147 | + end |
| 148 | + |
| 149 | + describe '#perform_request' do |
| 150 | + it 'returns a failure when it cannot construct test uri' do |
| 151 | + allow(Lending::Config).to receive(:iiif_base_uri).and_return(nil) |
| 152 | + |
| 153 | + result = check.send(:perform_request) |
| 154 | + |
| 155 | + expect(result).to eq(message: 'Unable to construct healthcheck URI', failure: true, rsp: nil) |
| 156 | + end |
| 157 | + |
| 158 | + context 'with IIIF item checks' do |
| 159 | + include_context 'IIIF item checks' |
| 160 | + it 'returns a failure when the GET response is not successful' do |
| 161 | + response = instance_double('Faraday::Response', |
| 162 | + success?: false, |
| 163 | + status: 503, |
| 164 | + headers: {}) |
| 165 | + |
| 166 | + allow(connection).to receive(:get).with(test_uri).and_return(response) |
| 167 | + |
| 168 | + result = check.send(:perform_request) |
| 169 | + |
| 170 | + expect(result[:failure]).to be(true) |
| 171 | + expect(result[:message]).to match(/returned status 503/) |
| 172 | + end |
| 173 | + |
| 174 | + it 'returns a failure when Access-Control-Allow-Origin header is missing/blank' do |
| 175 | + response = instance_double('Faraday::Response', |
| 176 | + success?: true, |
| 177 | + status: 200, |
| 178 | + headers: { 'Access-Control-Allow-Origin' => '' }) |
| 179 | + |
| 180 | + allow(connection).to receive(:get).with(test_uri).and_return(response) |
| 181 | + |
| 182 | + result = check.send(:perform_request) |
| 183 | + |
| 184 | + expect(result).to eq( |
| 185 | + message: "GET #{test_uri} missing Access-Control-Allow-Origin header", |
| 186 | + failure: true, |
| 187 | + rsp: response |
| 188 | + ) |
| 189 | + end |
| 190 | + |
| 191 | + it 'returns ok when reachable and ACAO header present' do |
| 192 | + response = instance_double('Faraday::Response', |
| 193 | + success?: true, |
| 194 | + status: 200, |
| 195 | + headers: { 'Access-Control-Allow-Origin' => '*' }) |
| 196 | + |
| 197 | + allow(connection).to receive(:get).with(test_uri).and_return(response) |
| 198 | + |
| 199 | + result = check.send(:perform_request) |
| 200 | + |
| 201 | + expect(result).to eq(message: 'HTTP check successful', failure: false, rsp: response) |
| 202 | + end |
| 203 | + end |
| 204 | + end |
| 205 | + end |
| 206 | +end |
0 commit comments