require "spec_helper" class TestUser < TestModel validates :email, email: true end class TestUserMX < TestModel validates :email, email: { mx: true } end class TestUserDisallowDisposable < TestModel validates :email, email: { disposable: true } end class TestUserDisallowBlacklisted < TestModel validates :email, email: { blacklist: true } end describe EmailAssessor do describe "basic validation" do subject(:user) { TestUser.new(email: "") } it "should be valid when email is empty" do user.valid?.should be_true end it "should not be valid when domain is missing" do user = TestUser.new(email: "foo") user.valid?.should be_false end it "should be invalid when email is malformed" do user = TestUser.new(email: "foo@bar") user.valid?.should be_false end it "should be invalid when email contains a trailing symbol" do user = TestUser.new(email: "foo@bar.com/") user.valid?.should be_false end it "should be invalid if Mail::AddressListsParser raises exception" do user = TestUser.new(email: "foo@gmail.com") Mail::Address.stub(:new).and_raise(Mail::Field::ParseError.new(nil, nil, nil)) user.valid?.should be_false end it "shouldn't be valid if the domain constains consecutives dots" do user = TestUser.new(email: "foo@bar..com") user.valid?.should be_false end end describe "disposable domains" do let(:disposable_domain) { disposable_domain = File.open(described_class::DISPOSABLE_DOMAINS_FILE, &:readline) } it "should be valid when email is not in the list of disposable domains" do user = TestUserDisallowDisposable.new(email: "foo@gmail.com") user.valid?.should be_true end it "should be invalid when email is in the list of disposable domains" do user = TestUserDisallowDisposable.new(email: "foo@#{disposable_domain}") user.valid?.should be_false end it "should be invalid when email is in the list of disposable domains regardless of subdomain" do user = TestUserDisallowDisposable.new(email: "foo@abc123.#{disposable_domain}") user.valid?.should be_false end end describe "blacklisted domains" do let(:blacklisted_domain) { File.open(described_class::BLACKLISTED_DOMAINS_FILE, &:readline) } it "should be valid when email domain is not in the blacklist" do user = TestUserDisallowBlacklisted.new(email: "foo@gmail.com") user.valid?.should be_true end it "should be invalid when email domain is in the blacklist" do user = TestUserDisallowBlacklisted.new(email: "foo@#{blacklisted_domain}") user.valid?.should be_false end it "should be invalid when email domain is in the blacklist regardless of subdomain" do user = TestUserDisallowBlacklisted.new(email: "foo@abc123.#{blacklisted_domain}") user.valid?.should be_false end end describe "mx lookup" do it "should be valid if mx records are found" do user = TestUserMX.new(email: "foo@gmail.com") user.valid?.should be_true end it "should be invalid if no mx records are found" do user = TestUserMX.new(email: "foo@subdomain.gmail.com") user.valid?.should be_false end end end