1
|
|
|
"""Download endpoint unit and integration tests.""" |
2
|
|
|
|
3
|
|
|
from unittest import mock |
4
|
|
|
from io import BytesIO |
5
|
|
|
from ddt import data, ddt |
6
|
|
|
from falcon.testing import TestCase |
7
|
|
|
from couchdb_download_token import api |
8
|
|
|
from .helpers import ServerPatch |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
@ddt |
12
|
|
|
@mock.patch('couchdb_download_token.connection', new_callable=ServerPatch) |
13
|
|
|
class TestDownloadResource(TestCase): |
14
|
|
|
|
15
|
|
|
def setUp(self): |
16
|
|
|
self.api = api |
17
|
|
|
|
18
|
|
|
def simulate_simple_query(self, token='12345', status=None, |
19
|
|
|
**kwargs): |
20
|
|
|
res = self.simulate_get('/db/doc/file.txt', |
21
|
|
|
query_string='token=%s' % token, |
22
|
|
|
**kwargs) |
23
|
|
|
if status is not None: |
24
|
|
|
self.assertEqual(res.status_code, status) |
25
|
|
|
return res |
26
|
|
|
|
27
|
|
|
def test_gets_correct_attachment(self, server_patch): |
28
|
|
|
server_patch.set_document() |
29
|
|
|
res = self.simulate_simple_query(status=200) |
30
|
|
|
server_patch.database.get_attachment.assert_called_with('doc', |
31
|
|
|
'file.txt') |
32
|
|
|
|
33
|
|
|
def test_403_if_token_is_invalid(self, server_patch): |
34
|
|
|
server_patch.set_document() |
35
|
|
|
res = self.simulate_simple_query('54321', 403) |
36
|
|
|
|
37
|
|
|
@data(None, 'other=asd') |
38
|
|
|
def test_403_with_no_token_in_query(self, query_string, server_patch): |
39
|
|
|
server_patch.set_document() |
40
|
|
|
res = self.simulate_get('/db/doc/file.txt', query_string=query_string) |
41
|
|
|
self.assertEqual(res.status_code, 403) |
42
|
|
|
|
43
|
|
|
@data(None, 'token=12345') |
44
|
|
|
def test_403_if_no_or_null_token(self, query_string, server_patch): |
45
|
|
|
server_patch.set_document(download_token=None) |
46
|
|
|
res = self.simulate_get('/db/doc/file.txt', query_string=query_string) |
47
|
|
|
self.assertEqual(res.status_code, 403) |
48
|
|
|
|
49
|
|
|
server_patch.reset_mock() |
50
|
|
|
del server_patch.base_document['download_token'] |
51
|
|
|
server_patch.set_document() |
52
|
|
|
|
53
|
|
|
def test_403_if_invalid_token_and_unexistent_attachment(self, |
54
|
|
|
server_patch): |
55
|
|
|
server_patch.database.get_attachment.return_value = None |
56
|
|
|
server_patch.set_document() |
57
|
|
|
self.simulate_simple_query('bad', 403) |
58
|
|
|
|
59
|
|
|
def test_404_if_valid_token_and_unexistent_attachment(self, |
60
|
|
|
server_patch): |
61
|
|
|
server_patch.database.get_attachment.return_value = None |
62
|
|
|
server_patch.set_document() |
63
|
|
|
self.simulate_simple_query(status=404) |
64
|
|
|
|
65
|
|
|
def test_correct_document_body(self, server_patch): |
66
|
|
|
server_patch.set_document() |
67
|
|
|
file_contents = bytes(bytearray(range(256))) # Test encoding issues |
68
|
|
|
with BytesIO(file_contents) as fp: |
69
|
|
|
server_patch.database.get_attachment.return_value = fp |
70
|
|
|
res = self.simulate_simple_query() |
71
|
|
|
self.assertEqual(res.content, file_contents) |
72
|
|
|
|
73
|
|
|
def test_correct_content_type(self, server_patch): |
74
|
|
|
server_patch.set_document(_attachments={ |
75
|
|
|
"file.txt": {"content_type": "custom/type"}}) |
76
|
|
|
res = self.simulate_simple_query() |
77
|
|
|
self.assertEqual(res.headers['Content-Type'], 'custom/type') |
78
|
|
|
|