fabriquedoc/backend/test-confiance.py
François Pelletier dd22432b48 Gros refactoring
2024-12-31 21:01:12 -05:00

101 lines
No EOL
3.4 KiB
Python

import requests
# Base URL for the API
BASE_URL = "http://127.0.0.1:8080"
# Function to get the access token
def get_access_token():
login_url = f"{BASE_URL}/token/"
headers = {"Content-Type": "application/x-www-form-urlencoded"}
data = {"username": "francois", "password": "password"}
response = requests.post(login_url, headers=headers, data=data)
if response.status_code == 200:
return response.json()["access_token"]
else:
raise Exception(f"Login failed: {response.status_code}, {response.text}")
# Get the access token
access_token = get_access_token()
print(f"Access token: {access_token}")
# Common headers for all requests
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {access_token}"
}
# Test cases
test_cases = [
{
"name": "Instagram Story",
"accept": "application/zip",
"data": {
"format": "instagram-story",
"style": "consultation-express",
"linkcolor": "blue",
"pdfengine": "lualatex",
"content": "# Ceci est un titre\n## Ceci est un sous-titre\n\nCeci est un paragraphe\n\n## Ceci est un autre sous-titre\n\n> Ceci est du code\n\nCeci est un emoji :heart_eyes:\n\n::: {.center}\nCeci est centré\n:::",
"fontsize": 14,
"paperwidth": 1080,
"paperheight": 1920,
"margin": 90,
"vmargin": 180,
"fps": 15,
"stilltime": 2
}
},
{
"name": "Instagram Fullscreen",
"accept": "application/zip",
"data": {
"format": "instagram-fullscreen",
"style": "consultation-express",
"linkcolor": "blue",
"pdfengine": "lualatex",
"content": "# Ceci est un titre\n## Ceci est un sous-titre\n\nCeci est un paragraphe\n\n## Ceci est un autre sous-titre\n\n> Ceci est du code\n\nCeci est un emoji :heart_eyes:\n\n::: {.center}\nCeci est centré\n:::",
"fontsize": 14,
"paperwidth": 1080,
"paperheight": 1350,
"margin": 90,
"vmargin": 180,
"fps": 15,
"stilltime": 2
}
},
{
"name": "Instagram Carré",
"accept": "application/pdf",
"data": {
"format": "instagram-carre",
"style": "consultation-express",
"linkcolor": "blue",
"pdfengine": "lualatex",
"content": "# Ceci est un titre\n## Ceci est un sous-titre\n\nCeci est un paragraphe\n\n## Ceci est un autre sous-titre\n\n> Ceci est du code\n\nCeci est un emoji :heart_eyes:\n\n::: {.center}\nCeci est centré\n:::",
"fontsize": 14,
"paperwidth": 1080,
"paperheight": 1080,
"margin": 90,
"vmargin": 180,
"fps": 15,
"stilltime": 2
}
}
]
# Run tests
for test in test_cases:
print(f"\nRunning test: {test['name']}")
headers["Accept"] = test["accept"]
response = requests.post(f"{BASE_URL}/generer/", headers=headers, json=test["data"])
print(f"Status code: {response.status_code}")
if response.status_code == 200:
print("Test passed")
if test["accept"] == "application/zip":
print("Received ZIP file")
elif test["accept"] == "application/pdf":
print("Received PDF file")
else:
print(f"Test failed: {response.text}")