def test_noisy_flags_separates_domains_in_report(): """Tests domains with the noisy flag set are .""" new = ( ('www.example1.com', '127.0.0.1'), ('www.example7.com', '127.0.0.9'), ) updated = ( ('www.example2.com', '127.0.0.1', '127.0.0.2'), ('www.example9.com', '127.0.0.3', '127.0.0.4'), ) deleted = ( 'www.example3.com', 'www.example15.com', ) noisy = (('www.example7.com', 'www.example2.com', 'www.example3.com')) delta_report = { 'new': new, 'updated': updated, 'deleted': deleted, } report = EmailReport(delta_report, noisy, include_noisy_domains=True) assert report.new == (('www.example1.com', '127.0.0.1'), ) assert report.updated == (('www.example9.com', '127.0.0.3', '127.0.0.4'), ) assert report.deleted == ('www.example15.com', ) assert report.noisy == ('www.example7.com', 'www.example2.com', 'www.example3.com')
def test_generate_example_email_no_noisy(): """Email when no noisy domains.""" new = (('www.examp1e.com', '127.0.0.1'), ) updated = (('www.exampl3.com', '127.0.0.1', '127.0.0.2'), ) deleted = ( 'www.examplle.com', 'www.eeexamplez.com', 'wwwexample.com', ) noisy = set() delta_report = { 'new': new, 'updated': updated, 'deleted': deleted, } report = EmailReport(delta_report, noisy, include_noisy_domains=True) template = email_tools.render_email( 'report.html', domain='www.example.com', report=report, unsubscribe_link='https://dnstwister.report/...', ) with open('tests/manual/example_email_no_noisy.html', 'wb') as email_f: email_f.write(FORMATTING) email_f.write(template)
def test_email_renderer(): """Test the email rendering helper.""" new = (('www.examp1e.com', '127.0.0.1'), ) updated = (('www.exampl3.com', '127.0.0.1', '127.0.0.2'), ) deleted = ('www.examplle.com', ) noisy = ('www.examplle.com', ) delta_report = { 'new': new, 'updated': updated, 'deleted': deleted, } report = EmailReport(delta_report, noisy, include_noisy_domains=True) template = email_tools.render_email( 'report.html', domain='www.example.com', report=report, unsubscribe_link='https://dnstwister.report/...', ) assert template.strip() == textwrap.dedent(""" <style type="text/css"> td,th { padding-right: 10px; text-align: left; } </style> <h1>dnstwister report for 'www<span>.</span>example<span>.</span>com'</h1> <p> <a href="https://dnstwister.report/...">Unsubscribe</a> </p> <h2>New registrations (1)</h2> <table> <thead> <tr> <th>Domain</th> <th>IP address</th> <th>Tools</th> </tr> </thead> <tbody> <tr> <td> www<span>.</span>examp1e<span>.</span>com </td> <td>127.0.0.1</td> <td><a href="https://dnstwister.report/analyse/7777772e6578616d7031652e636f6d">analyse</a></td> </tr> </tbody> </table> <h2>Updated registrations (1)</h2> <table> <thead> <tr> <th>Domain</th> <th>Old IP address</th> <th>New IP address</th> <th>Tools</th> </tr> </thead> <tbody> <tr> <td> www<span>.</span>exampl3<span>.</span>com </td> <td>127.0.0.1</td> <td>127.0.0.2</td> <td><a href="https://dnstwister.report/analyse/7777772e6578616d706c332e636f6d">analyse</a></td> </tr> </tbody> </table> <h2>Noisy domains (1)</h2> <p> The following domains continually change IP or fail then succeed IP resolution. We'll only send you these once a week. </p> <table> <thead> <tr> <th>Domain</th> <th>Tools</th> </tr> </thead> <tbody> <tr> <td> www<span>.</span>examplle<span>.</span>com </td> <td><a href="https://dnstwister.report/analyse/7777772e6578616d706c6c652e636f6d">analyse</a></td> </tr> </tbody> </table> <p> <a href="https://dnstwister.report/...">Unsubscribe</a> </p> """).strip()
def test_email_renderer_domain_sorting(): """Test the email rendering helper sorts domains.""" new = ( ('www.examp2e.com', ''), ('www.examp1e.com', ''), ('www.examp2f.com', ''), ('www.axample.com', 'z'), ('www.axample.com', 'a'), ) updated = [] deleted = [] noisy = [] delta_report = { 'new': new, 'updated': updated, 'deleted': deleted, } report = EmailReport(delta_report, noisy, include_noisy_domains=True) template = email_tools.render_email( 'report.html', domain='www.example.com', report=report, unsubscribe_link='https://dnstwister.report/...', ) assert template.strip() == textwrap.dedent(""" <style type="text/css"> td,th { padding-right: 10px; text-align: left; } </style> <h1>dnstwister report for 'www<span>.</span>example<span>.</span>com'</h1> <p> <a href="https://dnstwister.report/...">Unsubscribe</a> </p> <h2>New registrations (5)</h2> <table> <thead> <tr> <th>Domain</th> <th>IP address</th> <th>Tools</th> </tr> </thead> <tbody> <tr> <td> www<span>.</span>axample<span>.</span>com </td> <td>a</td> <td><a href="https://dnstwister.report/analyse/7777772e6178616d706c652e636f6d">analyse</a></td> </tr> <tr> <td> www<span>.</span>axample<span>.</span>com </td> <td>z</td> <td><a href="https://dnstwister.report/analyse/7777772e6178616d706c652e636f6d">analyse</a></td> </tr> <tr> <td> www<span>.</span>examp1e<span>.</span>com </td> <td></td> <td><a href="https://dnstwister.report/analyse/7777772e6578616d7031652e636f6d">analyse</a></td> </tr> <tr> <td> www<span>.</span>examp2e<span>.</span>com </td> <td></td> <td><a href="https://dnstwister.report/analyse/7777772e6578616d7032652e636f6d">analyse</a></td> </tr> <tr> <td> www<span>.</span>examp2f<span>.</span>com </td> <td></td> <td><a href="https://dnstwister.report/analyse/7777772e6578616d7032662e636f6d">analyse</a></td> </tr> </tbody> </table> <p> <a href="https://dnstwister.report/...">Unsubscribe</a> </p> """).strip()
def process_sub(sub_id, detail): """Process a subscription.""" domain = detail['domain'] email_address = detail['email_address'] # Ensure the domain is registered for reporting, register if not. repository.register_domain(domain) # Mark delta report as "read" so it's not unsubscribed. repository.mark_delta_report_as_read(domain) # Don't send more than once every 24 hours last_sent = repository.email_last_send_for_sub(sub_id) if last_sent is not None: age_last_sent = datetime.datetime.now() - last_sent if age_last_sent < datetime.timedelta(seconds=PERIOD): print 'Skipping {} + {}, < 24h hours'.format(email_address, domain) return delta_report = repository.get_delta_report(domain) if delta_report is None: print 'Skipping {} + {}, no delta report yet'.format( email_address, domain) return delta_updated = repository.delta_report_updated(domain) # If the delta report was updated > 23 hours ago, we're too close to the # next delta report. This means we should hold off so we don't send the # same delta report twice. if delta_updated is not None: age_delta_updated = datetime.datetime.now() - delta_updated if age_delta_updated > datetime.timedelta(hours=23): print 'Skipping {} + {}, delta > 23h hours old'.format( email_address, domain) return delta_domains = delta_reports.extract_domains(delta_report) noisy_domains = get_noisy_domains(delta_domains) report = EmailReport(delta_report, noisy_domains, include_noisy_domains=include_noisy_domains()) if not report.has_results(): print 'Skipping {} + {}, no results to put in email'.format( email_address, domain) return try: send_email(domain, email_address, sub_id, report) except: print 'Failed to send email for {}:\n {}'.format( domain, traceback.format_exc()) finally: # Mark as emailed to ensure we don't flood if there's an error after # the actual email has been sent. repository.update_last_email_sub_sent_date(sub_id)