From 36ab1e9976e67d0014c42f7f641e418e53375904 Mon Sep 17 00:00:00 2001 From: cm-dyoshikawa Date: Mon, 3 Feb 2025 16:31:38 +0900 Subject: [PATCH] fix domain checking logic in address.rb --- lib/valid_email2/address.rb | 8 +++++--- spec/address_spec.rb | 32 ++++++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/lib/valid_email2/address.rb b/lib/valid_email2/address.rb index aa9348c..994f6d7 100644 --- a/lib/valid_email2/address.rb +++ b/lib/valid_email2/address.rb @@ -119,10 +119,12 @@ def domain_is_in?(domain_list) address_domain = address.domain.downcase return true if domain_list.include?(address_domain) - i = address_domain.index('.') - return false unless i + while i = address_domain.index('.') + address_domain = address_domain[(i + 1)..-1] + return true if domain_list.include?(address_domain) + end - domain_list.include?(address_domain[(i + 1)..-1]) + false end def mx_server_is_in?(domain_list) diff --git a/spec/address_spec.rb b/spec/address_spec.rb index 7b713a0..998224a 100644 --- a/spec/address_spec.rb +++ b/spec/address_spec.rb @@ -56,6 +56,30 @@ end end + describe "#disposable_domain?" do + let(:disposable_domain) { ValidEmail2.disposable_emails.first } + + it "is true if the domain is in the disposable_emails list" do + address = described_class.new("foo@#{disposable_domain}") + expect(address.disposable_domain?).to eq true + end + + it "is true if the domain is a subdomain of a disposable domain" do + address = described_class.new("foo@sub.#{disposable_domain}") + expect(address.disposable_domain?).to eq true + end + + it "is true if the domain is a deeply nested subdomain of a disposable domain" do + address = described_class.new("foo@sub3.sub2.sub1.#{disposable_domain}") + expect(address.disposable_domain?).to eq true + end + + it "is false if the domain is not in the disposable_emails list" do + address = described_class.new("foo@example.com") + expect(address.disposable_domain?).to eq false + end + end + describe "caching" do let(:email_address) { "example@ymail.com" } let(:email_instance) { described_class.new(email_address) } @@ -181,7 +205,7 @@ email_instance.valid_strict_mx? end - it "calls the the MX servers lookup" do + it "calls the the MX servers lookup" do email_instance.valid_strict_mx? expect(Resolv::DNS).to have_received(:open).once @@ -308,8 +332,8 @@ stub_const("ValidEmail2::DnsRecordsCache::MAX_CACHE_SIZE", 0) end - it "prunes the cache" do - expect(dns_records_cache_instance).to receive(:prune_cache).once + it "prunes the cache" do + expect(dns_records_cache_instance).to receive(:prune_cache).once email_instance.valid_mx? end @@ -331,7 +355,7 @@ end end end - end + end end end end