|
| 1 | +from copy import copy |
| 2 | + |
| 3 | +from flask import url_for |
| 4 | +import json |
| 5 | +import time |
| 6 | +from .util import live_server_setup, wait_for_all_checks |
| 7 | + |
| 8 | + |
| 9 | +def test_api_search(client, live_server): |
| 10 | + live_server_setup(live_server) |
| 11 | + api_key = live_server.app.config['DATASTORE'].data['settings']['application'].get('api_access_token') |
| 12 | + |
| 13 | + watch_data = {} |
| 14 | + # Add some test watches |
| 15 | + urls = [ |
| 16 | + 'https://example.com/page1', |
| 17 | + 'https://example.org/testing', |
| 18 | + 'https://test-site.com/example' |
| 19 | + ] |
| 20 | + |
| 21 | + # Import the test URLs |
| 22 | + res = client.post( |
| 23 | + url_for("imports.import_page"), |
| 24 | + data={"urls": "\r\n".join(urls)}, |
| 25 | + follow_redirects=True |
| 26 | + ) |
| 27 | + assert b"3 Imported" in res.data |
| 28 | + wait_for_all_checks(client) |
| 29 | + |
| 30 | + # Get a listing, it will be the first one |
| 31 | + watches_response = client.get( |
| 32 | + url_for("createwatch"), |
| 33 | + headers={'x-api-key': api_key} |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | + # Add a title to one watch for title search testing |
| 38 | + for uuid, watch in watches_response.json.items(): |
| 39 | + |
| 40 | + watch_data = client.get(url_for("watch", uuid=uuid), |
| 41 | + follow_redirects=True, |
| 42 | + headers={'x-api-key': api_key} |
| 43 | + ) |
| 44 | + |
| 45 | + if urls[0] == watch_data.json['url']: |
| 46 | + # HTTP PUT ( UPDATE an existing watch ) |
| 47 | + client.put( |
| 48 | + url_for("watch", uuid=uuid), |
| 49 | + headers={'x-api-key': api_key, 'content-type': 'application/json'}, |
| 50 | + data=json.dumps({'title': 'Example Title Test'}), |
| 51 | + ) |
| 52 | + |
| 53 | + # Test search by URL |
| 54 | + res = client.get(url_for("search")+"?q=https://example.com/page1", headers={'x-api-key': api_key, 'content-type': 'application/json'}) |
| 55 | + assert len(res.json) == 1 |
| 56 | + assert list(res.json.values())[0]['url'] == urls[0] |
| 57 | + |
| 58 | + # Test search by URL - partial should NOT match without ?partial=true flag |
| 59 | + res = client.get(url_for("search")+"?q=https://example", headers={'x-api-key': api_key, 'content-type': 'application/json'}) |
| 60 | + assert len(res.json) == 0 |
| 61 | + |
| 62 | + |
| 63 | + # Test search by title |
| 64 | + res = client.get(url_for("search")+"?q=Example Title Test", headers={'x-api-key': api_key, 'content-type': 'application/json'}) |
| 65 | + assert len(res.json) == 1 |
| 66 | + assert list(res.json.values())[0]['url'] == urls[0] |
| 67 | + assert list(res.json.values())[0]['title'] == 'Example Title Test' |
| 68 | + |
| 69 | + # Test search that should return multiple results (partial = true) |
| 70 | + res = client.get(url_for("search")+"?q=https://example&partial=true", headers={'x-api-key': api_key, 'content-type': 'application/json'}) |
| 71 | + assert len(res.json) == 2 |
| 72 | + |
| 73 | + # Test empty search |
| 74 | + res = client.get(url_for("search")+"?q=", headers={'x-api-key': api_key, 'content-type': 'application/json'}) |
| 75 | + assert res.status_code == 400 |
| 76 | + |
| 77 | + # Add a tag to test search with tag filter |
| 78 | + tag_name = 'test-tag' |
| 79 | + res = client.post( |
| 80 | + url_for("tag"), |
| 81 | + data=json.dumps({"title": tag_name}), |
| 82 | + headers={'content-type': 'application/json', 'x-api-key': api_key} |
| 83 | + ) |
| 84 | + assert res.status_code == 201 |
| 85 | + tag_uuid = res.json['uuid'] |
| 86 | + |
| 87 | + # Add the tag to one watch |
| 88 | + for uuid, watch in watches_response.json.items(): |
| 89 | + if urls[2] == watch['url']: |
| 90 | + client.put( |
| 91 | + url_for("watch", uuid=uuid), |
| 92 | + headers={'x-api-key': api_key, 'content-type': 'application/json'}, |
| 93 | + data=json.dumps({'tags': [tag_uuid]}), |
| 94 | + ) |
| 95 | + |
| 96 | + |
| 97 | + # Test search with tag filter and q |
| 98 | + res = client.get(url_for("search") + f"?q={urls[2]}&tag={tag_name}", headers={'x-api-key': api_key, 'content-type': 'application/json'}) |
| 99 | + assert len(res.json) == 1 |
| 100 | + assert list(res.json.values())[0]['url'] == urls[2] |
| 101 | + |
0 commit comments