From 03735d4c6fc17193e5019d3bd595bad2ce41c61f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20N=C3=A4slund?= Date: Thu, 17 Nov 2022 22:04:24 +0100 Subject: added tests --- src/soc_collector/main.py | 46 +++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 21 deletions(-) (limited to 'src/soc_collector/main.py') diff --git a/src/soc_collector/main.py b/src/soc_collector/main.py index e70199b..fd1bded 100755 --- a/src/soc_collector/main.py +++ b/src/soc_collector/main.py @@ -10,9 +10,8 @@ from fastapi.responses import JSONResponse from .db import ( DBClient, SearchInput, - object_id_from_key, ) -from .schema import valid_schema +from .schema import valid_schema, object_id_from_data from .auth import authorize_client, load_api_keys # Get credentials @@ -52,10 +51,10 @@ async def search(request: Request, search_data: SearchInput) -> JSONResponse: @app.post("/sc/v0") -async def create(request: Request) -> JSONResponse: +async def insert(request: Request) -> JSONResponse: """/sc/v0, POST method - :param request: The request where we get the json body. + :param request: The client request. :return: JSONResponse """ @@ -75,19 +74,19 @@ async def create(request: Request) -> JSONResponse: if not valid_schema(json_data): return JSONResponse(content={"status": "error", "message": "Not our JSON schema"}, status_code=400) - key = await db.insert_one(json_data) + object_id = await db.insert_one(json_data) - if key is None: + if object_id is None: return JSONResponse(content={"status": "error", "message": "DB error"}, status_code=500) - return JSONResponse(content={"status": "success", "key": str(key)}) + return JSONResponse(content={"status": "success", "_id": str(object_id)}) @app.put("/sc/v0") async def replace(request: Request) -> JSONResponse: # pylint: disable=too-many-return-statements """/sc/v0, PUT method - :param request: The request where we get the json body. + :param request: The client request. :return: JSONResponse """ @@ -99,13 +98,9 @@ async def replace(request: Request) -> JSONResponse: # pylint: disable=too-many except JSONDecodeError: return JSONResponse(content={"status": "error", "message": "Invalid JSON"}, status_code=400) - if "_id" not in json_data: - return JSONResponse(content={"status": "error", "message": "Missing key '_id'"}, status_code=400) - # Get the key - if isinstance(json_data["_id"], str): - object_id = object_id_from_key(json_data["_id"]) - else: + object_id = object_id_from_data(json_data) + if object_id is None: return JSONResponse(content={"status": "error", "message": "Missing key '_id' with valid id"}, status_code=400) # Ensure the updating key exist @@ -123,24 +118,28 @@ async def replace(request: Request) -> JSONResponse: # pylint: disable=too-many json_data["_id"] = object_id returned_object_id = await db.replace_one(object_id, json_data) - if returned_object_id is None: + if returned_object_id is None or returned_object_id != object_id: return JSONResponse(content={"status": "error", "message": "DB error"}, status_code=500) - return JSONResponse(content={"status": "success", "key": str(object_id)}) + return JSONResponse(content={"status": "success", "_id": str(object_id)}) @app.get("/sc/v0/{key}") async def get(request: Request, key: str) -> JSONResponse: """/sc/v0/{key}, GET method - :param key: The document key in the database. + :param request: The client request. + :param key: The document id in the database. :return: JSONResponse """ # Ensure authorization authorize_client(request, API_KEYS) - object_id = object_id_from_key(key) + # Get the id + object_id = object_id_from_data(key) + if object_id is None: + return JSONResponse(content={"status": "error", "message": "Invalid id"}, status_code=400) document = await db.find_one(object_id) @@ -154,27 +153,32 @@ async def get(request: Request, key: str) -> JSONResponse: async def delete(request: Request, key: str) -> JSONResponse: """/sc/v0/{key}, DELETE method - :param key: The document key in the database. + :param request: The client request. + :param key: The document id in the database. :return: JSONResponse """ # Ensure authorization authorize_client(request, API_KEYS) - object_id = object_id_from_key(key) + # Get the id + object_id = object_id_from_data(key) + if object_id is None: + return JSONResponse(content={"status": "error", "message": "Invalid id"}, status_code=400) result = await db.delete_one(object_id) if result is None: return JSONResponse(content={"status": "error", "message": "Document not found"}, status_code=404) - return JSONResponse(content={"status": "success", "key": str(object_id)}) + return JSONResponse(content={"status": "success", "_id": str(object_id)}) @app.get("/info") async def info(request: Request) -> JSONResponse: """/info, GET method + :param request: The client request. :return: JSONResponse """ -- cgit v1.1