Skip to content

Commit f162951

Browse files
committed
Add test
1 parent 7563a8c commit f162951

File tree

4 files changed

+61
-13
lines changed

4 files changed

+61
-13
lines changed

backend/api/grants/mutations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ def send_grant(self, info: Info, input: SendGrantInput) -> SendGrantResult:
257257
},
258258
)
259259

260-
send_grant_application_confirmation_email(instance)
260+
send_grant_application_confirmation_email.delay(grant_id=instance.id)
261261

262262
# hack because we return django models
263263
instance.__strawberry_definition__ = Grant.__strawberry_definition__

backend/api/grants/tests/test_send_grant.py

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
from grants.tests.factories import GrantFactory
44
import pytest
55
from participants.models import Participant
6+
from grants.models import Grant
67

8+
from unittest.mock import call
79

810
pytestmark = pytest.mark.django_db
911

@@ -84,7 +86,10 @@ def _send_grant(client, conference, conference_code=None, **kwargs):
8486
return response
8587

8688

87-
def test_send_grant(graphql_client, user):
89+
def test_send_grant(graphql_client, user, mocker):
90+
mock_confirmation_email = mocker.patch(
91+
"api.grants.mutations.send_grant_application_confirmation_email"
92+
)
8893
graphql_client.force_login(user)
8994
conference = ConferenceFactory(active_grants=True)
9095

@@ -95,13 +100,18 @@ def test_send_grant(graphql_client, user):
95100

96101
participant = Participant.objects.get(conference=conference, user_id=user.id)
97102
assert participant.bio == "my bio"
98-
103+
grant = Grant.objects.get(id=response["data"]["sendGrant"]["id"])
104+
assert grant.conference == conference
99105
assert PrivacyPolicyAcceptanceRecord.objects.filter(
100106
user=user, conference=conference, privacy_policy="grant"
101107
).exists()
108+
mock_confirmation_email.delay.assert_called_once_with(grant_id=grant.id)
102109

103110

104-
def test_cannot_send_a_grant_if_grants_are_closed(graphql_client, user):
111+
def test_cannot_send_a_grant_if_grants_are_closed(graphql_client, user, mocker):
112+
mock_confirmation_email = mocker.patch(
113+
"api.grants.mutations.send_grant_application_confirmation_email"
114+
)
105115
graphql_client.force_login(user)
106116
conference = ConferenceFactory(active_grants=False)
107117

@@ -112,6 +122,7 @@ def test_cannot_send_a_grant_if_grants_are_closed(graphql_client, user):
112122
assert response["data"]["sendGrant"]["errors"]["nonFieldErrors"] == [
113123
"The grants form is not open!"
114124
]
125+
mock_confirmation_email.delay.assert_not_called()
115126

116127

117128
def test_cannot_send_a_grant_if_grants_deadline_do_not_exists(graphql_client, user):
@@ -136,7 +147,10 @@ def test_cannot_send_a_grant_as_unlogged_user(graphql_client):
136147
assert resp["errors"][0]["message"] == "User not logged in"
137148

138149

139-
def test_cannot_send_two_grants_to_the_same_conference(graphql_client, user):
150+
def test_cannot_send_two_grants_to_the_same_conference(graphql_client, user, mocker):
151+
mock_confirmation_email = mocker.patch(
152+
"api.grants.mutations.send_grant_application_confirmation_email"
153+
)
140154
graphql_client.force_login(user)
141155
conference = ConferenceFactory(active_grants=True)
142156
_send_grant(graphql_client, conference)
@@ -148,18 +162,29 @@ def test_cannot_send_two_grants_to_the_same_conference(graphql_client, user):
148162
assert response["data"]["sendGrant"]["errors"]["nonFieldErrors"] == [
149163
"Grant already submitted!"
150164
]
165+
mock_confirmation_email.delay.assert_called_once()
151166

152167

153-
def test_can_send_two_grants_to_different_conferences(graphql_client, user):
168+
def test_can_send_two_grants_to_different_conferences(graphql_client, user, mocker):
169+
mock_confirmation_email = mocker.patch(
170+
"api.grants.mutations.send_grant_application_confirmation_email"
171+
)
154172
graphql_client.force_login(user)
155173
conference = ConferenceFactory(active_grants=True)
156174
conference_2 = ConferenceFactory(active_grants=True)
157-
_send_grant(graphql_client, conference)
158-
159-
response = _send_grant(graphql_client, conference_2)
160-
161-
assert not response.get("errors")
162-
assert response["data"]["sendGrant"]["__typename"] == "Grant"
175+
first_response = _send_grant(graphql_client, conference)
176+
177+
second_response = _send_grant(graphql_client, conference_2)
178+
179+
assert not second_response.get("errors")
180+
assert second_response["data"]["sendGrant"]["__typename"] == "Grant"
181+
mock_confirmation_email.delay.assert_has_calls(
182+
[
183+
call(grant_id=int(first_response["data"]["sendGrant"]["id"])),
184+
call(grant_id=int(second_response["data"]["sendGrant"]["id"])),
185+
],
186+
any_order=True,
187+
)
163188

164189

165190
def test_invalid_conference(graphql_client, user):

backend/grants/tasks.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ def _new_send_grant_email(
173173
grant.save()
174174

175175

176-
def send_grant_application_confirmation_email(grant: Grant):
176+
@app.task
177+
def send_grant_application_confirmation_email(*, grant_id):
178+
grant = Grant.objects.get(id=grant_id)
177179
email_template = EmailTemplate.objects.for_conference(
178180
grant.conference
179181
).get_by_identifier(EmailTemplateIdentifier.grant_application_confirmation)

backend/grants/tests/test_tasks.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
send_grant_reply_approved_email,
1212
send_grant_reply_rejected_email,
1313
send_grant_reply_waiting_list_email,
14+
send_grant_application_confirmation_email,
1415
)
1516
from grants.models import Grant
1617

@@ -311,3 +312,23 @@ def test_send_grant_reply_waiting_list_update_email(settings):
311312
"reply_url": "https://pycon.it/grants/reply/",
312313
},
313314
)
315+
316+
317+
def test_send_grant_application_confirmation_email():
318+
user = UserFactory(
319+
full_name="Marco Acierno",
320+
321+
name="Marco",
322+
username="marco",
323+
)
324+
grant = GrantFactory(user=user)
325+
326+
with patch("grants.tasks.EmailTemplate") as mock_email_template:
327+
send_grant_application_confirmation_email(grant_id=grant.id)
328+
329+
mock_email_template.objects.for_conference().get_by_identifier().send_email.assert_called_once_with(
330+
recipient=user,
331+
placeholders={
332+
"user_name": "Marco Acierno",
333+
},
334+
)

0 commit comments

Comments
 (0)