From 3b2bfd06cc2091dec706bddceebe7e5e01fd6742 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Fri, 25 Aug 2023 12:49:57 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20fix(test=5Fdatabase.py):=20fix?= =?UTF-8?q?=20test=5Fcreate=5Fflow=20function=20signature=20to=20include?= =?UTF-8?q?=20active=5Fuser=20and=20logged=5Fin=5Fheaders=20parameters=20?= =?UTF-8?q?=F0=9F=94=A7=20fix(test=5Fdatabase.py):=20fix=20test=5Fread=5Ff?= =?UTF-8?q?lows=20function=20signature=20to=20include=20active=5Fuser=20an?= =?UTF-8?q?d=20logged=5Fin=5Fheaders=20parameters=20=F0=9F=94=A7=20fix(tes?= =?UTF-8?q?t=5Fdatabase.py):=20fix=20test=5Fread=5Fflow=20function=20signa?= =?UTF-8?q?ture=20to=20include=20active=5Fuser=20and=20logged=5Fin=5Fheade?= =?UTF-8?q?rs=20parameters=20=F0=9F=94=A7=20fix(test=5Fdatabase.py):=20fix?= =?UTF-8?q?=20test=5Fupdate=5Fflow=20function=20signature=20to=20include?= =?UTF-8?q?=20active=5Fuser=20and=20logged=5Fin=5Fheaders=20parameters=20?= =?UTF-8?q?=F0=9F=94=A7=20fix(test=5Fdatabase.py):=20fix=20test=5Fdelete?= =?UTF-8?q?=5Fflow=20function=20signature=20to=20include=20active=5Fuser?= =?UTF-8?q?=20and=20logged=5Fin=5Fheaders=20parameters=20=F0=9F=94=A7=20fi?= =?UTF-8?q?x(test=5Fdatabase.py):=20fix=20test=5Fcreate=5Fflows=20function?= =?UTF-8?q?=20signature=20to=20include=20logged=5Fin=5Fheaders=20parameter?= =?UTF-8?q?=20=F0=9F=94=A7=20fix(test=5Fdatabase.py):=20fix=20test=5Fuploa?= =?UTF-8?q?d=5Ffile=20function=20signature=20to=20include=20logged=5Fin=5F?= =?UTF-8?q?headers=20parameter=20=F0=9F=94=A7=20fix(test=5Fdatabase.py):?= =?UTF-8?q?=20fix=20test=5Fdownload=5Ffile=20function=20signature=20to=20i?= =?UTF-8?q?nclude=20active=5Fuser=20and=20logged=5Fin=5Fheaders=20paramete?= =?UTF-8?q?rs=20=F0=9F=94=A7=20fix(test=5Fdatabase.py):=20fix=20test=5Fcre?= =?UTF-8?q?ate=5Fflow=5Fwith=5Finvalid=5Fdata=20function=20signature=20to?= =?UTF-8?q?=20include=20active=5Fuser=20and=20logged=5Fin=5Fheaders=20para?= =?UTF-8?q?meters=20=F0=9F=94=A7=20fix(test=5Fdatabase.py):=20fix=20test?= =?UTF-8?q?=5Fget=5Fnonexistent=5Fflow=20function=20signature=20to=20inclu?= =?UTF-8?q?de=20active=5Fuser=20and=20logged=5Fin=5Fheaders=20parameters?= =?UTF-8?q?=20=F0=9F=94=A7=20fix(test=5Fdatabase.py):=20fix=20test=5Fupdat?= =?UTF-8?q?e=5Fflow=5Fidempotency=20function=20signature=20to=20include=20?= =?UTF-8?q?active=5Fuser=20and=20logged=5Fin=5Fheaders=20parameters=20?= =?UTF-8?q?=F0=9F=94=A7=20fix(test=5Fdatabase.py):=20fix=20test=5Fupdate?= =?UTF-8?q?=5Fnonexistent=5Fflow=20function=20signature=20to=20include=20a?= =?UTF-8?q?ctive=5Fuser=20and=20logged=5Fin=5Fheaders=20parameters=20?= =?UTF-8?q?=F0=9F=94=A7=20fix(test=5Fdatabase.py):=20fix=20test=5Fdelete?= =?UTF-8?q?=5Fnonexistent=5Fflow=20function=20signature=20to=20include=20a?= =?UTF-8?q?ctive=5Fuser=20and=20logged=5Fin=5Fheaders=20parameters=20?= =?UTF-8?q?=F0=9F=94=A7=20fix(test=5Fdatabase.py):=20fix=20test=5Fread=5Fe?= =?UTF-8?q?mpty=5Fflows=20function=20signature=20to=20include=20active=5Fu?= =?UTF-8?q?ser=20and=20logged=5Fin=5Fheaders=20parameters?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_database.py | 104 +++++++++++++++++++++++++++-------------- 1 file changed, 69 insertions(+), 35 deletions(-) diff --git a/tests/test_database.py b/tests/test_database.py index 48e253026..e4f68ca56 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -25,63 +25,69 @@ def json_style(): ) -def test_create_flow(client: TestClient, json_flow: str): +def test_create_flow( + client: TestClient, json_flow: str, active_user, logged_in_headers +): flow = orjson.loads(json_flow) data = flow["data"] flow = FlowCreate(name="Test Flow", description="description", data=data) - response = client.post("api/v1/flows/", json=flow.dict()) + response = client.post("api/v1/flows/", json=flow.dict(), headers=logged_in_headers) assert response.status_code == 201 assert response.json()["name"] == flow.name assert response.json()["data"] == flow.data # flow is optional so we can create a flow without a flow flow = FlowCreate(name="Test Flow") - response = client.post("api/v1/flows/", json=flow.dict(exclude_unset=True)) + response = client.post( + "api/v1/flows/", json=flow.dict(exclude_unset=True), headers=logged_in_headers + ) assert response.status_code == 201 assert response.json()["name"] == flow.name assert response.json()["data"] == flow.data -def test_read_flows(client: TestClient, json_flow: str): +def test_read_flows(client: TestClient, json_flow: str, active_user, logged_in_headers): flow_data = orjson.loads(json_flow) data = flow_data["data"] flow = FlowCreate(name="Test Flow", description="description", data=data) - response = client.post("api/v1/flows/", json=flow.dict()) + response = client.post("api/v1/flows/", json=flow.dict(), headers=logged_in_headers) assert response.status_code == 201 assert response.json()["name"] == flow.name assert response.json()["data"] == flow.data flow = FlowCreate(name="Test Flow", description="description", data=data) - response = client.post("api/v1/flows/", json=flow.dict()) + response = client.post("api/v1/flows/", json=flow.dict(), headers=logged_in_headers) assert response.status_code == 201 assert response.json()["name"] == flow.name assert response.json()["data"] == flow.data - response = client.get("api/v1/flows/") + response = client.get("api/v1/flows/", headers=logged_in_headers) assert response.status_code == 200 assert len(response.json()) > 0 -def test_read_flow(client: TestClient, json_flow: str): +def test_read_flow(client: TestClient, json_flow: str, active_user, logged_in_headers): flow = orjson.loads(json_flow) data = flow["data"] flow = FlowCreate(name="Test Flow", description="description", data=data) - response = client.post("api/v1/flows/", json=flow.dict()) + response = client.post("api/v1/flows/", json=flow.dict(), headers=logged_in_headers) flow_id = response.json()["id"] # flow_id should be a UUID but is a string # turn it into a UUID flow_id = UUID(flow_id) - response = client.get(f"api/v1/flows/{flow_id}") + response = client.get(f"api/v1/flows/{flow_id}", headers=logged_in_headers) assert response.status_code == 200 assert response.json()["name"] == flow.name assert response.json()["data"] == flow.data -def test_update_flow(client: TestClient, json_flow: str): +def test_update_flow( + client: TestClient, json_flow: str, active_user, logged_in_headers +): flow = orjson.loads(json_flow) data = flow["data"] flow = FlowCreate(name="Test Flow", description="description", data=data) - response = client.post("api/v1/flows/", json=flow.dict()) + response = client.post("api/v1/flows/", json=flow.dict(), headers=logged_in_headers) flow_id = response.json()["id"] updated_flow = FlowUpdate( @@ -89,7 +95,9 @@ def test_update_flow(client: TestClient, json_flow: str): description="updated description", data=data, ) - response = client.patch(f"api/v1/flows/{flow_id}", json=updated_flow.dict()) + response = client.patch( + f"api/v1/flows/{flow_id}", json=updated_flow.dict(), headers=logged_in_headers + ) assert response.status_code == 200 assert response.json()["name"] == updated_flow.name @@ -97,18 +105,22 @@ def test_update_flow(client: TestClient, json_flow: str): # assert response.json()["data"] == updated_flow.data -def test_delete_flow(client: TestClient, json_flow: str): +def test_delete_flow( + client: TestClient, json_flow: str, active_user, logged_in_headers +): flow = orjson.loads(json_flow) data = flow["data"] flow = FlowCreate(name="Test Flow", description="description", data=data) - response = client.post("api/v1/flows/", json=flow.dict()) + response = client.post("api/v1/flows/", json=flow.dict(), headers=logged_in_headers) flow_id = response.json()["id"] - response = client.delete(f"api/v1/flows/{flow_id}") + response = client.delete(f"api/v1/flows/{flow_id}", headers=logged_in_headers) assert response.status_code == 200 assert response.json()["message"] == "Flow deleted successfully" -def test_create_flows(client: TestClient, session: Session, json_flow: str): +def test_create_flows( + client: TestClient, session: Session, json_flow: str, logged_in_headers +): flow = orjson.loads(json_flow) data = flow["data"] # Create test data @@ -119,7 +131,9 @@ def test_create_flows(client: TestClient, session: Session, json_flow: str): ] ) # Make request to endpoint - response = client.post("api/v1/flows/batch/", json=flow_list.dict()) + response = client.post( + "api/v1/flows/batch/", json=flow_list.dict(), headers=logged_in_headers + ) # Check response status code assert response.status_code == 201 # Check response data @@ -133,7 +147,9 @@ def test_create_flows(client: TestClient, session: Session, json_flow: str): assert response_data[1]["data"] == data -def test_upload_file(client: TestClient, session: Session, json_flow: str): +def test_upload_file( + client: TestClient, session: Session, json_flow: str, logged_in_headers +): flow = orjson.loads(json_flow) data = flow["data"] # Create test data @@ -147,6 +163,7 @@ def test_upload_file(client: TestClient, session: Session, json_flow: str): response = client.post( "api/v1/flows/upload/", files={"file": ("examples.json", file_contents, "application/json")}, + headers=logged_in_headers, ) # Check response status code assert response.status_code == 201 @@ -161,7 +178,9 @@ def test_upload_file(client: TestClient, session: Session, json_flow: str): assert response_data[1]["data"] == data -def test_download_file(client: TestClient, session: Session, json_flow): +def test_download_file( + client: TestClient, session: Session, json_flow, active_user, logged_in_headers +): flow = orjson.loads(json_flow) data = flow["data"] # Create test data @@ -172,11 +191,12 @@ def test_download_file(client: TestClient, session: Session, json_flow): ] ) for flow in flow_list.flows: + flow.user_id = active_user.id db_flow = Flow.from_orm(flow) session.add(db_flow) session.commit() # Make request to endpoint - response = client.get("api/v1/flows/download/") + response = client.get("api/v1/flows/download/", headers=logged_in_headers) # Check response status code assert response.status_code == 200 # Check response data @@ -190,31 +210,43 @@ def test_download_file(client: TestClient, session: Session, json_flow): assert response_data[1]["data"] == data -def test_create_flow_with_invalid_data(client: TestClient): +def test_create_flow_with_invalid_data( + client: TestClient, active_user, logged_in_headers +): flow = {"name": "a" * 256, "data": "Invalid flow data"} - response = client.post("api/v1/flows/", json=flow) + response = client.post("api/v1/flows/", json=flow, headers=logged_in_headers) assert response.status_code == 422 -def test_get_nonexistent_flow(client: TestClient): +def test_get_nonexistent_flow(client: TestClient, active_user, logged_in_headers): uuid = uuid4() - response = client.get(f"api/v1/flows/{uuid}") + response = client.get(f"api/v1/flows/{uuid}", headers=logged_in_headers) assert response.status_code == 404 -def test_update_flow_idempotency(client: TestClient, json_flow: str): +def test_update_flow_idempotency( + client: TestClient, json_flow: str, active_user, logged_in_headers +): flow_data = orjson.loads(json_flow) data = flow_data["data"] flow_data = FlowCreate(name="Test Flow", description="description", data=data) - response = client.post("api/v1/flows/", json=flow_data.dict()) + response = client.post( + "api/v1/flows/", json=flow_data.dict(), headers=logged_in_headers + ) flow_id = response.json()["id"] updated_flow = FlowCreate(name="Updated Flow", description="description", data=data) - response1 = client.put(f"api/v1/flows/{flow_id}", json=updated_flow.dict()) - response2 = client.put(f"api/v1/flows/{flow_id}", json=updated_flow.dict()) + response1 = client.put( + f"api/v1/flows/{flow_id}", json=updated_flow.dict(), headers=logged_in_headers + ) + response2 = client.put( + f"api/v1/flows/{flow_id}", json=updated_flow.dict(), headers=logged_in_headers + ) assert response1.json() == response2.json() -def test_update_nonexistent_flow(client: TestClient, json_flow: str): +def test_update_nonexistent_flow( + client: TestClient, json_flow: str, active_user, logged_in_headers +): flow_data = orjson.loads(json_flow) data = flow_data["data"] uuid = uuid4() @@ -223,17 +255,19 @@ def test_update_nonexistent_flow(client: TestClient, json_flow: str): description="description", data=data, ) - response = client.patch(f"api/v1/flows/{uuid}", json=updated_flow.dict()) + response = client.patch( + f"api/v1/flows/{uuid}", json=updated_flow.dict(), headers=logged_in_headers + ) assert response.status_code == 404 -def test_delete_nonexistent_flow(client: TestClient): +def test_delete_nonexistent_flow(client: TestClient, active_user, logged_in_headers): uuid = uuid4() - response = client.delete(f"api/v1/flows/{uuid}") + response = client.delete(f"api/v1/flows/{uuid}", headers=logged_in_headers) assert response.status_code == 404 -def test_read_empty_flows(client: TestClient): - response = client.get("api/v1/flows/") +def test_read_empty_flows(client: TestClient, active_user, logged_in_headers): + response = client.get("api/v1/flows/", headers=logged_in_headers) assert response.status_code == 200 assert len(response.json()) == 0