File size: 3,742 Bytes
046723b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from copy import copy

from flask import url_for
import json
import time
from .util import live_server_setup, wait_for_all_checks


def test_api_search(client, live_server):
   #  live_server_setup(live_server) # Setup on conftest per function
    api_key = live_server.app.config['DATASTORE'].data['settings']['application'].get('api_access_token')

    watch_data = {}
    # Add some test watches
    urls = [
        'https://example.com/page1',
        'https://example.org/testing',
        'https://test-site.com/example'
    ]

    # Import the test URLs
    res = client.post(
        url_for("imports.import_page"),
        data={"urls": "\r\n".join(urls)},
        follow_redirects=True
    )
    assert b"3 Imported" in res.data
    wait_for_all_checks(client)

    # Get a listing, it will be the first one
    watches_response = client.get(
        url_for("createwatch"),
        headers={'x-api-key': api_key}
    )


    # Add a title to one watch for title search testing
    for uuid, watch in watches_response.json.items():

        watch_data = client.get(url_for("watch", uuid=uuid),
                                follow_redirects=True,
                                headers={'x-api-key': api_key}
                                )

        if urls[0] == watch_data.json['url']:
            # HTTP PUT ( UPDATE an existing watch )
            client.put(
                url_for("watch", uuid=uuid),
                headers={'x-api-key': api_key, 'content-type': 'application/json'},
                data=json.dumps({'title': 'Example Title Test'}),
            )

    # Test search by URL
    res = client.get(url_for("search")+"?q=https://example.com/page1", headers={'x-api-key': api_key, 'content-type': 'application/json'})
    assert len(res.json) == 1
    assert list(res.json.values())[0]['url'] == urls[0]

    # Test search by URL - partial should NOT match without ?partial=true flag
    res = client.get(url_for("search")+"?q=https://example", headers={'x-api-key': api_key, 'content-type': 'application/json'})
    assert len(res.json) == 0


    # Test search by title
    res = client.get(url_for("search")+"?q=Example Title Test", headers={'x-api-key': api_key, 'content-type': 'application/json'})
    assert len(res.json) == 1
    assert list(res.json.values())[0]['url'] == urls[0]
    assert list(res.json.values())[0]['title'] == 'Example Title Test'

    # Test search that should return multiple results (partial = true)
    res = client.get(url_for("search")+"?q=https://example&partial=true", headers={'x-api-key': api_key, 'content-type': 'application/json'})
    assert len(res.json) == 2

    # Test empty search
    res = client.get(url_for("search")+"?q=", headers={'x-api-key': api_key, 'content-type': 'application/json'})
    assert res.status_code == 400

    # Add a tag to test search with tag filter
    tag_name = 'test-tag'
    res = client.post(
        url_for("tag"),
        data=json.dumps({"title": tag_name}),
        headers={'content-type': 'application/json', 'x-api-key': api_key}
    )
    assert res.status_code == 201
    tag_uuid = res.json['uuid']

    # Add the tag to one watch
    for uuid, watch in watches_response.json.items():
        if urls[2] == watch['url']:
            client.put(
                url_for("watch", uuid=uuid),
                headers={'x-api-key': api_key, 'content-type': 'application/json'},
                data=json.dumps({'tags': [tag_uuid]}),
            )


    # Test search with tag filter and q
    res = client.get(url_for("search") + f"?q={urls[2]}&tag={tag_name}", headers={'x-api-key': api_key, 'content-type': 'application/json'})
    assert len(res.json) == 1
    assert list(res.json.values())[0]['url'] == urls[2]