Пример #1
0
    def test_send_reminder_email_error(self, mock_email):
        """ Test that we log an appropriate message if the code reminder email cannot be sent. """
        mock_email.side_effect = Exception('Ignore me - reminder')
        serializer = CouponCodeRemindSerializer(
            data=self.data, context={'coupon': self.coupon})
        expected = [
            (self.LOGGER_NAME, 'ERROR',
             '[Offer Reminder] Email for offer_assignment_id: {} with greeting \'{}\' and closing \'{}\' raised '
             'exception: Exception(\'Ignore me - reminder\',)'.format(
                 self.offer_assignment.id,
                 self.GREETING,
                 self.CLOSING,
             )),
        ]

        with self.assertRaises(Exception):
            with LogCapture(self.LOGGER_NAME) as log:
                serializer._trigger_email_sending_task(  # pylint: disable=protected-access
                    greeting=self.GREETING,
                    closing=self.CLOSING,
                    assigned_offer=self.offer_assignment,
                    redeemed_offer_count=3,
                    total_offer_count=5,
                )
        log.check_present(*expected)
Пример #2
0
 def test_send_assigned_offer_reminder_email_args(self, mock_remind_email):
     """ Test that the code_expiration_date passed is equal to coupon batch end date """
     serializer = CouponCodeRemindSerializer(
         data=self.data, context={'coupon': self.coupon})
     serializer._trigger_email_sending_task(  # pylint: disable=protected-access
         subject=self.SUBJECT,
         greeting=self.GREETING,
         closing=self.CLOSING,
         assigned_offer=self.offer_assignment,
         redeemed_offer_count=3,
         total_offer_count=5,
         sender_alias=self.SENDER_ALIAS,
     )
     expected_expiration_date = self.coupon.attr.coupon_vouchers.vouchers.first(
     ).end_datetime
     mock_remind_email.assert_called_with(
         subject=self.SUBJECT,
         greeting=self.GREETING,
         closing=self.CLOSING,
         learner_email=self.offer_assignment.user_email,
         code=self.offer_assignment.code,
         redeemed_offer_count=mock.ANY,
         total_offer_count=mock.ANY,
         code_expiration_date=expected_expiration_date.strftime(
             '%d %B, %Y %H:%M %Z'),
         sender_alias=self.SENDER_ALIAS)
Пример #3
0
    def test_send_reminder_email_error(self, mock_email):
        """ Test that we log an appropriate message if the code reminder email cannot be sent. """
        mock_email.side_effect = Exception('Ignore me - reminder')
        serializer = CouponCodeRemindSerializer(
            data=self.data, context={'coupon': self.coupon})
        expected = [
            (self.LOGGER_NAME, 'ERROR',
             '[Offer Reminder] Email for offer_assignment_id: {} with subject \'{}\', greeting \'{}\' '
             'closing \'{}\' attachments {}, and base_enterprise_url \'{}\' raised exception: {}'
             .format(self.offer_assignment.id, self.SUBJECT, self.GREETING,
                     self.CLOSING, self.ATTACHMENTS, self.BASE_ENTERPRISE_URL,
                     repr(Exception('Ignore me - reminder')))),
        ]

        with self.assertRaises(Exception):
            with LogCapture(self.LOGGER_NAME) as log:
                serializer._trigger_email_sending_task(  # pylint: disable=protected-access
                    subject=self.SUBJECT,
                    greeting=self.GREETING,
                    closing=self.CLOSING,
                    assigned_offer=self.offer_assignment,
                    redeemed_offer_count=3,
                    total_offer_count=5,
                    sender_alias=self.SENDER_ALIAS,
                    reply_to=self.REPLY_TO,
                    attachments=self.ATTACHMENTS,
                    base_enterprise_url=self.BASE_ENTERPRISE_URL,
                )
        log.check_present(*expected)
Пример #4
0
    def remind(self, request, pk):  # pylint: disable=unused-argument
        """
        Remind users of pending offer assignments by email.
        """
        coupon = self.get_object()
        self._validate_coupon_availablity(coupon, 'Coupon is not available for code remind')
        subject = request.data.pop('template_subject', '')
        greeting = request.data.pop('template_greeting', '')
        closing = request.data.pop('template_closing', '')
        template_id = request.data.pop('template_id', None)
        template = OfferAssignmentEmailTemplates.get_template(template_id)
        enterprise_customer = coupon.attr.enterprise_customer_uuid
        self._validate_email_fields(subject, greeting, closing)
        if request.data.get('assignments'):
            assignments = request.data.get('assignments')
        else:
            # If no assignment is passed, send reminder to all assignments associated with the coupon.
            vouchers = coupon.attr.coupon_vouchers.vouchers.all()
            code_filter = request.data.get('code_filter')

            if not code_filter:
                raise serializers.ValidationError('code_filter must be specified')

            if code_filter == VOUCHER_NOT_REDEEMED:
                assignment_usages = self._get_not_redeemed_usages(vouchers)
            elif code_filter == VOUCHER_PARTIAL_REDEEMED:
                assignment_usages = self._get_partial_redeemed_usages(vouchers)
            else:
                raise serializers.ValidationError('Invalid code_filter specified: {}'.format(code_filter))

            assignments = [
                {
                    'code': assignment['code'],
                    'email': assignment['user_email']
                }
                for assignment in assignment_usages
            ]

        serializer = CouponCodeRemindSerializer(
            data=assignments,
            many=True,
            context={
                'coupon': coupon,
                'subject': subject,
                'greeting': greeting,
                'closing': closing,
            }
        )
        if serializer.is_valid():
            serializer.save()
            # Create a record of the email sent
            self._create_offer_assignment_email_sent_record(enterprise_customer, REMIND, template)
            return Response(serializer.data, status=status.HTTP_200_OK)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Пример #5
0
    def remind(self, request, pk):  # pylint: disable=unused-argument
        """
        Remind users of pending offer assignments by email.
        """
        coupon = self.get_object()
        email_template = request.data.pop('template', None)
        if not email_template:
            log_message_and_raise_validation_error(
                str('Template is required.'))

        if request.data.get('assignments'):
            assignments = request.data.get('assignments')
        else:
            # If no assignment is passed, send reminder to all assignments associated with the coupon.
            vouchers = coupon.attr.coupon_vouchers.vouchers.all()
            code_filter = request.data.get('code_filter')

            if not code_filter:
                raise serializers.ValidationError(
                    'code_filter must be specified')

            if code_filter == VOUCHER_NOT_REDEEMED:
                assignment_usages = self._get_not_redeemed_usages(vouchers)
            elif code_filter == VOUCHER_PARTIAL_REDEEMED:
                assignment_usages = self._get_partial_redeemed_usages(vouchers)
            else:
                raise serializers.ValidationError(
                    'Invalid code_filter specified: {}'.format(code_filter))

            assignments = [{
                'code': assignment['code'],
                'email': assignment['user_email']
            } for assignment in assignment_usages]

        serializer = CouponCodeRemindSerializer(data=assignments,
                                                many=True,
                                                context={
                                                    'coupon': coupon,
                                                    'template': email_template
                                                })
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_200_OK)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Пример #6
0
    def remind(self, request, pk):  # pylint: disable=unused-argument
        """
        Remind users of pending offer assignments by email.
        """
        coupon = self.get_object()
        email_template = request.data.pop('template', None)
        if not email_template:
            log_message_and_raise_validation_error(
                str('Template is required.'))
        serializer = CouponCodeRemindSerializer(
            data=request.data.get('assignments'),
            many=True,
            context={
                'coupon': coupon,
                'template': email_template
            })
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_200_OK)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)