"Fossies" - the Fresh Open Source Software Archive

Member "buku-4.3/tests/test_server.py" (31 Jan 2020, 8849 Bytes) of package /linux/privat/buku-4.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 import json
    2 
    3 import pytest
    4 import flask
    5 from click.testing import CliRunner
    6 
    7 from bukuserver import server
    8 from bukuserver.response import response_template
    9 
   10 @pytest.mark.parametrize(
   11     'args,word',
   12     [
   13         ('--help', 'bukuserver'),
   14         ('--version', 'buku')
   15     ]
   16 )
   17 def test_cli(args, word):
   18     runner = CliRunner()
   19     result = runner.invoke(server.cli, [args])
   20     assert result.exit_code == 0
   21     assert word in result.output
   22 
   23 
   24 @pytest.fixture
   25 def client(tmp_path):
   26     test_db = tmp_path / 'test.db'
   27     app = server.create_app(test_db.as_posix())
   28     client = app.test_client()
   29     return client
   30 
   31 
   32 def test_home(client):
   33     rd = client.get('/')
   34     assert rd.status_code == 200
   35     assert not flask.g.bukudb.get_rec_all()
   36 
   37 
   38 @pytest.mark.parametrize(
   39     'url, exp_res', [
   40         ['/api/tags', {'tags': []}],
   41         ['/api/bookmarks', {'bookmarks': []}],
   42         ['/api/bookmarks/search', {'bookmarks': []}],
   43         ['/api/bookmarks/refresh', response_template['failure']]
   44     ]
   45 )
   46 def test_api_empty_db(client, url, exp_res):
   47     if url == '/api/bookmarks/refresh':
   48         rd = client.post(url)
   49         assert rd.status_code == 400
   50     else:
   51         rd = client.get(url)
   52         assert rd.status_code == 200
   53     assert rd.get_json() == exp_res
   54 
   55 
   56 @pytest.mark.parametrize(
   57     'url, exp_res, status_code, method', [
   58         ['/api/tags/1', {'message': 'This resource does not exist.'}, 404, 'get'],
   59         ['/api/tags/1', response_template['failure'], 400, 'put'],
   60         ['/api/bookmarks/1', response_template['failure'], 400, 'get'],
   61         ['/api/bookmarks/1', response_template['failure'], 400, 'put'],
   62         ['/api/bookmarks/1', response_template['failure'], 400, 'delete'],
   63         ['/api/bookmarks/1/refresh', response_template['failure'], 400, 'post'],
   64         ['/api/bookmarks/1/tiny', response_template['failure'], 400, 'get'],
   65         ['/api/bookmarks/1/2', response_template['failure'], 400, 'get'],
   66         ['/api/bookmarks/1/2', response_template['failure'], 400, 'put'],
   67         ['/api/bookmarks/1/2', response_template['failure'], 400, 'delete'],
   68     ]
   69 )
   70 def test_invalid_id(client, url, exp_res, status_code, method):
   71     rd = getattr(client, method)(url)
   72     assert rd.status_code == status_code
   73     assert rd.get_json() == exp_res
   74 
   75 
   76 def test_tag_api(client):
   77     url = 'http://google.com'
   78     rd = client.post('/api/bookmarks', data={'url': url, 'tags': 'tag1,tag2'})
   79     assert rd.status_code == 200
   80     assert rd.get_json() == response_template['success']
   81     rd = client.get('/api/tags')
   82     assert rd.status_code == 200
   83     assert rd.get_json() == {'tags': ['tag1', 'tag2']}
   84     rd = client.get('/api/tags/tag1')
   85     assert rd.status_code == 200
   86     assert rd.get_json() == {'name': 'tag1', 'usage_count': 1}
   87     rd = client.put('/api/tags/tag1', data={'tags': 'tag3,tag4'})
   88     assert rd.status_code == 200
   89     assert rd.get_json() == response_template['success']
   90     rd = client.get('/api/tags')
   91     assert rd.status_code == 200
   92     assert rd.get_json() == {'tags': ['tag2', 'tag3 tag4']}
   93     rd = client.put('/api/tags/tag2', data={'tags': 'tag5'})
   94     assert rd.status_code == 200
   95     assert rd.get_json() == response_template['success']
   96     rd = client.get('/api/tags')
   97     assert rd.status_code == 200
   98     assert rd.get_json() == {'tags': ['tag3 tag4', 'tag5']}
   99     rd = client.get('/api/bookmarks/1')
  100     assert rd.status_code == 200
  101     assert rd.get_json() == {
  102         'description': '', 'tags': ['tag3 tag4', 'tag5'], 'title': '',
  103         'url': 'http://google.com'}
  104 
  105 
  106 def test_bookmark_api(client):
  107     url = 'http://google.com'
  108     rd = client.post('/api/bookmarks', data={'url': url})
  109     assert rd.status_code == 200
  110     assert rd.get_json() == response_template['success']
  111     rd = client.post('/api/bookmarks', data={'url': url})
  112     assert rd.status_code == 400
  113     assert rd.get_json() == response_template['failure']
  114     rd = client.get('/api/bookmarks')
  115     assert rd.status_code == 200
  116     assert rd.get_json() == {'bookmarks': [{
  117         'description': '', 'tags': [], 'title': '', 'url': 'http://google.com'}]}
  118     rd = client.get('/api/bookmarks/1')
  119     assert rd.status_code == 200
  120     assert rd.get_json() == {
  121         'description': '', 'tags': [], 'title': '', 'url': 'http://google.com'}
  122     rd = client.put('/api/bookmarks/1', data={'tags': [',tag1,tag2,']})
  123     assert rd.status_code == 200
  124     assert rd.get_json() == response_template['success']
  125     rd = client.get('/api/bookmarks/1')
  126     assert rd.status_code == 200
  127     assert rd.get_json() == {
  128         'description': '', 'tags': ['tag1', 'tag2'], 'title': '', 'url': 'http://google.com'}
  129 
  130 
  131 @pytest.mark.parametrize('d_url', ['/api/bookmarks', '/api/bookmarks/1'])
  132 def test_bookmark_api_delete(client, d_url):
  133     url = 'http://google.com'
  134     rd = client.post('/api/bookmarks', data={'url': url})
  135     assert rd.status_code == 200
  136     assert rd.get_json() == response_template['success']
  137     rd = client.delete(d_url)
  138     assert rd.status_code == 200
  139     assert rd.get_json() == response_template['success']
  140 
  141 
  142 @pytest.mark.parametrize('api_url', ['/api/bookmarks/refresh', '/api/bookmarks/1/refresh'])
  143 def test_refresh_bookmark(client, api_url):
  144     url = 'http://google.com'
  145     rd = client.post('/api/bookmarks', data={'url': url})
  146     assert rd.status_code == 200
  147     assert rd.get_json() == response_template['success']
  148     rd = client.post(api_url)
  149     assert rd.status_code == 200
  150     assert rd.get_json() == response_template['success']
  151     rd = client.get('/api/bookmarks/1')
  152     assert rd.status_code == 200
  153     json_data = rd.get_json()
  154     json_data.pop('description')
  155     assert json_data == {'tags': [], 'title': 'Google', 'url': 'http://google.com'}
  156 
  157 
  158 @pytest.mark.parametrize(
  159     'url, exp_res, status_code', [
  160         ['http://google.com', {'url': 'http://tny.im/2'}, 200],
  161         ['chrome://bookmarks/', response_template['failure'], 400],
  162     ])
  163 def test_get_tiny_url(client, url, exp_res, status_code):
  164     rd = client.post('/api/bookmarks', data={'url': url})
  165     assert rd.status_code == 200
  166     assert rd.get_json() == response_template['success']
  167     rd = client.get('/api/bookmarks/1/tiny')
  168     assert rd.status_code == status_code
  169     assert rd.get_json() == exp_res
  170 
  171 
  172 @pytest.mark.parametrize('kwargs, status_code, exp_res', [
  173     [
  174         dict(data={'url': 'http://google.com'}),
  175         200,
  176         {
  177             'bad url': 0, 'recognized mime': 0,
  178             'tags': None, 'title': 'Google'}
  179     ],
  180     [{}, 400, response_template['failure']],
  181     [
  182         dict(data={'url': 'chrome://bookmarks/'}),
  183         200,
  184         {
  185             'bad url': 1, 'recognized mime': 0,
  186             'tags': None, 'title': None}
  187     ],
  188 ])
  189 def test_network_handle(client, kwargs, status_code, exp_res):
  190     rd = client.post('/api/network_handle', **kwargs)
  191     assert rd.status_code == status_code
  192     rd_json = rd.get_json()
  193     rd_json.pop('description', None)
  194     assert rd_json == exp_res
  195 
  196 
  197 def test_bookmark_range_api(client):
  198     status_code = 200
  199     kwargs_list = [
  200         dict(data={'url': 'http://google.com'}),
  201         dict(data={'url': 'http://example.com'})]
  202     for kwargs in kwargs_list:
  203         rd = client.post('/api/bookmarks', **kwargs)
  204         assert rd.status_code == status_code
  205     rd = client.get('/api/bookmarks/1/2')
  206     assert rd.status_code == status_code
  207     assert rd.get_json() == {
  208         'bookmarks': {
  209             '1': {'description': '', 'tags': [], 'title': '', 'url': 'http://google.com'},
  210             '2': {'description': '', 'tags': [], 'title': '', 'url': 'http://example.com'}}}
  211     put_data = json.dumps({1: {'tags': 'tag1'}, 2: {'tags': 'tag2'}})
  212     headers = {'content-type': 'application/json'}
  213     rd = client.put('/api/bookmarks/1/2', data=put_data, headers=headers)
  214     assert rd.status_code == status_code
  215     assert rd.get_json() == response_template['success']
  216     rd = client.delete('/api/bookmarks/1/2')
  217     assert rd.status_code == status_code
  218     assert rd.get_json() == response_template['success']
  219     rd = client.get('/api/bookmarks')
  220     assert rd.get_json() == {'bookmarks': []}
  221 
  222 
  223 def test_bookmark_search(client):
  224     status_code = 200
  225     rd = client.post('/api/bookmarks', data={'url': 'http://google.com'})
  226     assert rd.status_code == status_code
  227     assert rd.get_json() == response_template['success']
  228     rd = client.get('/api/bookmarks/search', query_string={'keywords': ['google']})
  229     assert rd.status_code == status_code
  230     assert rd.get_json() == {'bookmarks': [
  231         {'description': '', 'id': 1, 'tags': [], 'title': '', 'url': 'http://google.com'}]}
  232     rd = client.delete('/api/bookmarks/search', data={'keywords': ['google']})
  233     assert rd.status_code == status_code
  234     assert rd.get_json() == response_template['success']
  235     rd = client.get('/api/bookmarks')
  236     assert rd.get_json() == {'bookmarks': []}