def test_ace_send_happy_path(self): patch_policies(self, [StubPolicy([ChannelType.PUSH])]) mock_channel = Mock( channel_type=ChannelType.EMAIL, action_links=[], tracker_image_sources=[], ) recipient = Recipient(username=u'testuser') msg = Message( app_label=u'testapp', name=u'testmessage', recipient=recipient, ) channel_map = ChannelMap([ [u'sailthru_email', mock_channel], ]) with patch(u'edx_ace.channel.channels', return_value=channel_map): ace.send(msg) mock_channel.deliver.assert_called_once_with( msg, RenderedEmail( from_name=u'template from_name.txt', subject=u'template subject.txt', # The new lines are needed because the template has some tags, which means leftover newlines body_html=u'template body.html\n\n\n\n\n', head_html=u'template head.html\n', body=u'template body.txt', ), )
def _assert_template_for_offset(self, offset, message_count): current_day, offset, target_day, upgrade_deadline = self._get_dates(offset) user = UserFactory.create() for course_index in range(message_count): self._schedule_factory( offset=offset, enrollment__user=user, enrollment__course__id=CourseKey.from_string('edX/toy/course{}'.format(course_index)) ) patch_policies(self, [StubPolicy([ChannelType.PUSH])]) mock_channel = Mock( channel_type=ChannelType.EMAIL, action_links=[], tracker_image_sources=[], ) channel_map = ChannelMap([ ['sailthru', mock_channel], ]) sent_messages = [] with self.settings(TEMPLATES=self._get_template_overrides()): with patch.object(self.task, 'async_send_task') as mock_schedule_send: mock_schedule_send.apply_async = lambda args, *_a, **_kw: sent_messages.append(args) num_expected_queries = NUM_QUERIES_FIRST_MATCH if self.queries_deadline_for_each_course: # one query per course for opt-out and one for course modes num_expected_queries += (message_count * 2) - 1 else: num_expected_queries += 1 with self.assertNumQueries(num_expected_queries, table_blacklist=WAFFLE_TABLES): self.task().apply(kwargs=dict( site_id=self.site_config.site.id, target_day_str=serialize(target_day), day_offset=offset, bin_num=self._calculate_bin_for_user(user), )) num_expected_messages = 1 if self.consolidates_emails_for_learner else message_count self.assertEqual(len(sent_messages), num_expected_messages) with self.assertNumQueries(NUM_QUERIES_PER_MESSAGE_DELIVERY): with patch('openedx.core.djangoapps.schedules.tasks.segment.track') as mock_segment_track: with patch('edx_ace.channel.channels', return_value=channel_map): self.deliver_task(*sent_messages[0]) self.assertEqual(mock_segment_track.call_count, 1) self.assertEqual(mock_channel.deliver.call_count, 1) for (_name, (_msg, email), _kwargs) in mock_channel.deliver.mock_calls: for template in attr.astuple(email): self.assertNotIn("TEMPLATE WARNING", template) self.assertNotIn("{{", template) self.assertNotIn("}}", template) return mock_channel.deliver.mock_calls
def test_default_channel(self): channel_map = ChannelMap([ ['sailthru', SailthruEmailChannel], ]) message = Message(options={u'transactional': True}, **self.msg_kwargs) with patch(u'edx_ace.channel.channels', return_value=channel_map): channel = get_channel_for_message(ChannelType.EMAIL, message) assert channel is SailthruEmailChannel
def test_get_channel_for_message(self): channel_map = ChannelMap([ [u'file_email', FileEmailChannel], [u'sailthru_email', SailthruEmailChannel], ]) transactional_msg = Message(options={u'transactional': True}, **self.msg_kwargs) info_msg = Message(options={}, **self.msg_kwargs) with patch(u'edx_ace.channel.channels', return_value=channel_map): assert get_channel_for_message( ChannelType.EMAIL, transactional_msg) is FileEmailChannel assert get_channel_for_message(ChannelType.EMAIL, info_msg) is SailthruEmailChannel with self.assertRaises(UnsupportedChannelError): assert get_channel_for_message(ChannelType.PUSH, transactional_msg)