Skip to content

Commit

Permalink
adding spark email verification to person
Browse files Browse the repository at this point in the history
  • Loading branch information
enoodle committed Feb 9, 2019
1 parent 44c6c0c commit 3e15ac4
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/controllers/camps_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def create
return redirect_to edit_camp_path(id: @camp.id)
end

flash.now[:notice] = "#{t:errors_str}: #{@camp.errors.full_messages.uniq.join(', ')}"
flash.now[:alert] = "#{t:errors_str}: #{@camp.errors.full_messages.uniq.join(', ')}"
render :new
end

Expand Down
21 changes: 21 additions & 0 deletions app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,22 @@
# needs_early_arrival :boolean
#

class EmailInSpark < ActiveModel::Validator
def validate(record)
if !Person.email_registered?(record.email)
record.errors[:email] << I18n.t("email_not_in_spark") + " #{record.email}"
end
end
end

class Person < ActiveRecord::Base
CSV_ATTRIBUTES = %w{email name phone_number responsibilities
dream_name dream_number}.freeze
belongs_to :camp, validate: true
has_and_belongs_to_many :roles

validates :name, presence: true
validates_with EmailInSpark, :on => :create

schema_validations whitelist: [:id, :created_at, :updated_at, :camp]

Expand All @@ -39,4 +48,16 @@ def self.to_csv
end
end
end

def self.email_registered?(email)
r = HTTParty.post(
URI.join(ENV['SPARK_HOST'], 'volunteers/profiles').to_s,
body: {emails: [email]}.to_json,
headers: {
token: ENV['SPARK_TOKEN'],
'Content-Type': 'application/json'
}
)
JSON.parse(r.body)[0].key? 'user_data'
end
end
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ en:
modified_at: Modified at
new_dream_is_disabled: Creating new dreams is disabled at the moment. If you think
this is a mistake please contact bureau of dreams
email_not_in_spark: This email is not registered in Spark
dont_miss_out:
banner: Don't miss out! Only %{time} left to %{action}
actions:
Expand Down
1 change: 1 addition & 0 deletions config/locales/he.yml
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ he:

# Errors
new_dream_is_disabled: "יצירת חלומות חדשים מנוטרלת כרגע. אם אתה חושב שזאת שגיאה אנא צור קשר עם לשכת החלומות"
email_not_in_spark: "האימייל לא רשום בספארק:"

time:
am: am
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/camps_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
it 'won\'t create a camp without manager' do
post :create, camp: camp_attributes.merge!(people_attributes: {})
expect(Camp.find_by_contact_name(camp_leader)).to be_nil
expect(flash[:notice]).to_not be_nil
expect(flash[:alert]).to_not be_nil
end
end

Expand Down
5 changes: 5 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
# ...rather than:
# # => "be bigger than 2"
expectations.include_chain_clauses_in_custom_matcher_descriptions = true

# Dont do email verification in tests:
config.before(:each) do
allow(Person).to receive(:email_registered?).and_return(true)
end
end

# rspec-mocks config goes here. You can use an alternate test double
Expand Down

0 comments on commit 3e15ac4

Please sign in to comment.