|
39 | 39 | expect(address.valid?).to eq true
|
40 | 40 | end
|
41 | 41 | end
|
| 42 | + |
| 43 | + describe "caching" do |
| 44 | + let(:email_address) { "[email protected]" } |
| 45 | + let(:email_instance) { described_class.new(email_address) } |
| 46 | + let(:dns_records_cache_instance) { ValidEmail2::DnsRecordsCache.new } |
| 47 | + let(:ttl) { 1_000 } |
| 48 | + let(:mock_resolv_dns) { instance_double(Resolv::DNS) } |
| 49 | + let(:mock_mx_records) { [double("MX", exchange: "mx.ymail.com", preference: 10, ttl: ttl)] } |
| 50 | + |
| 51 | + before do |
| 52 | + allow(email_instance).to receive(:null_mx?).and_return(false) |
| 53 | + allow(Resolv::DNS).to receive(:open).and_yield(mock_resolv_dns) |
| 54 | + allow(mock_resolv_dns).to receive(:timeouts=) |
| 55 | + end |
| 56 | + |
| 57 | + describe "#valid_strict_mx?" do |
| 58 | + let(:cached_at) { Time.now } |
| 59 | + let(:mock_cache_data) { { email_instance.address.domain => { records: mock_mx_records, cached_at: cached_at, ttl: ttl } } } |
| 60 | + |
| 61 | + before do |
| 62 | + allow(mock_resolv_dns).to receive(:getresources) |
| 63 | + .with(email_instance.address.domain, Resolv::DNS::Resource::IN::MX) |
| 64 | + .and_return(mock_mx_records) |
| 65 | + end |
| 66 | + |
| 67 | + it "calls the MX servers lookup when the email is not cached" do |
| 68 | + result = email_instance.valid_strict_mx? |
| 69 | + |
| 70 | + expect(Resolv::DNS).to have_received(:open).once |
| 71 | + expect(result).to be true |
| 72 | + end |
| 73 | + |
| 74 | + it "does not call the MX servers lookup when the email is cached" do |
| 75 | + email_instance.valid_strict_mx? |
| 76 | + email_instance.valid_strict_mx? |
| 77 | + |
| 78 | + expect(Resolv::DNS).to have_received(:open).once |
| 79 | + end |
| 80 | + |
| 81 | + it "returns the cached result for subsequent calls" do |
| 82 | + first_result = email_instance.valid_strict_mx? |
| 83 | + expect(first_result).to be true |
| 84 | + |
| 85 | + allow(mock_resolv_dns).to receive(:getresources) |
| 86 | + .with(email_instance.address.domain, Resolv::DNS::Resource::IN::MX) |
| 87 | + .and_return([]) |
| 88 | + |
| 89 | + second_result = email_instance.valid_strict_mx? |
| 90 | + expect(second_result).to be true |
| 91 | + end |
| 92 | + |
| 93 | + describe "ttl" do |
| 94 | + before do |
| 95 | + dns_records_cache_instance.instance_variable_set(:@cache, mock_cache_data) |
| 96 | + allow(ValidEmail2::DnsRecordsCache).to receive(:new).and_return(dns_records_cache_instance) |
| 97 | + allow(dns_records_cache_instance).to receive(:fetch).with(email_instance.address.domain).and_call_original |
| 98 | + end |
| 99 | + |
| 100 | + context "when the time since last lookup is less than the cached ttl entry" do |
| 101 | + let(:cached_at) { Time.now } |
| 102 | + |
| 103 | + it "does not call the MX servers lookup" do |
| 104 | + email_instance.valid_strict_mx? |
| 105 | + |
| 106 | + expect(Resolv::DNS).not_to have_received(:open) |
| 107 | + end |
| 108 | + end |
| 109 | + |
| 110 | + context "when the time since last lookup is greater than the cached ttl entry" do |
| 111 | + let(:cached_at) { Time.now - ttl } |
| 112 | + |
| 113 | + it "calls the MX servers lookup" do |
| 114 | + email_instance.valid_strict_mx? |
| 115 | + |
| 116 | + expect(Resolv::DNS).to have_received(:open).once |
| 117 | + end |
| 118 | + end |
| 119 | + end |
| 120 | + |
| 121 | + describe "cache size" do |
| 122 | + before do |
| 123 | + dns_records_cache_instance.instance_variable_set(:@cache, mock_cache_data) |
| 124 | + allow(ValidEmail2::DnsRecordsCache).to receive(:new).and_return(dns_records_cache_instance) |
| 125 | + allow(dns_records_cache_instance).to receive(:fetch).with(email_instance.address.domain).and_call_original |
| 126 | + end |
| 127 | + |
| 128 | + context "when the cache size is less than or equal to the max cache size" do |
| 129 | + before do |
| 130 | + stub_const("ValidEmail2::DnsRecordsCache::MAX_CACHE_SIZE", 1) |
| 131 | + end |
| 132 | + |
| 133 | + it "does not prune the cache" do |
| 134 | + expect(dns_records_cache_instance).not_to receive(:prune_cache) |
| 135 | + |
| 136 | + email_instance.valid_strict_mx? |
| 137 | + end |
| 138 | + |
| 139 | + it "does not call the MX servers lookup" do |
| 140 | + email_instance.valid_strict_mx? |
| 141 | + |
| 142 | + expect(Resolv::DNS).not_to have_received(:open) |
| 143 | + end |
| 144 | + |
| 145 | + context "and there are older cached entries" do |
| 146 | + let(:mock_cache_data) { { "another_domain.com" => { records: mock_mx_records, cached_at: cached_at - 100, ttl: ttl } } } |
| 147 | + |
| 148 | + it "does not prune those entries" do |
| 149 | + email_instance.valid_strict_mx? |
| 150 | + |
| 151 | + expect(dns_records_cache_instance.instance_variable_get(:@cache).keys.size).to eq 2 |
| 152 | + expect(dns_records_cache_instance.instance_variable_get(:@cache).keys).to match_array([email_instance.address.domain, "another_domain.com"]) |
| 153 | + end |
| 154 | + end |
| 155 | + end |
| 156 | + |
| 157 | + context "when the cache size is greater than the max cache size" do |
| 158 | + before do |
| 159 | + stub_const("ValidEmail2::DnsRecordsCache::MAX_CACHE_SIZE", 0) |
| 160 | + end |
| 161 | + |
| 162 | + it "prunes the cache" do |
| 163 | + expect(dns_records_cache_instance).to receive(:prune_cache).once |
| 164 | + |
| 165 | + email_instance.valid_strict_mx? |
| 166 | + end |
| 167 | + |
| 168 | + it "calls the the MX servers lookup" do |
| 169 | + email_instance.valid_strict_mx? |
| 170 | + |
| 171 | + expect(Resolv::DNS).to have_received(:open).once |
| 172 | + end |
| 173 | + |
| 174 | + context "and there are older cached entries" do |
| 175 | + let(:mock_cache_data) { { "another_domain.com" => { records: mock_mx_records, cached_at: cached_at - 100, ttl: ttl } } } |
| 176 | + |
| 177 | + it "prunes those entries" do |
| 178 | + email_instance.valid_strict_mx? |
| 179 | + |
| 180 | + expect(dns_records_cache_instance.instance_variable_get(:@cache).keys.size).to eq 1 |
| 181 | + expect(dns_records_cache_instance.instance_variable_get(:@cache).keys).to match_array([email_instance.address.domain]) |
| 182 | + end |
| 183 | + end |
| 184 | + end |
| 185 | + end |
| 186 | + end |
| 187 | + |
| 188 | + describe "#valid_mx?" do |
| 189 | + let(:cached_at) { Time.now } |
| 190 | + let(:mock_cache_data) { { email_instance.address.domain => { records: mock_a_records, cached_at: cached_at, ttl: ttl } } } |
| 191 | + let(:mock_a_records) { [double("A", address: "192.168.1.1", ttl: ttl)] } |
| 192 | + |
| 193 | + before do |
| 194 | + allow(email_instance).to receive(:mx_servers).and_return(mock_mx_records) |
| 195 | + allow(mock_resolv_dns).to receive(:getresources) |
| 196 | + .with(email_instance.address.domain, Resolv::DNS::Resource::IN::A) |
| 197 | + .and_return(mock_a_records) |
| 198 | + end |
| 199 | + |
| 200 | + it "calls the MX or A servers lookup when the email is not cached" do |
| 201 | + result = email_instance.valid_mx? |
| 202 | + |
| 203 | + expect(Resolv::DNS).to have_received(:open).once |
| 204 | + expect(result).to be true |
| 205 | + end |
| 206 | + |
| 207 | + it "does not call the MX or A servers lookup when the email is cached" do |
| 208 | + email_instance.valid_mx? |
| 209 | + email_instance.valid_mx? |
| 210 | + |
| 211 | + expect(Resolv::DNS).to have_received(:open).once |
| 212 | + end |
| 213 | + |
| 214 | + it "returns the cached result for subsequent calls" do |
| 215 | + first_result = email_instance.valid_mx? |
| 216 | + expect(first_result).to be true |
| 217 | + |
| 218 | + allow(mock_resolv_dns).to receive(:getresources) |
| 219 | + .with(email_instance.address.domain, Resolv::DNS::Resource::IN::A) |
| 220 | + .and_return([]) |
| 221 | + |
| 222 | + second_result = email_instance.valid_mx? |
| 223 | + expect(second_result).to be true |
| 224 | + end |
| 225 | + |
| 226 | + describe "ttl" do |
| 227 | + before do |
| 228 | + dns_records_cache_instance.instance_variable_set(:@cache, mock_cache_data) |
| 229 | + allow(ValidEmail2::DnsRecordsCache).to receive(:new).and_return(dns_records_cache_instance) |
| 230 | + allow(dns_records_cache_instance).to receive(:fetch).with(email_instance.address.domain).and_call_original |
| 231 | + end |
| 232 | + |
| 233 | + context "when the time since last lookup is less than the cached ttl entry" do |
| 234 | + let(:cached_at) { Time.now } |
| 235 | + |
| 236 | + it "does not call the MX or A servers lookup" do |
| 237 | + email_instance.valid_mx? |
| 238 | + |
| 239 | + expect(Resolv::DNS).not_to have_received(:open) |
| 240 | + end |
| 241 | + end |
| 242 | + |
| 243 | + context "when the time since last lookup is greater than the cached ttl entry" do |
| 244 | + let(:cached_at) { Time.now - ttl } |
| 245 | + |
| 246 | + it "calls the MX or A servers lookup " do |
| 247 | + email_instance.valid_mx? |
| 248 | + |
| 249 | + expect(Resolv::DNS).to have_received(:open).once |
| 250 | + end |
| 251 | + end |
| 252 | + end |
| 253 | + |
| 254 | + describe "cache size" do |
| 255 | + before do |
| 256 | + dns_records_cache_instance.instance_variable_set(:@cache, mock_cache_data) |
| 257 | + allow(ValidEmail2::DnsRecordsCache).to receive(:new).and_return(dns_records_cache_instance) |
| 258 | + allow(dns_records_cache_instance).to receive(:fetch).with(email_instance.address.domain).and_call_original |
| 259 | + end |
| 260 | + |
| 261 | + context "when the cache size is less than or equal to the max cache size" do |
| 262 | + before do |
| 263 | + stub_const("ValidEmail2::DnsRecordsCache::MAX_CACHE_SIZE", 1) |
| 264 | + end |
| 265 | + |
| 266 | + it "does not prune the cache" do |
| 267 | + expect(email_instance).not_to receive(:prune_cache) |
| 268 | + |
| 269 | + email_instance.valid_mx? |
| 270 | + end |
| 271 | + |
| 272 | + it "does not call the MX or A servers lookup" do |
| 273 | + email_instance.valid_mx? |
| 274 | + |
| 275 | + expect(Resolv::DNS).not_to have_received(:open) |
| 276 | + end |
| 277 | + |
| 278 | + context "and there are older cached entries" do |
| 279 | + let(:mock_cache_data) { { "another_domain.com" => { records: mock_a_records, cached_at: cached_at - 100, ttl: ttl } } } |
| 280 | + |
| 281 | + it "does not prune those entries" do |
| 282 | + email_instance.valid_mx? |
| 283 | + |
| 284 | + expect(dns_records_cache_instance.instance_variable_get(:@cache).keys.size).to eq 2 |
| 285 | + expect(dns_records_cache_instance.instance_variable_get(:@cache).keys).to match_array([email_instance.address.domain, "another_domain.com"]) |
| 286 | + end |
| 287 | + end |
| 288 | + end |
| 289 | + |
| 290 | + context "when the cache size is greater than the max cache size" do |
| 291 | + before do |
| 292 | + stub_const("ValidEmail2::DnsRecordsCache::MAX_CACHE_SIZE", 0) |
| 293 | + end |
| 294 | + |
| 295 | + it "prunes the cache" do |
| 296 | + expect(dns_records_cache_instance).to receive(:prune_cache).once |
| 297 | + |
| 298 | + email_instance.valid_mx? |
| 299 | + end |
| 300 | + |
| 301 | + it "calls the MX or A servers lookup" do |
| 302 | + email_instance.valid_mx? |
| 303 | + |
| 304 | + expect(Resolv::DNS).to have_received(:open).once |
| 305 | + end |
| 306 | + |
| 307 | + context "and there are older cached entries" do |
| 308 | + let(:mock_cache_data) { { "another_domain.com" => { records: mock_a_records, cached_at: cached_at - 100, ttl: ttl } } } |
| 309 | + |
| 310 | + it "prunes those entries" do |
| 311 | + email_instance.valid_mx? |
| 312 | + |
| 313 | + expect(dns_records_cache_instance.instance_variable_get(:@cache).keys.size).to eq 1 |
| 314 | + expect(dns_records_cache_instance.instance_variable_get(:@cache).keys).to match_array([email_instance.address.domain]) |
| 315 | + end |
| 316 | + end |
| 317 | + end |
| 318 | + end |
| 319 | + end |
| 320 | + end |
42 | 321 | end
|
0 commit comments