44import pytest
55from participants .models import Participant
66from grants .models import Grant
7+ from notifications .models import EmailTemplateIdentifier
8+ from notifications .tests .factories import EmailTemplateFactory
79
8- from unittest .mock import call
910
1011pytestmark = pytest .mark .django_db
1112
@@ -90,32 +91,43 @@ def _send_grant(client, conference, conference_code=None, **kwargs):
9091 return response
9192
9293
93- def test_send_grant (graphql_client , user , mocker ):
94- mock_confirmation_email = mocker .patch (
95- "api.grants.mutations.send_grant_application_confirmation_email"
96- )
94+ def test_send_grant (graphql_client , user , mocker , django_capture_on_commit_callbacks ):
95+ mock_email_template = mocker .patch ("api.grants.mutations.EmailTemplate" )
9796 graphql_client .force_login (user )
9897 conference = ConferenceFactory (active_grants = True )
98+ EmailTemplateFactory (
99+ conference = conference ,
100+ identifier = EmailTemplateIdentifier .grant_application_confirmation ,
101+ )
99102
100- response = _send_grant (graphql_client , conference )
103+ with django_capture_on_commit_callbacks (execute = True ):
104+ response = _send_grant (graphql_client , conference )
101105
106+ # The API response is successful
102107 assert response ["data" ]["sendGrant" ]["__typename" ] == "Grant"
103108 assert response ["data" ]["sendGrant" ]["id" ]
104109
110+ # A participant is created
105111 participant = Participant .objects .get (conference = conference , user_id = user .id )
106112 assert participant .bio == "my bio"
113+
114+ # A grant object is created
107115 grant = Grant .objects .get (id = response ["data" ]["sendGrant" ]["id" ])
108116 assert grant .conference == conference
109117 assert PrivacyPolicyAcceptanceRecord .objects .filter (
110118 user = user , conference = conference , privacy_policy = "grant"
111119 ).exists ()
112- mock_confirmation_email .delay .assert_called_once_with (grant_id = grant .id )
120+
121+ # An email is sent to the user
122+ mock_email_template .objects .for_conference ().get_by_identifier ().send_email .assert_called_once_with (
123+ recipient = user ,
124+ placeholders = {
125+ "user_name" : user .full_name ,
126+ },
127+ )
113128
114129
115130def test_cannot_send_a_grant_if_grants_are_closed (graphql_client , user , mocker ):
116- mock_confirmation_email = mocker .patch (
117- "api.grants.mutations.send_grant_application_confirmation_email"
118- )
119131 graphql_client .force_login (user )
120132 conference = ConferenceFactory (active_grants = False )
121133
@@ -126,7 +138,6 @@ def test_cannot_send_a_grant_if_grants_are_closed(graphql_client, user, mocker):
126138 assert response ["data" ]["sendGrant" ]["errors" ]["nonFieldErrors" ] == [
127139 "The grants form is not open!"
128140 ]
129- mock_confirmation_email .delay .assert_not_called ()
130141
131142
132143def test_cannot_send_a_grant_if_grants_deadline_do_not_exists (graphql_client , user ):
@@ -152,11 +163,12 @@ def test_cannot_send_a_grant_as_unlogged_user(graphql_client):
152163
153164
154165def test_cannot_send_two_grants_to_the_same_conference (graphql_client , user , mocker ):
155- mock_confirmation_email = mocker .patch (
156- "api.grants.mutations.send_grant_application_confirmation_email"
157- )
158166 graphql_client .force_login (user )
159167 conference = ConferenceFactory (active_grants = True )
168+ EmailTemplateFactory (
169+ conference = conference ,
170+ identifier = EmailTemplateIdentifier .grant_application_confirmation ,
171+ )
160172 _send_grant (graphql_client , conference )
161173
162174 response = _send_grant (graphql_client , conference )
@@ -166,29 +178,30 @@ def test_cannot_send_two_grants_to_the_same_conference(graphql_client, user, moc
166178 assert response ["data" ]["sendGrant" ]["errors" ]["nonFieldErrors" ] == [
167179 "Grant already submitted!"
168180 ]
169- mock_confirmation_email .delay .assert_called_once ()
170181
171182
172- def test_can_send_two_grants_to_different_conferences (graphql_client , user , mocker ):
173- mock_confirmation_email = mocker .patch (
174- "api.grants.mutations.send_grant_application_confirmation_email"
175- )
183+ def test_can_send_two_grants_to_different_conferences (
184+ graphql_client , user , mocker , django_capture_on_commit_callbacks
185+ ):
176186 graphql_client .force_login (user )
177187 conference = ConferenceFactory (active_grants = True )
178188 conference_2 = ConferenceFactory (active_grants = True )
189+ EmailTemplateFactory (
190+ conference = conference ,
191+ identifier = EmailTemplateIdentifier .grant_application_confirmation ,
192+ )
193+ EmailTemplateFactory (
194+ conference = conference_2 ,
195+ identifier = EmailTemplateIdentifier .grant_application_confirmation ,
196+ )
179197 first_response = _send_grant (graphql_client , conference )
198+ assert not first_response .get ("errors" )
199+ assert first_response ["data" ]["sendGrant" ]["__typename" ] == "Grant"
180200
181201 second_response = _send_grant (graphql_client , conference_2 )
182202
183203 assert not second_response .get ("errors" )
184204 assert second_response ["data" ]["sendGrant" ]["__typename" ] == "Grant"
185- mock_confirmation_email .delay .assert_has_calls (
186- [
187- call (grant_id = int (first_response ["data" ]["sendGrant" ]["id" ])),
188- call (grant_id = int (second_response ["data" ]["sendGrant" ]["id" ])),
189- ],
190- any_order = True ,
191- )
192205
193206
194207def test_invalid_conference (graphql_client , user ):
@@ -260,3 +273,36 @@ def test_cannot_send_grant_with_empty_values(
260273 assert response ["data" ]["sendGrant" ]["errors" ]["validationFullName" ] == [
261274 "full_name: Cannot be empty"
262275 ]
276+
277+
278+ def test_submit_grant_with_existing_participant (graphql_client , user ):
279+ graphql_client .force_login (user )
280+ conference = ConferenceFactory (
281+ active_grants = True ,
282+ )
283+ EmailTemplateFactory (
284+ conference = conference ,
285+ identifier = EmailTemplateIdentifier .grant_application_confirmation ,
286+ )
287+ participant = Participant .objects .create (
288+ conference = conference , user_id = user .id , bio = "old bio"
289+ )
290+
291+ response = _send_grant (
292+ graphql_client ,
293+ conference ,
294+ participantBio = "my bio" ,
295+ participantWebsite = "https://sushi.com" ,
296+ )
297+
298+ assert response ["data" ]["sendGrant" ]["__typename" ] == "Grant"
299+ assert response ["data" ]["sendGrant" ]["id" ]
300+
301+ grant = Grant .objects .get (id = response ["data" ]["sendGrant" ]["id" ])
302+ assert grant .status == Grant .Status .pending
303+ assert grant .conference == conference
304+ assert grant .user_id == user .id
305+
306+ participant .refresh_from_db ()
307+ assert participant .bio == "my bio"
308+ assert participant .website == "https://sushi.com"
0 commit comments