-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmutation_test.py
More file actions
234 lines (206 loc) · 7.41 KB
/
mutation_test.py
File metadata and controls
234 lines (206 loc) · 7.41 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
import typing
from captcha.models import CaptchaStore
from apps.resources.models import ContactRequest, RequestDemo
from apps.tool_picker.factories import ToolFactory
from apps.user.factories import UserFactory
from main.tests import TestCase
class TestContactRequestMutation(TestCase):
class Mutation:
CREATE_CONTACT_REQUEST = """
mutation CreateContactRequest($data: ContactRequestInput!) {
createContactRequest(data: $data) {
... on ContactRequestTypeMutationResponseType {
errors
ok
result {
id
name
email
nationalSociety
content
}
}
... on OperationInfo {
__typename
messages {
code
field
kind
message
}
}
}
}
"""
@typing.override
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.user = UserFactory.create(email="test@gmail.com")
def _create_contact_request_mutation(self, data: dict[str, str], **kwargs: typing.Any):
return self.query_check(
query=self.Mutation.CREATE_CONTACT_REQUEST,
variables={
"data": data,
},
)
def test_create_contact_request(self):
# Generates CAPTCHA key and value
captcha_key = CaptchaStore.generate_key()
captcha_obj = CaptchaStore.objects.get(hashkey=captcha_key)
captcha_code = captcha_obj.response
contact_request_data = {
"name": "John",
"email": "john@test.com",
"nationalSociety": "Test National Society",
"content": "This is test content",
"captchaHashkey": captcha_key,
"captchaCode": captcha_code,
}
content = self._create_contact_request_mutation(data=contact_request_data)
response_data = content["data"]["createContactRequest"]
assert response_data["ok"] is True
assert response_data["errors"] is None
contact_request = ContactRequest.objects.get(pk=response_data["result"]["id"])
assert response_data["result"] == {
"id": self.gID(contact_request.pk),
"name": contact_request.name,
"email": contact_request.email,
"content": contact_request.content,
"nationalSociety": contact_request.national_society,
}
def test_create_contact_request_without_captcha(self):
contact_request_data = {
"name": "John",
"email": "john@test.com",
"nationalSociety": "Test National Society",
"content": "This is test content",
"captchaHashkey": "",
"captchaCode": "",
}
content = self._create_contact_request_mutation(data=contact_request_data)
response = content["data"]["createContactRequest"]
assert response["ok"] is False
assert response["result"] is None
assert response["errors"] == [
{
"field": "captchaCode",
"client_id": None,
"messages": "This field may not be blank.",
"object_errors": None,
"array_errors": None,
"pydantic_errors": None,
},
{
"field": "captchaHashkey",
"client_id": None,
"messages": "This field may not be blank.",
"object_errors": None,
"array_errors": None,
"pydantic_errors": None,
},
]
class TestRequestDemoMutation(TestCase):
class Mutation:
CREATE_DEMO_REQUEST = """
mutation createDemoRequest($data: RequestDemoInput!) {
createDemoRequest(data: $data) {
... on RequestDemoTypeMutationResponseType {
errors
ok
result {
id
name
email
nationalSociety
content
tool{
pk
}
}
}
... on OperationInfo {
__typename
messages {
code
field
kind
message
}
}
}
}
"""
@typing.override
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.user = UserFactory.create(email="test@gmail.com")
cls.tool = ToolFactory.create()
def _create_tool_demo_request_mutation(self, data: dict[str, str | int], **kwargs: typing.Any):
return self.query_check(
query=self.Mutation.CREATE_DEMO_REQUEST,
variables={
"data": data,
},
)
def test_create_tool_demo_request(self):
captcha_key = CaptchaStore.generate_key()
captcha_obj = CaptchaStore.objects.get(hashkey=captcha_key)
captcha_code = captcha_obj.response
tool_demo_request_data = {
"name": "John",
"email": "john@test.com",
"nationalSociety": "Test National Society",
"content": "This is test content",
"tool": self.tool.id,
"captchaHashkey": captcha_key,
"captchaCode": captcha_code,
}
content = self._create_tool_demo_request_mutation(data=tool_demo_request_data)
response_data = content["data"]["createDemoRequest"]
assert response_data["ok"] is True
assert response_data["errors"] is None
demo_request = RequestDemo.objects.get(pk=response_data["result"]["id"])
assert response_data["result"] == {
"id": self.gID(demo_request.pk),
"name": demo_request.name,
"email": demo_request.email,
"content": demo_request.content,
"nationalSociety": demo_request.national_society,
"tool": {
"pk": self.gID(demo_request.tool.pk),
},
}
def test_create_tool_demo_request_without_captcha(self):
tool_demo_request_data = {
"name": "John",
"email": "john@test.com",
"nationalSociety": "Test National Society",
"content": "This is test content",
"tool": self.tool.id,
"captchaHashkey": "",
"captchaCode": "",
}
content = self._create_tool_demo_request_mutation(data=tool_demo_request_data)
response = content["data"]["createDemoRequest"]
assert response["ok"] is False
assert response["result"] is None
assert response["errors"] == [
{
"field": "captchaCode",
"client_id": None,
"messages": "This field may not be blank.",
"object_errors": None,
"array_errors": None,
"pydantic_errors": None,
},
{
"field": "captchaHashkey",
"client_id": None,
"messages": "This field may not be blank.",
"object_errors": None,
"array_errors": None,
"pydantic_errors": None,
},
]