Files
adif-api-reverse-engineering/test_simple.py

108 lines
3.1 KiB
Python

#!/usr/bin/env python3
"""
Test simple para verificar que la autenticación funciona de manera reproducible
"""
import requests
from adif_auth import AdifAuthenticator
import json
import uuid
ACCESS_KEY = "and20210615"
SECRET_KEY = "Jthjtr946RTt"
def test_departures_once(user_id, test_num):
"""
Hace una petición simple de departures
"""
auth = AdifAuthenticator(access_key=ACCESS_KEY, secret_key=SECRET_KEY)
url = "https://circulacion.api.adif.es/portroyalmanager/secure/circulationpaths/departures/traffictype/"
payload = {
"commercialService": "BOTH",
"commercialStopType": "BOTH",
"page": {"pageNumber": 0},
"stationCode": "10200",
"trafficType": "ALL"
}
headers = auth.get_auth_headers("POST", url, payload, user_id=user_id)
headers["User-key"] = auth.USER_KEY_CIRCULATION
response = requests.post(url, json=payload, headers=headers, timeout=10)
status = "" if response.status_code == 200 else ""
print(f"{status} Test #{test_num}: Status {response.status_code}")
if response.status_code == 200:
data = response.json()
total = data.get('totalElements', 'N/A')
print(f" Total de salidas: {total}")
return True
else:
print(f" Error: {response.text[:100]}")
return False
def test_betweenstations_once(user_id, test_num):
"""
Hace una petición de betweenstations
"""
auth = AdifAuthenticator(access_key=ACCESS_KEY, secret_key=SECRET_KEY)
url = "https://circulacion.api.adif.es/portroyalmanager/secure/circulationpaths/betweenstations/traffictype/"
payload = {
"commercialService": "BOTH",
"commercialStopType": "BOTH",
"originStationCode": "10200",
"destinationStationCode": "71801",
"page": {"pageNumber": 0},
"trafficType": "ALL"
}
headers = auth.get_auth_headers("POST", url, payload, user_id=user_id)
headers["User-key"] = auth.USER_KEY_CIRCULATION
response = requests.post(url, json=payload, headers=headers, timeout=10)
status = "" if response.status_code == 200 else ""
print(f"{status} Test #{test_num}: Status {response.status_code}")
if response.status_code == 200:
data = response.json()
total = data.get('totalElements', 'N/A')
print(f" Total de trenes: {total}")
return True
else:
print(f" Error: {response.text[:100]}")
return False
def main():
print("="*70)
print("TEST SIMPLE - Verificar reproducibilidad")
print("="*70)
user_id = str(uuid.uuid4())
print(f"\nUSER_ID: {user_id}\n")
# Probar departures 3 veces
print("-" * 70)
print("DEPARTURES (debería funcionar todas las veces):")
print("-" * 70)
for i in range(1, 4):
test_departures_once(user_id, i)
print()
# Probar betweenstations 3 veces
print("-" * 70)
print("BETWEENSTATIONS (probar si funciona):")
print("-" * 70)
for i in range(1, 4):
test_betweenstations_once(user_id, i)
print()
if __name__ == "__main__":
main()