Skip to content
This repository was archived by the owner on Jul 24, 2020. It is now read-only.

Commit 5e132f4

Browse files
author
Sydney Young
committed
Create helper method to check ENV variables
Resolves #1422
1 parent f5eac92 commit 5e132f4

18 files changed

+73
-35
lines changed

app/controllers/application_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ def markdown_help
322322
# modify redirect after signing in
323323
def after_sign_in_path_for(user)
324324
# CODE FOR CAS LOGIN --> NEW USER
325-
if ENV['CAS_AUTH'] && current_user && current_user.id.nil? &&
325+
if env?('CAS_AUTH') && current_user && current_user.id.nil? &&
326326
current_user.username
327327
# store username in session since there's a request in between
328328
session[:new_username] = current_user.username

app/controllers/import_users_controller.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ def valid_input_file?(imported_users, file) # rubocop:disable all
5858
end
5959

6060
# make sure we have username data (otherwise all will always fail)
61-
unless imported_users.first.keys.include?(:username) ||
62-
ENV['CAS_AUTH'].nil?
61+
unless imported_users.first.keys.include?(:username) || !env?('CAS_AUTH')
6362
flash[:error] = 'Unable to import CSV file. None of the users will be '\
6463
'able to log in without specifying \'username\' data.'
6564
redirect_to(:back) && return

app/controllers/users_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def set_user
2525
end
2626

2727
def check_cas_auth
28-
@cas_auth = ENV['CAS_AUTH']
28+
@cas_auth = env?('CAS_AUTH')
2929
end
3030

3131
# ------------ end before filter methods ------------ #

app/models/user.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class User < ActiveRecord::Base
1010
# :cas_authenticatable module. If not, we implement password authentcation
1111
# using the :database_authenticatable module and also allow for password
1212
# resets.
13-
if ENV['CAS_AUTH']
13+
if env?('CAS_AUTH')
1414
devise :cas_authenticatable
1515
else
1616
devise :database_authenticatable, :recoverable, :rememberable
@@ -38,7 +38,7 @@ class User < ActiveRecord::Base
3838
presence: true, uniqueness: true,
3939
format: { with: /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i }
4040
# validations for CAS authentication
41-
if ENV['CAS_AUTH']
41+
if env?('CAS_AUTH')
4242
validates :cas_login, presence: true, uniqueness: true
4343
# validations for password authentication
4444
else
@@ -92,9 +92,9 @@ def equipment_items
9292
# rubocop:disable CyclomaticComplexity
9393
def self.search_ldap(login)
9494
return nil if login.blank?
95-
return nil unless ENV['USE_LDAP']
95+
return nil unless env?('USE_LDAP')
9696

97-
filter_param = if ENV['CAS_AUTH']
97+
filter_param = if env?('CAS_AUTH')
9898
Rails.application.secrets.ldap_login
9999
else
100100
Rails.application.secrets.ldap_email
@@ -139,7 +139,7 @@ def self.search_ldap(login)
139139
.select { |s| s && !s.empty? }.join(' ')
140140

141141
# define username based on authentication method
142-
out[:username] = if ENV['CAS_AUTH']
142+
out[:username] = if env?('CAS_AUTH')
143143
result[Rails.application.secrets.ldap_login.to_sym][0]
144144
else
145145
out[:email]
@@ -158,7 +158,7 @@ def self.select_options
158158
end
159159

160160
def render_name
161-
ENV['CAS_AUTH'] ? "#{name} #{username}" : name.to_s
161+
env?('CAS_AUTH') ? "#{name} #{username}" : name.to_s
162162
end
163163

164164
# ---- Reservation methods ---- #

app/views/users/_find_user.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<%= label_tag :searched_id, 'Find User' %>
44
<div class="input-group">
55
<%= autocomplete_field_tag 'fake_searched_id', '', autocomplete_user_last_name_users_path,
6-
size: 30, update_elements: {id: '#searched_id'}, placeholder: "Enter a name or #{ENV['CAS_AUTH'] ? 'username' : 'email'}", class: 'submittable form-control' %>
6+
size: 30, update_elements: {id: '#searched_id'}, placeholder: "Enter a name or #{env?('CAS_AUTH') ? 'username' : 'email'}", class: 'submittable form-control' %>
77
<%= hidden_field_tag 'searched_id' %>
88
<span class="input-group-btn">
99
<%= button_tag sanitize("#{content_tag :i, nil, class: "fa fa-search"}"), class: "btn btn-default", rel: 'tooltip', title: 'Search Users' %>

config/environments/production.rb

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22
Rails.application.configure do
3+
ENV_FALSE = [0, '0', false, 'false', nil].freeze
4+
35
# Settings specified here will take precedence over those in
46
# config/application.rb
57

@@ -24,7 +26,7 @@
2426

2527
# Disable serving static files from the `/public` folder by default since
2628
# Apache or NGINX already handles this.
27-
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
29+
config.serve_static_files = !FALSE.include?(ENV['RAILS_SERVE_STATIC_FILES'])
2830

2931
# Compress JavaScripts and CSS.
3032
config.assets.js_compressor = :uglifier
@@ -77,7 +79,8 @@
7779
# config.action_controller.asset_host = 'http://assets.example.com'
7880

7981
# Disable e-mails if environment variable is set
80-
config.action_mailer.perform_deliveries = ENV['DISABLE_EMAILS'].nil?
82+
config.action_mailer.perform_deliveries = \
83+
!FALSE.include?(ENV['DISABLE_EMAILS'])
8184

8285
# Disable delivery errors, bad email addresses will be ignored
8386
# config.action_mailer.raise_delivery_errors = false
@@ -105,5 +108,7 @@
105108
config.assets.precompile += %w(print.css)
106109

107110
# set up PartyFoul
108-
config.middleware.use('PartyFoul::Middleware') if ENV['PARTY_FOUL_TOKEN']
111+
unless FALSE.include?(ENV['PARTY_FOUL_TOKEN'])
112+
config.middleware.use('PartyFoul::Middleware')
113+
end
109114
end

config/initializers/000_env_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# frozen_string_literal: true
2+
include EnvironmentHandler

config/initializers/00_devise.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
config.skip_session_storage = [:http_auth]
3232

3333
# ==> Configuration for :database_authenticatable
34-
unless ENV['CAS_AUTH']
34+
unless env?('CAS_AUTH')
3535
# For bcrypt, this is the cost for hashing the password and defaults to 10.
3636
# If using other encryptors, it sets how many times you want the password
3737
# re-encrypted.
@@ -72,7 +72,7 @@
7272
end
7373

7474
# ==> devise_cas_authenticatable configuration
75-
if ENV['CAS_AUTH']
75+
if env?('CAS_AUTH')
7676
# configure the base URL of your CAS server
7777
config.cas_base_url = Rails.application.secrets.cas_base_url
7878

config/initializers/authentication.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# frozen_string_literal: true
22
# Check for authentication method and copy data over if necessary (ENV variable
33
# to skip if necessary, skip if migrating from a pre-v4.1.0 DB or no table)
4-
unless ENV['SKIP_AUTH_INIT'] || !User.table_exists? ||
4+
unless env?('SKIP_AUTH_INIT') || !User.table_exists? ||
55
!User.column_names.include?('username')
66

77
user = User.first
88

99
# if we want to use CAS authentication and the username parameter doesn't
1010
# match the cas_login parameter, we need to copy that over
11-
if ENV['CAS_AUTH'] && user && (user.username != user.cas_login)
11+
if env?('CAS_AUTH') && user && (user.username != user.cas_login)
1212
# if there are any users that don't have cas_logins, we can't use CAS
1313
if User.where(cas_login: ['', nil]).count > 0
1414
raise 'There are users missing their CAS logins, you cannot use CAS '\
@@ -18,7 +18,7 @@
1818
end
1919
# if we want to use password authentication all users can reset their
2020
# passwords so it doesn't matter if they already have them or not
21-
elsif ENV['CAS_AUTH'].nil? && user && (user.username != user.email)
21+
elsif env?('CAS_AUTH')
2222
User.update_all 'username = email'
2323
end
2424
end

config/initializers/setup_mail.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
}
1010

1111
# optional server authentication
12-
if ENV['RES_SMTP_AUTH']
12+
if env?('RES_SMTP_AUTH')
1313
ActionMailer::Base.smtp_settings[:authentication] = :login
1414
ActionMailer::Base.smtp_settings[:user_name] =
1515
Rails.application.secrets.smtp_username
@@ -20,7 +20,7 @@
2020
# logging of automatically sent emails
2121
class MailObserver
2222
def self.delivered_email(message)
23-
if ENV['LOG_EMAILS']
23+
if env?('LOG_EMAILS')
2424
Rails.logger.info "Sent #{message.subject} to #{message.to}"
2525
end
2626
end

db/migrate/20150323013431_add_cas_login_to_users.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ def change
33
add_column :users, :cas_login, :string
44

55
# copy username to cas_login if you use CAS already
6-
if ENV['CAS_AUTH']
6+
if env?('CAS_AUTH')
77
ActiveRecord::Base.connection
88
.execute('update users set cas_login=username')
99
end

db/seeds.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,11 @@ def generate_user
154154
u.nickname = FFaker::Name.first_name
155155
u.phone = FFaker::PhoneNumber.short_phone_number
156156
u.email = FFaker::Internet.email
157-
u.cas_login = FFaker::Internet.user_name if ENV['CAS_AUTH']
157+
u.cas_login = FFaker::Internet.user_name if env?('CAS_AUTH')
158158
u.affiliation = 'YC ' + %w(BK BR CC DC ES JE MC PC SM SY TC TD).sample +
159159
' ' + rand(2012..2015).to_s
160160
u.role = %w(normal checkout).sample
161-
u.username = ENV['CAS_AUTH'] ? u.cas_login : u.email
161+
u.username = env?('CAS_AUTH') ? u.cas_login : u.email
162162
end
163163
end
164164

@@ -356,7 +356,7 @@ def generate_objs(method, obj, n)
356356
u.save
357357

358358
if MINIMAL || SEMI
359-
if ENV['CAS_AUTH']
359+
if env?('CAS_AUTH')
360360
prompt_field(u, :cas_login)
361361
u.username = u.cas_login
362362
else
@@ -373,7 +373,7 @@ def generate_objs(method, obj, n)
373373
prompt_field(u, :phone)
374374
prompt_field(u, :email)
375375
prompt_field(u, :affiliation)
376-
if ENV['CAS_AUTH']
376+
if env?('CAS_AUTH')
377377
prompt_field(u, :cas_login)
378378
u.username = u.cas_login
379379
u.save
@@ -477,6 +477,6 @@ def generate_objs(method, obj, n)
477477
end
478478

479479
puts "\n***Successfully seeded all records! (#{Time.zone.now - t1}s)***\n\n"
480-
if !ENV['CAS_AUTH'] && MINIMAL && display_login_msg
480+
if !env?('CAS_AUTH') && MINIMAL && display_login_msg
481481
puts "You can log in using e-mail '[email protected]' and password 'passw0rd'\n"
482482
end

lib/csv_import.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def import_users(array_of_user_data, overwrite = false, user_type = 'normal')
4949
user_data[:role] = user_type
5050
user_data[:csv_import] = true
5151
next if attempt_save_with_csv_data?(user_data)
52-
if ENV['USE_LDAP']
52+
if env?('USE_LDAP')
5353
attempt_save_with_ldap(user_data)
5454
else
5555
@array_of_fail << [user_data, 'Invalid user parameters.']
@@ -65,7 +65,7 @@ def import_users(array_of_user_data, overwrite = false, user_type = 'normal')
6565
# otherwise it replaces the keys in the data hash with the ldap data.
6666
def import_with_ldap(user_data)
6767
# use username if using cas, email otherwise
68-
ldap_param = user_data[ENV['CAS_AUTH'] ? :username : :email]
68+
ldap_param = user_data[env?('CAS_AUTH') ? :username : :email]
6969

7070
# check LDAP for missing data
7171
ldap_user_hash = User.search_ldap(ldap_param)
@@ -85,7 +85,7 @@ def import_with_ldap(user_data)
8585
# tries to save using only the csv data. This method will return
8686
# false if the data specified in the csv is invalid on the user model.
8787
def attempt_save_with_csv_data?(user_data)
88-
if ENV['CAS_AUTH']
88+
if env?('CAS_AUTH')
8989
# set the cas login
9090
user_data[:cas_login] = user_data[:username]
9191
else

lib/environment_handler.rb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
module EnvironmentHandler
3+
FALSE = [0, '0', false, 'false', nil].freeze
4+
5+
def env?(var)
6+
!FALSE.include? ENV[var]
7+
end
8+
end

lib/tasks/setup_application.rake

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace :app do
3535
phone = STDIN.gets.chomp
3636
puts 'Email Address:'
3737
email = STDIN.gets.chomp
38-
if ENV['CAS_AUTH']
38+
if env?('CAS_AUTH')
3939
puts 'Username (i.e. NetID, ensure this is correct):'
4040
cas_login = STDIN.gets.chomp
4141
username = cas_login
@@ -56,12 +56,12 @@ namespace :app do
5656
u.last_name = last_name
5757
u.phone = phone
5858
u.email = email
59-
u.cas_login = cas_login if ENV['CAS_AUTH']
59+
u.cas_login = cas_login if env?('CAS_AUTH')
6060
u.username = username
6161
u.affiliation = affiliation
6262
u.role = 'superuser'
6363
u.view_mode = 'superuser'
64-
unless ENV['CAS_AUTH']
64+
unless env?('CAS_AUTH')
6565
u.password = password
6666
u.password_confirmation = password_confirmation
6767
end

spec/factories/users.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
role 'normal'
1919
view_mode 'normal'
2020

21-
if ENV['CAS_AUTH']
21+
if env?('CAS_AUTH')
2222
username { cas_login }
2323
else
2424
username { email }

spec/lib/environment_handler_spec.rb

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
require 'spec_helper'
3+
include EnvironmentHandler
4+
5+
describe EnvironmentHandler do
6+
describe '.env?' do
7+
it 'returns false when variable not set' do
8+
allow(ENV).to receive(:[]).with('CAS_AUTH').and_return(nil)
9+
expect(env?('CAS_AUTH')).to be_falsey
10+
end
11+
it 'returns false when variable set to 0' do
12+
allow(ENV).to receive(:[]).with('CAS_AUTH').and_return('0')
13+
expect(env?('CAS_AUTH')).to be_falsey
14+
end
15+
it 'returns false when variable set to false' do
16+
allow(ENV).to receive(:[]).with('CAS_AUTH').and_return('false')
17+
expect(env?('CAS_AUTH')).to be_falsey
18+
end
19+
it 'returns true when variable set to anything except 0 or false' do
20+
allow(ENV).to receive(:[]).with('CAS_AUTH').and_return('true')
21+
expect(env?('CAS_AUTH')).to be_truthy
22+
end
23+
end
24+
end

spec/mailers/user_mailer_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
end
6565

6666
it "doesn't log if the env is not set" do
67-
expect(ENV['LOG_EMAILS']).to be_nil
67+
expect(env?('LOG_EMAILS')).to be_falsey
6868
expect(Rails.logger).to receive(:info).with(/Sent/).exactly(0).times
6969
# force a request email; there is not an email for a basic reservation
7070
@mail = UserMailer.reservation_status_update(@res,

0 commit comments

Comments
 (0)