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

Commit efb539f

Browse files
author
Sydney Young
committed
Create helper method to check ENV variables
Resolves #1422
1 parent 6e6c3d4 commit efb539f

18 files changed

+65
-34
lines changed

app/controllers/application_controller.rb

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

app/controllers/import_users_controller.rb

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

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

app/controllers/users_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def set_user
2424
end
2525

2626
def check_cas_auth
27-
@cas_auth = ENV['CAS_AUTH']
27+
@cas_auth = env?('CAS_AUTH')
2828
end
2929

3030
# ------------ end before filter methods ------------ #

app/models/user.rb

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

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

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

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

163163
# ---- 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

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
# Disable serving static files from the `/public` folder by default since
2525
# Apache or NGINX already handles this.
26-
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
26+
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES']
2727

2828
# Compress JavaScripts and CSS.
2929
config.assets.js_compressor = :uglifier
@@ -76,7 +76,7 @@
7676
# config.action_controller.asset_host = 'http://assets.example.com'
7777

7878
# Disable e-mails if environment variable is set
79-
config.action_mailer.perform_deliveries = ENV['DISABLE_EMAILS'].nil?
79+
config.action_mailer.perform_deliveries = ENV['DISABLE_EMAILS']
8080

8181
# Disable delivery errors, bad email addresses will be ignored
8282
# config.action_mailer.raise_delivery_errors = false
@@ -104,5 +104,5 @@
104104
config.assets.precompile += %w(print.css)
105105

106106
# set up PartyFoul
107-
config.middleware.use('PartyFoul::Middleware') if ENV['PARTY_FOUL_TOKEN']
107+
config.middleware.use('PartyFoul::Middleware') if env?('PARTY_FOUL_TOKEN')
108108
end

config/initializers/000_env_helper.rb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include EnvironmentHandler

config/initializers/00_devise.rb

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

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

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

config/initializers/authentication.rb

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

66
user = User.first
77

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

config/initializers/setup_mail.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
}
99

1010
# optional server authentication
11-
if ENV['RES_SMTP_AUTH']
11+
if env?('RES_SMTP_AUTH')
1212
ActionMailer::Base.smtp_settings[:authentication] = :login
1313
ActionMailer::Base.smtp_settings[:user_name] =
1414
Rails.application.secrets.smtp_username
@@ -19,7 +19,7 @@
1919
# logging of automatically sent emails
2020
class MailObserver
2121
def self.delivered_email(message)
22-
if ENV['LOG_EMAILS']
22+
if env?('LOG_EMAILS')
2323
Rails.logger.info "Sent #{message.subject} to #{message.to}"
2424
end
2525
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
@@ -153,11 +153,11 @@ def generate_user
153153
u.nickname = FFaker::Name.first_name
154154
u.phone = FFaker::PhoneNumber.short_phone_number
155155
u.email = FFaker::Internet.email
156-
u.cas_login = FFaker::Internet.user_name if ENV['CAS_AUTH']
156+
u.cas_login = FFaker::Internet.user_name if env?('CAS_AUTH')
157157
u.affiliation = 'YC ' + %w(BK BR CC DC ES JE MC PC SM SY TC TD).sample +
158158
' ' + rand(2012..2015).to_s
159159
u.role = %w(normal checkout).sample
160-
u.username = ENV['CAS_AUTH'] ? u.cas_login : u.email
160+
u.username = env?('CAS_AUTH') ? u.cas_login : u.email
161161
end
162162
end
163163

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

357357
if MINIMAL || SEMI
358-
if ENV['CAS_AUTH']
358+
if env?('CAS_AUTH')
359359
prompt_field(u, :cas_login)
360360
u.username = u.cas_login
361361
else
@@ -372,7 +372,7 @@ def generate_objs(method, obj, n)
372372
prompt_field(u, :phone)
373373
prompt_field(u, :email)
374374
prompt_field(u, :affiliation)
375-
if ENV['CAS_AUTH']
375+
if env?('CAS_AUTH')
376376
prompt_field(u, :cas_login)
377377
u.username = u.cas_login
378378
u.save
@@ -476,6 +476,6 @@ def generate_objs(method, obj, n)
476476
end
477477

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

lib/csv_import.rb

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

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

lib/environment_handler.rb

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

lib/tasks/setup_application.rake

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace :app do
3434
phone = STDIN.gets.chomp
3535
puts 'Email Address:'
3636
email = STDIN.gets.chomp
37-
if ENV['CAS_AUTH']
37+
if env?('CAS_AUTH')
3838
puts 'Username (i.e. NetID, ensure this is correct):'
3939
cas_login = STDIN.gets.chomp
4040
username = cas_login
@@ -55,12 +55,12 @@ namespace :app do
5555
u.last_name = last_name
5656
u.phone = phone
5757
u.email = email
58-
u.cas_login = cas_login if ENV['CAS_AUTH']
58+
u.cas_login = cas_login if env?('CAS_AUTH')
5959
u.username = username
6060
u.affiliation = affiliation
6161
u.role = 'superuser'
6262
u.view_mode = 'superuser'
63-
unless ENV['CAS_AUTH']
63+
unless env?('CAS_AUTH')
6464
u.password = password
6565
u.password_confirmation = password_confirmation
6666
end

spec/factories/users.rb

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

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

spec/lib/environment_handler_spec.rb

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

spec/mailers/user_mailer_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
end
6464

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

0 commit comments

Comments
 (0)