This repository has been archived by the owner on Jan 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
test_apiproxy.py
416 lines (331 loc) · 13.7 KB
/
test_apiproxy.py
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import datetime
import http
import json
import os
import shutil
import tempfile
import time
import unittest
from subprocess import TimeoutExpired
import pyotp
import pytest
from sdclientapi import API, RequestTimeoutError
from sdclientapi.sdlocalobjects import (
BaseError,
Reply,
ReplyError,
Source,
Submission,
WrongUUIDError,
)
from utils import dastollervey_datasaver, load_auth, save_auth
NUM_REPLIES_PER_SOURCE = 2
class TestAPIProxy(unittest.TestCase):
@dastollervey_datasaver
def setUp(self):
self.totp = pyotp.TOTP("JHCOGO7VCER3EJ4L")
self.username = "journalist"
self.password = "correct horse battery staple profanity oil chewy"
self.server = "http://localhost:8081/"
self.api = API(self.server, self.username, self.password, str(self.totp.now()), proxy=True)
for i in range(3):
if os.path.exists("testtoken.json"):
token = load_auth()
self.api.token = token
self.api.update_auth_header()
break
# The following is True when we did a logout
if os.path.exists("logout.txt"):
os.unlink("logout.txt")
time.sleep(31)
# Now let us try to login
try:
self.api = API(
self.server, self.username, self.password, str(self.totp.now()), proxy=True
)
self.api.authenticate()
with open("login.txt", "w") as fobj:
fobj.write("in")
except Exception as err:
print(err)
time.sleep(31)
continue
save_auth(self.api.token)
break
def test_api_auth(self):
self.assertTrue(isinstance(self.api.token, str))
self.assertTrue(isinstance(self.api.token_expiration, datetime.datetime))
self.assertTrue(isinstance(self.api.token_journalist_uuid, str))
self.assertTrue(isinstance(self.api.journalist_first_name, (str, type(None))))
self.assertTrue(isinstance(self.api.journalist_last_name, (str, type(None))))
@dastollervey_datasaver
def test_get_sources(self):
sources = self.api.get_sources()
for source in sources:
# Assert expected fields are present
assert source.journalist_designation
assert source.uuid
assert source.last_updated
@dastollervey_datasaver
def test_star_add_remove(self):
s = self.api.get_sources()[0]
self.assertTrue(self.api.add_star(s))
self.assertTrue(self.api.remove_star(s))
for source in self.api.get_sources():
if source.uuid == s.uuid:
self.assertFalse(source.is_starred)
@dastollervey_datasaver
def test_get_single_source(self):
s = self.api.get_sources()[0]
# Now we will try to get the same source again
s2 = self.api.get_source(s)
self.assertEqual(s.journalist_designation, s2.journalist_designation)
self.assertEqual(s.uuid, s2.uuid)
@dastollervey_datasaver
def test_get_single_source_from_string(self):
s = self.api.get_sources()[0]
# Now we will try to get the same source again using uuid
s2 = self.api.get_source_from_string(s.uuid)
self.assertEqual(s.journalist_designation, s2.journalist_designation)
self.assertEqual(s.uuid, s2.uuid)
@dastollervey_datasaver
def test_failed_single_source(self):
with self.assertRaises(WrongUUIDError):
self.api.get_source(Source(uuid="not there"))
@dastollervey_datasaver
def test_get_submissions(self):
s = self.api.get_sources()[0]
subs = self.api.get_submissions(s)
for submission in subs:
assert submission.filename
@dastollervey_datasaver
def test_get_submission(self):
# Get a source with submissions
source_uuid = self.api.get_all_submissions()[0].source_uuid
s = self.api.get_source(Source(uuid=source_uuid))
subs = self.api.get_submissions(s)
sub = self.api.get_submission(subs[0])
self.assertEqual(sub.filename, subs[0].filename)
@dastollervey_datasaver
def test_get_submission_from_string(self):
# Get a source with submissions
source_uuid = self.api.get_all_submissions()[0].source_uuid
s = self.api.get_source(Source(uuid=source_uuid))
subs = self.api.get_submissions(s)
sub = self.api.get_submission_from_string(subs[0].uuid, s.uuid)
self.assertEqual(sub.filename, subs[0].filename)
@dastollervey_datasaver
def test_get_wrong_submissions(self):
s = self.api.get_sources()[0]
s.uuid = "rofl-missing"
with self.assertRaises(WrongUUIDError):
self.api.get_submissions(s)
@dastollervey_datasaver
def test_get_all_submissions(self):
subs = self.api.get_all_submissions()
for submission in subs:
assert submission.filename
@dastollervey_datasaver
def test_flag_source(self):
s = self.api.get_sources()[0]
self.assertTrue(self.api.flag_source(s))
# Now we will try to get the same source again
s2 = self.api.get_source(s)
self.assertTrue(s2.is_flagged)
@dastollervey_datasaver
def test_delete_source(self):
number_of_sources_before = len(self.api.get_sources())
s = self.api.get_sources()[0]
self.assertTrue(self.api.delete_source(s))
# Now there should be one less source
sources = self.api.get_sources()
self.assertEqual(len(sources), number_of_sources_before - 1)
@dastollervey_datasaver
def test_delete_source_from_string(self):
number_of_sources_before = len(self.api.get_sources())
s = self.api.get_sources()[0]
self.assertTrue(self.api.delete_source_from_string(s.uuid))
# Now there should be one less source
sources = self.api.get_sources()
self.assertEqual(len(sources), number_of_sources_before - 1)
@dastollervey_datasaver
def test_delete_submission(self):
number_of_submissions_before = len(self.api.get_all_submissions())
subs = self.api.get_all_submissions()
self.assertTrue(self.api.delete_submission(subs[0]))
new_subs = self.api.get_all_submissions()
# We now should have 1 less submission
self.assertEqual(len(new_subs), number_of_submissions_before - 1)
# Let us make sure that sub[0] is not there
for s in new_subs:
self.assertNotEqual(s.uuid, subs[0].uuid)
@dastollervey_datasaver
def test_delete_submission_from_string(self):
number_of_submissions_before = len(self.api.get_all_submissions())
s = self.api.get_sources()[0]
subs = self.api.get_submissions(s)
self.assertTrue(self.api.delete_submission(subs[0]))
new_subs = self.api.get_all_submissions()
# We now should have 1 less submission
self.assertEqual(len(new_subs), number_of_submissions_before - 1)
# Let us make sure that sub[0] is not there
for s in new_subs:
self.assertNotEqual(s.uuid, subs[0].uuid)
@dastollervey_datasaver
def test_get_current_user(self):
user = self.api.get_current_user()
self.assertTrue(user["is_admin"])
self.assertEqual(user["username"], "journalist")
self.assertTrue("first_name" in user)
self.assertTrue("last_name" in user)
@dastollervey_datasaver
def test_error_unencrypted_reply(self):
s = self.api.get_sources()[0]
with self.assertRaises(ReplyError) as err:
self.api.reply_source(s, "hello")
self.assertEqual(err.exception.msg, "bad request")
@dastollervey_datasaver
def test_reply_source(self):
s = self.api.get_sources()[0]
dirname = os.path.dirname(__file__)
with open(os.path.join(dirname, "encrypted_msg.asc")) as fobj:
data = fobj.read()
reply = self.api.reply_source(s, data)
assert isinstance(reply, Reply)
assert reply.uuid
assert reply.filename
@dastollervey_datasaver
def test_reply_source_with_uuid(self):
s = self.api.get_sources()[0]
dirname = os.path.dirname(__file__)
with open(os.path.join(dirname, "encrypted_msg.asc")) as fobj:
data = fobj.read()
msg_uuid = "e467868c-1fbb-4b5e-bca2-87944ea83855"
reply = self.api.reply_source(s, data, msg_uuid)
assert reply.uuid == msg_uuid
@dastollervey_datasaver
def test_download_submission(self):
s = self.api.get_all_submissions()[0]
self.assertFalse(s.is_read)
# We need a temporary directory to download
tmpdir = tempfile.mkdtemp()
_, filepath = self.api.download_submission(s, tmpdir)
# Uncomment the following part only on QubesOS
# for testing against real server.
# now let us read the downloaded file
# with open(filepath, "rb") as fobj:
# fobj.read()
# Now the submission should have is_read as True.
s = self.api.get_submission(s)
self.assertTrue(s.is_read)
# Let us remove the temporary directory
shutil.rmtree(tmpdir)
@dastollervey_datasaver
def test_get_replies_from_source(self):
s = self.api.get_sources()[0]
replies = self.api.get_replies_from_source(s)
self.assertEqual(len(replies), NUM_REPLIES_PER_SOURCE)
@dastollervey_datasaver
def test_get_reply_from_source(self):
s = self.api.get_sources()[0]
replies = self.api.get_replies_from_source(s)
reply = replies[0]
r = self.api.get_reply_from_source(s, reply.uuid)
self.assertEqual(reply.filename, r.filename)
self.assertEqual(reply.size, r.size)
self.assertEqual(reply.reply_url, r.reply_url)
self.assertEqual(reply.journalist_username, r.journalist_username)
@dastollervey_datasaver
def test_get_all_replies(self):
num_sources = len(self.api.get_sources())
replies = self.api.get_all_replies()
self.assertEqual(len(replies), NUM_REPLIES_PER_SOURCE * num_sources)
@dastollervey_datasaver
def test_download_reply(self):
r = self.api.get_all_replies()[0]
# We need a temporary directory to download
tmpdir = tempfile.mkdtemp()
_, filepath = self.api.download_reply(r, tmpdir)
# Uncomment the following part only on QubesOS
# for testing against real server.
# now let us read the downloaded file
# with open(filepath, "rb") as fobj:
# fobj.read()
# Let us remove the temporary directory
shutil.rmtree(tmpdir)
@dastollervey_datasaver
def test_delete_reply(self):
r = self.api.get_all_replies()[0]
number_of_replies_before = len(self.api.get_all_replies())
self.assertTrue(self.api.delete_reply(r))
# We deleted one, so there must be 1 less reply now
self.assertEqual(len(self.api.get_all_replies()), number_of_replies_before - 1)
@dastollervey_datasaver
def test_logout(self):
r = self.api.logout()
self.assertTrue(r)
if os.path.exists("login.txt"):
os.unlink("login.txt")
if os.path.exists("testtoken.json"):
os.unlink("testtoken.json")
with open("logout.txt", "w") as fobj:
fobj.write("out")
def test_request_timeout(mocker):
class MockedPopen:
def __init__(self, *nargs, **kwargs) -> None:
self.stdin = mocker.MagicMock()
def communicate(self, *nargs, **kwargs) -> None:
raise TimeoutExpired(["mock"], 123)
api = API("mock", "mock", "mock", "mock", proxy=True)
mocker.patch("sdclientapi.Popen", MockedPopen)
with pytest.raises(RequestTimeoutError):
api.authenticate()
def test_download_reply_timeout(mocker):
class MockedPopen:
def __init__(self, *nargs, **kwargs) -> None:
self.stdin = mocker.MagicMock()
def communicate(self, *nargs, **kwargs) -> None:
raise TimeoutExpired(["mock"], 123)
api = API("mock", "mock", "mock", "mock", proxy=True)
mocker.patch("sdclientapi.Popen", MockedPopen)
with pytest.raises(RequestTimeoutError):
r = Reply(uuid="humanproblem", filename="secret.txt")
api.download_reply(r)
def test_download_submission_timeout(mocker):
class MockedPopen:
def __init__(self, *nargs, **kwargs) -> None:
self.stdin = mocker.MagicMock()
def communicate(self, *nargs, **kwargs) -> None:
raise TimeoutExpired(["mock"], 123)
api = API("mock", "mock", "mock", "mock", proxy=True)
mocker.patch("sdclientapi.Popen", MockedPopen)
with pytest.raises(RequestTimeoutError):
s = Submission(uuid="climateproblem")
api.download_submission(s)
def test_download_get_sources_error_request_timeout(mocker):
api = API("mock", "mock", "mock", "mock", True)
mocker.patch(
"sdclientapi.json_query",
return_value=(
json.dumps(
{
"body": json.dumps({"error": "wah"}),
"status": http.HTTPStatus.GATEWAY_TIMEOUT,
"headers": "foo",
}
)
),
)
with pytest.raises(RequestTimeoutError):
api.get_sources()
def test_filename_key_not_in_download_response(mocker):
api = API("mock", "mock", "mock", "mock", True)
s = Submission(uuid="foobar")
mocker.patch(
"sdclientapi.json_query",
return_value=(
json.dumps({"body": json.dumps({"error": "wah"}), "status": 200, "headers": "foo"})
),
)
with pytest.raises(BaseError):
api.download_submission(s)