Skip to content

Commit 5a36a39

Browse files
committed
Add CORS support
1 parent 98c801b commit 5a36a39

File tree

5 files changed

+33
-12
lines changed

5 files changed

+33
-12
lines changed

config/application.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,16 @@ class Application < Rails::Application
3636
config.middleware.use config.session_store, config.session_options
3737

3838
# CAS configuration
39-
# - NOTE: overridden in production.rb
40-
config.cas_host = ENV.fetch('CAS_HOST') { 'auth-test.berkeley.edu' }
39+
config.cas_host = ENV.fetch('CAS_HOST') do
40+
"#{Rails.env.production? ? 'auth' : 'auth-test'}.berkeley.edu"
41+
end
42+
43+
Rails.application.config.hosts.append(
44+
'.ucblib.org',
45+
'.lib.berkeley.edu',
46+
'.pantheon.berkeley.edu',
47+
config.cas_host
48+
)
4149

4250
BerkeleyLibrary::Alma::Config.default!
4351

config/environments/production.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,4 @@
6363
# logger.formatter = config.log_formatter
6464
# config.logger = ActiveSupport::TaggedLogging.new(logger)
6565
# end
66-
67-
# CAS configuration
68-
# - NOTE: overrides application.rb
69-
config.cas_host = ENV.fetch('CAS_HOST') { 'auth.berkeley.edu' }
70-
71-
if ENV['CI'].blank? # TODO: what if it's not?
72-
Rails.application.config.hosts << '.ucblib.org'
73-
Rails.application.config.hosts << '.lib.berkeley.edu'
74-
Rails.application.config.hosts << config.cas_host
75-
end
7666
end

config/environments/test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,9 @@
4747

4848
# Annotate rendered view with file names.
4949
# config.action_view.annotate_rendered_view_with_filenames = true
50+
51+
Rails.application.config.hosts.append(
52+
ActionDispatch::Integration::Session::DEFAULT_HOST,
53+
ActionController::Renderer::DEFAULTS['http_host']
54+
)
5055
end

spec/lib/cors_helper_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,15 @@
66
invalid_uri = 'not a URI'
77
expect(CorsHelper.allow?(invalid_uri)).to eq(false)
88
end
9+
10+
it 'returns true for a lib.berkeley.edu origin' do
11+
source = 'https://www.lib.berkeley.edu/'
12+
expect(CorsHelper.allow?(source)).to eq(true)
13+
end
14+
15+
it 'returns true for a pantheon.berkeley.edu origin' do
16+
source = 'https://dev.pantheon.berkeley.edu/'
17+
expect(CorsHelper.allow?(source)).to eq(true)
18+
end
919
end
1020
end

spec/requests/facets_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@
2727
parsed_response = JSON.parse(response.body)
2828
expect(parsed_response).to contain_jsonapi_for(Facet.all, { include: [:terms] })
2929
end
30+
31+
it 'returns a Access-Control-Allow-Origin header in response to an Origin header' do
32+
origin = 'www.lib.berkeley.edu'
33+
get facets_url, params: { include: 'terms' }, headers: { 'Origin' => origin }
34+
35+
expect(response).to be_successful
36+
expect(response.headers['Access-Control-Allow-Origin']).to eq(origin)
37+
end
3038
end
3139
end
3240
end

0 commit comments

Comments
 (0)