diff --git a/app/models/stacks_rights.rb b/app/models/stacks_rights.rb index fb24257d..1eec6afc 100644 --- a/app/models/stacks_rights.rb +++ b/app/models/stacks_rights.rb @@ -59,9 +59,11 @@ def cocina_file end def find_file - public_json.dig('structural', 'contains') - .lazy.flat_map { |file_set| file_set.dig('structural', 'contains') } - .find { |file| file['filename'] == file_name } || raise(ActionController::MissingFile, "File not found '#{file_name}'") + file_sets = public_json.dig('structural', 'contains') + raise(ActionController::MissingFile, "File not found '#{file_name}'") unless file_sets # Trap for Collections + + file_sets.lazy.flat_map { |file_set| file_set.dig('structural', 'contains') } + .find { |file| file['filename'] == file_name } || raise(ActionController::MissingFile, "File not found '#{file_name}'") end def public_json diff --git a/spec/requests/iiif_spec.rb b/spec/requests/iiif_spec.rb index 892a348b..7f435381 100644 --- a/spec/requests/iiif_spec.rb +++ b/spec/requests/iiif_spec.rb @@ -265,17 +265,6 @@ end describe 'image requests for world readable items' do - let(:ability) { instance_double(CocinaAbility, can?: false) } - - before do - # for the cache headers - allow_any_instance_of(IiifController).to receive(:anonymous_ability).and_return ability - # for authorize! in #show - allow_any_instance_of(IiifController).to receive(:authorize!).with(:read, Projection).and_return(true) - # for current_image - allow_any_instance_of(IiifController).to receive(:can?).with(:download, StacksImage).and_return(true) - end - context 'when the request is valid' do before do stub_request(:get, "http://imageserver-prod.stanford.edu/iiif/2/#{image_server_path('nr349ct7889', 'image.jp2')}/0,640,2552,2552/100,100/0/default.jpg") @@ -319,6 +308,19 @@ end end + context 'when the object has no filesets (e.g. a collection)' do + let(:public_json) do + { 'structural' => {} } + end + + it 'returns 404 Not Found' do + stub_request(:get, "http://imageserver-prod.stanford.edu/iiif/2/nr%2F349%2Fct%2F7889%2Fimage.jp2/0,640,2552,2552/100,100/0/default.jpg") + .to_return(status: 200, body: "", headers: {}) + get "/image/iiif/nr349ct7889%2Fimage.jp2/0,640,2552,2552/100,100/0/default.jpg" + expect(response).to have_http_status :not_found + end + end + context 'with the download flag set' do before do stub_request(:get, "http://imageserver-prod.stanford.edu/iiif/2/#{image_server_path('nr349ct7889', 'image.jp2')}/0,640,2552,2552/100,100/0/default.jpg")