-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathtest_send_grant.py
More file actions
312 lines (255 loc) · 11 KB
/
test_send_grant.py
File metadata and controls
312 lines (255 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
from privacy_policy.models import PrivacyPolicyAcceptanceRecord
from conferences.tests.factories import ConferenceFactory
from grants.tests.factories import GrantFactory
import pytest
from participants.models import Participant
from grants.models import Grant
from notifications.models import EmailTemplateIdentifier
from notifications.tests.factories import EmailTemplateFactory
pytestmark = pytest.mark.django_db
def _send_grant(client, conference, conference_code=None, **kwargs):
grant = GrantFactory.build(conference=conference)
document = """
mutation SendGrant($input: SendGrantInput!) {
sendGrant(input: $input) {
__typename
... on Grant {
id
}
... on GrantErrors {
errors {
validationConference: conference
validationName: name
validationFullName: fullName
validationGender: gender
validationGrantType: grantType
validationOccupation: occupation
validationAgeGroup: ageGroup
validationPythonUsage: pythonUsage
validationCommunityContribution: communityContribution
validationBeenToOtherEvents: beenToOtherEvents
validationNeedsFundsForTravel: needsFundsForTravel
validationNeedVisa: needVisa
validationNeedAccommodation: needAccommodation
validationWhy: why
validationNotes: notes
validationDepartureCountry: departureCountry
validationNationality: nationality
validationDepartureCity: departureCity
validationParticipantBio: participantBio
validationParticipantWebsite: participantWebsite
validationParticipantTwitterHandle: participantTwitterHandle
validationparticipantInstagramHandle: participantInstagramHandle
validationParticipantLinkedinUrl: participantLinkedinUrl
validationParticipantFacebookUrl: participantFacebookUrl
validationParticipantMastodonHandle: participantMastodonHandle
nonFieldErrors
}
}
}
}
"""
defaults = {
"name": grant.name,
"fullName": grant.full_name,
"conference": conference_code or conference.code,
"ageGroup": grant.age_group,
"gender": grant.gender,
"occupation": grant.occupation,
"grantType": grant.grant_type,
"pythonUsage": grant.python_usage,
"communityContribution": grant.community_contribution,
"beenToOtherEvents": grant.been_to_other_events,
"needsFundsForTravel": grant.needs_funds_for_travel,
"needVisa": grant.need_visa,
"needAccommodation": grant.need_accommodation,
"why": grant.why,
"notes": grant.notes,
"departureCountry": grant.departure_country,
"nationality": grant.nationality,
"departureCity": grant.departure_city,
"participantBio": "my bio",
"participantWebsite": "http://website.it",
"participantTwitterHandle": "handle",
"participantInstagramHandle": "handleinsta",
"participantLinkedinUrl": "https://linkedin.com/fake-link",
"participantFacebookUrl": "https://facebook.com/fake-link",
"participantMastodonHandle": "fake@mastodon.social",
}
variables = {**defaults, **kwargs}
response = client.query(document, variables={"input": variables})
return response
def test_send_grant(graphql_client, user, mocker, django_capture_on_commit_callbacks, sent_emails):
graphql_client.force_login(user)
conference = ConferenceFactory(active_grants=True)
EmailTemplateFactory(
conference=conference,
identifier=EmailTemplateIdentifier.grant_application_confirmation,
)
with django_capture_on_commit_callbacks(execute=True):
response = _send_grant(graphql_client, conference)
# The API response is successful
assert response["data"]["sendGrant"]["__typename"] == "Grant"
assert response["data"]["sendGrant"]["id"]
# A participant is created
participant = Participant.objects.get(conference=conference, user_id=user.id)
assert participant.bio == "my bio"
# A grant object is created
grant = Grant.objects.get(id=response["data"]["sendGrant"]["id"])
assert grant.conference == conference
assert PrivacyPolicyAcceptanceRecord.objects.filter(
user=user, conference=conference, privacy_policy="grant"
).exists()
# Verify that the correct email template was used and email was sent
emails_sent = sent_emails()
assert emails_sent.count() == 1
sent_email = emails_sent.first()
assert sent_email.email_template.identifier == EmailTemplateIdentifier.grant_application_confirmation
assert sent_email.email_template.conference == conference
assert sent_email.recipient == user
assert sent_email.recipient_email == user.email
# Verify placeholders were processed correctly
assert sent_email.placeholders["user_name"] == user.full_name
def test_cannot_send_a_grant_if_grants_are_closed(graphql_client, user, mocker):
graphql_client.force_login(user)
conference = ConferenceFactory(active_grants=False)
response = _send_grant(graphql_client, conference)
assert not response.get("errors")
assert response["data"]["sendGrant"]["__typename"] == "GrantErrors"
assert response["data"]["sendGrant"]["errors"]["nonFieldErrors"] == [
"The grants form is not open!"
]
def test_cannot_send_a_grant_if_grants_deadline_do_not_exists(graphql_client, user):
conference = ConferenceFactory()
assert list(conference.deadlines.all()) == []
graphql_client.force_login(user)
response = _send_grant(graphql_client, conference)
assert not response.get("errors")
assert response["data"]["sendGrant"]["__typename"] == "GrantErrors"
assert response["data"]["sendGrant"]["errors"]["nonFieldErrors"] == [
"The grants form is not open!"
]
def test_cannot_send_a_grant_as_unlogged_user(graphql_client):
conference = ConferenceFactory()
resp = _send_grant(graphql_client, conference)
assert resp["errors"][0]["message"] == "User not logged in"
def test_cannot_send_two_grants_to_the_same_conference(graphql_client, user, mocker):
graphql_client.force_login(user)
conference = ConferenceFactory(active_grants=True)
EmailTemplateFactory(
conference=conference,
identifier=EmailTemplateIdentifier.grant_application_confirmation,
)
_send_grant(graphql_client, conference)
response = _send_grant(graphql_client, conference)
assert not response.get("errors")
assert response["data"]["sendGrant"]["__typename"] == "GrantErrors"
assert response["data"]["sendGrant"]["errors"]["nonFieldErrors"] == [
"Grant already submitted!"
]
def test_can_send_two_grants_to_different_conferences(
graphql_client, user, mocker, django_capture_on_commit_callbacks
):
graphql_client.force_login(user)
conference = ConferenceFactory(active_grants=True)
conference_2 = ConferenceFactory(active_grants=True)
EmailTemplateFactory(
conference=conference,
identifier=EmailTemplateIdentifier.grant_application_confirmation,
)
EmailTemplateFactory(
conference=conference_2,
identifier=EmailTemplateIdentifier.grant_application_confirmation,
)
first_response = _send_grant(graphql_client, conference)
assert not first_response.get("errors")
assert first_response["data"]["sendGrant"]["__typename"] == "Grant"
second_response = _send_grant(graphql_client, conference_2)
assert not second_response.get("errors")
assert second_response["data"]["sendGrant"]["__typename"] == "Grant"
def test_invalid_conference(graphql_client, user):
graphql_client.force_login(user)
response = _send_grant(
graphql_client, ConferenceFactory(), conference_code="invalid"
)
assert not response.get("errors")
assert response["data"]["sendGrant"]["__typename"] == "GrantErrors"
assert response["data"]["sendGrant"]["errors"]["validationConference"] == [
"Invalid conference"
]
def test_cannot_send_grant_outside_allowed_values(
graphql_client,
user,
):
graphql_client.force_login(user)
conference = ConferenceFactory(
active_grants=True,
)
response = _send_grant(
graphql_client,
conference,
name="Marcotte" * 50,
departureCountry="Very long location" * 50,
nationality="Freedonia" * 50,
departureCity="Emerald City " * 50,
)
assert response["data"]["sendGrant"]["__typename"] == "GrantErrors"
assert response["data"]["sendGrant"]["errors"]["validationName"] == [
"name: Cannot be more than 300 chars"
]
assert response["data"]["sendGrant"]["errors"]["validationDepartureCountry"] == [
"departure_country: Cannot be more than 100 chars"
]
assert response["data"]["sendGrant"]["errors"]["validationNationality"] == [
"nationality: Cannot be more than 100 chars"
]
assert response["data"]["sendGrant"]["errors"]["validationDepartureCity"] == [
"departure_city: Cannot be more than 100 chars"
]
def test_cannot_send_grant_with_empty_values(
graphql_client,
user,
):
graphql_client.force_login(user)
conference = ConferenceFactory(
active_grants=True,
)
response = _send_grant(
graphql_client,
conference,
fullName="",
pythonUsage="",
beenToOtherEvents="",
why="",
)
assert response["data"]["sendGrant"]["__typename"] == "GrantErrors"
assert response["data"]["sendGrant"]["errors"]["validationFullName"] == [
"full_name: Cannot be empty"
]
def test_submit_grant_with_existing_participant(graphql_client, user):
graphql_client.force_login(user)
conference = ConferenceFactory(
active_grants=True,
)
EmailTemplateFactory(
conference=conference,
identifier=EmailTemplateIdentifier.grant_application_confirmation,
)
participant = Participant.objects.create(
conference=conference, user_id=user.id, bio="old bio"
)
response = _send_grant(
graphql_client,
conference,
participantBio="my bio",
participantWebsite="https://sushi.com",
)
assert response["data"]["sendGrant"]["__typename"] == "Grant"
assert response["data"]["sendGrant"]["id"]
grant = Grant.objects.get(id=response["data"]["sendGrant"]["id"])
assert grant.status == Grant.Status.pending
assert grant.conference == conference
assert grant.user_id == user.id
participant.refresh_from_db()
assert participant.bio == "my bio"
assert participant.website == "https://sushi.com"