diff --git a/tests/test_pins.py b/tests/test_pins.py index 46a27ba..2d8d5ca 100644 --- a/tests/test_pins.py +++ b/tests/test_pins.py @@ -9,18 +9,20 @@ def create_test_image(): img_byte_arr.seek(0) return img_byte_arr -def test_add_pin(token, user_id): - # D'abord créer une image +def add_test_image(token): test_image = create_test_image() - image_response = client.post( + 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"] + return response.json()["id"] + +def create_test_pin(token, user_id, image_id=None): + if image_id is None: + image_id = add_test_image(token) - # Créer un pin avec cette image response = client.post( "/pin/add", json={ @@ -32,9 +34,12 @@ def test_add_pin(token, user_id): }, headers={"Authorization": f"Bearer {token}"} ) - assert response.status_code == 200 - data = response.json() - assert "id" in data + return response.json()["id"] + +def test_add_pin(token, user_id): + image_id = add_test_image(token) + pin_id = create_test_pin(token, user_id, image_id) + assert pin_id is not None def test_list_pins(token): response = client.get("/pins", headers={"Authorization": f"Bearer {token}"}) @@ -43,31 +48,9 @@ def test_list_pins(token): assert isinstance(data, list) def test_get_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"] + image_id = add_test_image(token) + pin_id = create_test_pin(token, user_id, image_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"] - - # Récupérer le pin response = client.get( f"/pin/{pin_id}", headers={"Authorization": f"Bearer {token}"} @@ -88,31 +71,9 @@ def test_get_pin_unknown_id(token): assert response.status_code == 404 def test_update_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"] + image_id = add_test_image(token) + pin_id = create_test_pin(token, user_id, image_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"] - - # Mettre à jour le pin response = client.patch( f"/pin/{pin_id}", json={ @@ -126,7 +87,6 @@ def test_update_pin(token, user_id): ) assert response.status_code == 200 - # Vérifier que le pin a été mis à jour get_response = client.get( f"/pin/{pin_id}", headers={"Authorization": f"Bearer {token}"} @@ -146,38 +106,15 @@ def test_update_wrong_unknown_id(token): 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"] + image_id = add_test_image(token) + pin_id = create_test_pin(token, user_id, image_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}"}