def check_notify_domain_expiring(dry_run=True): """ Loop all user accounts and all domains and identify all "expiring" domains: less than 90 days left before domain get expired based on `expiry_date` field. If `dry_run` is True only print out identified users and domains. Otherwise actually will send email notifications about those domains. Also checks notifications history to make sure only one email was sent for given domain. Skip sending if user disabled email notifications in Profile settings. """ outgoing_emails = [] for user in Account.users.all(): if not hasattr(user, 'profile'): continue if not user.profile.email_notifications_enabled: continue expiring_domains = {} for domain in user.domains.all(): if not domain.epp_id or domain.status != 'active': # only take in account domains which are registered and active continue t_domain = domain.expiry_date t_now = timezone.now() dt = t_domain - t_now if dt.days > 90: # domain is not expiring at the moment continue if dt.days <= 0: # domain already expired - no email needed continue expiring_domains[domain.name] = domain.expiry_date.date() domains_to_be_notified = [] # now look up all already sent notifications and find only domains # which we did not send notification yet domains_notified = user.notifications.values_list('domain_name', flat=True) domains_to_be_notified = list(set(expiring_domains.keys()).difference(set(domains_notified))) if not domains_to_be_notified: continue for expiring_domain in domains_to_be_notified: logger.info('for %r domain %r is expiring', user, expiring_domain) outgoing_emails.append((user, expiring_domain, expiring_domains[expiring_domain], )) if dry_run: continue notifications.start_email_notification_domain_expiring( user=user, domain_name=expiring_domain, expiry_date=expiring_domains[expiring_domain], ) return outgoing_emails
def test_execute_email_notification_failed(mock_send): tester = testsupport.prepare_tester_account() new_notification = notifications.start_email_notification_domain_expiring( user=tester, domain_name='abcd.ai', expiry_date='2020-01-01', ) mock_send.side_effect = Exception('some error while sending') assert notifications.execute_email_notification(new_notification) is False
def test_execute_email_notification_success(mock_send): tester = testsupport.prepare_tester_account() new_notification = notifications.start_email_notification_domain_expiring( user=tester, domain_name='abcd.ai', expiry_date='2020-01-01', ) mock_send.return_value = True assert notifications.execute_email_notification(new_notification) is True
def test_process_notifications_queue_with_failed(mock_send): tester = testsupport.prepare_tester_account() new_notification = notifications.start_email_notification_domain_expiring( user=tester, domain_name='abcd.ai', expiry_date='2020-01-01', ) mock_send.side_effect = Exception('some error while sending') notifications.process_notifications_queue(iterations=1, delay=0.1) new_notification.refresh_from_db() assert new_notification.status == 'failed'
def test_process_notifications_queue_with_success(mock_send): tester = testsupport.prepare_tester_account() new_notification = notifications.start_email_notification_domain_expiring( user=tester, domain_name='abcd.ai', expiry_date='2020-01-01', ) mock_send.return_value = True notifications.process_notifications_queue(iterations=1, delay=0.1) new_notification.refresh_from_db() assert new_notification.status == 'sent'
def test_start_email_notification_domain_expiring(): tester = testsupport.prepare_tester_account() new_notification = notifications.start_email_notification_domain_expiring( user=tester, domain_name='abcd.ai', expiry_date='2020-01-01', ) assert new_notification.account == tester assert new_notification.recipient == '*****@*****.**' assert new_notification.status == 'started' assert new_notification.type == 'email' assert new_notification.subject == 'domain_expiring' assert new_notification.domain_name == 'abcd.ai' assert new_notification.details == { 'expiry_date': '2020-01-01', }