def test_links_environment_env_var_not_set(self): """Deals with the case where nothing is overridden and `environment` not set.""" # Prepare signal with status change to `AFGEHANDELD`. status = StatusFactory.create(_signal=self.signal, state=workflow.AFGEHANDELD) self.signal.status = status self.signal.save() # Check that generated emails contain the correct links for all # configured environments: env_fe_mapping = feedback_settings.FEEDBACK_ENV_FE_MAPPING self.assertEqual(len(env_fe_mapping), 1) for environment, fe_location in env_fe_mapping.items(): with mock.patch.dict('os.environ', {}, clear=True): mail.outbox = [] ma = MailActions() ma.apply(signal_id=self.signal.id) self.assertEqual(len(mail.outbox), 1) message = mail.outbox[0] self.assertIn('http://dummy_link', message.body) self.assertIn('http://dummy_link', message.alternatives[0][0]) # we want a history entry when a email was sent self.assertEqual(Note.objects.count(), len(env_fe_mapping))
def test_send_mail_reporter_status_changed_afgehandeld_no_email(self): # Prepare signal with status change to `AFGEHANDELD`. StatusFactory.create(_signal=self.signal_no_email, state=workflow.BEHANDELING) status = StatusFactory.create(_signal=self.signal_no_email, state=workflow.AFGEHANDELD) self.signal_no_email.status = status self.signal_no_email.save() self._apply_mail_actions(['Send mail signal handled'], self.signal_no_email) # no mail rule should activate actions = self._get_mail_rules(['Send mail signal handled' ])._get_actions(self.signal_no_email) self.assertEqual(len(actions), 0) # Check mail contents ma = MailActions() ma.apply(signal_id=self.signal_no_email.id) self.assertEqual(0, Feedback.objects.count()) self.assertEqual(len(mail.outbox), 0) # we want no history entry when no email was sent self.assertEqual(Note.objects.count(), 0)
def test_links_in_different_environments(self): """Test that generated feedback links contain the correct host.""" # Prepare signal with status change to `AFGEHANDELD`. StatusFactory.create(_signal=self.signal, state=workflow.BEHANDELING) status = StatusFactory.create(_signal=self.signal, state=workflow.AFGEHANDELD) self.signal.status = status self.signal.save() # Check that generated emails contain the correct links for all # configured environments: env_fe_mapping = getattr(settings, 'FEEDBACK_ENV_FE_MAPPING', feedback_settings.FEEDBACK_ENV_FE_MAPPING) self.assertEqual(len(env_fe_mapping), 3) # sanity check Amsterdam installation has three for environment, fe_location in env_fe_mapping.items(): local_env = {'ENVIRONMENT': environment} with mock.patch.dict('os.environ', local_env): mail.outbox = [] ma = MailActions() ma.apply(signal_id=self.signal.id) self.assertEqual(len(mail.outbox), 1) message = mail.outbox[0] self.assertIn(fe_location, message.body) self.assertIn(fe_location, message.alternatives[0][0]) # we want a history entry when a email was sent self.assertEqual(Note.objects.count(), len(env_fe_mapping))
def test_send_mail_reporter_status_changed_afgehandeld(self): # Prepare signal with status change from `BEHANDELING` to `AFGEHANDELD`. StatusFactory.create(_signal=self.signal, state=workflow.BEHANDELING) status = StatusFactory.create(_signal=self.signal, state=workflow.AFGEHANDELD) self.signal.status = status self.signal.save() # Is the intended rule activated? actions = self._get_mail_rules(['Send mail signal handled' ])._get_actions(self.signal) self.assertEqual(len(actions), 1) # Is it the only one that activates? ma = MailActions(mail_rules=SIGNAL_MAIL_RULES) activated = ma._get_actions(self.signal) self.assertEqual(set(actions), set(activated)) # Check mail contents ma.apply(signal_id=self.signal.id) self.assertEqual(1, Feedback.objects.count()) self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].subject, f'Betreft melding: {self.signal.id}') self.assertEqual(mail.outbox[0].to, [ self.signal.reporter.email, ]) # we want a history entry when a email was sent self.assertEqual(Note.objects.count(), 1)
def test_send_mail_reporter_created(self): # Is the intended rule activated? actions = self._get_mail_rules(['Send mail signal created' ])._get_actions(self.signal) self.assertEqual(len(actions), 1) # Is it the only one that activates? ma = MailActions(mail_rules=SIGNAL_MAIL_RULES) activated = ma._get_actions(self.signal) self.assertEqual(set(actions), set(activated)) # Check mail contents ma.apply(signal_id=self.signal.id) self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].subject, f'Bedankt voor uw melding ({self.signal.id})') self.assertEqual(mail.outbox[0].to, [ self.signal.reporter.email, ]) self.assertIn('10 oktober 2018 12:00', mail.outbox[0].body) category = self.signal.category_assignment.category self.assertIn(category.handling_message, mail.outbox[0].body) # we want a history entry when a email was sent self.assertEqual(Note.objects.count(), 1)
def test_no_email_for_anonymous_reporter(self): ma = MailActions(mail_rules=SIGNAL_MAIL_RULES) ma.apply(self.signal_no_email.id, send_mail=True) self.assertEqual(len(mail.outbox), 0) # we want no history entry when no email was sent self.assertEqual(Note.objects.count(), 0)
def _apply_mail_actions(self, action_names, signal): """ Apply only specified mail rules. (allows better isolation of tests) """ mail_rules = [ r for r in SIGNAL_MAIL_RULES if r['name'] in set(action_names) ] ma = MailActions(mail_rules=mail_rules) ma.apply(signal_id=signal.id)
def test_send_mail_reporter_status_changed_afgehandeld_txt_and_html(self): # Prepare signal with status change from `BEHANDELING` to `AFGEHANDELD`. StatusFactory.create(_signal=self.signal, state=workflow.BEHANDELING) status = StatusFactory.create(_signal=self.signal, state=workflow.AFGEHANDELD) self.signal.status = status self.signal.save() # Is the intended rule activated? actions = self._get_mail_rules(['Send mail signal handled' ])._get_actions(self.signal) self.assertEqual(len(actions), 1) # Is it the only one that activates? ma = MailActions(mail_rules=SIGNAL_MAIL_RULES) activated = ma._get_actions(self.signal) self.assertEqual(set(actions), set(activated)) # Check mail contents ma.apply(signal_id=self.signal.id) self.assertEqual(len(mail.outbox), 1) self.assertEqual(1, Feedback.objects.count()) feedback = Feedback.objects.get(_signal__id=self.signal.id) message = mail.outbox[0] self.assertEqual(message.subject, f'Meer over uw melding {self.signal.id}') self.assertEqual(message.to, [ self.signal.reporter.email, ]) self.assertEqual(mail.outbox[0].from_email, settings.DEFAULT_FROM_EMAIL) positive_feedback_url, negative_feedback_url = get_feedback_urls( feedback) context = { 'negative_feedback_url': negative_feedback_url, 'positive_feedback_url': positive_feedback_url, 'signal': self.signal, 'status': status, 'ORGANIZATION_NAME': settings.ORGANIZATION_NAME, } txt_message = loader.get_template( 'email/signal_status_changed_afgehandeld.txt').render(context) html_message = loader.get_template( 'email/signal_status_changed_afgehandeld.html').render(context) self.assertEqual(message.body, txt_message) content, mime_type = message.alternatives[0] self.assertEqual(mime_type, 'text/html') self.assertEqual(content, html_message) # we want a history entry when a email was sent self.assertEqual(Note.objects.count(), 1)
def test_never_send_mail_for_child_signal(self): ma = MailActions(mail_rules=SIGNAL_MAIL_RULES) actions = ma._get_actions(self.child_signal) self.assertEqual(len(actions), 0) ma.apply(signal_id=self.child_signal.id, send_mail=True) self.assertEqual(len(mail.outbox), 0) # we want no history entry when no email was sent: self.assertEqual(Note.objects.count(), 0)
def test_send_mail_reporter_created_custom_handling_message(self): # Make sure a category's handling messages makes it to the reporter via # the mail generated on creation of a nuisance complaint. category = self.signal.category_assignment.category category.handling_message = 'This text should end up in the mail to the reporter' category.save() self.signal.refresh_from_db() MailActions(mail_rules=SIGNAL_MAIL_RULES).apply( signal_id=self.signal.id) self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].subject, f'Bedankt voor uw melding {self.signal.id}') self.assertEqual(mail.outbox[0].to, [ self.signal.reporter.email, ]) self.assertEqual(mail.outbox[0].from_email, settings.DEFAULT_FROM_EMAIL) self.assertIn(settings.ORGANIZATION_NAME, mail.outbox[0].body) self.assertIn('10 oktober 2018 12:00', mail.outbox[0].body) self.assertIn(category.handling_message, mail.outbox[0].body) # we want a history entry when a email was sent self.assertEqual(Note.objects.count(), 1)
def _get_mail_rules(self, action_names): """ Get MailActions object instantiated with specific mail rules. """ mail_rules = [ r for r in SIGNAL_MAIL_RULES if r['name'] in set(action_names) ] return MailActions(mail_rules=mail_rules)
def test_send_mail_reporter_status_changed_afgehandeld_no_status_afgehandeld( self): # Note: SignalFactory always creates a signal with GEMELD status. # TODO: test is redundant, remove status = StatusFactory.create(_signal=self.signal, state=workflow.BEHANDELING) self.signal.status = status self.signal.save() # no mail rule should activate ma = MailActions(mail_rules=SIGNAL_MAIL_RULES) activated = ma._get_actions(self.signal) self.assertEqual(len(activated), 0) # Check mail contents ma.apply(signal_id=self.signal.id) self.assertEqual(0, Feedback.objects.count()) self.assertEqual(len(mail.outbox), 0) # we want no history entry when no email was sent self.assertEqual(Note.objects.count(), 0)
def test_send_no_mail_reporter_status_changed_afgehandeld_after_verzoek_tot_heropenen( self): # Prepare signal with status change from `VERZOEK_TOT_HEROPENEN` to `AFGEHANDELD`, # this should not lead to an email being sent. StatusFactory.create(_signal=self.signal, state=workflow.VERZOEK_TOT_HEROPENEN) status = StatusFactory.create(_signal=self.signal, state=workflow.AFGEHANDELD) self.signal.status = status self.signal.save() # no mail rule should activate ma = MailActions(mail_rules=SIGNAL_MAIL_RULES) activated = ma._get_actions(self.signal) self.assertEqual(len(activated), 0) # Check mail contents ma.apply(signal_id=self.signal.id) self.assertEqual(0, Feedback.objects.count()) self.assertEqual(len(mail.outbox), 0) # we want no history entry when no email was sent self.assertEqual(Note.objects.count(), 0)
def test_send_mail_reporter_created_only_once(self): signal = SignalFactory.create(reporter__email='*****@*****.**') status = StatusFactory.create(_signal=signal, state=workflow.BEHANDELING) signal.status = status signal.save() status = StatusFactory.create(_signal=signal, state=workflow.GEMELD) signal.status = status signal.save() MailActions().apply(signal_id=signal.id, send_mail=True) self.assertEqual(len(mail.outbox), 0) # we want no history entry when no email was sent self.assertEqual(Note.objects.count(), 0)
def send_mail_reporter_status_changed(signal, *args, **kwargs): mail_actions = MailActions() return mail_actions.apply(signal_id=signal.pk)