Skip to content

Commit f09a93e

Browse files
authored
Refactor preview environment loading (#14)
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']. * address code review changes and fix test * prefer assert_select over a regex parsing the rendered page * Improve configuration injection; separate preview from AuthController * ACAB includes rubocop 💎🚔
1 parent 85e1e56 commit f09a93e

File tree

12 files changed

+83
-28
lines changed

12 files changed

+83
-28
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: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,6 @@
33
class AuthController < ApplicationController
44
ERR_TICKET_MISMATCH = 'Ticket from callback URL parameter does not match credential from OmniAuth hash'.freeze
55

6-
# Debug UI for staging, 404 Not Found for production
7-
def index
8-
raise Error::NotFoundError if ENV['SERVE_TEST_UI'].blank?
9-
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'
14-
end
15-
166
def callback
177
logger.debug({ msg: 'Received omniauth callback', omniauth: auth_hash, params: params.to_unsafe_h })
188

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 = ENV['GALC_API_URL'] || 'http://localhost:3000'
6+
end
7+
end
8+
end
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
Rails.application.routes.draw do
2-
root to: 'auth#index'
32

43
direct(:login) { '/auth/calnet' } # convenience to provide login_url helper
54
get '/logout', to: 'auth#logout', as: :logout
65
get '/auth/:provider/callback', to: 'auth#callback', as: :omniauth_callback
76

7+
scope module: 'preview' do
8+
constraints(->(_) { ENV['SERVE_TEST_UI'].present? }) do
9+
root to: 'preview#index', as: :preview
10+
end
11+
end
12+
813
defaults format: :jsonapi do
914
constraints(->(req) { req.format == :jsonapi }) do
1015
resources :closures

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ services:
88
- db
99
environment:
1010
- SERVE_TEST_UI=${SERVE_TEST_UI:-true}
11+
- GALC_API_URL=${GALC_API_URL:-http://localhost:3000}
1112
init: true
1213
networks:
1314
default:

spec/requests/auth_spec.rb

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,6 @@
3939
# ------------------------------------------------------------
4040
# Tests
4141

42-
describe 'GET /' do
43-
it 'returns 404 Not Found' do
44-
allow(ENV).to receive(:[]).with('SERVE_TEST_UI').and_return(nil)
45-
get root_path
46-
expect(response).to have_http_status(:not_found)
47-
end
48-
49-
it 'serves a test UI in staging' do
50-
allow(ENV).to receive(:[]).with('SERVE_TEST_UI').and_return(true)
51-
52-
get root_path
53-
expect(response).to have_http_status(:ok)
54-
expect(response.content_type).to start_with('text/html')
55-
end
56-
end
57-
5842
describe 'GET /auth/calnet' do
5943
# See https://github.com/omniauth/omniauth/wiki/Resolving-CVE-2015-9284
6044
it 'is disallowed' do
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe Preview::PreviewController, type: :request do
4+
describe 'GET /' do
5+
context 'with the preview environment enabled' do
6+
before do
7+
allow(ENV).to receive(:[]).with('SERVE_TEST_UI').and_return(true)
8+
end
9+
10+
it 'shows a test UI in when SERVE_TEST_UI is true' do
11+
allow(ENV).to receive(:[]).with('GALC_API_URL').and_return('https://galc.biz')
12+
get preview_path
13+
expect(response).to have_http_status(:ok)
14+
expect(response.content_type).to start_with('text/html')
15+
end
16+
17+
context 'sets the API URL correctly' do
18+
it 'sets the url when GALC_API_URL is present' do
19+
allow(ENV).to receive(:[]).with('GALC_API_URL').and_return('https://galc.biz')
20+
get preview_path
21+
expect(assigns(:api_url)).to eq('https://galc.biz')
22+
end
23+
24+
it 'sets the API URL when GALC_API_URL is not present' do
25+
allow(ENV).to receive(:[]).with('GALC_API_URL').and_return(nil)
26+
get preview_path
27+
expect(assigns(:api_url)).to eq('http://localhost:3000')
28+
end
29+
end
30+
end
31+
end
32+
end

0 commit comments

Comments
 (0)