def test_send_revocation_email_error(self, mock_email): """ Test that we log an appropriate message if the code revocation email cannot be sent. """ mock_email.side_effect = Exception('Ignore me - revocation') validated_data = { 'code': self.code, 'email': self.email, 'offer_assignments': self.offer_assignments, } context = { 'coupon': self.coupon, 'greeting': self.GREETING, 'closing': self.CLOSING, } serializer = CouponCodeRevokeSerializer(data=self.data, context=context) expected = [ (self.LOGGER_NAME, 'ERROR', '[Offer Revocation] Encountered error when revoking code {} for user {} with greeting \'{}\' and ' 'closing \'{}\''.format( self.code, self.email, self.GREETING, self.CLOSING, )), ] with LogCapture(self.LOGGER_NAME) as log: serializer.create(validated_data=validated_data) log.check_present(*expected)
def revoke(self, request, pk): # pylint: disable=unused-argument """ Revoke users by email from codes within the Coupon. """ coupon = self.get_object() self._validate_coupon_availablity( coupon, 'Coupon is not available for code revoke') subject = request.data.pop('template_subject', '') greeting = request.data.pop('template_greeting', '') closing = request.data.pop('template_closing', '') self._validate_email_fields(subject, greeting, closing) serializer = CouponCodeRevokeSerializer( data=request.data.get('assignments'), many=True, context={ 'coupon': coupon, 'subject': subject, 'greeting': greeting, 'closing': closing }) 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)
def test_send_revocation_email_error_no_greeting(self): """ Test that we log an appropriate message if the code revocation email cannot be sent. """ serializer = CouponCodeRevokeSerializer( data=self.data, context={'coupon': self.coupon}) expected = [ ( # pylint: disable=too-many-format-args self.LOGGER_NAME, 'ERROR', '[Offer Revocation] Encountered error when revoking code {} for user {} with subject {}, ' 'greeting {} closing {} base_enterprise_url \'{}\' and files []' .format( None, None, None, None, None, '', )), ] with LogCapture(self.LOGGER_NAME) as log: serializer.create( validated_data={ 'sender_id': None, 'template': None, 'user': { 'email': None }, 'enterprise_customer_uuid': uuid4() }) log.check_present(*expected)
def test_send_revoke_email_args_with_base_url(self, mock_revoke_email): """ Test that revoke email is passed correct enterprise_base_url """ context = { 'coupon': self.coupon, 'subject': self.SUBJECT, 'greeting': self.GREETING, 'closing': self.CLOSING, 'files': self.ATTACHMENTS, 'base_enterprise_url': self.BASE_ENTERPRISE_URL, } validated_data = { 'code': self.code, 'user': { 'email': self.email }, 'offer_assignments': self.offer_assignments, 'sender_id': None, 'template': None, 'enterprise_customer_uuid': uuid4() } serializer = CouponCodeRevokeSerializer(data=self.data, context=context) serializer.create(validated_data=validated_data) mock_revoke_email.assert_called_with( subject=self.SUBJECT, greeting=self.GREETING, closing=self.CLOSING, learner_email=self.email, code=self.code, sender_alias=self.SENDER_ALIAS, reply_to='', base_enterprise_url=self.BASE_ENTERPRISE_URL, attachments=self.ATTACHMENTS)
def test_send_revocation_email_error_no_greeting(self): """ Test that we log an appropriate message if the code revocation email cannot be sent. """ serializer = CouponCodeRevokeSerializer( data=self.data, context={'coupon': self.coupon}) expected = [ (self.LOGGER_NAME, 'ERROR', '[Offer Revocation] Encountered error when revoking code {} for user {} with greeting {} and ' 'closing {}'.format(None, None, None, None)), ] with LogCapture(self.LOGGER_NAME) as log: serializer.create(validated_data={}) log.check_present(*expected)
def revoke(self, request, pk): # pylint: disable=unused-argument """ Revoke users by email from codes within the Coupon. """ coupon = self.get_object() email_template = request.data.pop('template', None) serializer = CouponCodeRevokeSerializer( 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)
def revoke(self, request, pk): # pylint: disable=unused-argument """ Revoke users by email from codes within the Coupon. """ coupon = self.get_object() self._validate_coupon_availablity( coupon, 'Coupon is not available for code revoke') 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) self._validate_assignments_data(request.data.get('assignments')) do_not_email = request.data.get('do_not_email') assignments = [ dict(**assignment, do_not_email=do_not_email) for assignment in request.data.get('assignments') ] serializer = CouponCodeRevokeSerializer(data=assignments, many=True, context={ 'coupon': coupon, 'subject': subject, 'greeting': greeting, 'closing': closing, }) if serializer.is_valid(): serializer.save() if not do_not_email: # Create a record of the email sent self._create_offer_assignment_email_sent_record( enterprise_customer, REVOKE, template) # unsubscribe user from receiving nudge emails CodeAssignmentNudgeEmails.unsubscribe_from_nudging( map(lambda assignment: assignment['code'], assignments), map(lambda assignment: assignment['email'], assignments)) return Response(serializer.data, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def test_send_revocation_email_error(self, mock_email): """ Test that we log an appropriate message if the code revocation email cannot be sent. """ mock_email.side_effect = Exception('Ignore me - revocation') validated_data = { 'code': self.code, 'user': { 'email': self.email }, 'offer_assignments': self.offer_assignments, 'sender_id': None, 'template': None, 'enterprise_customer_uuid': uuid4() } context = { 'coupon': self.coupon, 'subject': self.SUBJECT, 'greeting': self.GREETING, 'closing': self.CLOSING, 'base_enterprise_url': self.BASE_ENTERPRISE_URL, 'files': self.ATTACHMENTS } serializer = CouponCodeRevokeSerializer(data=self.data, context=context) expected = [ (self.LOGGER_NAME, 'ERROR', '[Offer Revocation] Encountered error when revoking code {} for user {} with subject \'{}\', ' 'greeting \'{}\' closing \'{}\' base_enterprise_url \'{}\' and files {}' .format( self.code, self.email, self.SUBJECT, self.GREETING, self.CLOSING, self.BASE_ENTERPRISE_URL, self.ATTACHMENTS, )), ] with LogCapture(self.LOGGER_NAME) as log: serializer.create(validated_data=validated_data) log.check_present(*expected)