def update(self, instance, validated_data): request = self.context.get('request') state = validated_data.get('name') preview_accepted = validated_data.get('preview_accepted') if state: try: instance.change_state(state=state, user=request.user) except TransitionNotAllowed: # pylint: disable=no-member raise serializers.ValidationError( { 'name': _('Cannot switch from state `{state}` to `{target_state}`').format( state=instance.name, target_state=state ) } ) elif preview_accepted: # Mark preview accepted and change ownership to Publisher. instance.preview_accepted = True instance.owner_role = PublisherUserRole.Publisher instance.owner_role_modified = timezone.now() instance.save() if waffle.switch_is_active('enable_publisher_email_notifications'): send_email_preview_accepted(instance.course_run) return instance
def test_preview_accepted_email_with_notification_disabled(self): """ Verify that preview accepted email not sent if notification disabled by user.""" factories.UserAttributeFactory(user=self.course.publisher, enable_email_notification=False) emails.send_email_preview_accepted(self.run_state.course_run) self.assertEqual(len(mail.outbox), 0)
def test_preview_accepted_email(self): """ Verify that preview accepted email functionality works fine. """ lms_course_id = 'course-v1:edX+DemoX+Demo_Course' self.run_state.course_run.lms_course_id = lms_course_id emails.send_email_preview_accepted(self.run_state.course_run, self.site) course_key = CourseKey.from_string(lms_course_id) subject = 'Publication requested: {course_name} {run_number}'.format( course_name=self.course.title, run_number=course_key.run) self.assertEqual(len(mail.outbox), 1) self.assertEqual([ self.course.publisher.email, self.course.project_coordinator.email ], mail.outbox[0].bcc) self.assertEqual(str(mail.outbox[0].subject), subject) body = mail.outbox[0].body.strip() page_path = reverse('publisher:publisher_course_run_detail', kwargs={'pk': self.run_state.course_run.id}) page_url = 'https://{host}{path}'.format( host=self.site.domain.strip('/'), path=page_path) self.assertIn(page_url, body) self.assertIn('You can now publish this About page.', body)
def test_preview_accepted_email_with_error(self): """ Verify that email failure log error message.""" message = 'Failed to send email notifications for preview approved of course-run [{}]'.format( self.run_state.course_run.id) with mock.patch('django.core.mail.message.EmailMessage.send', side_effect=TypeError): with self.assertRaises(Exception) as ex: self.assertEqual(str(ex.exception), message) with LogCapture(emails.logger.name) as l: emails.send_email_preview_accepted( self.run_state.course_run) l.check((emails.logger.name, 'ERROR', message))
def test_preview_accepted_email(self): """ Verify that preview accepted email functionality works fine. """ emails.send_email_preview_accepted(self.run_state.course_run) run_name = '{pacing_type}: {start_date}'.format( pacing_type=self.run_state.course_run.get_pacing_type_display(), start_date=self.run_state.course_run.start.strftime("%B %d, %Y") ) subject = 'Preview for {run_name} has been approved'.format( run_name=run_name ) self.assertEqual(len(mail.outbox), 1) self.assertEqual([self.course.publisher.email, self.course.project_coordinator.email], mail.outbox[0].bcc) self.assertEqual(str(mail.outbox[0].subject), subject) body = mail.outbox[0].body.strip() page_path = reverse('publisher:publisher_course_run_detail', kwargs={'pk': self.run_state.course_run.id}) page_url = 'https://{host}{path}'.format(host=Site.objects.get_current().domain.strip('/'), path=page_path) self.assertIn(page_url, body) self.assertIn('has beed approved by course team.', body)