-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into 215787_drop_cash_assist
- Loading branch information
Showing
8 changed files
with
149 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import json | ||
from datetime import datetime | ||
from typing import Any | ||
from unittest.mock import patch | ||
|
||
from django.conf import settings | ||
from django.http import HttpRequest | ||
from django.test import TestCase, override_settings | ||
|
||
from constance.test import override_config | ||
|
||
from hct_mis_api.api.admin import TOKEN_INFO_EMAIL, APITokenAdmin | ||
from hct_mis_api.api.models import Grant | ||
from hct_mis_api.apps.account.fixtures import UserFactory | ||
from hct_mis_api.apps.core.fixtures import create_afghanistan | ||
from tests.unit.api.factories import APITokenFactory | ||
|
||
|
||
class TestApiToken(TestCase): | ||
@classmethod | ||
def setUpClass(cls) -> None: | ||
super().setUpClass() | ||
cls.afg = create_afghanistan() | ||
cls.user = UserFactory( | ||
email="[email protected]", | ||
) | ||
cls.token = APITokenFactory( | ||
user=cls.user, | ||
grants=[ | ||
Grant.API_READ_ONLY.name, | ||
Grant.API_RDI_UPLOAD.name, | ||
Grant.API_PROGRAM_CREATE.name, | ||
], | ||
valid_to=datetime(2050, 1, 1), | ||
) | ||
cls.token.valid_for.set([cls.afg]) | ||
|
||
@patch("hct_mis_api.apps.utils.mailjet.requests.post") | ||
@patch.object(APITokenAdmin, "message_user", return_value=None) | ||
@patch.object(APITokenAdmin, "__init__", return_value=None) | ||
@override_settings(EMAIL_SUBJECT_PREFIX="test") | ||
@override_config(ENABLE_MAILJET=True) | ||
def test_send_api_token( | ||
self, mocked_requests_init: Any, mocked_requests_user: Any, mocked_requests_post: Any | ||
) -> None: | ||
request = HttpRequest() | ||
|
||
APITokenAdmin()._send_token_email(request, self.token, TOKEN_INFO_EMAIL) | ||
|
||
mocked_requests_post.assert_called_once() | ||
|
||
expected_data = json.dumps( | ||
{ | ||
"Messages": [ | ||
{ | ||
"From": {"Email": settings.DEFAULT_EMAIL, "Name": settings.DEFAULT_EMAIL_DISPLAY}, | ||
"Subject": f"[test] HOPE API Token {self.token} infos", | ||
"To": [ | ||
{ | ||
"Email": "[email protected]", | ||
}, | ||
], | ||
"Cc": [], | ||
"TextPart": f"\nDear {self.user.first_name},\n\nplease find below API token infos\n\nName: {self.token}\nKey: {self.token.key}\nGrants: {self.token.grants}\nExpires: {self.token.valid_to}\nBusiness Areas: {', '.join(self.token.valid_for.values_list('name', flat=True))}\n\nRegards\n\nThe HOPE Team\n", | ||
} | ||
] | ||
} | ||
) | ||
mocked_requests_post.assert_called_with( | ||
"https://api.mailjet.com/v3.1/send", | ||
auth=(settings.MAILJET_API_KEY, settings.MAILJET_SECRET_KEY), | ||
data=expected_data, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -265,7 +265,7 @@ def test_send_email_notification(self, mock_send: Any) -> None: | |
payment_notification.send_email_notification() | ||
self.assertEqual( | ||
mock_send.call_count, | ||
3, | ||
1, | ||
) | ||
|
||
@mock.patch("hct_mis_api.apps.payment.notifications.MailjetClient.send_email") | ||
|
@@ -278,8 +278,7 @@ def test_send_email_notification_subject_test_env(self, mock_send: Any) -> None: | |
self.user_action_user, | ||
f"{timezone.now():%-d %B %Y}", | ||
) | ||
for mailjet_client in payment_notification.emails: | ||
self.assertEqual(mailjet_client.subject, "[test] Payment pending for Approval") | ||
self.assertEqual(payment_notification.email.subject, "[test] Payment pending for Approval") | ||
|
||
@mock.patch("hct_mis_api.apps.payment.notifications.MailjetClient.send_email") | ||
@override_config(SEND_PAYMENT_PLANS_NOTIFICATION=True) | ||
|
@@ -291,8 +290,7 @@ def test_send_email_notification_subject_prod_env(self, mock_send: Any) -> None: | |
self.user_action_user, | ||
f"{timezone.now():%-d %B %Y}", | ||
) | ||
for mailjet_client in payment_notification.emails: | ||
self.assertEqual(mailjet_client.subject, "Payment pending for Approval") | ||
self.assertEqual(payment_notification.email.subject, "Payment pending for Approval") | ||
|
||
@mock.patch("hct_mis_api.apps.utils.mailjet.requests.post") | ||
@override_config( | ||
|
@@ -307,14 +305,18 @@ def test_send_email_notification_catch_all_email(self, mock_post: Any) -> None: | |
f"{timezone.now():%-d %B %Y}", | ||
) | ||
payment_notification.send_email_notification() | ||
for mailjet_client in payment_notification.emails: | ||
self.assertEqual( | ||
mailjet_client.recipients, | ||
["[email protected]", "[email protected]"], | ||
) | ||
self.assertEqual(len(payment_notification.email.recipients), 2) | ||
self.assertIn( | ||
"[email protected]", | ||
payment_notification.email.recipients, | ||
) | ||
self.assertIn( | ||
"[email protected]", | ||
payment_notification.email.recipients, | ||
) | ||
self.assertEqual( | ||
mock_post.call_count, | ||
3, | ||
1, | ||
) | ||
|
||
@mock.patch("hct_mis_api.apps.utils.mailjet.requests.post") | ||
|
@@ -329,18 +331,22 @@ def test_send_email_notification_without_catch_all_email(self, mock_post: Any) - | |
f"{timezone.now():%-d %B %Y}", | ||
) | ||
payment_notification.send_email_notification() | ||
for mailjet_client in payment_notification.emails: | ||
self.assertIn( | ||
mailjet_client.recipients[0], | ||
[ | ||
self.user_with_approval_permission_partner_unicef.email, | ||
self.user_with_approval_permission_partner_with_program_access.email, | ||
self.user_with_partner_action_permissions_and_program_access.email, | ||
], | ||
) | ||
self.assertEqual(len(payment_notification.email.recipients), 3) | ||
self.assertIn( | ||
self.user_with_approval_permission_partner_unicef.email, | ||
payment_notification.email.recipients, | ||
) | ||
self.assertIn( | ||
self.user_with_approval_permission_partner_with_program_access.email, | ||
payment_notification.email.recipients, | ||
) | ||
self.assertIn( | ||
self.user_with_partner_action_permissions_and_program_access.email, | ||
payment_notification.email.recipients, | ||
) | ||
self.assertEqual( | ||
mock_post.call_count, | ||
3, | ||
1, | ||
) | ||
|
||
@mock.patch("hct_mis_api.apps.utils.mailjet.requests.post") | ||
|
@@ -358,16 +364,16 @@ def test_send_email_notification_exclude_superuser(self, mock_post: Any) -> None | |
f"{timezone.now():%-d %B %Y}", | ||
) | ||
payment_notification.send_email_notification() | ||
for mailjet_client in payment_notification.emails: | ||
self.assertIn( | ||
mailjet_client.recipients[0], | ||
[ | ||
self.user_with_approval_permission_partner_with_program_access.email, | ||
self.user_with_partner_action_permissions_and_program_access.email, | ||
], | ||
) | ||
self.assertNotIn(self.user_with_approval_permission_partner_unicef.email, payment_notification.emails) | ||
self.assertEqual(len(payment_notification.email.recipients), 2) | ||
self.assertIn( | ||
self.user_with_approval_permission_partner_with_program_access.email, | ||
payment_notification.email.recipients, | ||
) | ||
self.assertIn( | ||
self.user_with_partner_action_permissions_and_program_access.email, | ||
payment_notification.email.recipients, | ||
) | ||
self.assertEqual( | ||
mock_post.call_count, | ||
2, | ||
1, | ||
) |