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 add_test_image(token): test_image = create_test_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}"} ) 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) 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}"} ) 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_add_pin_invalid_files_format(token, user_id): response = client.post( "/pin/add", json={ "title": "Test Pin", "description": "Test Description", "location": [0,0], "files": ["abc"], "user_id": user_id }, headers={"Authorization": f"Bearer {token}"} ) assert response.status_code == 422 assert "Invalid image ID format" in response.json()["detail"][0]["msg"] def test_list_pins(token): response = client.get("/pins", headers={"Authorization": f"Bearer {token}"}) assert response.status_code == 200 data = response.json() assert isinstance(data, list) def test_get_pin(token, user_id): image_id = add_test_image(token) pin_id = create_test_pin(token, user_id, image_id) 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 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 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): image_id = add_test_image(token) pin_id = create_test_pin(token, user_id, image_id) 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_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"] == [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}"}) assert response.status_code == 422 def test_update_wrong_unknown_id(token): response = client.get(f"/pin/123456789987654321abcdef", headers={"Authorization": f"Bearer {token}"}) assert response.status_code == 404 def test_delete_pin(token, user_id): image_id = add_test_image(token) pin_id = create_test_pin(token, user_id, image_id) response = client.delete( f"/pin/{pin_id}", headers={"Authorization": f"Bearer {token}"} ) assert response.status_code == 200 get_response = client.get( f"/pin/{pin_id}", headers={"Authorization": f"Bearer {token}"} ) assert get_response.status_code == 404