Пример #1
0
    def save(self, **kwargs):
        """
        Save method for this serializer
        """
        self.instance.status = self.validated_data["action"]
        if self.instance.status == FinancialAidStatus.APPROVED:
            self.instance.tier_program = self.validated_data["tier_program"]
            self.instance.justification = self.validated_data["justification"]
        elif self.instance.status == FinancialAidStatus.PENDING_MANUAL_APPROVAL:
            # This is intentionally left blank for clarity that this is a valid status for .save()
            pass
        elif self.instance.status == FinancialAidStatus.RESET:
            self.instance.justification = "Reset via the financial aid review form"

        # also saves history of this change in FinancialAidAudit.
        self.instance.save_and_log(self.context["request"].user)

        # Send email notification
        MailgunClient.send_financial_aid_email(
            acting_user=self.context["request"].user,
            financial_aid=self.instance,
            **generate_financial_aid_email(self.instance)
        )

        return self.instance
Пример #2
0
 def test_financial_aid_email_with_blank_subject_and_body(self, mock_post):
     """
     Test that MailgunClient.send_financial_aid_email() sends an individual message
     with blank subject and blank email, and that the audit record saves correctly
     """
     assert FinancialAidEmailAudit.objects.count() == 0
     MailgunClient.send_financial_aid_email(
         self.staff_user_profile.user,
         self.financial_aid,
         '',
         ''
     )
     # Check method call
     assert mock_post.called
     called_args, called_kwargs = mock_post.call_args
     assert list(called_args)[0] == '{}/{}'.format(settings.MAILGUN_URL, 'messages')
     assert called_kwargs['auth'] == ('api', settings.MAILGUN_KEY)
     assert called_kwargs['data']['text'] == ''
     assert called_kwargs['data']['subject'] == ''
     assert called_kwargs['data']['to'] == [self.financial_aid.user.email]
     assert called_kwargs['data']['from'] == settings.EMAIL_SUPPORT
     # Check audit creation
     assert FinancialAidEmailAudit.objects.count() == 1
     audit = FinancialAidEmailAudit.objects.first()
     assert audit.acting_user == self.staff_user_profile.user
     assert audit.financial_aid == self.financial_aid
     assert audit.to_email == self.financial_aid.user.email
     assert audit.from_email == settings.EMAIL_SUPPORT
     assert audit.email_subject == ''
     assert audit.email_body == ''
Пример #3
0
 def test_financial_aid_email_with_blank_subject_and_body(self, mock_post):
     """
     Test that MailgunClient.send_financial_aid_email() sends an individual message
     with blank subject and blank email, and that the audit record saves correctly
     """
     assert FinancialAidEmailAudit.objects.count() == 0
     MailgunClient.send_financial_aid_email(self.staff_user_profile.user,
                                            self.financial_aid, '', '')
     # Check method call
     assert mock_post.called
     called_args, called_kwargs = mock_post.call_args
     assert list(called_args)[0] == '{}/{}'.format(settings.MAILGUN_URL,
                                                   'messages')
     assert called_kwargs['auth'] == ('api', settings.MAILGUN_KEY)
     assert called_kwargs['data']['text'] == ''
     assert called_kwargs['data']['subject'] == ''
     assert called_kwargs['data']['to'] == [self.financial_aid.user.email]
     assert called_kwargs['data']['from'] == settings.EMAIL_SUPPORT
     # Check audit creation
     assert FinancialAidEmailAudit.objects.count() == 1
     audit = FinancialAidEmailAudit.objects.first()
     assert audit.acting_user == self.staff_user_profile.user
     assert audit.financial_aid == self.financial_aid
     assert audit.to_email == self.financial_aid.user.email
     assert audit.from_email == settings.EMAIL_SUPPORT
     assert audit.email_subject == ''
     assert audit.email_body == ''
Пример #4
0
 def test_financial_aid_email(self, mock_post):
     """
     Test that MailgunClient.send_financial_aid_email() sends an individual message
     """
     assert FinancialAidEmailAudit.objects.count() == 0
     response = MailgunClient.send_financial_aid_email(
         self.staff_user_profile.user,
         self.financial_aid,
         'email subject',
         'email body'
     )
     assert response.status_code == HTTP_200_OK
     # Check method call
     assert mock_post.called
     called_args, called_kwargs = mock_post.call_args
     assert list(called_args)[0] == '{}/{}'.format(settings.MAILGUN_URL, 'messages')
     assert called_kwargs['auth'] == ('api', settings.MAILGUN_KEY)
     assert called_kwargs['data']['text'] == 'email body'
     assert called_kwargs['data']['subject'] == 'email subject'
     assert called_kwargs['data']['to'] == [self.financial_aid.user.email]
     assert called_kwargs['data']['from'] == settings.EMAIL_SUPPORT
     # Check audit creation
     assert FinancialAidEmailAudit.objects.count() == 1
     audit = FinancialAidEmailAudit.objects.first()
     assert audit.acting_user == self.staff_user_profile.user
     assert audit.financial_aid == self.financial_aid
     assert audit.to_email == self.financial_aid.user.email
     assert audit.from_email == settings.EMAIL_SUPPORT
     assert audit.email_subject == 'email subject'
     assert audit.email_body == 'email body'
Пример #5
0
 def test_financial_aid_email(self, mock_post):
     """
     Test that MailgunClient.send_financial_aid_email() sends an individual message
     """
     assert FinancialAidEmailAudit.objects.count() == 0
     response = MailgunClient.send_financial_aid_email(
         self.staff_user_profile.user, self.financial_aid, 'email subject',
         'email body')
     assert response.status_code == HTTP_200_OK
     # Check method call
     assert mock_post.called
     called_args, called_kwargs = mock_post.call_args
     assert list(called_args)[0] == '{}/{}'.format(settings.MAILGUN_URL,
                                                   'messages')
     assert called_kwargs['auth'] == ('api', settings.MAILGUN_KEY)
     assert called_kwargs['data']['text'] == 'email body'
     assert called_kwargs['data']['subject'] == 'email subject'
     assert called_kwargs['data']['to'] == [self.financial_aid.user.email]
     assert called_kwargs['data']['from'] == settings.EMAIL_SUPPORT
     # Check audit creation
     assert FinancialAidEmailAudit.objects.count() == 1
     audit = FinancialAidEmailAudit.objects.first()
     assert audit.acting_user == self.staff_user_profile.user
     assert audit.financial_aid == self.financial_aid
     assert audit.to_email == self.financial_aid.user.email
     assert audit.from_email == settings.EMAIL_SUPPORT
     assert audit.email_subject == 'email subject'
     assert audit.email_body == 'email body'
Пример #6
0
 def post(self, request, *args, **kwargs):  # pylint: disable=unused-argument
     """
     Post request to send emails to an individual learner
     """
     financial_aid = self.get_object()
     serializer = self.get_serializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     mailgun_response = MailgunClient.send_financial_aid_email(
         body=serializer.data['email_body'],
         acting_user=request.user,
         subject=serializer.data['email_subject'],
         financial_aid=financial_aid)
     return Response(status=mailgun_response.status_code,
                     data=generate_mailgun_response_json(mailgun_response))
Пример #7
0
    def save(self, **kwargs):
        """
        Save method for this serializer
        """
        self.instance.status = self.validated_data["action"]
        if self.instance.status == FinancialAidStatus.APPROVED:
            self.instance.tier_program = self.validated_data["tier_program"]
            self.instance.justification = self.validated_data["justification"]
        elif self.instance.status == FinancialAidStatus.PENDING_MANUAL_APPROVAL:
            # This is intentionally left blank for clarity that this is a valid status for .save()
            pass
        elif self.instance.status == FinancialAidStatus.RESET:
            self.instance.justification = "Reset via the financial aid review form"

        # also saves history of this change in FinancialAidAudit.
        self.instance.save_and_log(self.context["request"].user)

        # Send email notification
        MailgunClient.send_financial_aid_email(
            acting_user=self.context["request"].user,
            financial_aid=self.instance,
            **generate_financial_aid_email(self.instance))

        return self.instance
Пример #8
0
 def post(self, request, *args, **kwargs):
     """
     Post request to send emails to an individual learner
     """
     financial_aid = self.get_object()
     serializer = self.get_serializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     mailgun_response = MailgunClient.send_financial_aid_email(
         body=serializer.data['email_body'],
         acting_user=request.user,
         subject=serializer.data['email_subject'],
         financial_aid=financial_aid
     )
     return Response(
         status=mailgun_response.status_code,
         data=generate_mailgun_response_json(mailgun_response)
     )
Пример #9
0
    def test_financial_aid_email_error(self, raise_for_status, mock_post):
        """
        Test that send_financial_aid_email handles errors correctly
        """
        mock_post.return_value = Mock(
            spec=Response,
            status_code=HTTP_400_BAD_REQUEST,
            json=mocked_json(),
        )

        response = MailgunClient.send_financial_aid_email(
            self.staff_user_profile.user,
            self.financial_aid,
            'email subject',
            'email body',
            raise_for_status=raise_for_status,
        )

        assert response.raise_for_status.called is raise_for_status
        assert response.status_code == HTTP_400_BAD_REQUEST
        assert response.json() == {}
Пример #10
0
    def test_financial_aid_email_error(self, raise_for_status, mock_post):
        """
        Test that send_financial_aid_email handles errors correctly
        """
        mock_post.return_value = Mock(
            spec=Response,
            status_code=HTTP_400_BAD_REQUEST,
            json=mocked_json(),
        )

        response = MailgunClient.send_financial_aid_email(
            self.staff_user_profile.user,
            self.financial_aid,
            'email subject',
            'email body',
            raise_for_status=raise_for_status,
        )

        assert response.raise_for_status.called is raise_for_status
        assert response.status_code == HTTP_400_BAD_REQUEST
        assert response.json() == {}