diff --git a/testing/test_register_face.py b/testing/test_register_face.py index f3c0779..283bb8b 100644 --- a/testing/test_register_face.py +++ b/testing/test_register_face.py @@ -1,31 +1,25 @@ import pytest import requests - - +from fastapi.testclient import TestClient +from main import app +import os + +client = TestClient(app) def test_register_face(): - with open('test-faces/07c64ef1-b32e-4396-97ea-0894249d58ee.jpg', 'rb') as image_file: - url = "http://localhost:8000/upload/" - response = requests.post(url, files={'file': image_file}) - + # Open a test image file in binary mode + IMAGEDIR = "test-faces/" + with open("test-faces/07c64ef1-b32e-4396-97ea-0894249d58ee.jpg", "rb") as image_file: + # Create a tuple with the file's name and its content + file_tuple = ("test_image.jpg", image_file.read()) + # Create a dictionary with the file's data + data = {"file": file_tuple} + # Send a POST request to the register_face endpoint with the test image + response = client.post("/upload/", files=data) + # Assert that the response status code is 200 (success) assert response.status_code == 200 - filename = response.json()['filename'] - url = f"http://localhost:8000/delete/{filename}" - response = requests.delete(url) - assert response.status_code == 200 - assert response.json() == {'message': 'Face deleted successfully'} - - - - - # assert response.json() == {'message': 'Face registered successfully'} -# Open the image file in binary mode -# with open('test-faces/07c64ef1-b32e-4396-97ea-0894249d58ee.jpg', 'rb') as image_file: -# # Define the URL where you want to send the POST request -# url = "http://localhost:8000/upload/" - -# # Send the POST request with the file -# response = requests.post(url, files={'file': image_file}) - -# # Print the response -# assert response.status_code == 200 -# # print(response.json()) \ No newline at end of file + # Assert that the response json contains the filename + assert "filename" in response.json() + # Assert that the file was saved correctly + assert os.path.exists(f"{IMAGEDIR}{response.json()['filename']}") + # Clean up the created file + os.remove(f"{IMAGEDIR}{response.json()['filename']}") \ No newline at end of file