def test_send_email_does_not_resend_within_duplicate_interval(self): can_send_emails_ctx = self.swap( feconf, 'CAN_SEND_EMAILS_TO_USERS', True) duplicate_email_ctx = self.swap( feconf, 'DUPLICATE_EMAIL_INTERVAL_MINS', 2) logged_errors = [] def _log_error_for_tests(error_message): logged_errors.append(error_message) log_new_error_counter = test_utils.CallCounter(_log_error_for_tests) log_new_error_ctx = self.swap( email_manager, 'log_new_error', log_new_error_counter) with can_send_emails_ctx, duplicate_email_ctx, log_new_error_ctx: config_services.set_property( self.admin_id, email_manager.EMAIL_SENDER_NAME.name, 'Email Sender') all_models = email_models.SentEmailModel.get_all().fetch() self.assertEqual(len(all_models), 0) # pylint: disable=protected-access email_manager._send_email( self.new_user_id, feconf.SYSTEM_COMMITTER_ID, feconf.EMAIL_INTENT_SIGNUP, 'Email Subject', 'Email Body', feconf.SYSTEM_EMAIL_ADDRESS) # Check that a new email was sent. messages = self.mail_stub.get_sent_messages(to=self.NEW_USER_EMAIL) self.assertEqual(1, len(messages)) # Check that the content of this email was recorded in # SentEmailModel. all_models = email_models.SentEmailModel.get_all().fetch() self.assertEqual(len(all_models), 1) # No error should be recorded in the logs. self.assertEqual(log_new_error_counter.times_called, 0) email_manager._send_email( self.new_user_id, feconf.SYSTEM_COMMITTER_ID, feconf.EMAIL_INTENT_SIGNUP, 'Email Subject', 'Email Body', feconf.SYSTEM_EMAIL_ADDRESS) # pylint: enable=protected-access # An error should be recorded in the logs. self.assertEqual(log_new_error_counter.times_called, 1) self.assertRegexpMatches(logged_errors[0], 'Duplicate email') # Check that a new email was not sent. messages = self.mail_stub.get_sent_messages(to=self.NEW_USER_EMAIL) self.assertEqual(1, len(messages)) # Check that the content of this email was not recorded in # SentEmailModel. all_models = email_models.SentEmailModel.get_all().fetch() self.assertEqual(len(all_models), 1)
def test_send_email_does_not_resend_within_duplicate_interval(self): can_send_emails_ctx = self.swap(feconf, 'CAN_SEND_EMAILS_TO_USERS', True) duplicate_email_ctx = self.swap(feconf, 'DUPLICATE_EMAIL_INTERVAL_MINS', 2) logged_errors = [] def _log_error_for_tests(error_message): logged_errors.append(error_message) log_new_error_counter = test_utils.CallCounter(_log_error_for_tests) log_new_error_ctx = self.swap(email_manager, 'log_new_error', log_new_error_counter) with can_send_emails_ctx, duplicate_email_ctx, log_new_error_ctx: config_services.set_property(self.admin_id, email_manager.EMAIL_SENDER_NAME.name, 'Email Sender') all_models = email_models.SentEmailModel.get_all().fetch() self.assertEqual(len(all_models), 0) # pylint: disable=protected-access email_manager._send_email(self.new_user_id, feconf.SYSTEM_COMMITTER_ID, feconf.EMAIL_INTENT_SIGNUP, 'Email Subject', 'Email Body') # Check that a new email was sent. messages = self.mail_stub.get_sent_messages(to=self.NEW_USER_EMAIL) self.assertEqual(1, len(messages)) # Check that the content of this email was recorded in # SentEmailModel. all_models = email_models.SentEmailModel.get_all().fetch() self.assertEqual(len(all_models), 1) # No error should be recorded in the logs. self.assertEqual(log_new_error_counter.times_called, 0) email_manager._send_email(self.new_user_id, feconf.SYSTEM_COMMITTER_ID, feconf.EMAIL_INTENT_SIGNUP, 'Email Subject', 'Email Body') # pylint: enable=protected-access # An error should be recorded in the logs. self.assertEqual(log_new_error_counter.times_called, 1) self.assertRegexpMatches(logged_errors[0], 'Duplicate email') # Check that a new email was not sent. messages = self.mail_stub.get_sent_messages(to=self.NEW_USER_EMAIL) self.assertEqual(1, len(messages)) # Check that the content of this email was not recorded in # SentEmailModel. all_models = email_models.SentEmailModel.get_all().fetch() self.assertEqual(len(all_models), 1)
def test_sending_email_with_different_body_but_same_hash(self): """Hash for both messages is same but body is different""" can_send_emails_ctx = self.swap(feconf, 'CAN_SEND_EMAILS_TO_USERS', True) duplicate_email_ctx = self.swap(feconf, 'DUPLICATE_EMAIL_INTERVAL_MINS', 2) with can_send_emails_ctx, duplicate_email_ctx, self.generate_hash_ctx: all_models = email_models.SentEmailModel.get_all().fetch() self.assertEqual(len(all_models), 0) email_models.SentEmailModel.create(self.new_user_id, self.NEW_USER_EMAIL, feconf.SYSTEM_COMMITTER_ID, feconf.SYSTEM_EMAIL_ADDRESS, feconf.EMAIL_INTENT_SIGNUP, 'Email Subject', 'Email Body1', datetime.datetime.utcnow()) # Check that the content of this email was recorded in # SentEmailModel. all_models = email_models.SentEmailModel.get_all().fetch() self.assertEqual(len(all_models), 1) # pylint: disable=protected-access email_manager._send_email(self.new_user_id, feconf.SYSTEM_COMMITTER_ID, feconf.EMAIL_INTENT_SIGNUP, 'Email Subject', 'Email Body') # pylint: enable=protected-access # Check that a new email was sent. messages = self.mail_stub.get_sent_messages(to=self.NEW_USER_EMAIL) self.assertEqual(1, len(messages)) # Check that the content of this email was recorded in # SentEmailModel. all_models = email_models.SentEmailModel.get_all().fetch() self.assertEqual(len(all_models), 2) # Check that the contents of the model are correct. sent_email_model1 = all_models[0] sent_email_model2 = all_models[1] self.assertEqual(sent_email_model1.email_hash, sent_email_model2.email_hash) self.assertEqual(sent_email_model1.recipient_id, sent_email_model2.recipient_id) self.assertEqual(sent_email_model1.subject, sent_email_model2.subject) self.assertNotEqual(sent_email_model1.html_body, sent_email_model2.html_body)
def test_sending_email_with_different_body_but_same_hash(self): """Hash for both messages is same but body is different""" can_send_emails_ctx = self.swap( feconf, 'CAN_SEND_EMAILS_TO_USERS', True) duplicate_email_ctx = self.swap( feconf, 'DUPLICATE_EMAIL_INTERVAL_MINS', 2) with can_send_emails_ctx, duplicate_email_ctx, self.generate_hash_ctx: all_models = email_models.SentEmailModel.get_all().fetch() self.assertEqual(len(all_models), 0) email_models.SentEmailModel.create( self.new_user_id, self.NEW_USER_EMAIL, feconf.SYSTEM_COMMITTER_ID, feconf.SYSTEM_EMAIL_ADDRESS, feconf.EMAIL_INTENT_SIGNUP, 'Email Subject', 'Email Body1', datetime.datetime.utcnow()) # Check that the content of this email was recorded in # SentEmailModel. all_models = email_models.SentEmailModel.get_all().fetch() self.assertEqual(len(all_models), 1) # pylint: disable=protected-access email_manager._send_email( self.new_user_id, feconf.SYSTEM_COMMITTER_ID, feconf.EMAIL_INTENT_SIGNUP, 'Email Subject', 'Email Body', feconf.SYSTEM_EMAIL_ADDRESS) # pylint: enable=protected-access # Check that a new email was sent. messages = self.mail_stub.get_sent_messages(to=self.NEW_USER_EMAIL) self.assertEqual(1, len(messages)) # Check that the content of this email was recorded in # SentEmailModel. all_models = email_models.SentEmailModel.get_all().fetch() self.assertEqual(len(all_models), 2) # Check that the contents of the model are correct. sent_email_model1 = all_models[0] sent_email_model2 = all_models[1] self.assertEqual( sent_email_model1.email_hash, sent_email_model2.email_hash) self.assertEqual( sent_email_model1.recipient_id, sent_email_model2.recipient_id) self.assertEqual( sent_email_model1.subject, sent_email_model2.subject) self.assertNotEqual( sent_email_model1.html_body, sent_email_model2.html_body)
def test_duplicate_emails_are_sent_after_some_time_has_elapsed(self): can_send_emails_ctx = self.swap( feconf, 'CAN_SEND_EMAILS_TO_USERS', True) duplicate_email_ctx = self.swap( feconf, 'DUPLICATE_EMAIL_INTERVAL_MINS', 2) with can_send_emails_ctx, duplicate_email_ctx: all_models = email_models.SentEmailModel.get_all().fetch() self.assertEqual(len(all_models), 0) email_sent_time = (datetime.datetime.utcnow() - datetime.timedelta(minutes=4)) email_models.SentEmailModel.create( self.new_user_id, self.NEW_USER_EMAIL, feconf.SYSTEM_COMMITTER_ID, feconf.SYSTEM_EMAIL_ADDRESS, feconf.EMAIL_INTENT_SIGNUP, 'Email Subject', 'Email Body', email_sent_time) # Check that the content of this email was recorded in # SentEmailModel. all_models = email_models.SentEmailModel.get_all().fetch() self.assertEqual(len(all_models), 1) email_sent_time = (datetime.datetime.utcnow() - datetime.timedelta(minutes=2)) email_models.SentEmailModel.create( self.new_user_id, self.NEW_USER_EMAIL, feconf.SYSTEM_COMMITTER_ID, feconf.SYSTEM_EMAIL_ADDRESS, feconf.EMAIL_INTENT_SIGNUP, 'Email Subject', 'Email Body', email_sent_time) # Check that the content of this email was recorded in # SentEmailModel. all_models = email_models.SentEmailModel.get_all().fetch() self.assertEqual(len(all_models), 2) # pylint: disable=protected-access email_manager._send_email( self.new_user_id, feconf.SYSTEM_COMMITTER_ID, feconf.EMAIL_INTENT_SIGNUP, 'Email Subject', 'Email Body') # pylint: enable=protected-access # Check that a new email was sent. messages = self.mail_stub.get_sent_messages(to=self.NEW_USER_EMAIL) self.assertEqual(1, len(messages)) # Check that the content of this email was recorded in # SentEmailModel. all_models = email_models.SentEmailModel.get_all().fetch() self.assertEqual(len(all_models), 3) # Check that the contents of the model are correct. sent_email_model1 = all_models[0] sent_email_model2 = all_models[1] sent_email_model3 = all_models[2] self.assertEqual( sent_email_model1.email_hash, sent_email_model2.email_hash) self.assertEqual( sent_email_model1.email_hash, sent_email_model3.email_hash)
def test_duplicate_emails_are_sent_after_some_time_has_elapsed(self): can_send_emails_ctx = self.swap(feconf, 'CAN_SEND_EMAILS_TO_USERS', True) duplicate_email_ctx = self.swap(feconf, 'DUPLICATE_EMAIL_INTERVAL_MINS', 2) with can_send_emails_ctx, duplicate_email_ctx: all_models = email_models.SentEmailModel.get_all().fetch() self.assertEqual(len(all_models), 0) email_sent_time = (datetime.datetime.utcnow() - datetime.timedelta(minutes=4)) email_models.SentEmailModel.create( self.new_user_id, self.NEW_USER_EMAIL, feconf.SYSTEM_COMMITTER_ID, feconf.SYSTEM_EMAIL_ADDRESS, feconf.EMAIL_INTENT_SIGNUP, 'Email Subject', 'Email Body', email_sent_time) # Check that the content of this email was recorded in # SentEmailModel. all_models = email_models.SentEmailModel.get_all().fetch() self.assertEqual(len(all_models), 1) email_sent_time = (datetime.datetime.utcnow() - datetime.timedelta(minutes=2)) email_models.SentEmailModel.create( self.new_user_id, self.NEW_USER_EMAIL, feconf.SYSTEM_COMMITTER_ID, feconf.SYSTEM_EMAIL_ADDRESS, feconf.EMAIL_INTENT_SIGNUP, 'Email Subject', 'Email Body', email_sent_time) # Check that the content of this email was recorded in # SentEmailModel. all_models = email_models.SentEmailModel.get_all().fetch() self.assertEqual(len(all_models), 2) # pylint: disable=protected-access email_manager._send_email(self.new_user_id, feconf.SYSTEM_COMMITTER_ID, feconf.EMAIL_INTENT_SIGNUP, 'Email Subject', 'Email Body') # pylint: enable=protected-access # Check that a new email was sent. messages = self.mail_stub.get_sent_messages(to=self.NEW_USER_EMAIL) self.assertEqual(1, len(messages)) # Check that the content of this email was recorded in # SentEmailModel. all_models = email_models.SentEmailModel.get_all().fetch() self.assertEqual(len(all_models), 3) # Check that the contents of the model are correct. sent_email_model1 = all_models[0] sent_email_model2 = all_models[1] sent_email_model3 = all_models[2] self.assertEqual(sent_email_model1.email_hash, sent_email_model2.email_hash) self.assertEqual(sent_email_model1.email_hash, sent_email_model3.email_hash)