-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_credential_fallback.py
More file actions
147 lines (117 loc) · 4.83 KB
/
test_credential_fallback.py
File metadata and controls
147 lines (117 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python3
"""
Test script for Enhanced Credential Fallback System
Tests cross-environment fallback and auto-provisioning from environment variables
"""
import sys
import os
sys.path.insert(0, 'backend')
from app.services.get_env_credentials import (
get_credentials_from_env,
get_available_env_services,
get_env_service_status,
SERVICE_ENV_PATTERNS
)
def test_get_credentials_from_env():
"""Test retrieving credentials from environment variables"""
print("Testing get_credentials_from_env...")
# Test with missing environment variables
result = get_credentials_from_env("razorpay")
print(f" Razorpay (no env vars): {result}")
assert result is None, "Should return None when env vars not set"
# Set environment variables for testing
os.environ["RAZORPAY_KEY_ID"] = "test_key_id"
os.environ["RAZORPAY_KEY_SECRET"] = "test_key_secret"
# Test with environment variables set
result = get_credentials_from_env("razorpay")
print(f" Razorpay (with env vars): {result}")
assert result is not None, "Should return credentials when env vars are set"
assert result["RAZORPAY_KEY_ID"] == "test_key_id"
assert result["RAZORPAY_KEY_SECRET"] == "test_key_secret"
# Clean up
del os.environ["RAZORPAY_KEY_ID"]
del os.environ["RAZORPAY_KEY_SECRET"]
print(" get_credentials_from_env tests passed!")
def test_get_available_env_services():
"""Test getting list of services with environment variables"""
print("Testing get_available_env_services...")
# Initially no services should be available
available = get_available_env_services()
print(f" Available (no vars): {available}")
assert len(available) == 0, "Should have no available services"
# Set environment variables
os.environ["PAYPAL_CLIENT_ID"] = "test_client_id"
os.environ["PAYPAL_CLIENT_SECRET"] = "test_client_secret"
# Now PayPal should be available
available = get_available_env_services()
print(f" Available (with paypal): {available}")
assert "paypal" in available, "PayPal should be available"
# Clean up
del os.environ["PAYPAL_CLIENT_ID"]
del os.environ["PAYPAL_CLIENT_SECRET"]
print(" get_available_env_services tests passed!")
def test_get_env_service_status():
"""Test getting status of all services"""
print("Testing get_env_service_status...")
# Clear all environment variables first
for pattern in SERVICE_ENV_PATTERNS.values():
for var in pattern["required"]:
if var in os.environ:
del os.environ[var]
# Get status
status = get_env_service_status()
print(f" Status keys: {list(status.keys())}")
# Razorpay should show as not available with missing required vars
assert "razorpay" in status
assert status["razorpay"]["available"] == False
assert "RAZORPAY_KEY_ID" in status["razorpay"]["missing_required"]
assert "RAZORPAY_KEY_SECRET" in status["razorpay"]["missing_required"]
# Set environment variables
os.environ["RAZORPAY_KEY_ID"] = "test_key_id"
os.environ["RAZORPAY_KEY_SECRET"] = "test_key_secret"
# Now razorpay should be available
status = get_env_service_status()
print(f" Razorpay status (with vars): {status['razorpay']}")
assert status["razorpay"]["available"] == True
assert len(status["razorpay"]["missing_required"]) == 0
# Clean up
del os.environ["RAZORPAY_KEY_ID"]
del os.environ["RAZORPAY_KEY_SECRET"]
print(" get_env_service_status tests passed!")
def test_service_env_patterns():
"""Test that all expected services have patterns defined"""
print("Testing SERVICE_ENV_PATTERNS...")
expected_services = ["razorpay", "paypal", "twilio", "aws_s3"]
for service in expected_services:
assert service in SERVICE_ENV_PATTERNS, f"{service} should be in patterns"
pattern = SERVICE_ENV_PATTERNS[service]
assert len(pattern["required"]) > 0, f"{service} should have required fields"
print(f" {service}: required={pattern['required']}")
print(" SERVICE_ENV_PATTERNS tests passed!")
def run_all_tests():
"""Run all tests"""
print("=" * 60)
print("Enhanced Credential Fallback System Tests")
print("=" * 60)
print()
try:
test_service_env_patterns()
print()
test_get_credentials_from_env()
print()
test_get_available_env_services()
print()
test_get_env_service_status()
print()
print("=" * 60)
print("All tests passed!")
print("=" * 60)
return True
except Exception as e:
print(f"\nTest failed: {e}")
import traceback
traceback.print_exc()
return False
if __name__ == "__main__":
success = run_all_tests()
sys.exit(0 if success else 1)