From f06aed6541da6b791611453e08c260fd417dc2aa Mon Sep 17 00:00:00 2001 From: Mark Moser Date: Mon, 1 Aug 2016 19:35:10 -0500 Subject: [PATCH] email validator tests, live coding later error message --- app/models/candidate.rb | 5 +-- app/validators/answer_format_validator.rb | 6 ++- app/validators/email_format_validator.rb | 4 +- .../answer_format_validator_test.rb | 2 +- .../validators/email_format_validator_test.rb | 41 +++++++++++++++++++ 5 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 test/validators/email_format_validator_test.rb diff --git a/app/models/candidate.rb b/app/models/candidate.rb index b219635..afae0eb 100644 --- a/app/models/candidate.rb +++ b/app/models/candidate.rb @@ -6,10 +6,9 @@ class Candidate < ApplicationRecord before_validation(:generate_test_hash, on: :create) - validates_presence_of :name - validates_presence_of :email - validates_presence_of :experience validates_presence_of :recruiter_id + validates_presence_of :name + validates_presence_of :experience validates :email, uniqueness: true, presence: true, email_format: true validates :test_hash, uniqueness: true, presence: true diff --git a/app/validators/answer_format_validator.rb b/app/validators/answer_format_validator.rb index 9e71bb0..1cbcfea 100644 --- a/app/validators/answer_format_validator.rb +++ b/app/validators/answer_format_validator.rb @@ -33,7 +33,11 @@ class AnswerFormatValidator < ActiveModel::EachValidator def live_code record, attribute, value return unless value.nil? || value.values.join.blank? - msg = "You must write code in one of the above textareas to progress." + msg = if value.present? && value.keys.count == 1 + "Please check that you will come back to complete the code example." + else + "You must write code in one of the above textareas to progress." + end record.errors[attribute] << (options[:message] || msg) end end diff --git a/app/validators/email_format_validator.rb b/app/validators/email_format_validator.rb index a862eb8..2418922 100644 --- a/app/validators/email_format_validator.rb +++ b/app/validators/email_format_validator.rb @@ -9,8 +9,6 @@ class EmailFormatValidator < ActiveModel::EachValidator (v.strip =~ /^([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})$/i) || v.strip.blank? end - if results.include?(false) - record.errors[attribute] << (options[:message] || "is not formatted properly") - end + record.errors[attribute] << (options[:message] || "is not formatted properly") if results.include?(false) end end diff --git a/test/validators/answer_format_validator_test.rb b/test/validators/answer_format_validator_test.rb index 56b4881..b8a7d2f 100644 --- a/test/validators/answer_format_validator_test.rb +++ b/test/validators/answer_format_validator_test.rb @@ -144,7 +144,7 @@ class AnswerFormatValidatorTest < ActiveSupport::TestCase obj.answer = { "later" => "" } refute obj.valid? - assert_match(/write.*code/, obj.errors.messages[:answer][0]) + assert_match(/come back/, obj.errors.messages[:answer][0]) end test "live_code should FAIL without values" do diff --git a/test/validators/email_format_validator_test.rb b/test/validators/email_format_validator_test.rb new file mode 100644 index 0000000..2de25a8 --- /dev/null +++ b/test/validators/email_format_validator_test.rb @@ -0,0 +1,41 @@ +require 'test_helper' + +class EmailValidatable + include ActiveModel::Validations + attr_accessor :email + validates :email, email_format: true +end + +class EmailFormatValidatorTest < ActiveSupport::TestCase + test "tld length" do + obj = EmailValidatable.new + + obj.email = "me@no.yes.x" + refute obj.valid?, 'allowed single length tld' + + obj.email = "me@no.yes.co" + assert obj.valid?, 'did not allow tld length 2' + + obj.email = "me@no.yes.com" + assert obj.valid?, 'did not allow tld length 3' + + obj.email = "me@no.yes.commets" + assert obj.valid?, 'did not allow tld length > 3' + end + + test "can handle comma seperated addresses" do + obj = EmailValidatable.new + obj.email = "me@no.yes, me@yes.no" + + assert obj.valid?, 'did not allow multiple address [comma seperated]' + end + + test "provides proper error message" do + obj = EmailValidatable.new + obj.email = "this is a bad email address" + obj.valid? + + refute obj.errors.messages.empty?, 'needs an error message' + assert_match(/not formatted properly/, obj.errors.messages[:email].join) + end +end