-
-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy pathtest_server.py
319 lines (282 loc) · 13.2 KB
/
test_server.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
from typing import Any, Dict
import pytest
import flask
from flask_api.status import HTTP_405_METHOD_NOT_ALLOWED
from click.testing import CliRunner
from buku import FetchResult
from bukuserver import server
from bukuserver.response import Response
from bukuserver.server import get_bool_from_env_var
from tests.util import mock_http, mock_fetch
def assert_response(response, exp_res: Response, data: Dict[str, Any] = None):
assert response.status_code == exp_res.status_code
assert response.get_json() == exp_res.json(data=data)
@pytest.mark.parametrize(
'data, exp_json', [
[None, {'status': 0, 'message': 'Success.'}],
[{}, {'status': 0, 'message': 'Success.'}],
[{'key': 'value'}, {'status': 0, 'message': 'Success.', 'key': 'value'}],
]
)
def test_response_json(data, exp_json):
assert Response.SUCCESS.json(data=data) == exp_json
@pytest.mark.parametrize(
'args,word',
[
('--help', 'bukuserver'),
('--version', 'buku')
]
)
def test_cli(args, word):
runner = CliRunner()
result = runner.invoke(server.cli, [args])
assert result.exit_code == 0
assert word in result.output
@pytest.fixture
def client(tmp_path):
test_db = tmp_path / 'test.db'
app = server.create_app(test_db.as_posix())
client = app.test_client()
return client
def test_home(client):
rd = client.get('/')
assert rd.status_code == 200
assert not flask.g.bukudb.get_rec_all()
@pytest.mark.parametrize(
'method, url, exp_res, data', [
['get', '/api/tags', Response.SUCCESS, {'tags': []}],
['get', '/api/bookmarks', Response.SUCCESS, {'bookmarks': []}],
['get', '/api/bookmarks/search', Response.SUCCESS, {'bookmarks': []}],
['post', '/api/bookmarks/refresh', Response.FAILURE, None]
]
)
def test_api_empty_db(client, method, url, exp_res, data):
rd = getattr(client, method)(url)
assert_response(rd, exp_res, data)
@pytest.mark.parametrize(
'url, methods', [
['api/tags', ['post', 'put', 'delete']],
['/api/tags/tag1', ['post']],
['api/bookmarks', ['put']],
['/api/bookmarks/1', ['post']],
['/api/bookmarks/refresh', ['get', 'put', 'delete']],
['api/bookmarks/1/refresh', ['get', 'put', 'delete']],
['api/bookmarks/1/tiny', ['post', 'put', 'delete']],
['/api/bookmarks/1/2', ['post']],
]
)
def test_not_allowed(client, url, methods):
for method in methods:
rd = getattr(client, method)(url)
assert rd.status_code == HTTP_405_METHOD_NOT_ALLOWED
@pytest.mark.parametrize(
'method, url, json, exp_res', [
['get', '/api/tags/tag1', None, Response.TAG_NOT_FOUND],
['put', '/api/tags/tag1', {'tags': ['tag2']}, Response.TAG_NOT_FOUND],
['delete', '/api/tags/tag1', None, Response.TAG_NOT_FOUND],
['get', '/api/bookmarks/1', None, Response.BOOKMARK_NOT_FOUND],
['put', '/api/bookmarks/1', {'title': 'none'}, Response.FAILURE],
['delete', '/api/bookmarks/1', None, Response.FAILURE],
['post', '/api/bookmarks/1/refresh', None, Response.FAILURE],
['get', '/api/bookmarks/1/tiny', None, Response.FAILURE],
['get', '/api/bookmarks/1/2', None, Response.RANGE_NOT_VALID],
['put', '/api/bookmarks/1/2', {1: {'title': 'one'}, 2: {'title': 'two'}}, Response.RANGE_NOT_VALID],
['delete', '/api/bookmarks/1/2', None, Response.RANGE_NOT_VALID],
]
)
def test_invalid_id(client, method, url, json, exp_res):
rd = getattr(client, method)(url, json=json)
assert_response(rd, exp_res)
def test_tag_api(client):
url = 'http://google.com'
with mock_fetch(title='Google'):
rd = client.post('/api/bookmarks', json={'url': url, 'tags': ['tag1', 'TAG2']})
assert_response(rd, Response.SUCCESS)
rd = client.get('/api/tags')
assert_response(rd, Response.SUCCESS, {'tags': ['tag1', 'tag2']})
rd = client.get('/api/tags/tag1')
assert_response(rd, Response.SUCCESS, {'name': 'tag1', 'usage_count': 1})
rd = client.put('/api/tags/tag1', json={'tags': 'string'})
assert_response(rd, Response.INPUT_NOT_VALID, data={'errors': {'tags': 'List of tags expected.'}})
for json in [{}, {'tags': None}, {'tags': ''}, {'tags':[]}]:
rd = client.put('/api/tags/tag1', json={'tags': []})
assert_response(rd, Response.INPUT_NOT_VALID, data={'errors': {'tags': [['This field is required.']]}})
rd = client.put('/api/tags/tag1', json={'tags': ['ok', '', None]})
errors = {'tags': [[], ['This field is required.'], ['This field is required.']]}
assert_response(rd, Response.INPUT_NOT_VALID, data={'errors': errors})
rd = client.put('/api/tags/tag1', json={'tags': ['one,two', 3,]})
errors = {'tags': [['Tag must not contain delimiter \",\".'], ['Tag must be a string.']]}
assert_response(rd, Response.INPUT_NOT_VALID, data={'errors': errors})
rd = client.put('/api/tags/tag1', json={'tags': ['tag3', 'TAG 4']})
assert_response(rd, Response.SUCCESS)
rd = client.get('/api/tags')
assert_response(rd, Response.SUCCESS, {'tags': ['tag 4', 'tag2', 'tag3']})
rd = client.put('/api/tags/tag 4', json={'tags': ['tag5']})
assert_response(rd, Response.SUCCESS)
rd = client.get('/api/tags')
assert_response(rd, Response.SUCCESS, {'tags': ['tag2', 'tag3', 'tag5']})
rd = client.delete('/api/tags/tag3')
assert_response(rd, Response.SUCCESS)
rd = client.delete('/api/tags/tag3')
assert_response(rd, Response.TAG_NOT_FOUND)
rd = client.delete('/api/tags/tag,2')
assert_response(rd, Response.TAG_NOT_VALID)
rd = client.get('/api/bookmarks/1')
assert_response(rd, Response.SUCCESS, {'description': '', 'tags': ['tag2', 'tag5'], 'title': 'Google', 'url': url})
def test_bookmark_api(client):
url = 'http://google.com'
rd = client.post('/api/bookmarks', json={})
errors = {'url': ['This field is required.']}
assert_response(rd, Response.INPUT_NOT_VALID, data={'errors': errors})
with mock_fetch(title='Google'):
rd = client.post('/api/bookmarks', json={'url': url})
assert_response(rd, Response.SUCCESS)
rd = client.post('/api/bookmarks', json={'url': url})
assert_response(rd, Response.FAILURE)
rd = client.get('/api/bookmarks')
assert_response(rd, Response.SUCCESS, {'bookmarks': [{'description': '', 'tags': [], 'title': 'Google', 'url': url}]})
rd = client.get('/api/bookmarks/1')
assert_response(rd, Response.SUCCESS, {'description': '', 'tags': [], 'title': 'Google', 'url': url})
rd = client.put('/api/bookmarks/1', json={'tags': 'not a list'})
assert_response(rd, Response.INPUT_NOT_VALID, data={'errors': {'tags': 'List of tags expected.'}})
rd = client.put('/api/bookmarks/1', json={'tags': ['tag1', 'tag2']})
assert_response(rd, Response.SUCCESS)
with mock_fetch(title='Google'):
rd = client.put('/api/bookmarks/1', json={})
assert_response(rd, Response.SUCCESS)
rd = client.get('/api/bookmarks/1')
assert_response(rd, Response.SUCCESS, {'description': '', 'tags': ['tag1', 'tag2'], 'title': 'Google', 'url': url})
rd = client.put('/api/bookmarks/1', json={'tags': [], 'description': 'Description'})
assert_response(rd, Response.SUCCESS)
rd = client.get('/api/bookmarks/1')
assert_response(rd, Response.SUCCESS, {'description': 'Description', 'tags': [], 'title': 'Google', 'url': url})
@pytest.mark.parametrize('d_url', ['/api/bookmarks', '/api/bookmarks/1'])
def test_bookmark_api_delete(client, d_url):
url = 'http://google.com'
rd = client.post('/api/bookmarks', json={'url': url, 'fetch': False})
assert_response(rd, Response.SUCCESS)
rd = client.delete(d_url)
assert_response(rd, Response.SUCCESS)
@pytest.mark.parametrize('api_url', ['/api/bookmarks/refresh', '/api/bookmarks/1/refresh'])
def test_refresh_bookmark(client, api_url):
url = 'http://google.com'
with mock_fetch(title='Google'):
rd = client.post('/api/bookmarks', json={'url': url})
assert_response(rd, Response.SUCCESS)
rd = client.post(api_url)
assert_response(rd, Response.SUCCESS)
rd = client.get('/api/bookmarks/1')
assert_response(rd, Response.SUCCESS, {'description': '', 'tags': [], 'title': 'Google', 'url': url})
@pytest.mark.parametrize(
'url, title, exp_res, tiny', [
['http://google.com', 'Google', Response.SUCCESS, 'http://tny.im/2'],
['chrome://bookmarks/', '', Response.FAILURE, None],
])
def test_get_tiny_url(client, url, title, exp_res, tiny):
with mock_fetch(title=title):
rd = client.post('/api/bookmarks', json={'url': url})
assert_response(rd, Response.SUCCESS)
with mock_http(body=tiny, status=(200 if tiny else 400)):
rd = client.get('/api/bookmarks/1/tiny')
assert_response(rd, exp_res, tiny and {'url': tiny})
@pytest.mark.parametrize('kwargs, kwmock, exp_res, data', [
[
{'data': {'url': 'http://google.com'}},
{'title': 'Google', 'fetch_status': 200},
Response.SUCCESS,
{'bad url': 0, 'recognized mime': 0, 'tags': '', 'title': 'Google'}
],
[{}, {}, Response.FAILURE, None],
[
{'data': {'url': 'chrome://bookmarks/'}},
{'bad': True},
Response.SUCCESS,
{'bad url': 1, 'recognized mime': 0, 'tags': '', 'title': ''}
],
])
@pytest.mark.parametrize('endpoint', ['/api/fetch_data', '/api/network_handle'])
def test_fetch_data(client, endpoint, kwargs, kwmock, exp_res, data):
with mock_fetch(**kwmock):
rd = client.post(endpoint, **kwargs)
assert rd.status_code == exp_res.status_code
rd_json = rd.get_json()
rd_json.pop('description', None)
if endpoint == '/api/fetch_data':
data = data and FetchResult(kwargs['data']['url'], **kwmock)._asdict()
assert rd_json == exp_res.json(data=data)
def test_bookmark_range_api(client):
bookmarks = [('http://google.com', 'Google'),
('http://example.com', 'Example Domain')]
for url, title in bookmarks:
with mock_fetch(title=title):
rd = client.post('/api/bookmarks', json={'url': url})
assert_response(rd, Response.SUCCESS)
rd = client.put('/api/bookmarks/1/2', json={
'1': {'tags': ['tag1 A', 'tag1 B', 'tag1 C']},
'2': {'tags': ['tag2']}
})
assert_response(rd, Response.SUCCESS)
rd = client.get('/api/bookmarks/1/2')
assert_response(rd, Response.SUCCESS, {'bookmarks': {
'1': {'description': '', 'tags': ['tag1 a', 'tag1 b', 'tag1 c'], 'title': 'Google', 'url': 'http://google.com'},
'2': {'description': '', 'tags': ['tag2',], 'title': 'Example Domain', 'url': 'http://example.com'}}})
rd = client.put('/api/bookmarks/1/2', json={
'1': {'title': 'Bookmark 1', 'tags': ['tag1 C', 'tag1 A'], 'del_tags': True},
'2': {'title': 'Bookmark 2', 'tags': ['-', 'tag2'], 'del_tags': False}
})
assert_response(rd, Response.SUCCESS)
rd = client.get('/api/bookmarks/1/2')
assert_response(rd, Response.SUCCESS, {'bookmarks': {
'1': {'description': '', 'tags': ['tag1 b'], 'title': 'Bookmark 1', 'url': 'http://google.com'},
'2': {'description': '', 'tags': ['-', 'tag2',], 'title': 'Bookmark 2', 'url': 'http://example.com'}}})
rd = client.put('/api/bookmarks/2/1', json={})
assert_response(rd, Response.RANGE_NOT_VALID)
rd = client.put('/api/bookmarks/1/2', json={})
assert_response(rd, Response.INPUT_NOT_VALID, data={
'errors': {
'1': 'Input required.',
'2': 'Input required.'
}
})
rd = client.put('/api/bookmarks/1/2', json={'1': {'tags': []}})
assert_response(rd, Response.INPUT_NOT_VALID, data={'errors': {'2': 'Input required.'}})
rd = client.put('/api/bookmarks/1/2', json={
'1': {'tags': ['ok', 'with,delim']},
'2': {'tags': 'string'},
})
assert_response(rd, Response.INPUT_NOT_VALID, data={
'errors': {
'1': {'tags': [[], ['Tag must not contain delimiter \",\".']]},
'2': {'tags': 'List of tags expected.'}
}
})
rd = client.get('/api/bookmarks/2/1')
assert_response(rd, Response.RANGE_NOT_VALID)
rd = client.delete('/api/bookmarks/1/2')
assert_response(rd, Response.SUCCESS)
rd = client.get('/api/bookmarks')
assert_response(rd, Response.SUCCESS, {'bookmarks': []})
def test_bookmark_search(client):
with mock_fetch(title='Google'):
rd = client.post('/api/bookmarks', json={'url': 'http://google.com'})
assert_response(rd, Response.SUCCESS)
rd = client.get('/api/bookmarks/search', query_string={'keywords': ['google']})
assert_response(rd, Response.SUCCESS, {'bookmarks': [
{'description': '', 'id': 1, 'tags': [], 'title': 'Google', 'url': 'http://google.com'}]})
rd = client.delete('/api/bookmarks/search', data={'keywords': ['google']})
assert_response(rd, Response.SUCCESS)
rd = client.get('/api/bookmarks')
assert_response(rd, Response.SUCCESS, {'bookmarks': []})
@pytest.mark.parametrize('env_val, exp_val', [
['true', True],
['false', False],
['0', False],
['1', True],
[None, True],
['random', True]
])
def test_get_bool_from_env_var(monkeypatch, env_val, exp_val):
key = 'BUKUSERVER_TEST'
if env_val is not None:
monkeypatch.setenv(key, env_val)
assert get_bool_from_env_var(key, True) == exp_val