33from grants .tests .factories import GrantFactory
44import pytest
55from participants .models import Participant
6+ from grants .models import Grant
67
8+ from unittest .mock import call
79
810pytestmark = 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
117128def 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
165190def test_invalid_conference (graphql_client , user ):
0 commit comments