def test_mark_documents_received_docs_sent(self, mock_mailgun_client): """ Tests FinancialAidActionView when documents are checked as received from DOCS_SENT """ mock_mailgun_client.send_financial_aid_email.return_value = Mock( spec=Response, status_code=status.HTTP_200_OK, json=mocked_json() ) # Set status to docs sent assert self.financialaid.tier_program == self.tier_programs["25k"] self.financialaid.status = FinancialAidStatus.DOCS_SENT self.financialaid.save() assert FinancialAidAudit.objects.count() == 0 # Set action to pending manual approval from pending-docs self.make_http_request( self.client.patch, self.action_url, status.HTTP_200_OK, data={"action": FinancialAidStatus.PENDING_MANUAL_APPROVAL} ) self.financialaid.refresh_from_db() # Check that the tier does not change: assert FinancialAidAudit.objects.count() == 1 assert self.financialaid.tier_program == self.tier_programs["25k"] assert self.financialaid.status == FinancialAidStatus.PENDING_MANUAL_APPROVAL assert mock_mailgun_client.send_financial_aid_email.called _, called_kwargs = mock_mailgun_client.send_financial_aid_email.call_args assert called_kwargs["acting_user"] == self.staff_user_profile.user assert called_kwargs["financial_aid"] == self.financialaid financial_aid_email = generate_financial_aid_email(self.financialaid) assert called_kwargs["subject"] == financial_aid_email["subject"] assert called_kwargs["body"] == financial_aid_email["body"]
def test_reset_financial_aid_review(self, financial_aid_status, mock_mailgun_client): """ Tests FinancialAidActionView, when submitted action is RESET """ mock_mailgun_client.send_financial_aid_email.return_value = Mock( spec=Response, status_code=status.HTTP_200_OK, json=mocked_json() ) # Set status to docs sent self.financialaid.status = financial_aid_status self.financialaid.save() assert FinancialAidAudit.objects.count() == 0 # Set action to pending manual approval from pending-docs self.make_http_request( self.client.patch, self.action_url, status.HTTP_200_OK, data={"action": FinancialAidStatus.RESET} ) self.financialaid.refresh_from_db() # Check that the tier does not change: assert FinancialAidAudit.objects.count() == 1 assert self.financialaid.status == FinancialAidStatus.RESET assert mock_mailgun_client.send_financial_aid_email.called _, called_kwargs = mock_mailgun_client.send_financial_aid_email.call_args assert called_kwargs["acting_user"] == self.staff_user_profile.user assert called_kwargs["financial_aid"] == self.financialaid financial_aid_email = generate_financial_aid_email(self.financialaid) assert called_kwargs["subject"] == financial_aid_email["subject"] assert called_kwargs["body"] == financial_aid_email["body"]
def test_send_batch_400_no_raise(self, mock_post): """ Test that if raise_for_status is False we don't raise an exception for a 400 response """ mock_post.return_value = Mock( spec=Response, status_code=HTTP_400_BAD_REQUEST, json=mocked_json() ) chunk_size = 10 recipient_tuples = [("{0}@example.com".format(letter), None) for letter in string.ascii_letters] assert len(recipient_tuples) == 52 with override_settings( MAILGUN_RECIPIENT_OVERRIDE=None, ): resp_list = MailgunClient.send_batch( 'email subject', 'email body', recipient_tuples, chunk_size=chunk_size, raise_for_status=False ) assert len(resp_list) == 6 for resp in resp_list: assert resp.status_code == HTTP_400_BAD_REQUEST assert mock_post.call_count == 6 assert mock_post.return_value.raise_for_status.called is False
def test_approval_different_tier_program(self, mock_mailgun_client): """ Tests FinancialAidActionView when application is approved for a different tier program """ mock_mailgun_client.send_financial_aid_email.return_value = Mock( spec=Response, status_code=status.HTTP_200_OK, json=mocked_json() ) assert FinancialAidAudit.objects.count() == 0 assert self.financialaid.tier_program != self.tier_programs["50k"] assert self.financialaid.status != FinancialAidStatus.APPROVED self.data["tier_program_id"] = self.tier_programs["50k"].id self.make_http_request(self.client.patch, self.action_url, status.HTTP_200_OK, data=self.data) # Application is approved for a different tier program self.financialaid.refresh_from_db() assert FinancialAidAudit.objects.count() == 1 assert self.financialaid.tier_program == self.tier_programs["50k"] assert self.financialaid.status == FinancialAidStatus.APPROVED assert mock_mailgun_client.send_financial_aid_email.called _, called_kwargs = mock_mailgun_client.send_financial_aid_email.call_args assert called_kwargs["acting_user"] == self.staff_user_profile.user assert called_kwargs["financial_aid"] == self.financialaid financial_aid_email = generate_financial_aid_email(self.financialaid) assert called_kwargs["subject"] == financial_aid_email["subject"] assert called_kwargs["body"] == financial_aid_email["body"]
def test_generate_mailgun_response_json(self): """ Tests that generate_mailgun_response_json() returns response.json() """ response = Mock(spec=Response, status_code=status.HTTP_200_OK, json=mocked_json()) assert generate_mailgun_response_json(response) == response.json()
def test_generate_mailgun_response_json(self): """ Tests that generate_mailgun_response_json() returns response.json() """ response = Mock( spec=Response, status_code=status.HTTP_200_OK, json=mocked_json() ) assert generate_mailgun_response_json(response) == response.json()
def test_send_individual_email_error(self, raise_for_status, mock_post): """ Test handling of errors for send_individual_email """ mock_post.return_value = Mock(spec=Response, status_code=HTTP_400_BAD_REQUEST, json=mocked_json()) response = MailgunClient.send_individual_email( subject='email subject', body='email body', recipient='*****@*****.**', 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() == {}
def test_send_individual_email_error(self, raise_for_status, mock_post): """ Test handling of errors for send_individual_email """ mock_post.return_value = Mock( spec=Response, status_code=HTTP_400_BAD_REQUEST, json=mocked_json() ) response = MailgunClient.send_individual_email( subject='email subject', body='email body', recipient='*****@*****.**', 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() == {}
def test_send_to_course_team_error(self, raise_for_status, mock_post): """ Test that send_course_team_email handles errors correctly """ mock_post.return_value = Mock( spec=Response, status_code=HTTP_400_BAD_REQUEST, json=mocked_json(), ) course_with_email = CourseFactory.create(title='course with email', contact_email='*****@*****.**') response = MailgunClient.send_course_team_email( self.user, course_with_email, '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() == {}
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() == {}
def test_send_to_course_team_error(self, raise_for_status, mock_post): """ Test that send_course_team_email handles errors correctly """ mock_post.return_value = Mock( spec=Response, status_code=HTTP_400_BAD_REQUEST, json=mocked_json(), ) course_with_email = CourseFactory.create( title='course with email', contact_email='*****@*****.**') response = MailgunClient.send_course_team_email( self.user, course_with_email, '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() == {}
def test_send_batch_400_no_raise(self, mock_post): """ Test that if raise_for_status is False we don't raise an exception for a 400 response """ mock_post.return_value = Mock(spec=Response, status_code=HTTP_400_BAD_REQUEST, json=mocked_json()) chunk_size = 10 recipient_tuples = [("{0}@example.com".format(letter), None) for letter in string.ascii_letters] assert len(recipient_tuples) == 52 with override_settings(MAILGUN_RECIPIENT_OVERRIDE=None, ): resp_list = MailgunClient.send_batch('email subject', 'email body', recipient_tuples, chunk_size=chunk_size, raise_for_status=False) assert len(resp_list) == 6 for resp in resp_list: assert resp.status_code == HTTP_400_BAD_REQUEST assert mock_post.call_count == 6 assert mock_post.return_value.raise_for_status.called is False
) from mail.factories import AutomaticEmailFactory from mail.views_test import mocked_json from profiles.factories import ProfileFactory from micromasters.factories import UserFactory from search.api import adjust_search_for_percolator from search.base import MockedESTestCase from search.factories import PercolateQueryFactory from search.models import PercolateQuery @ddt @patch('requests.post', autospec=True, return_value=Mock( spec=Response, status_code=HTTP_200_OK, json=mocked_json() )) class MailAPITests(MockedESTestCase): """ Tests for the Mailgun client class """ batch_recipient_arg = [('*****@*****.**', None), ('*****@*****.**', {"name": "B"})] individual_recipient_arg = '*****@*****.**' @override_settings(MAILGUN_RECIPIENT_OVERRIDE=None) @data(None, 'Tester') def test_send_batch(self, sender_name, mock_post): """ Test that MailgunClient.send_batch sends expected parameters to the Mailgun API Base case with only one batch call to the Mailgun API. """
from mail.factories import AutomaticEmailFactory from mail.views_test import mocked_json from profiles.factories import ProfileFactory from micromasters.factories import UserFactory from search.api import adjust_search_for_percolator from search.base import MockedESTestCase from search.factories import PercolateQueryFactory from search.models import PercolateQuery @ddt @patch('requests.post', autospec=True, return_value=Mock(spec=Response, status_code=HTTP_200_OK, json=mocked_json())) class MailAPITests(MockedESTestCase): """ Tests for the Mailgun client class """ batch_recipient_arg = [('*****@*****.**', None), ('*****@*****.**', { "name": "B" })] individual_recipient_arg = '*****@*****.**' @override_settings(MAILGUN_RECIPIENT_OVERRIDE=None) @data(None, 'Tester') def test_send_batch(self, sender_name, mock_post): """ Test that MailgunClient.send_batch sends expected parameters to the Mailgun API