forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_client.py
More file actions
299 lines (231 loc) · 10.9 KB
/
test_client.py
File metadata and controls
299 lines (231 loc) · 10.9 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
"""Tests for methods on the Client class"""
from gql.transport.exceptions import (
TransportQueryError,
TransportServerError,
TransportProtocolError,
)
from graphql.error import GraphQLError
import pytest
from tracker_client.client import Client
import tracker_client.queries as queries
# pylint: disable=no-member
def test_client_execute_query_transport_query_error(mocker):
"""Test that execute_query properly handles an error message from the server"""
server_error_response = {
"message": "No organization with the provided slug could be found.",
"locations": [{"line": 2, "column": 3}],
"path": ["findOrganizationBySlug"],
"extensions": {"code": "INTERNAL_SERVER_ERROR"},
}
# Stop Client.__init__ from connecting to Tracker
mocker.patch("tracker_client.client.get_auth_token")
mocker.patch("tracker_client.client.create_client")
test_client = Client()
test_client.gql_client.execute = mocker.MagicMock(
side_effect=TransportQueryError(
str(server_error_response),
errors=[server_error_response],
data={"foo": None},
)
)
result = test_client.execute_query(None)
assert result == {
"error": [
{
"message": "No organization with the provided slug could be found.",
"path": ["findOrganizationBySlug"],
}
]
}
def test_client_execute_query_transport_protocol_error(mocker, capsys):
"""Test that TransportProtocolError is properly re-raised"""
mocker.patch("tracker_client.client.get_auth_token")
mocker.patch("tracker_client.client.create_client")
test_client = Client()
test_client.gql_client.execute = mocker.MagicMock(
side_effect=TransportProtocolError
)
with pytest.raises(TransportProtocolError):
test_client.execute_query(None)
# Check that the warning for TransportProtocolError was printed
captured = capsys.readouterr()
assert "Unexpected response from server:" in captured.out
def test_client_execute_query_transport_server_error(mocker, capsys):
"""Test that TransportServerError is properly re-raised"""
mocker.patch("tracker_client.client.get_auth_token")
mocker.patch("tracker_client.client.create_client")
test_client = Client()
test_client.gql_client.execute = mocker.MagicMock(side_effect=TransportServerError)
with pytest.raises(TransportServerError):
test_client.execute_query(None)
# Check that the warning for TransportServerError was printed
captured = capsys.readouterr()
assert "Server error:" in captured.out
def test_client_execute_query_graphql_error(mocker, capsys):
"""Test that GraphQLError is properly re-raised"""
mocker.patch("tracker_client.client.get_auth_token")
mocker.patch("tracker_client.client.create_client")
test_client = Client()
# GraphQLError requires a message
test_client.gql_client.execute = mocker.MagicMock(side_effect=GraphQLError("test"))
with pytest.raises(GraphQLError):
test_client.execute_query(None)
# Check that the warning for GraphQLError was printed
captured = capsys.readouterr()
assert "Query validation error, client may be out of date:" in captured.out
def test_client_execute_query_other_error(mocker, capsys):
"""Test that other exceptions are properly re-raised"""
mocker.patch("tracker_client.client.get_auth_token")
mocker.patch("tracker_client.client.create_client")
test_client = Client()
test_client.gql_client.execute = mocker.MagicMock(side_effect=ValueError)
with pytest.raises(ValueError):
test_client.execute_query(None)
# Check that the warning for other errors was printed
captured = capsys.readouterr()
assert "Fatal error:" in captured.out
def test_client_execute_query_success(mocker, client_all_domains_input):
"""Test that a successful response is passed on unchanged"""
mocker.patch("tracker_client.client.get_auth_token")
mocker.patch("tracker_client.client.create_client")
test_client = Client()
test_client.gql_client.execute = mocker.MagicMock(
return_value=client_all_domains_input
)
result = test_client.execute_query(None)
assert result == client_all_domains_input
def test_client_get_organizations(mocker, client_all_orgs_input):
"""Test that Client.get_organizations produces correct output"""
mocker.patch("tracker_client.client.get_auth_token")
mocker.patch("tracker_client.client.create_client")
test_client = Client()
test_client.execute_query = mocker.MagicMock(return_value=client_all_orgs_input)
org_list = test_client.get_organizations()
test_client.execute_query.assert_called_once_with(
queries.GET_ALL_ORGS, {"after": "abc", "search": ""}
)
assert org_list[0].acronym == "FOO"
assert org_list[1].name == "Fizz Bang"
assert org_list[0].domain_count == 10
assert org_list[1].verified
def test_client_get_organizations_pagination(
mocker, client_all_orgs_input, client_all_orgs_has_next_input
):
"""Test that Client.get_organizations correctly requests more organizations if hasNextPage is true"""
def mock_return(query, params):
if params["after"] == "abc":
return client_all_orgs_input
return client_all_orgs_has_next_input
mocker.patch("tracker_client.client.get_auth_token")
mocker.patch("tracker_client.client.create_client")
test_client = Client()
test_client.execute_query = mock_return
org_list = test_client.get_organizations()
# If get_domains didn't try to paginate, len(domain_list) will be 2.
# If it didn't stop trying to get more domains after hasNextPage became false
# then the length will be greater than 4.
assert len(org_list) == 4
def test_client_get_organizations_error(mocker, error_message, capsys):
"""Test that Client.get_organizations correctly handles error response"""
mocker.patch("tracker_client.client.get_auth_token")
mocker.patch("tracker_client.client.create_client")
test_client = Client()
test_client.execute_query = mocker.MagicMock(return_value=error_message)
with pytest.raises(ValueError, match=r"Unable to get your organizations"):
test_client.get_organizations()
captured = capsys.readouterr()
assert "Server error:" in captured.out
def test_client_get_organization(mocker, client_org_input):
"""Test that Client.get_organization produces correct output"""
mocker.patch("tracker_client.client.get_auth_token")
mocker.patch("tracker_client.client.create_client")
test_client = Client()
test_client.execute_query = mocker.MagicMock(return_value=client_org_input)
org = test_client.get_organization("Foo Bar")
test_client.execute_query.assert_called_once_with(
queries.GET_ORG, {"orgSlug": "foo-bar"}
)
assert org.acronym == "FOO"
assert org.name == "Foo Bar"
assert org.zone == "FED"
assert org.sector == "TBS"
assert org.country == "Canada"
assert org.province == "Ontario"
assert org.city == "Ottawa"
assert org.domain_count == 10
assert org.verified
def test_client_get_organization_error(mocker, error_message, capsys):
"""Test that Client.get_organization correctly handles error response"""
mocker.patch("tracker_client.client.get_auth_token")
mocker.patch("tracker_client.client.create_client")
test_client = Client()
test_client.execute_query = mocker.MagicMock(return_value=error_message)
with pytest.raises(ValueError, match=r"Unable to get organization Foo Bar"):
test_client.get_organization("Foo Bar")
captured = capsys.readouterr()
assert "Server error:" in captured.out
def test_client_get_domains(mocker, client_all_domains_input):
"""Test that Client.get_domains produces correct output"""
mocker.patch("tracker_client.client.get_auth_token")
mocker.patch("tracker_client.client.create_client")
test_client = Client()
test_client.execute_query = mocker.MagicMock(return_value=client_all_domains_input)
domain_list = test_client.get_domains()
test_client.execute_query.assert_called_once_with(
queries.GET_ALL_DOMAINS, {"after": "abc", "search": ""}
)
assert domain_list[0].domain_name == "foo.bar"
assert domain_list[1].dmarc_phase == "not implemented"
assert domain_list[2].last_ran == "2021-01-27 23:24:26.911236"
assert domain_list[0].dkim_selectors == []
def test_client_get_domains_pagination(
mocker, client_all_domains_input, client_all_domains_has_next_input
):
"""Test that Client.get_domains correctly requests more domains if hasNextPage is true"""
def mock_return(query, params):
if params["after"] == "abc":
return client_all_domains_input
return client_all_domains_has_next_input
mocker.patch("tracker_client.client.get_auth_token")
mocker.patch("tracker_client.client.create_client")
test_client = Client()
test_client.execute_query = mock_return
domain_list = test_client.get_domains()
# If get_domains didn't try to paginate, len(domain_list) will be 3.
# If it didn't stop trying to get more domains after hasNextPage became false
# then the length will be greater than 6.
assert len(domain_list) == 6
def test_client_get_domains_error(mocker, error_message, capsys):
"""Test that Client.get_domains correctly handles error response"""
mocker.patch("tracker_client.client.get_auth_token")
mocker.patch("tracker_client.client.create_client")
test_client = Client()
test_client.execute_query = mocker.MagicMock(return_value=error_message)
with pytest.raises(ValueError, match=r"Unable to get your domains"):
test_client.get_domains()
captured = capsys.readouterr()
assert "Server error:" in captured.out
def test_client_get_domain(mocker, client_domain_input):
"""Test that Client.get_domain produces correct output"""
mocker.patch("tracker_client.client.get_auth_token")
mocker.patch("tracker_client.client.create_client")
test_client = Client()
test_client.execute_query = mocker.MagicMock(return_value=client_domain_input)
domain = test_client.get_domain("foo.bar")
test_client.execute_query.assert_called_once_with(
queries.GET_DOMAIN, {"domain": "foo.bar"}
)
assert domain.domain_name == "foo.bar"
assert domain.dmarc_phase == "not implemented"
assert domain.last_ran == "2021-01-27 23:24:26.911236"
assert domain.dkim_selectors == []
def test_client_get_domain_error(mocker, error_message, capsys):
"""Test that Client.get_domains correctly handles error response"""
mocker.patch("tracker_client.client.get_auth_token")
mocker.patch("tracker_client.client.create_client")
test_client = Client()
test_client.execute_query = mocker.MagicMock(return_value=error_message)
with pytest.raises(ValueError, match=r"Unable to get domain foo.bar"):
test_client.get_domain("foo.bar")
captured = capsys.readouterr()
assert "Server error:" in captured.out