85 lines
2.5 KiB
Python
85 lines
2.5 KiB
Python
import pytest
|
|
from io import BytesIO
|
|
from app import app
|
|
|
|
@pytest.fixture
|
|
def client():
|
|
app.config['TESTING'] = True
|
|
with app.test_client() as client:
|
|
yield client
|
|
|
|
def test_index(client):
|
|
response = client.get('/')
|
|
assert response.status_code == 200
|
|
assert b'<html' in response.data # Verifica se uma página HTML é retornada
|
|
|
|
def test_list_faces(client, requests_mock):
|
|
test_url = "http://example.com/faces"
|
|
expected_response = {"faces": ["face1", "face2"]}
|
|
requests_mock.get(test_url, json=expected_response)
|
|
|
|
response = client.post('/list_faces', json={"url": test_url})
|
|
assert response.status_code == 200
|
|
assert response.json == expected_response
|
|
|
|
def test_get_image(client, requests_mock):
|
|
base_url = "http://example.com"
|
|
bucket = "test-bucket"
|
|
image_path = "images/test.jpg"
|
|
image_content = b"fake-image-data"
|
|
|
|
full_url = f"{base_url}/{bucket}/{image_path}"
|
|
requests_mock.get(full_url, content=image_content)
|
|
|
|
response = client.post('/get_image', json={
|
|
"base_url": base_url,
|
|
"bucket": bucket,
|
|
"image_path": image_path
|
|
})
|
|
|
|
assert response.status_code == 200
|
|
assert response.data == image_content
|
|
assert response.mimetype == 'image/jpeg'
|
|
|
|
def test_update_name(client, requests_mock):
|
|
base_url = "http://example.com"
|
|
userID = "123"
|
|
new_name = "new_name"
|
|
full_url = f"{base_url}/{userID}/{new_name}"
|
|
expected_response = {"message": "Name updated successfully"}
|
|
|
|
requests_mock.post(full_url, json=expected_response)
|
|
|
|
response = client.post('/update_name', json={
|
|
"url": base_url,
|
|
"userID": userID,
|
|
"new_name": new_name
|
|
})
|
|
|
|
assert response.status_code == 200
|
|
assert response.json == expected_response
|
|
|
|
def test_list_faces_error(client):
|
|
response = client.post('/list_faces', json={"url": "invalid-url"})
|
|
assert response.status_code == 200
|
|
assert "error" in response.json
|
|
|
|
def test_get_image_error(client):
|
|
response = client.post('/get_image', json={
|
|
"base_url": "http://example.com",
|
|
"bucket": "nonexistent",
|
|
"image_path": "invalid.jpg"
|
|
})
|
|
|
|
assert response.status_code == 400
|
|
assert "error" in response.json
|
|
|
|
def test_update_name_error(client):
|
|
response = client.post('/update_name', json={
|
|
"url": "http://example.com",
|
|
"userID": "nonexistent",
|
|
"new_name": "invalid_name"
|
|
})
|
|
|
|
assert response.status_code == 400
|
|
assert "error" in response.json |