2022-03-09 16:52:07 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-05-04 23:11:20 +02:00
|
|
|
"""Test API
|
2022-03-09 16:52:07 +01:00
|
|
|
|
|
|
|
Utilisation :
|
|
|
|
créer les variables d'environnement: (indiquer les valeurs
|
|
|
|
pour le serveur ScoDoc que vous voulez interroger)
|
|
|
|
|
|
|
|
export SCODOC_URL="https://scodoc.xxx.net/"
|
2022-05-04 23:11:20 +02:00
|
|
|
export API_USER="xxx"
|
2022-03-09 16:52:07 +01:00
|
|
|
export SCODOC_PASSWD="xxx"
|
|
|
|
export CHECK_CERTIFICATE=0 # ou 1 si serveur de production avec certif SSL valide
|
|
|
|
|
|
|
|
(on peut aussi placer ces valeurs dans un fichier .env du répertoire tests/api).
|
|
|
|
"""
|
|
|
|
import os
|
|
|
|
import requests
|
2022-05-04 23:11:20 +02:00
|
|
|
from dotenv import load_dotenv
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
BASEDIR = "/opt/scodoc/tests/api"
|
|
|
|
load_dotenv(os.path.join(BASEDIR, ".env"))
|
|
|
|
CHECK_CERTIFICATE = bool(os.environ.get("CHECK_CERTIFICATE", False))
|
|
|
|
SCODOC_URL = os.environ["SCODOC_URL"]
|
|
|
|
API_URL = SCODOC_URL + "/ScoDoc/api"
|
|
|
|
API_USER = os.environ.get("API_USER", "test")
|
|
|
|
API_PASSWORD = os.environ.get("API_PASSWD", "test")
|
2022-08-05 06:55:05 +02:00
|
|
|
API_USER_ADMIN = os.environ.get("API_USER_ADMIN", "admin_api")
|
|
|
|
API_PASSWORD_ADMIN = os.environ.get("API_PASSWD_ADMIN", "admin_api")
|
2022-05-11 04:14:42 +02:00
|
|
|
DEPT_ACRONYM = "TAPI"
|
2022-05-04 23:11:20 +02:00
|
|
|
print(f"SCODOC_URL={SCODOC_URL}")
|
|
|
|
print(f"API URL={API_URL}")
|
|
|
|
|
|
|
|
|
2022-08-05 06:55:05 +02:00
|
|
|
def get_auth_headers(user, password) -> dict:
|
|
|
|
"Demande de jeton, dict à utiliser dans les en-têtes de requêtes http"
|
|
|
|
r0 = requests.post(API_URL + "/tokens", auth=(user, password))
|
2022-03-09 16:52:07 +01:00
|
|
|
token = r0.json()["token"]
|
2022-04-13 12:39:10 +02:00
|
|
|
return {"Authorization": f"Bearer {token}"}
|
2022-08-01 21:42:19 +02:00
|
|
|
|
|
|
|
|
2022-08-05 06:55:05 +02:00
|
|
|
@pytest.fixture
|
|
|
|
def api_headers() -> dict:
|
|
|
|
"""Jeton, utilisateur API ordinaire"""
|
|
|
|
return get_auth_headers(API_USER, API_PASSWORD)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def api_admin_headers() -> dict:
|
|
|
|
"""Jeton, utilisateur API SuperAdmin"""
|
|
|
|
return get_auth_headers(API_USER_ADMIN, API_PASSWORD_ADMIN)
|
|
|
|
|
|
|
|
|
2022-08-01 21:42:19 +02:00
|
|
|
class APIError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def GET(path: str, headers={}, errmsg=None, dept=None):
|
|
|
|
"""Get and returns as JSON"""
|
|
|
|
if dept:
|
|
|
|
url = SCODOC_URL + f"/ScoDoc/{dept}/api" + path
|
|
|
|
else:
|
|
|
|
url = API_URL + path
|
2022-08-02 16:21:41 +02:00
|
|
|
r = requests.get(url, headers=headers or CUR_HEADERS, verify=CHECK_CERTIFICATE)
|
2022-08-01 21:42:19 +02:00
|
|
|
if r.status_code != 200:
|
|
|
|
raise APIError(errmsg or f"""erreur status={r.status_code} !\n{r.text}""")
|
|
|
|
return r.json() # decode la reponse JSON
|
|
|
|
|
|
|
|
|
|
|
|
def POST_JSON(path: str, data: dict = {}, headers={}, errmsg=None):
|
|
|
|
"""Post"""
|
|
|
|
r = requests.post(
|
|
|
|
API_URL + path,
|
|
|
|
json=data,
|
|
|
|
headers=headers,
|
|
|
|
verify=CHECK_CERTIFICATE,
|
|
|
|
)
|
|
|
|
if r.status_code != 200:
|
|
|
|
raise APIError(errmsg or f"erreur status={r.status_code} !\n{r.text}")
|
|
|
|
return r.json() # decode la reponse JSON
|