bumping test

API-interface
dgtlmoon 3 years ago
parent 408c8878f3
commit 91fe2dd420

@ -1,5 +1,6 @@
from flask_restful import abort, Resource
from flask import request
from flask import request, make_response
import validators
# https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
@ -69,7 +70,10 @@ class WatchSingleHistory(Resource):
with open(watch['history'][timestamp], 'r') as f:
content = f.read()
return content, '200', {'Content-type': 'text/plain'}
response = make_response(content, 200)
response.mimetype = "text/plain"
return response
class CreateWatch(Resource):
@ -82,8 +86,13 @@ class CreateWatch(Resource):
# curl http://localhost:4000/api/v1/watch -H "Content-Type: application/json" -d '{"url": "https://my-nice.com", "tag": "one, two" }'
json_data = request.get_json()
tag = json_data['tag'].strip() if json_data.get('tag') else ''
if not validators.url(json_data['url'].strip()):
return "Invalid or unsupported URL", 400
new_uuid = self.datastore.add_watch(url=json_data['url'].strip(), tag=tag)
return new_uuid, 201
self.update_q.put(new_uuid)
return {'uuid': new_uuid}, 201
# Return concise list of available watches and some very basic info
# curl http://localhost:4000/api/v1/watch|python -mjson.tool
@ -99,6 +108,6 @@ class CreateWatch(Resource):
if request.args.get('recheck'):
for uuid in self.datastore.data['watching'].items():
self.update_q.put(uuid)
return "OK", 200
return {'status':"OK"}, 200
return list, 200

@ -2,22 +2,42 @@
import time
from flask import url_for
from . util import live_server_setup
from .util import live_server_setup
import json
import uuid
def is_valid_uuid(val):
try:
uuid.UUID(str(val))
return True
except ValueError:
return False
def test_api_simple(client, live_server):
live_server_setup(live_server)
watch_uuid = None
res = client.post(
url_for("createwatch"),
data={"url": "https://nice.com"},
data=json.dumps({"url": "h://xxxxxxxxxom"}),
headers={'content-type': 'application/json'},
follow_redirects=True
)
assert len(res.data) >= 20
assert res.status_code == 201
assert res.status_code == 400
# try invalid url
res = client.post(
url_for("createwatch"),
data=json.dumps({"url": "https://nice.com"}),
headers={'content-type': 'application/json'},
follow_redirects=True
)
s = json.loads(res.data)
assert is_valid_uuid(s['uuid'])
watch_uuid = s['uuid']
#assert res.status_code == 400
assert res.status_code == 201

Loading…
Cancel
Save