Skip to content

Commit

Permalink
fix: Update domain hierarchy max depth to 3 and adjust related tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cm-dyoshikawa committed Feb 5, 2025
1 parent 34dd381 commit 980e198
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
4 changes: 2 additions & 2 deletions lib/valid_email2/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def self.permitted_multibyte_characters_regex=(val)
@permitted_multibyte_characters_regex = val
end

def initialize(address, dns_timeout = 5, dns_nameserver = nil, domain_hierarchy_max_depth = 2)
def initialize(address, dns_timeout = 5, dns_nameserver = nil, domain_hierarchy_max_depth: 3)
@parse_error = false
@raw_address = address
@dns_timeout = dns_timeout
Expand Down Expand Up @@ -124,7 +124,7 @@ def domain_is_in?(domain_list)
return false if tokens.length < 3

max_depth = [@domain_hierarchy_max_depth, tokens.length].min
(3..max_depth).each do |depth|
(2..max_depth).each do |depth|
partial_domain = tokens.reverse.first(depth).reverse.join('.')
return true if domain_list.include?(partial_domain)
end
Expand Down
42 changes: 23 additions & 19 deletions spec/address_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,43 +57,47 @@
end

describe "#disposable_domain?" do
let(:disposable_domain) { ValidEmail2.disposable_emails.first }
let(:domain_hierarchy_max_depth) { 10 }

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
context "when the disposable domain does not have subdomains" do
let(:disposable_domain) { ValidEmail2.disposable_emails.find { |domain| domain.count(".") == 1 } }

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 in the disposable_emails list" do
address = described_class.new("foo@#{disposable_domain}", nil, nil, domain_hierarchy_max_depth:)
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 true if the domain is a subdomain of a disposable domain" do
address = described_class.new("foo@sub.#{disposable_domain}", nil, nil, domain_hierarchy_max_depth:)
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("[email protected]")
expect(address.disposable_domain?).to eq false
it "is true if the domain is a deeply nested subdomain of a disposable domain" do
address = described_class.new("[email protected].#{disposable_domain}", nil, nil, domain_hierarchy_max_depth:)
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("[email protected]", nil, nil, domain_hierarchy_max_depth:)
expect(address.disposable_domain?).to eq false
end
end

context "when the disposable domain has subdomains" do
let(:disposable_domain) { ValidEmail2.disposable_emails.find { |domain| domain.count(".") > 1 } }

it "is true if the domain is in the disposable_emails list" do
address = described_class.new("foo@#{disposable_domain}")
address = described_class.new("foo@#{disposable_domain}", nil, nil, domain_hierarchy_max_depth:)
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}")
address = described_class.new("foo@sub.#{disposable_domain}", nil, nil, domain_hierarchy_max_depth:)
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("[email protected].#{disposable_domain}")
address = described_class.new("[email protected].#{disposable_domain}", nil, nil, domain_hierarchy_max_depth:)
expect(address.disposable_domain?).to eq true
end
end
Expand Down

0 comments on commit 980e198

Please sign in to comment.