Skip to content

Instantly share code, notes, and snippets.

@donovanbray
Created June 10, 2013 21:32
Show Gist options
  • Save donovanbray/5752530 to your computer and use it in GitHub Desktop.
Save donovanbray/5752530 to your computer and use it in GitHub Desktop.
test sample
describe User, 'as super_admin' do
subject { Factory(:super_admin, :email => 'super@duper.com')}
before(:each) do
PartnerScope.with(Partner.serve) { Factory(:super_admin, :email => 'super@duper.com') }
end
describe "#super_admin_for" do
it "returns multiple partners" do
expect(subject.super_admin_for).to have_at_least(2).items
end
it "doesn't include partners without a lib/config/partners configuration" do
# this occurs when a more recent build has been deployed to an ecosystem, then
# an older build is deployed that doesn't have the configuration for that partner.
partner = Factory(:partner, :slug => "noexist", :abbreviation => 'noexist', :url => "http://noexist.localhost")
# the email address formatter makes a call that expects a configuration to be present and will fail the test prematurely.
EmailAddress.should_receive(:valid_format).and_return(EmailAddress::DEFAULT_EMAIL_FORMAT)
# the email address spamtrap? makes a call that expects a configuration to be present and will fail the test prematurely.
EmailAddress.should_receive(:spamtrap?).and_return(false)
PartnerScope.with(partner) { Factory(:super_admin, :email => 'super@duper.com') }
expect(subject.super_admin_for.find{|p| p.slug == 'noexist'}).to be_empty
end
end
end
@donovanbray
Copy link
Author

The current problem is the partner.config.restrict_by_email call.

  # facebook users don't need email addresses
  # also, only check email validity for new users
  # additional email addresses are validated when added to EmailAddress
  # new non-fb user, or fb user setting an email for the first time
  def validate_email
    validates_unique :email, :only_if_modified => true, :allow_nil => true
    if new? && fb_uid.nil? or fb_uid && email && (new? || email_addresses_dataset.count == 0)
      validates_presence :email
      if email.present?
        validates_format EmailAddress.valid_format, :email
        errors.add(:email, "invalid") if EmailAddress.spamtrap?(email) and !bypass_spamtrap
        if partner.config.restrict_by_email && !$redis.sismember("valid_emails:#{partner.pk}", self.email.downcase)
          errors.add("email", "is not allowed access at this time")
        end
      end
    end
  end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment