buku/tests/test_server.py

237 lines
8.6 KiB
Python
Raw Normal View History

2019-05-16 08:01:10 -05:00
import json
2019-05-07 07:14:36 -05:00
import pytest
import flask
from click.testing import CliRunner
from bukuserver import server
2019-05-14 02:44:14 -05:00
from bukuserver.response import response_template
@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
2019-05-07 07:14:36 -05:00
2019-05-13 13:47:29 -05:00
@pytest.fixture
def client(tmp_path):
2019-05-07 07:14:36 -05:00
test_db = tmp_path / 'test.db'
app = server.create_app(test_db.as_posix())
client = app.test_client()
2019-05-13 13:47:29 -05:00
return client
def test_home(client):
2019-05-07 07:14:36 -05:00
rd = client.get('/')
assert rd.status_code == 200
assert not flask.g.bukudb.get_rec_all()
2019-05-13 13:47:29 -05:00
@pytest.mark.parametrize(
'url, exp_res', [
['/api/tags', {'tags': []}],
['/api/bookmarks', {'bookmarks': []}],
['/api/bookmarks/search', {'bookmarks': []}],
['/api/bookmarks/refresh', response_template['failure']]
2019-05-13 13:47:29 -05:00
]
)
def test_api_empty_db(client, url, exp_res):
if url == '/api/bookmarks/refresh':
rd = client.post(url)
assert rd.status_code == 400
else:
rd = client.get(url)
assert rd.status_code == 200
2019-05-13 13:47:29 -05:00
assert rd.get_json() == exp_res
2019-05-13 15:56:03 -05:00
@pytest.mark.parametrize(
2019-05-14 00:15:37 -05:00
'url, exp_res, status_code, method', [
['/api/tags/1', {'message': 'This resource does not exist.'}, 404, 'get'],
2019-05-14 02:44:14 -05:00
['/api/tags/1', response_template['failure'], 400, 'put'],
['/api/bookmarks/1', response_template['failure'], 400, 'get'],
['/api/bookmarks/1', response_template['failure'], 400, 'put'],
['/api/bookmarks/1', response_template['failure'], 400, 'delete'],
['/api/bookmarks/1/refresh', response_template['failure'], 400, 'post'],
2019-05-14 05:57:05 -05:00
['/api/bookmarks/1/tiny', response_template['failure'], 400, 'get'],
2019-05-16 08:01:10 -05:00
['/api/bookmarks/1/2', response_template['failure'], 400, 'get'],
['/api/bookmarks/1/2', response_template['failure'], 400, 'put'],
['/api/bookmarks/1/2', response_template['failure'], 400, 'delete'],
2019-05-13 15:56:03 -05:00
]
)
2019-05-14 00:15:37 -05:00
def test_invalid_id(client, url, exp_res, status_code, method):
rd = getattr(client, method)(url)
2019-05-13 15:56:03 -05:00
assert rd.status_code == status_code
assert rd.get_json() == exp_res
2019-05-14 01:05:50 -05:00
def test_tag_api(client):
url = 'http://google.com'
rd = client.post('/api/bookmarks', data={'url': url, 'tags': 'tag1,tag2'})
assert rd.status_code == 200
2019-05-14 02:44:14 -05:00
assert rd.get_json() == response_template['success']
2019-05-14 01:05:50 -05:00
rd = client.get('/api/tags')
assert rd.status_code == 200
assert rd.get_json() == {'tags': ['tag1', 'tag2']}
rd = client.get('/api/tags/tag1')
assert rd.status_code == 200
assert rd.get_json() == {'name': 'tag1', 'usage_count': 1}
rd = client.put('/api/tags/tag1', data={'tags': 'tag3,tag4'})
assert rd.status_code == 200
2019-05-14 02:44:14 -05:00
assert rd.get_json() == response_template['success']
2019-05-14 01:05:50 -05:00
rd = client.get('/api/tags')
assert rd.status_code == 200
assert rd.get_json() == {'tags': ['tag2', 'tag3 tag4']}
rd = client.put('/api/tags/tag2', data={'tags': 'tag5'})
assert rd.status_code == 200
2019-05-14 02:44:14 -05:00
assert rd.get_json() == response_template['success']
2019-05-14 01:05:50 -05:00
rd = client.get('/api/tags')
assert rd.status_code == 200
assert rd.get_json() == {'tags': ['tag3 tag4', 'tag5']}
rd = client.get('/api/bookmarks/1')
assert rd.status_code == 200
assert rd.get_json() == {
'description': '', 'tags': ['tag3 tag4', 'tag5'], 'title': '',
'url': 'http://google.com'}
2019-05-14 00:15:37 -05:00
def test_bookmark_api(client):
2019-05-13 15:56:03 -05:00
url = 'http://google.com'
rd = client.post('/api/bookmarks', data={'url': url})
assert rd.status_code == 200
2019-05-14 02:44:14 -05:00
assert rd.get_json() == response_template['success']
2019-05-13 15:56:03 -05:00
rd = client.post('/api/bookmarks', data={'url': url})
assert rd.status_code == 400
2019-05-14 02:44:14 -05:00
assert rd.get_json() == response_template['failure']
rd = client.get('/api/bookmarks')
2019-05-13 15:56:03 -05:00
assert rd.status_code == 200
2019-05-14 02:44:14 -05:00
assert rd.get_json() == {'bookmarks': [{
'description': '', 'tags': [], 'title': '', 'url': 'http://google.com'}]}
2019-05-13 15:56:03 -05:00
rd = client.get('/api/bookmarks/1')
assert rd.status_code == 200
assert rd.get_json() == {
'description': '', 'tags': [], 'title': '', 'url': 'http://google.com'}
2019-05-14 02:44:14 -05:00
rd = client.put('/api/bookmarks/1', data={'tags': [',tag1,tag2,']})
assert rd.status_code == 200
assert rd.get_json() == response_template['success']
rd = client.get('/api/bookmarks/1')
assert rd.status_code == 200
assert rd.get_json() == {
'description': '', 'tags': ['tag1', 'tag2'], 'title': '', 'url': 'http://google.com'}
2019-05-14 01:35:47 -05:00
@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', data={'url': url})
assert rd.status_code == 200
assert rd.get_json() == response_template['success']
rd = client.delete(d_url)
assert rd.status_code == 200
assert rd.get_json() == response_template['success']
2019-05-14 04:54:11 -05:00
@pytest.mark.parametrize('api_url', ['/api/bookmarks/refresh', '/api/bookmarks/1/refresh'])
def test_refresh_bookmark(client, api_url):
url = 'http://google.com'
rd = client.post('/api/bookmarks', data={'url': url})
assert rd.status_code == 200
assert rd.get_json() == response_template['success']
rd = client.post(api_url)
assert rd.status_code == 200
assert rd.get_json() == response_template['success']
rd = client.get('/api/bookmarks/1')
assert rd.status_code == 200
2019-05-16 09:23:54 -05:00
json_data = rd.get_json()
json_data.pop('description')
assert json_data == {'tags': [], 'title': 'Google', 'url': 'http://google.com'}
2019-05-14 05:57:05 -05:00
@pytest.mark.parametrize(
'url, exp_res, status_code', [
['http://google.com', {'url': 'http://tny.im/2'}, 200],
['chrome://bookmarks/', response_template['failure'], 400],
])
def test_get_tiny_url(client, url, exp_res, status_code):
rd = client.post('/api/bookmarks', data={'url': url})
assert rd.status_code == 200
assert rd.get_json() == response_template['success']
rd = client.get('/api/bookmarks/1/tiny')
assert rd.status_code == status_code
assert rd.get_json() == exp_res
2019-05-14 06:49:52 -05:00
@pytest.mark.parametrize('kwargs, status_code, exp_res', [
[
dict(data={'url': 'http://google.com'}),
200,
2019-05-14 07:07:09 -05:00
{
2019-05-16 09:23:54 -05:00
'bad url': 0, 'recognized mime': 0,
'tags': None, 'title': 'Google'}
2019-05-14 06:49:52 -05:00
],
[{}, 400, response_template['failure']],
[
dict(data={'url': 'chrome://bookmarks/'}),
200,
2019-05-14 07:07:09 -05:00
{
2019-05-16 09:23:54 -05:00
'bad url': 1, 'recognized mime': 0,
'tags': None, 'title': None}
2019-05-14 06:49:52 -05:00
],
])
def test_network_handle(client, kwargs, status_code, exp_res):
rd = client.post('/api/network_handle', **kwargs)
assert rd.status_code == status_code
2019-05-16 09:23:54 -05:00
rd_json = rd.get_json()
rd_json.pop('description', None)
assert rd_json == exp_res
2019-05-16 08:01:10 -05:00
def test_bookmark_range_api(client):
status_code = 200
kwargs_list = [
dict(data={'url': 'http://google.com'}),
dict(data={'url': 'http://example.com'})]
for kwargs in kwargs_list:
rd = client.post('/api/bookmarks', **kwargs)
assert rd.status_code == status_code
rd = client.get('/api/bookmarks/1/2')
assert rd.status_code == status_code
assert rd.get_json() == {
'bookmarks': {
'1': {'description': '', 'tags': [], 'title': '', 'url': 'http://google.com'},
'2': {'description': '', 'tags': [], 'title': '', 'url': 'http://example.com'}}}
put_data = json.dumps({1: {'tags': 'tag1'}, 2: {'tags': 'tag2'}})
headers = {'content-type': 'application/json'}
rd = client.put('/api/bookmarks/1/2', data=put_data, headers=headers)
assert rd.status_code == status_code
assert rd.get_json() == response_template['success']
rd = client.delete('/api/bookmarks/1/2')
assert rd.status_code == status_code
assert rd.get_json() == response_template['success']
rd = client.get('/api/bookmarks')
assert rd.get_json() == {'bookmarks': []}
2019-05-16 08:40:29 -05:00
def test_bookmark_search(client):
status_code = 200
rd = client.post('/api/bookmarks', data={'url': 'http://google.com'})
assert rd.status_code == status_code
assert rd.get_json() == response_template['success']
rd = client.get('/api/bookmarks/search', query_string={'keywords': ['google']})
assert rd.status_code == status_code
assert rd.get_json() == {'bookmarks': [
{'description': '', 'id': 1, 'tags': [], 'title': '', 'url': 'http://google.com'}]}
rd = client.delete('/api/bookmarks/search', data={'keywords': ['google']})
assert rd.status_code == status_code
assert rd.get_json() == response_template['success']
rd = client.get('/api/bookmarks')
assert rd.get_json() == {'bookmarks': []}