From 90cbac7176522d6747d2e660a397cb4e59f41fd9 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Mon, 15 May 2023 16:14:15 +1000 Subject: [PATCH] Validate email domains The gem checks the DNS system for a valid domain entry. --- app/models/spree/user.rb | 2 +- spec/base_spec_helper.rb | 9 +++++++++ spec/models/spree/user_spec.rb | 5 ++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/app/models/spree/user.rb b/app/models/spree/user.rb index 5415bb623b..a923f23e5c 100644 --- a/app/models/spree/user.rb +++ b/app/models/spree/user.rb @@ -48,7 +48,7 @@ module Spree after_create :associate_customers, :associate_orders - validates :email, 'valid_email_2/email': true, if: :email_changed? + validates :email, 'valid_email_2/email': { mx: true }, if: :email_changed? validate :limit_owned_enterprises validates :uid, uniqueness: true, if: lambda { uid.present? } validates_email :uid, if: lambda { uid.present? } diff --git a/spec/base_spec_helper.rb b/spec/base_spec_helper.rb index e30ddc382c..7986a3b639 100644 --- a/spec/base_spec_helper.rb +++ b/spec/base_spec_helper.rb @@ -127,6 +127,15 @@ RSpec.configure do |config| end end + # Don't validate our invalid test data with expensive network requests. + config.before(:each) do + allow_any_instance_of(ValidEmail2::Address).to receive_messages( + valid_mx?: true, + valid_strict_mx?: true, + mx_server_is_in?: false + ) + end + # Webmock raises errors that inherit directly from Exception (not StandardError). # The messages contain useful information for debugging stubbed requests to external # services (in tests), but they normally don't appear in the test output. diff --git a/spec/models/spree/user_spec.rb b/spec/models/spree/user_spec.rb index 2a2caedf18..e7d1d727f8 100644 --- a/spec/models/spree/user_spec.rb +++ b/spec/models/spree/user_spec.rb @@ -102,7 +102,10 @@ describe Spree::User do expect(user).to be_valid end - pending "detects typos in the domain" do + it "detects typos in the domain" do + # We mock this validation in all other tests because our test data is not + # valid, the network requests slow down tests and could make them flaky. + expect_any_instance_of(ValidEmail2::Address).to receive(:valid_mx?).and_call_original user.email = "example@ho020tmail.com" expect(user).to_not be_valid end