Skip to content

Commit 178de48

Browse files
committed
Improve configuration injection; separate preview from AuthController
1 parent f7175ff commit 178de48

File tree

6 files changed

+11
-32
lines changed

6 files changed

+11
-32
lines changed

app/controllers/auth_controller.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +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-
redirect_to preview_path
11-
end
12-
136
def callback
147
logger.debug({ msg: 'Received omniauth callback', omniauth: auth_hash, params: params.to_unsafe_h })
158

app/controllers/preview/preview_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Preview
22
class PreviewController < Preview::ApplicationController
33

44
def index
5-
@api_url = Rails.env.production? ? 'https://galc-api.ucblib.org' : 'http://localhost:3000'
5+
@api_url = ENV['GALC_API_URL'] || 'http://localhost:3000'
66
end
77
end
88
end

config/routes.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
Rails.application.routes.draw do
2-
root to: 'auth#index'
3-
2+
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

87
scope module: 'preview' do
98
constraints(->(_) { ENV['SERVE_TEST_UI'].present? }) do
10-
get '/preview', to: 'preview#index', as: :preview
9+
root to: 'preview#index', as: :preview
1110
end
1211
end
1312

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 & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +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 'redirects to 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 redirect_to(preview_path)
54-
end
55-
end
56-
5742
describe 'GET /auth/calnet' do
5843
# See https://github.com/omniauth/omniauth/wiki/Resolving-CVE-2015-9284
5944
it 'is disallowed' do

spec/requests/preview/preview_spec.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
require 'rails_helper'
22

33
RSpec.describe Preview::PreviewController, type: :request do
4-
describe 'GET /preview' do
4+
describe 'GET /' do
55
context 'with the preview environment enabled' do
66
before do
77
allow(ENV).to receive(:[]).with('SERVE_TEST_UI').and_return(true)
88
end
99

1010
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')
1112
get preview_path
1213
expect(response).to have_http_status(:ok)
1314
expect(response.content_type).to start_with('text/html')
1415
end
1516

1617
context 'sets the API URL correctly' do
17-
it 'sets the url for staging API in production' do
18-
allow(Rails.env).to receive(:production?).and_return(true)
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')
1920
get preview_path
20-
expect(assigns(:api_url)).to eq('https://galc-api.ucblib.org')
21+
expect(assigns(:api_url)).to eq('https://galc.biz')
2122
end
2223

23-
it 'sets the url for API to be localhost in dev/test' do
24-
allow(Rails.env).to receive(:production?).and_return(false)
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)
2526
get preview_path
2627
expect(assigns(:api_url)).to eq('http://localhost:3000')
2728
end

0 commit comments

Comments
 (0)