forked from freedomofpress/securedrop-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_api.py
299 lines (235 loc) · 10.4 KB
/
test_api.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
import os
import pyotp
import shutil
import tempfile
import time
import unittest
import vcr
from sdclientapi import API
from sdclientapi.sdlocalobjects import (
BaseError,
WrongUUIDError,
ReplyError,
Reply,
Source,
)
from utils import load_auth, save_auth
NUM_REPLIES_PER_SOURCE = 2
class TestAPI(unittest.TestCase):
@vcr.use_cassette("data/test-setup.yml")
def setUp(self):
self.totp = pyotp.TOTP("JHCOGO7VCER3EJ4L")
self.username = "journalist"
self.password = "correct horse battery staple profanity oil chewy"
self.server = "http://127.0.0.1:8081/"
self.api = API(self.server, self.username, self.password, str(self.totp.now()))
for i in range(3):
try:
self.api.authenticate()
except BaseError:
token = load_auth()
if token:
self.api.token = token
self.api.update_auth_header()
break
time.sleep(31)
save_auth(self.api.token)
break
def test_api_auth(self):
self.assertTrue(self.api.token)
@vcr.use_cassette("data/test-get-sources.yml")
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
@vcr.use_cassette("data/test-star-add-remove.yml")
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)
@vcr.use_cassette("data/test-get-single-source.yml")
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)
@vcr.use_cassette("data/test-get-single-source.yml")
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)
@vcr.use_cassette("data/test-failed-single-source.yml")
def test_failed_single_source(self):
with self.assertRaises(WrongUUIDError):
self.api.get_source(Source(uuid="not there"))
@vcr.use_cassette("data/test-get-submissions.yml")
def test_get_submissions(self):
s = self.api.get_sources()[0]
subs = self.api.get_submissions(s)
for submission in subs:
assert submission.filename
@vcr.use_cassette("data/test-get-submission.yml")
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)
@vcr.use_cassette("data/test-get-submission.yml")
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)
@vcr.use_cassette("data/test-get-wrong-submissions.yml")
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)
@vcr.use_cassette("data/test-get-all-submissions.yml")
def test_get_all_submissions(self):
subs = self.api.get_all_submissions()
for submission in subs:
assert submission.filename
@vcr.use_cassette("data/test-flag-source.yml")
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)
@vcr.use_cassette("data/test-delete-source.yml")
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)
@vcr.use_cassette("data/test-delete-source.yml")
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)
@vcr.use_cassette("data/test-delete-submission.yml")
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)
@vcr.use_cassette("data/test-delete-submission-from-string.yml")
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)
@vcr.use_cassette("data/test-get-current-user.yml")
def test_get_current_user(self):
user = self.api.get_current_user()
self.assertTrue(user["is_admin"])
self.assertEqual(user["username"], "journalist")
@vcr.use_cassette("data/test-error-unencrypted-reply.yml")
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, "You must encrypt replies client side")
@vcr.use_cassette("data/test-reply-source.yml")
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
@vcr.use_cassette("data/test-reply-source-with-uuid.yml")
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
@vcr.use_cassette("data/test-download-submission.yml")
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)
# 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)
@vcr.use_cassette("data/test-get-replies-from-source.yml")
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)
@vcr.use_cassette("data/test-get-reply-from-source.yml")
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)
@vcr.use_cassette("data/test-get-all-replies.yml")
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)
@vcr.use_cassette("data/test-download-reply.yml")
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)
# now let us read the downloaded file
with open(filepath, "rb") as fobj:
fobj.read()
# Let us remove the temporary directory
shutil.rmtree(tmpdir)
@vcr.use_cassette("data/test-delete-reply.yml")
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)