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