parent
96c900343a
commit
7912432db3
@ -0,0 +1,225 @@
|
||||
from test_main import *
|
||||
from PIL import Image
|
||||
import io
|
||||
import numpy as np
|
||||
|
||||
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_image_without_pin(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}"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert "id" in data
|
||||
|
||||
def test_add_image_with_pin(token, user_id):
|
||||
# Créer d'abord un pin
|
||||
pin_response = client.post(
|
||||
"/pin/add",
|
||||
json={
|
||||
"title": "Test Pin",
|
||||
"description": "Test Description",
|
||||
"location": [0,0],
|
||||
"files": [],
|
||||
"user_id": user_id
|
||||
},
|
||||
headers={"Authorization": f"Bearer {token}"}
|
||||
)
|
||||
pin_id = pin_response.json()["id"]
|
||||
|
||||
# Ajouter une image au pin
|
||||
test_image = create_test_image()
|
||||
response = client.post(
|
||||
f"/image/pin/{pin_id}/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}"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert "id" in data
|
||||
|
||||
def test_get_image(token):
|
||||
# D'abord créer un pin
|
||||
pin_response = client.post(
|
||||
"/pin/add",
|
||||
json={
|
||||
"title": "Test Pin",
|
||||
"description": "Test Description",
|
||||
"location": [0,0],
|
||||
"files": [],
|
||||
"user_id": "682c71ee4b4ac435f3d5af17"
|
||||
},
|
||||
headers={"Authorization": f"Bearer {token}"}
|
||||
)
|
||||
pin_id = pin_response.json()["id"]
|
||||
|
||||
# Ajouter une image au pin
|
||||
test_image = create_test_image()
|
||||
add_response = client.post(
|
||||
f"/image/pin/{pin_id}/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 = add_response.json()["id"]
|
||||
|
||||
# Récupérer l'image
|
||||
response = client.get(
|
||||
f"/image/{image_id}",
|
||||
headers={"Authorization": f"Bearer {token}"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert response.headers["content-type"] == "image/jpeg"
|
||||
|
||||
def test_get_image_wrong_format(token):
|
||||
response = client.get(
|
||||
"/image/randomIdThatDoesntExists",
|
||||
headers={"Authorization": f"Bearer {token}"}
|
||||
)
|
||||
assert response.status_code == 422
|
||||
|
||||
def test_get_image_unknown_id(token):
|
||||
response = client.get(
|
||||
"/image/123456789987654321abcdef",
|
||||
headers={"Authorization": f"Bearer {token}"}
|
||||
)
|
||||
assert response.status_code == 404
|
||||
|
||||
def test_delete_image(token):
|
||||
# D'abord créer un pin
|
||||
pin_response = client.post(
|
||||
"/pin/add",
|
||||
json={
|
||||
"title": "Test Pin",
|
||||
"description": "Test Description",
|
||||
"location": [0,0],
|
||||
"files": [],
|
||||
"user_id": "682c71ee4b4ac435f3d5af17"
|
||||
},
|
||||
headers={"Authorization": f"Bearer {token}"}
|
||||
)
|
||||
pin_id = pin_response.json()["id"]
|
||||
|
||||
# Ajouter une image au pin
|
||||
test_image = create_test_image()
|
||||
add_response = client.post(
|
||||
f"/image/pin/{pin_id}/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 = add_response.json()["id"]
|
||||
|
||||
# Supprimer l'image
|
||||
response = client.delete(
|
||||
f"/image/{image_id}",
|
||||
headers={"Authorization": f"Bearer {token}"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
|
||||
# Vérifier que l'image n'est plus accessible
|
||||
get_response = client.get(
|
||||
f"/image/{image_id}",
|
||||
headers={"Authorization": f"Bearer {token}"}
|
||||
)
|
||||
assert get_response.status_code == 404
|
||||
|
||||
def test_update_caption(token):
|
||||
# D'abord créer un pin
|
||||
pin_response = client.post(
|
||||
"/pin/add",
|
||||
json={
|
||||
"title": "Test Pin",
|
||||
"description": "Test Description",
|
||||
"location": [0,0],
|
||||
"files": [],
|
||||
"user_id": "682c71ee4b4ac435f3d5af17"
|
||||
},
|
||||
headers={"Authorization": f"Bearer {token}"}
|
||||
)
|
||||
pin_id = pin_response.json()["id"]
|
||||
|
||||
# Ajouter une image au pin
|
||||
test_image = create_test_image()
|
||||
add_response = client.post(
|
||||
f"/image/pin/{pin_id}/add",
|
||||
files={"image": ("test.jpg", test_image, "image/jpeg")},
|
||||
data={"exif_date": "2024-03-20T12:00:00", "caption": "Original caption"},
|
||||
headers={"Authorization": f"Bearer {token}"}
|
||||
)
|
||||
image_id = add_response.json()["id"]
|
||||
|
||||
# Mettre à jour la légende
|
||||
response = client.patch(
|
||||
f"/image/{image_id}/caption",
|
||||
json={"caption": "Updated caption"},
|
||||
headers={"Authorization": f"Bearer {token}"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
|
||||
def test_image_permissions(token, token_second):
|
||||
# Ajouter une image avec le premier utilisateur
|
||||
test_image = create_test_image()
|
||||
add_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 = add_response.json()["id"]
|
||||
|
||||
# Le deuxième utilisateur ne devrait pas pouvoir accéder à l'image
|
||||
response = client.get(
|
||||
f"/image/{image_id}",
|
||||
headers={"Authorization": f"Bearer {token_second}"}
|
||||
)
|
||||
|
||||
assert response.status_code == 403
|
||||
|
||||
def test_invalid_image_type(token):
|
||||
# Créer un fichier texte au lieu d'une image
|
||||
text_file = io.BytesIO(b"Not an image")
|
||||
text_file.name = "test.txt" # Ajouter un nom de fichier
|
||||
|
||||
response = client.post(
|
||||
"/image/pin/null/add",
|
||||
files={"image": ("test.txt", text_file, "text/plain")},
|
||||
data={"exif_date": "2024-03-20T12:00:00", "caption": "Test caption"},
|
||||
headers={"Authorization": f"Bearer {token}"}
|
||||
)
|
||||
assert response.status_code == 415
|
||||
|
||||
# K.O
|
||||
#
|
||||
# def test_image_too_large(token):
|
||||
# # Créer une grande image
|
||||
# img = Image.fromarray(np.random.randint(0, 256, (8000, 8000, 3), dtype=np.uint8), 'RGB')
|
||||
# buf = io.BytesIO()
|
||||
# img.save(buf, format='JPEG', quality=100)
|
||||
# buf.seek(0)
|
||||
# buf.name = "large.jpg"
|
||||
|
||||
# # Vérifier la taille de l'image
|
||||
# image_size = len(buf.getvalue())
|
||||
# print(f"Image size: {image_size} bytes ({image_size / (1024*1024):.2f} MB)")
|
||||
|
||||
# response = client.post(
|
||||
# "/image/pin/null/add",
|
||||
# files={"image": ("large.jpg", buf, "image/jpeg")},
|
||||
# data={"exif_date": "2024-03-20T12:00:00", "caption": "Test caption"},
|
||||
# headers={"Authorization": f"Bearer {token}"}
|
||||
# )
|
||||
# print(f"Response status: {response.status_code}")
|
||||
# print(f"Response body: {response.text}")
|
||||
# assert response.status_code == 413
|
Loading…
Reference in new issue