Skip to content

Commit b1bf78e

Browse files
committed
Refactor preview environment loading
This allows us to load preview environments in the correct context: e.g. on staging (where Rails.env.production? and !ENV['SERVE_TEST_UI'].blank?) the preview should point to https://galc-api.ucblib.org/. Routing is explicitly not enabled for the new preview controller/routes unless there is a value for ENV['SERVE_TEST_UI'].
1 parent 85e1e56 commit b1bf78e

File tree

11 files changed

+83
-8
lines changed

11 files changed

+83
-8
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ end
3636
group :test do
3737
gem 'database_cleaner-active_record', '~> 2.0'
3838
gem 'factory_bot_rails'
39+
gem 'rails-controller-testing'
3940
gem 'rspec', '~> 3.10'
4041
gem 'rspec_junit_formatter', '~> 0.5'
4142
gem 'rspec-rails', '~> 5.0'

Gemfile.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ GEM
218218
activesupport (= 7.0.4.3)
219219
bundler (>= 1.15.0)
220220
railties (= 7.0.4.3)
221+
rails-controller-testing (1.0.5)
222+
actionpack (>= 5.0.1.rc1)
223+
actionview (>= 5.0.1.rc1)
224+
activesupport (>= 5.0.1.rc1)
221225
rails-dom-testing (2.0.3)
222226
activesupport (>= 4.2.0)
223227
nokogiri (>= 1.6)
@@ -347,6 +351,7 @@ DEPENDENCIES
347351
puma (~> 5.0)
348352
rack-cors
349353
rails (~> 7.0.4)
354+
rails-controller-testing
350355
ransack (~> 2.6)
351356
rspec (~> 3.10)
352357
rspec-rails (~> 5.0)

app/controllers/auth_controller.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ class AuthController < ApplicationController
77
def index
88
raise Error::NotFoundError if ENV['SERVE_TEST_UI'].blank?
99

10-
# TODO: Something more elegant
11-
# Hack to get around the fact that API-only apps don't include an HTML renderer
12-
pathname = Rails.root.join('public', 'index.html')
13-
render xml: File.read(pathname), content_type: 'text/html'
10+
redirect_to preview_path
1411
end
1512

1613
def callback
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module Preview
2+
class ApplicationController < ActionController::Base; end
3+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module Preview
2+
class PreviewController < Preview::ApplicationController
3+
4+
def index
5+
@api_url = Rails.env.production? ? 'https://galc-api.ucblib.org' : 'http://localhost:3000'
6+
end
7+
end
8+
end

public/index.html renamed to app/views/preview/preview/index.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<main>
1717
<div class="layout-content">
1818
<div class="region region-content">
19-
<div id="galc-app" data-api-base-url="http://localhost:3000" class="block-views"></div>
19+
<div id="galc-app" data-api-base-url="<%= @api_url %>" class="block-views"></div>
2020
</div>
2121
</div>
2222
</main>

config/routes.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
get '/logout', to: 'auth#logout', as: :logout
66
get '/auth/:provider/callback', to: 'auth#callback', as: :omniauth_callback
77

8+
scope module: 'preview' do
9+
constraints(->(_) { ENV['SERVE_TEST_UI'].present? }) do
10+
get '/preview', to: 'preview#index', as: :preview
11+
end
12+
end
13+
814
defaults format: :jsonapi do
915
constraints(->(req) { req.format == :jsonapi }) do
1016
resources :closures

spec/requests/auth_spec.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,11 @@
4646
expect(response).to have_http_status(:not_found)
4747
end
4848

49-
it 'serves a test UI in staging' do
49+
it 'redirects to a test UI in staging' do
5050
allow(ENV).to receive(:[]).with('SERVE_TEST_UI').and_return(true)
5151

5252
get root_path
53-
expect(response).to have_http_status(:ok)
54-
expect(response.content_type).to start_with('text/html')
53+
expect(response).to redirect_to(preview_path)
5554
end
5655
end
5756

spec/requests/preview/preview_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe Preview::PreviewController, type: :request do
4+
describe 'GET /preview' do
5+
context 'with the preview environment enabled' do
6+
it 'shows a test UI in when SERVE_TEST_UI is true' do
7+
allow(ENV).to receive(:[]).with('SERVE_TEST_UI').and_return(true)
8+
get preview_path
9+
expect(response).to have_http_status(:ok)
10+
expect(response.content_type).to start_with('text/html')
11+
end
12+
13+
context 'sets the API URL correctly' do
14+
it 'sets the url for staging API in production' do
15+
allow(ENV).to receive(:[]).with('SERVE_TEST_UI').and_return(true)
16+
allow(Rails.env).to receive(:production?).and_return(true)
17+
get preview_path
18+
expect(assigns(:api_url)).to eq('https://galc-api.ucblib.org')
19+
end
20+
21+
it 'sets the url for API to be localhost in dev/test' do
22+
allow(ENV).to receive(:[]).with('SERVE_TEST_UI').and_return(true)
23+
allow(Rails.env).to receive(:production?).and_return(false)
24+
get preview_path
25+
expect(assigns(:api_url)).to eq('http://localhost:3000')
26+
end
27+
end
28+
end
29+
end
30+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe 'preview routing', type: :routing do
4+
it 'is not routable with an empty SERVE_TEST_UI' do
5+
allow(ENV).to receive(:[]).with('SERVE_TEST_UI').and_return(nil)
6+
expect(get: preview_path).not_to be_routable
7+
end
8+
9+
it 'routes to Preview::PreviewController when SERVE_TEST_UI is set' do
10+
allow(ENV).to receive(:[]).with('SERVE_TEST_UI').and_return('something')
11+
expect(get: preview_path).to route_to('preview/preview#index')
12+
end
13+
end

0 commit comments

Comments
 (0)