From 91fe2dd4200ce5014a0e71cd92413db3e4b9a5dc Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Fri, 20 May 2022 09:33:35 +0200 Subject: [PATCH] bumping test --- changedetectionio/api_v1.py | 17 +++++++++++---- changedetectionio/tests/test_api.py | 32 +++++++++++++++++++++++------ 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/changedetectionio/api_v1.py b/changedetectionio/api_v1.py index 6094fa87..9f443924 100644 --- a/changedetectionio/api_v1.py +++ b/changedetectionio/api_v1.py @@ -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 diff --git a/changedetectionio/tests/test_api.py b/changedetectionio/tests/test_api.py index 836ca63a..f74c166f 100644 --- a/changedetectionio/tests/test_api.py +++ b/changedetectionio/tests/test_api.py @@ -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