def test_email_body_three_declines(self, attach_image, finder):
     measure = Measure.objects.get(pk='cerazette')
     call_mocked_command_with_defaults(
         _makeContext(declines=[
             {
                 'measure': measure,
                 'from': 99.92,
                 'to': 0.12
             },
             {
                 'measure': measure,
                 'from': 30,
                 'to': 10
             },
             {
                 'measure': measure,
                 'from': 20,
                 'to': 10
             },
         ]), finder)
     message = mail.outbox[-1].alternatives[0]
     html = message[0]
     self.assertRegexpMatches(html, 'It also slipped:')
     self.assertRegexpMatches(
         html,
         re.compile('<ul.*<li>considerably on.*'
                    '<li>moderately on.*</ul>', re.DOTALL))
 def test_bad_alert_image_error_not_sent_and_not_raised(self, attach_image, finder):
     attach_image.side_effect = BadAlertImageError
     measure = MagicMock()
     measure.id = "measureid"
     test_context = _makeContext(worst=[measure])
     call_mocked_command(test_context, finder, max_errors=0)
     self.assertEqual(len(mail.outbox), 0)
 def test_email_body_three_declines(self, attach_image, finder):
     measure = Measure.objects.get(pk="cerazette")
     call_mocked_command_with_defaults(
         _makeContext(declines=[
             {
                 "measure": measure,
                 "from": 99.92,
                 "to": 0.12
             },
             {
                 "measure": measure,
                 "from": 30,
                 "to": 10
             },
             {
                 "measure": measure,
                 "from": 20,
                 "to": 10
             },
         ]),
         finder,
     )
     message = mail.outbox[-1].alternatives[0]
     html = message[0]
     self.assertRegex(html, "It also slipped:")
     self.assertRegex(
         html,
         re.compile("<ul.*<li>considerably on.*"
                    "<li>moderately on.*</ul>", re.DOTALL),
     )
 def test_email_headers(self, attach_image, finder):
     test_context = _makeContext()
     call_mocked_command_with_defaults(test_context, finder)
     message = mail.outbox[-1]
     self.assertIn(
         message.extra_headers['list-unsubscribe'],
         '<http://localhost/bookmarks/dummykey>')
 def test_email_body_achieved_saving(self, attach_image, finder):
     measure = Measure.objects.get(pk="cerazette")
     call_mocked_command_with_defaults(
         _makeContext(achieved_savings=[(measure, 9.9)]), finder)
     message = mail.outbox[-1].alternatives[0]
     html = message[0]
     self.assertIn("this practice saved around <b>£10", html)
 def test_successful_sends(self, attach_image, finder):
     attach_image.side_effect = [StandardError, None, None]
     test_context = _makeContext(worst=[MagicMock()])
     with self.assertRaises(BatchedEmailErrors):
         call_mocked_command(test_context, finder, max_errors=4)
     self.assertEqual(EmailMessage.objects.count(), 2)
     self.assertEqual(len(mail.outbox), 2)
 def test_email_body_total_savings(self, attach_image, finder):
     call_mocked_command_with_defaults(
         _makeContext(possible_top_savings_total=9000.1), finder)
     message = mail.outbox[-1].alternatives[0]
     html = message[0]
     self.assertIn("it could save around <b>£9,000</b>".decode('utf-8'),
                   html)
 def test_email_recipient(self, attach_image, finder):
     test_context = _makeContext()
     call_mocked_command_with_defaults(test_context, finder)
     self.assertEqual(EmailMessage.objects.count(), 1)
     self.assertEqual(len(mail.outbox), 1)
     email_message = EmailMessage.objects.first()
     self.assertEqual(mail.outbox[-1].to, email_message.to)
     self.assertEqual(mail.outbox[-1].to, ['*****@*****.**'])
 def test_email_body_has_ga_tracking(self, attach_image, finder):
     measure = Measure.objects.get(pk="cerazette")
     call_mocked_command_with_defaults(
         _makeContext(declines=[{"measure": measure, "from": 99.92, "to": 0.12}]),
         finder,
     )
     message = mail.outbox[-1].alternatives[0]
     html = message[0]
     self.assertRegex(html, '<a href=".*&utm_content=.*#cerazette".*>')
 def test_email_body_has_ga_tracking(self, attach_image, finder):
     measure = Measure.objects.get(pk='cerazette')
     call_mocked_command_with_defaults(
         _makeContext(declines=[(measure, 99.92, 0.12, 10.002)]),
         finder)
     message = mail.outbox[-1].alternatives[0]
     html = message[0]
     self.assertRegexpMatches(
         html, '<a href=".*&utm_content=.*#cerazette".*>')
 def test_email_body_two_achieved_savings(self, attach_image, finder):
     measure = Measure.objects.get(pk="cerazette")
     call_mocked_command_with_defaults(
         _makeContext(achieved_savings=[(measure, 9.9), (measure, 12.0)]),
         finder)
     message = mail.outbox[-1].alternatives[0]
     html = message[0]
     self.assertIn("<li>\n<b>£10</b> on", html)
     self.assertIn("<li>\n<b>£10</b> on", html)
 def test_max_errors(self, attach_image, finder):
     attach_image.side_effect = [Exception, None, None]
     measure = MagicMock()
     measure.id = "measureid"
     test_context = _makeContext(worst=[measure])
     self.assertEqual(EmailMessage.objects.count(), 1)
     with self.assertRaises(BatchedEmailErrors):
         call_mocked_command(test_context, finder, max_errors=0)
     self.assertEqual(EmailMessage.objects.count(), 1)
     self.assertEqual(len(mail.outbox), 0)
 def test_email_body_no_data(self, attach_image, finder):
     test_context = _makeContext()
     call_mocked_command_with_defaults(test_context, finder)
     message = mail.outbox[-1].alternatives[0]
     html = message[0]
     # Name of the practice
     self.assertIn("1/ST Andrews Medical Practice", html)
     # Unsubscribe link
     self.assertIn("/bookmarks/dummykey/", html)
     self.assertIn("We've no new information", html)
 def test_email_body_three_worst(self, attach_image, finder):
     measure = Measure.objects.get(pk='cerazette')
     call_mocked_command_with_defaults(
         _makeContext(worst=[measure, measure, measure]), finder)
     message = mail.outbox[-1].alternatives[0]
     html = message[0]
     self.assertRegexpMatches(html, 'It was also in the worst 10% on:')
     self.assertRegexpMatches(
         html,
         re.compile('<ul.*<li>.*Desogestrel.*'
                    '<li>.*Desogestrel.*</ul>', re.DOTALL))
 def test_email_body_one_saving(self, attach_image, finder):
     measure = Measure.objects.get(pk='cerazette')
     call_mocked_command_with_defaults(
         _makeContext(possible_savings=[(measure, 9.9)]), finder)
     message = mail.outbox[-1].alternatives[0]
     html = message[0]
     self.assertIn("if it had prescribed in line with the average practice",
                   html)
     self.assertRegexpMatches(
         html, 'it could have saved about <b>£10</b> on '
         '<a href=".*/practice/P87629/.*#cerazette".*>'
         "Cerazette vs. Desogestrel</a>".decode('utf-8'))
 def test_email_body_two_declines(self, attach_image, finder):
     measure = Measure.objects.get(pk='cerazette')
     call_mocked_command_with_defaults(
         _makeContext(declines=[
             (measure, 99.92, 0.12, 10.002),
             (measure, 30, 10, 0),
         ]),
         finder)
     message = mail.outbox[-1].alternatives[0]
     html = message[0]
     self.assertRegexpMatches(
         html, 'It also slipped considerably')
 def test_email_body_worst(self, attach_image, finder):
     measure = Measure.objects.get(pk='cerazette')
     attach_image.return_value = 'unique-image-id'
     call_mocked_command_with_defaults(
         _makeContext(worst=[measure]), finder)
     message = mail.outbox[-1].alternatives[0]
     html = message[0]
     self.assertIn("We've found", html)
     self.assertRegexpMatches(
         html, re.compile(
             'the worst 10% on.*<a href=".*/practice/P87629'
             '/.*#cerazette".*>'
             "Cerazette vs. Desogestrel</a>", re.DOTALL))
     self.assertIn('<img src="cid:unique-image-id', html)
 def test_email_body_two_declines(self, attach_image, finder):
     measure = Measure.objects.get(pk="cerazette")
     call_mocked_command_with_defaults(
         _makeContext(
             declines=[
                 {"measure": measure, "from": 99.92, "to": 0.12},
                 {"measure": measure, "from": 30, "to": 10},
             ]
         ),
         finder,
     )
     message = mail.outbox[-1].alternatives[0]
     html = message[0]
     self.assertRegex(html, "It also slipped considerably")
 def test_email_body_two_savings(self, attach_image, finder):
     measure = Measure.objects.get(pk='cerazette')
     call_mocked_command_with_defaults(
         _makeContext(possible_savings=[(measure, 9.9), (measure, 1.12)]),
         finder)
     message = mail.outbox[-1].alternatives[0]
     html = message[0]
     self.assertIn(
         "These add up to around <b>£10</b> of "
         "potential savings".decode('utf-8'), html)
     self.assertRegexpMatches(
         html, '<li.*>\n<b>£10</b> on <a href=".*/practice/P87629'
         '/.*#cerazette".*>'
         "Cerazette vs. Desogestrel</a>".decode('utf-8'))
 def test_email_body_declines(self, attach_image, finder):
     attach_image.return_value = 'unique-image-id'
     measure = Measure.objects.get(pk='cerazette')
     call_mocked_command_with_defaults(
         _makeContext(declines=[(measure, 99.92, 0.12, 10.002)]),
         finder)
     message = mail.outbox[-1].alternatives[0]
     html = message[0]
     self.assertIn("this practice slipped", html)
     self.assertRegexpMatches(
         html, 'slipped massively .* on '
         '<a href=".*/practice/P87629/.*#cerazette".*>'
         'Cerazette vs. Desogestrel</a>')
     self.assertIn('<span class="worse"', html)
     self.assertIn('<img src="cid:unique-image-id', html)
     self.assertNotIn("Your best prescribing areas", html)
     self.assertNotIn("Cost savings", html)
 def test_email_body_declines(self, attach_image, finder):
     attach_image.return_value = "unique-image-id"
     measure = Measure.objects.get(pk="cerazette")
     call_mocked_command_with_defaults(
         _makeContext(declines=[{"measure": measure, "from": 99.92, "to": 0.12}]),
         finder,
     )
     message = mail.outbox[-1].alternatives[0]
     html = message[0]
     self.assertIn("this practice slipped", html)
     self.assertRegex(
         html,
         "slipped massively on "
         '<a href=".*/practice/P87629/.*#cerazette".*>'
         "Cerazette vs. Desogestrel</a>",
     )
     self.assertIn('<span class="worse"', html)
     self.assertIn('<img src="cid:unique-image-id', html)
     self.assertNotIn("Your best prescribing areas", html)
     self.assertNotIn("Cost savings", html)
 def test_email_body_text(self, attach_image, finder):
     test_context = _makeContext()
     call_mocked_command_with_defaults(test_context, finder)
     message = mail.outbox[-1].body
     self.assertIn("**Hello!**", message)
 def test_email_all_recipients(self, attach_image, finder):
     test_context = _makeContext()
     self.assertEqual(EmailMessage.objects.count(), 1)
     call_mocked_command(test_context, finder)
     self.assertEqual(EmailMessage.objects.count(), 4)
     self.assertEqual(len(mail.outbox), 3)