Skip to content

Commit

Permalink
[Owner] Validate email domain has valid MX records.
Browse files Browse the repository at this point in the history
  • Loading branch information
alloy committed Feb 8, 2014
1 parent 64907b3 commit 70c56fb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
9 changes: 8 additions & 1 deletion app/models/owner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,14 @@ def create_session!(confirmation_url_template)
def validate
super
validates_presence :name
validates_format RFC822::EMAIL, :email
validates_format RFC822::EMAIL, :email, :message => 'invalid format'
validates_mx_records :email
end

def validates_mx_records(attr)
if RFC822.mx_records(send(attr)).empty?
errors.add(:email, 'unverifiable domain')
end
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ def connect
end
end

require 'rfc822'
module RFC822
def self.mx_records(address)
if address.split('@').last == 'example.com'
[MXRecord.new(20, 'mail.example.com')]
else
[]
end
end
end

# Used in GitHub fixtures
DESTINATION_PATH = 'AFNetworking/1.2.0/AFNetworking.podspec.yaml'
MESSAGE = '[Add] AFNetworking 1.2.0'
12 changes: 11 additions & 1 deletion spec/unit/owner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,21 @@ module Pod::TrunkApp
@owner.should.validate_with(:name, 'Jenny')
end

it "needs a valid email" do
it "needs a valid formatted email" do
@owner.should.not.validate_with(:email, nil)
@owner.errors.on(:email).should.include('invalid format')
@owner.should.not.validate_with(:email, '')
@owner.errors.on(:email).should.include('invalid format')
@owner.should.not.validate_with(:email, ' ')
@owner.errors.on(:email).should.include('invalid format')
@owner.should.not.validate_with(:email, 'jenny')
@owner.errors.on(:email).should.include('invalid format')
@owner.should.validate_with(:email, 'jenny@example.com')
end

it "needs a valid email domain" do
@owner.should.not.validate_with(:email, 'jenny@example')
@owner.errors.on(:email).should.include('unverifiable domain')
@owner.should.validate_with(:email, 'jenny@example.com')
end

Expand Down

0 comments on commit 70c56fb

Please sign in to comment.