Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cors headers for file show method #1068

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/controllers/file_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class FileController < ApplicationController
render plain: 'File not found', status: :not_found
end

before_action :set_cors_headers, only: [:show], if: proc { current_file.stacks_rights.stanford_restricted? }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well that looks easy -- nicely done!


# rubocop:disable Metrics/AbcSize
def show
return unless stale?(**cache_headers)
Expand Down
43 changes: 43 additions & 0 deletions spec/controllers/file_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,30 @@
Factories.cocina_with_file
end

let(:stanford_json) do
{
'structural' => {
'contains' => [
{
'structural' => {
'contains' => [
{
'filename' => 'xf680rd3068_1.jp2',
'access' => {
'view' => 'stanford',
'download' => 'stanford'
}
}
]
}
}
]
}
}
end

let(:file) { StacksFile.new(id: druid, file_name: 'xf680rd3068_1.jp2') }

describe '#show' do
let(:druid) { 'nr349ct7889' }

Expand All @@ -20,6 +44,7 @@
it 'sends the file to the user' do
expect(controller).to receive(:send_file).with(path, filename: 'image.jp2', disposition: :inline).and_call_original
subject
expect(response.headers.to_h).to include 'Access-Control-Allow-Origin' => '*'
end

context 'when file is not in a content addressable path' do
Expand All @@ -32,6 +57,24 @@
"content-disposition" => "attachment; filename=\"image.jp2\"; filename*=UTF-8''image.jp2"
)
end

it 'sets disposition attachment with download param' do
expect(controller).to receive(:send_file).with(file.path, disposition: :attachment).and_call_original
get :show, params: { id: 'xf680rd3068', file_name: 'xf680rd3068_1.jp2', download: 'any' }
end

context 'when Stanford restricted' do
before do
# stub_rights_xml(stanford_restricted_rights_xml)
allow(Purl).to receive(:public_json).and_return(stanford_json)
end

it 'sends host-specific and credentials CORS headers' do
subject
expect(response.headers.to_h).to include 'Access-Control-Allow-Origin' => 'https://embed.stanford.edu',
'Access-Control-Allow-Credentials' => 'true'
end
end
end

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth adding a test for when the file is not Stanford Only to make sure the header is *?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, can just add that check to an existing spec (basically ensure the header is not there).

are you interested in taking that on and seeing if you can sort why CI is failing?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I can take a look.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that CI is running the tests like this, which fails for me locally (unlike a regular rspec run):

SETTINGS__FEATURES__COCINA=true bin/rake spec

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, yes. good catch. likely an issue with the mocking in the test not accounting for cocina rights computation

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Had a problem in my mocked cocina json (it wasn't internally consistent for stanford only).

context 'when file is in a content addressable path' do
Expand Down
Loading