require 'spec_helper' describe EmailValidator do subject { klass.new } context "with valid format" do let(:klass) do Class.new do include ActiveModel::Validations attr_accessor :email, :name validates :email, email: true end end [ "s_u@example.com", "super.user@example.com", "super+user@example.com", "super-user@example.com", "super+user@example-site.com", "user@example.com", "user@example-site.com", "user@en.example.com", "user@example.museum", "user@123.com", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@example.com", "01234567890@example.com" ].each do |email| it "#{email.inspect} should be valid" do should allow_value(email).for(:email) end end [ "", " ", "example.com", "super user@example.com", " user@example.com", " user@example.com ", "user@example.com ", "user", "user@com", "user@.com", "user@example", "user@example.", "user@example.c", "user_example.com", "user@example_site.com", "user@example.com@example.com", "user@", "user@! \"\#$%(),/;<>_[]\`|.com", "user@127.0.0.1", "user@127.0.0.1:25", "user@example.com\n", "@example.com", "@example", "@", "! \#$%\`|@example.com", "<>@[]\`|@example.com" ].each do |email| it "#{email.inspect} should be invalid" do should_not allow_value(email).for(:email) end end it { should ensure_valid_email_format_of(:email) } it { should_not ensure_valid_email_format_of(:name) } end context "is in the specific domain" do context "domain specified as string" do let(:klass) do Class.new do include ActiveModel::Validations attr_accessor :email, :name validates :email, email: { domain: "edu" } end end it { should allow_value("user@example.edu").for(:email) } it { should_not allow_value("user@example.com").for(:email) } it { should ensure_valid_email_format_of(:email) } it { should_not ensure_valid_email_format_of(:name) } end context "set as an array of strings and symbols" do let(:klass) do Class.new do include ActiveModel::Validations attr_accessor :email, :name validates :email, email: { domain: ['com', :edu, 'Com.Au'] } end end it { should allow_value("user@example.com").for(:email) } it { should allow_value("user@example.edu").for(:email) } it { should allow_value("user@example.com.au").for(:email) } it { should allow_value("user@example.Com.Au").for(:email) } it { should_not allow_value("user@example.org").for(:email) } it { should ensure_valid_email_format_of(:email) } it { should_not ensure_valid_email_format_of(:name) } end end end