forked from mbillow/python-chargepoint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_token_caching.py
More file actions
63 lines (50 loc) · 1.88 KB
/
test_token_caching.py
File metadata and controls
63 lines (50 loc) · 1.88 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
#!/usr/bin/env python3
"""
Simple test script to verify token caching functionality.
"""
import tempfile
import os
from pathlib import Path
from python_chargepoint.token_cache import TokenCache
def test_token_cache():
"""Test the token cache functionality."""
print("Testing token cache functionality...")
# Create a temporary directory for testing
with tempfile.TemporaryDirectory() as temp_dir:
print(f"Using temporary directory: {temp_dir}")
# Initialize cache
cache = TokenCache(cache_dir=temp_dir)
print("✓ TokenCache initialized")
# Test saving a token
username = "testuser"
session_token = "test_token_123"
user_id = "12345"
cache.save_token(username, session_token, user_id, expires_in_hours=1)
print("✓ Token saved to cache")
# Test loading the token
cached_data = cache.load_token(username)
if cached_data:
print("✓ Token loaded from cache")
print(f" Username: {cached_data['username']}")
print(f" User ID: {cached_data['user_id']}")
print(f" Token: {cached_data['session_token'][:10]}...")
else:
print("✗ Failed to load token from cache")
return False
# Test clearing the token
cache.clear_token(username)
cached_data = cache.load_token(username)
if cached_data is None:
print("✓ Token cleared from cache")
else:
print("✗ Failed to clear token from cache")
return False
print("✓ All token cache tests passed!")
return True
if __name__ == "__main__":
success = test_token_cache()
if success:
print("\n🎉 Token caching is working correctly!")
else:
print("\n❌ Token caching tests failed!")
exit(1)