174 lines
No EOL
5.7 KiB
Python
174 lines
No EOL
5.7 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Test du système de credentials sans connexion réelle
|
|
"""
|
|
|
|
from atmo_data_wrapper import AtmoDataClient, AtmoDataException
|
|
import json
|
|
import os
|
|
import tempfile
|
|
|
|
def test_credentials_loading():
|
|
"""Test du chargement des credentials"""
|
|
print("=== Test du système de credentials ===\n")
|
|
|
|
# Test 1: Fichier manquant
|
|
print("1. Test fichier credentials manquant...")
|
|
client = AtmoDataClient(credentials_file="inexistant.json")
|
|
|
|
try:
|
|
client._load_credentials()
|
|
print("❌ Erreur: Exception attendue pour fichier manquant")
|
|
except AtmoDataException as e:
|
|
if "non trouvé" in str(e):
|
|
print("✅ Exception correcte pour fichier manquant")
|
|
else:
|
|
print(f"❌ Message d'erreur inattendu: {e}")
|
|
|
|
# Test 2: Fichier JSON invalide
|
|
print("\n2. Test fichier JSON invalide...")
|
|
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.json') as f:
|
|
f.write("{ invalid json }")
|
|
invalid_file = f.name
|
|
|
|
try:
|
|
client_invalid = AtmoDataClient(credentials_file=invalid_file)
|
|
client_invalid._load_credentials()
|
|
print("❌ Erreur: Exception attendue pour JSON invalide")
|
|
except AtmoDataException as e:
|
|
if "JSON" in str(e):
|
|
print("✅ Exception correcte pour JSON invalide")
|
|
else:
|
|
print(f"❌ Message d'erreur inattendu: {e}")
|
|
finally:
|
|
os.unlink(invalid_file)
|
|
|
|
# Test 3: Champs manquants
|
|
print("\n3. Test champs manquants...")
|
|
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.json') as f:
|
|
json.dump({"username": "test"}, f) # Manque password
|
|
incomplete_file = f.name
|
|
|
|
try:
|
|
client_incomplete = AtmoDataClient(credentials_file=incomplete_file)
|
|
client_incomplete._load_credentials()
|
|
print("❌ Erreur: Exception attendue pour champs manquants")
|
|
except ValueError as e:
|
|
if "manquants" in str(e):
|
|
print("✅ Exception correcte pour champs manquants")
|
|
else:
|
|
print(f"❌ Message d'erreur inattendu: {e}")
|
|
finally:
|
|
os.unlink(incomplete_file)
|
|
|
|
# Test 4: Fichier valide
|
|
print("\n4. Test fichier credentials valide...")
|
|
test_credentials = {
|
|
"username": "test_user",
|
|
"password": "test_pass",
|
|
"api_url": "https://test-api.example.com"
|
|
}
|
|
|
|
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.json') as f:
|
|
json.dump(test_credentials, f)
|
|
valid_file = f.name
|
|
|
|
try:
|
|
client_valid = AtmoDataClient(credentials_file=valid_file)
|
|
credentials = client_valid._load_credentials()
|
|
|
|
if credentials['username'] == 'test_user' and credentials['password'] == 'test_pass':
|
|
print("✅ Credentials chargés correctement")
|
|
print(f" Username: {credentials['username']}")
|
|
print(f" API URL mise à jour: {client_valid.base_url}")
|
|
else:
|
|
print("❌ Credentials incorrects")
|
|
|
|
except Exception as e:
|
|
print(f"❌ Erreur inattendue: {e}")
|
|
finally:
|
|
os.unlink(valid_file)
|
|
|
|
# Test 5: Login avec credentials
|
|
print("\n5. Test méthode login avec credentials...")
|
|
|
|
# Créer un client avec mock
|
|
client_mock = AtmoDataClient(credentials_file=valid_file)
|
|
|
|
# Mock de la méthode _make_request pour simuler réponse login
|
|
def mock_post(url, json=None):
|
|
class MockResponse:
|
|
def raise_for_status(self):
|
|
pass
|
|
def json(self):
|
|
return {"token": "test_token_123"}
|
|
return MockResponse()
|
|
|
|
# Remplacer temporairement
|
|
original_post = client_mock.session.post
|
|
client_mock.session.post = mock_post
|
|
|
|
# Créer un fichier credentials temporaire pour ce test
|
|
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.json') as f:
|
|
json.dump(test_credentials, f)
|
|
temp_cred_file = f.name
|
|
|
|
try:
|
|
client_mock.credentials_file = temp_cred_file
|
|
success = client_mock.login() # Sans paramètres, doit utiliser le fichier
|
|
|
|
if success and client_mock.token == "test_token_123":
|
|
print("✅ Login avec credentials automatique réussi")
|
|
else:
|
|
print("❌ Login avec credentials échoué")
|
|
|
|
except Exception as e:
|
|
print(f"❌ Erreur login: {e}")
|
|
finally:
|
|
client_mock.session.post = original_post
|
|
os.unlink(temp_cred_file)
|
|
|
|
print("\n=== Tests du système de credentials terminés ===")
|
|
|
|
def test_auto_login():
|
|
"""Test de la méthode auto_login"""
|
|
print("\n=== Test auto_login ===\n")
|
|
|
|
test_credentials = {
|
|
"username": "auto_user",
|
|
"password": "auto_pass"
|
|
}
|
|
|
|
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.json') as f:
|
|
json.dump(test_credentials, f)
|
|
cred_file = f.name
|
|
|
|
try:
|
|
client = AtmoDataClient(credentials_file=cred_file)
|
|
|
|
# Mock de la session
|
|
def mock_post(url, json=None):
|
|
class MockResponse:
|
|
def raise_for_status(self):
|
|
pass
|
|
def json(self):
|
|
return {"token": "auto_token_456"}
|
|
return MockResponse()
|
|
|
|
client.session.post = mock_post
|
|
|
|
success = client.auto_login()
|
|
|
|
if success and client.token == "auto_token_456":
|
|
print("✅ auto_login() fonctionne correctement")
|
|
else:
|
|
print("❌ auto_login() a échoué")
|
|
|
|
except Exception as e:
|
|
print(f"❌ Erreur auto_login: {e}")
|
|
finally:
|
|
os.unlink(cred_file)
|
|
|
|
if __name__ == "__main__":
|
|
test_credentials_loading()
|
|
test_auto_login() |