Skip to content

Commit

Permalink
Merge pull request #48 from alphagov/better_email_field_validation
Browse files Browse the repository at this point in the history
better email field validation
  • Loading branch information
jamiecobbett committed Feb 12, 2013
2 parents bc855d5 + 84b0047 commit bc04384
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion app/models/shared/requester.rb
Expand Up @@ -4,10 +4,15 @@ class Requester < TablelessModel
attr_accessor :email attr_accessor :email


validates_presence_of :email validates_presence_of :email
validates :email, :format => {:with => /@/}
validates :email, format: { with: /@/ }


validate :collaborator_emails_are_all_valid validate :collaborator_emails_are_all_valid


def email=(new_email)
@email = new_email.nil? ? nil : new_email.gsub("\s", "")
end

def collaborator_emails def collaborator_emails
@collaborator_emails || [] @collaborator_emails || []
end end
Expand Down
11 changes: 11 additions & 0 deletions test/unit/models/requester_test.rb
Expand Up @@ -4,13 +4,24 @@ class RequesterTest < Test::Unit::TestCase
should validate_presence_of(:email) should validate_presence_of(:email)


should allow_value("ab@c.com").for(:email) should allow_value("ab@c.com").for(:email)
should allow_value("ab@ c.com").for(:email)
should allow_value("ab @c.com").for(:email)
should allow_value("ab@c.com ").for(:email)
should allow_value(" ab@c.com").for(:email)
should_not allow_value("ab").for(:email) should_not allow_value("ab").for(:email)


should allow_value("").for(:collaborator_emails) should allow_value("").for(:collaborator_emails)
should allow_value("ab@c.com").for(:collaborator_emails) should allow_value("ab@c.com").for(:collaborator_emails)
should allow_value("ab@c.com, de@f.com").for(:collaborator_emails) should allow_value("ab@c.com, de@f.com").for(:collaborator_emails)
should_not allow_value("ab, de@f.com").for(:collaborator_emails) should_not allow_value("ab, de@f.com").for(:collaborator_emails)


should "remove all whitespace from the email" do
assert_equal "ab@c.com", Requester.new(email: " ab@c.com").email
assert_equal "ab@c.com", Requester.new(email: "ab@c.com ").email
assert_equal "ab@c.com", Requester.new(email: "ab @c.com").email
assert_equal "ab@c.com", Requester.new(email: "ab@ c.com").email
end

should "have an empty list of collaborator emails if not set" do should "have an empty list of collaborator emails if not set" do
assert_equal [], Requester.new.collaborator_emails assert_equal [], Requester.new.collaborator_emails
end end
Expand Down

0 comments on commit bc04384

Please sign in to comment.