diff --git a/tests/test_pins.py b/tests/test_pins.py index d2f1c14..46a27ba 100644 --- a/tests/test_pins.py +++ b/tests/test_pins.py @@ -1,7 +1,37 @@ from test_main import * +from PIL import Image +import io + +def create_test_image(): + img = Image.new('RGB', (100, 100), color='red') + img_byte_arr = io.BytesIO() + img.save(img_byte_arr, format='JPEG') + img_byte_arr.seek(0) + return img_byte_arr def test_add_pin(token, user_id): - response = client.post("/pin/add", json={"title": "Test Pin", "description": "Test Description", "location": [0,0], "files": ["Test file 1"], "user_id": user_id}, headers={"Authorization": f"Bearer {token}"}) + # D'abord créer une image + test_image = create_test_image() + image_response = client.post( + "/image/pin/null/add", + files={"image": ("test.jpg", test_image, "image/jpeg")}, + data={"exif_date": "2024-03-20T12:00:00", "caption": "Test caption"}, + headers={"Authorization": f"Bearer {token}"} + ) + image_id = image_response.json()["id"] + + # Créer un pin avec cette image + response = client.post( + "/pin/add", + json={ + "title": "Test Pin", + "description": "Test Description", + "location": [0,0], + "files": [image_id], + "user_id": user_id + }, + headers={"Authorization": f"Bearer {token}"} + ) assert response.status_code == 200 data = response.json() assert "id" in data @@ -13,18 +43,42 @@ def test_list_pins(token): assert isinstance(data, list) def test_get_pin(token, user_id): - add_pin_response = client.post("/pin/add", json={"title": "Test Pin", "description": "Test Description", "location": [0,0], "files": ["Test file 1"], "user_id": user_id}, headers={"Authorization": f"Bearer {token}"}) + # D'abord créer une image + test_image = create_test_image() + image_response = client.post( + "/image/pin/null/add", + files={"image": ("test.jpg", test_image, "image/jpeg")}, + data={"exif_date": "2024-03-20T12:00:00", "caption": "Test caption"}, + headers={"Authorization": f"Bearer {token}"} + ) + image_id = image_response.json()["id"] + + # Ensuite créer un pin avec cette image + add_pin_response = client.post( + "/pin/add", + json={ + "title": "Test Pin", + "description": "Test Description", + "location": [0,0], + "files": [image_id], + "user_id": user_id + }, + headers={"Authorization": f"Bearer {token}"} + ) pin_id = add_pin_response.json()["id"] - response = client.get(f"/pin/{pin_id}", headers={"Authorization": f"Bearer {token}"}) + # Récupérer le pin + response = client.get( + f"/pin/{pin_id}", + headers={"Authorization": f"Bearer {token}"} + ) assert response.status_code == 200 data = response.json() assert data["title"] == "Test Pin" assert data["description"] == "Test Description" assert data["location"] == [0,0] - assert data["files"] == ["Test file 1"] + assert image_id in data["files"] - def test_get_pin_wrong_format(token): response = client.get(f"/pin/randomIdThatDoesntExists", headers={"Authorization": f"Bearer {token}"}) assert response.status_code == 422 @@ -33,21 +87,55 @@ def test_get_pin_unknown_id(token): response = client.get(f"/pin/123456789987654321abcdef", headers={"Authorization": f"Bearer {token}"}) assert response.status_code == 404 - def test_update_pin(token, user_id): - add_pin_response = client.post("/pin/add", json={"title": "Test Pin", "description": "Test Description", "location": [0,0], "files": ["Test file 1"], "user_id": user_id}, headers={"Authorization": f"Bearer {token}"}) + # D'abord créer une image + test_image = create_test_image() + image_response = client.post( + "/image/pin/null/add", + files={"image": ("test.jpg", test_image, "image/jpeg")}, + data={"exif_date": "2024-03-20T12:00:00", "caption": "Test caption"}, + headers={"Authorization": f"Bearer {token}"} + ) + image_id = image_response.json()["id"] + + # Créer un pin avec cette image + add_pin_response = client.post( + "/pin/add", + json={ + "title": "Test Pin", + "description": "Test Description", + "location": [0,0], + "files": [image_id], + "user_id": user_id + }, + headers={"Authorization": f"Bearer {token}"} + ) pin_id = add_pin_response.json()["id"] - update_response = client.patch(f"/pin/{pin_id}", json={"title": "Updated Pin", "description": "Updated Description", "location": [0,1], "files": ["Updated file 1"], "user_id": user_id}, headers={"Authorization": f"Bearer {token}"}) - assert update_response.status_code == 200 + # Mettre à jour le pin + response = client.patch( + f"/pin/{pin_id}", + json={ + "title": "Updated Pin", + "description": "Updated Description", + "location": [1,1], + "files": [image_id], + "user_id": user_id + }, + headers={"Authorization": f"Bearer {token}"} + ) + assert response.status_code == 200 - get_pin_response = client.get(f"/pin/{pin_id}", headers={"Authorization": f"Bearer {token}"}) - data = get_pin_response.json() + # Vérifier que le pin a été mis à jour + get_response = client.get( + f"/pin/{pin_id}", + headers={"Authorization": f"Bearer {token}"} + ) + data = get_response.json() assert data["title"] == "Updated Pin" assert data["description"] == "Updated Description" - assert data["location"] == [0,1] - assert data["files"] == ["Updated file 1"] - + assert data["location"] == [1,1] + assert image_id in data["files"] def test_update_wrong_format(token): response = client.get(f"/pin/randomIdThatDoesntExists", headers={"Authorization": f"Bearer {token}"}) @@ -55,4 +143,43 @@ def test_update_wrong_format(token): def test_update_wrong_unknown_id(token): response = client.get(f"/pin/123456789987654321abcdef", headers={"Authorization": f"Bearer {token}"}) - assert response.status_code == 404 \ No newline at end of file + assert response.status_code == 404 + +def test_delete_pin(token, user_id): + # D'abord créer une image + test_image = create_test_image() + image_response = client.post( + "/image/pin/null/add", + files={"image": ("test.jpg", test_image, "image/jpeg")}, + data={"exif_date": "2024-03-20T12:00:00", "caption": "Test caption"}, + headers={"Authorization": f"Bearer {token}"} + ) + image_id = image_response.json()["id"] + + # Créer un pin avec cette image + add_pin_response = client.post( + "/pin/add", + json={ + "title": "Test Pin", + "description": "Test Description", + "location": [0,0], + "files": [image_id], + "user_id": user_id + }, + headers={"Authorization": f"Bearer {token}"} + ) + pin_id = add_pin_response.json()["id"] + + # Supprimer le pin + response = client.delete( + f"/pin/{pin_id}", + headers={"Authorization": f"Bearer {token}"} + ) + assert response.status_code == 200 + + # Vérifier que le pin n'existe plus + get_response = client.get( + f"/pin/{pin_id}", + headers={"Authorization": f"Bearer {token}"} + ) + assert get_response.status_code == 404 \ No newline at end of file