forked from thesuss/shf-project
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get admin email and password from ENV in production (#73)
* added config/secrets.yml * spec: load info from ENV['SHF_ADMIN_EMAIL'] and ENV['SHF_ADMIN_EMAIL'] * load admin email and password from ENV variables ; raise error if there's a problem * only load from ENV in production (Rails.env.production?), else read from the seeds.rb file * Revert: added config/secrets.yml * formatting: indentation
- Loading branch information
1 parent
36d33da
commit cefb53a
Showing
2 changed files
with
104 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,19 +5,45 @@ | |
# | ||
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) | ||
# Character.create(name: 'Luke', movie: movies.first) | ||
User.create(email: '[email protected]', password: 'hundapor', admin: true) | ||
|
||
class SeedAdminENVError < StandardError | ||
end | ||
|
||
SEED_ERROR_MSG = 'Seed ERROR: Could not load either admin email or password. NO ADMIN was created!' | ||
|
||
private def env_invalid_blank(env_key) | ||
raise SeedAdminENVError, SEED_ERROR_MSG if (env_val = ENV.fetch(env_key)).blank? | ||
env_val | ||
end | ||
|
||
|
||
if Rails.env.production? | ||
begin | ||
email = env_invalid_blank('SHF_ADMIN_EMAIL') | ||
pwd = env_invalid_blank('SHF_ADMIN_PWD') | ||
|
||
User.create(email: email, password: pwd, admin: true) | ||
rescue | ||
raise SeedAdminENVError, SEED_ERROR_MSG | ||
end | ||
else | ||
email = '[email protected]' | ||
pwd = 'hundapor' | ||
User.create(email: email, password: pwd, admin: true) | ||
end | ||
|
||
|
||
require 'csv' | ||
|
||
csv_text = File.read(Rails.root.join('lib', 'seeds', 'user_table.csv')) | ||
csv = CSV.parse(csv_text, :headers => true, :encoding => 'ISO-8859-1') | ||
csv.each do |row| | ||
User.find_or_create_by(email: row['email']) do | user | | ||
User.find_or_create_by(email: row['email']) do |user| | ||
user.password = 'whatever' | ||
end | ||
end | ||
|
||
business_categories = %w(Träning Psykologi Rehab Butik Trim Friskvård Dagis Pensionat Skola) | ||
business_categories.each { |b_category| BusinessCategory.find_or_create_by(name: b_category)} | ||
business_categories.each { |b_category| BusinessCategory.find_or_create_by(name: b_category) } | ||
BusinessCategory.find_or_create_by(name: 'Sociala tjänstehundar', description: 'Terapi-, vård- & skolhund dvs hundar som jobbar tillsammans med sin förare/ägare inom vård, skola och omsorg.') | ||
BusinessCategory.find_or_create_by(name: 'Civila tjänstehundar', description: 'Assistanshundar dvs hundar som jobbar åt sin ägare som service-, signal, diabetes, PH-hund mm') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe 'load admin.email and admin.password from ENV in production' do | ||
|
||
env_shf_email = 'SHF_ADMIN_EMAIL' | ||
env_shf_pwd = 'SHF_ADMIN_PWD' | ||
admin_email = '[email protected]' | ||
admin_pwd = 'insecure-password' | ||
|
||
SEED_ERROR = 'Seed ERROR: Could not load either admin email or password. NO ADMIN was created!' | ||
|
||
|
||
describe 'happy path - all is valid' do | ||
|
||
before(:each) do | ||
allow(Rails).to receive(:env).and_return(ActiveSupport::StringInquirer.new('production')) | ||
stub_const('ENV', {env_shf_email => admin_email, env_shf_pwd => admin_pwd}) | ||
Rails.application.load_seed # loading seeds | ||
end | ||
|
||
let(:admin_in_db) { User.find_by_email(admin_email) } | ||
|
||
it "#{admin_email} is in the db" do | ||
expect(admin_in_db).not_to be_nil | ||
end | ||
|
||
it "#{admin_email} is an admin (admin=true) in db" do | ||
expect(admin_in_db.admin).to be_truthy | ||
end | ||
|
||
it "admin email is = ENV['SHF_ADMIN_EMAIL']" do | ||
expect(admin_in_db.email).to eq(admin_email) | ||
end | ||
|
||
it "admin email is = ENV['SHF_ADMIN_PWD']" do | ||
# User.find(1).valid_password?('password123') | ||
expect(admin_in_db.valid_password?(admin_pwd)).to be_truthy | ||
end | ||
|
||
end | ||
|
||
|
||
describe 'sad path - things go wrong' do | ||
|
||
before(:each) do | ||
allow(Rails).to receive(:env).and_return(ActiveSupport::StringInquirer.new('production')) | ||
end | ||
|
||
it "ENV[#{env_shf_email}] not found" do | ||
stub_const('ENV', {env_shf_pwd => admin_pwd}) | ||
|
||
expect { Rails.application.load_seed }.to raise_exception SEED_ERROR | ||
end | ||
|
||
it "ENV[#{env_shf_email}] is an empty string" do | ||
stub_const('ENV', {env_shf_email => '', env_shf_pwd => admin_pwd}) | ||
|
||
expect { Rails.application.load_seed }.to raise_exception SEED_ERROR | ||
end | ||
|
||
it "ENV[#{env_shf_pwd}] not found" do | ||
stub_const('ENV', {env_shf_email => admin_email}) | ||
|
||
expect { Rails.application.load_seed }.to raise_exception SEED_ERROR | ||
end | ||
|
||
it "ENV[#{env_shf_pwd}] is an empty string" do | ||
stub_const('ENV', {env_shf_email => admin_email, env_shf_pwd => ''}) | ||
|
||
expect { Rails.application.load_seed }.to raise_exception SEED_ERROR | ||
end | ||
end | ||
|
||
end |