forked from Ikalus1988/MisakaNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_token_manager_nokeyring.py
More file actions
178 lines (145 loc) · 6.46 KB
/
Copy pathtest_token_manager_nokeyring.py
File metadata and controls
178 lines (145 loc) · 6.46 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"""
Tests for hub/master/token_manager.py — no-keyring fallback paths.
Covers P1 item: token manager when keyring is not available.
Verifies that:
1. TokenManager gracefully falls back to plaintext JSON when keyring is absent
2. Warning is emitted for insecure fallback
3. Token save/load round-trips correctly without keyring
4. AuditLogger does not crash on init
"""
import json
import os
import sys
import tempfile
import unittest
from unittest import mock
# Ensure repo root is on path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# Mock aiohttp so hub.master can be imported without the optional dep
sys.modules["aiohttp"] = mock.MagicMock()
# Now safe to import
import hub.master.token_manager # noqa: E402
from hub.master.token_manager import TokenManager, AuditLogger # noqa: E402
class TestTokenManagerNoKeyring(unittest.TestCase):
"""Test TokenManager when keyring is NOT available."""
def setUp(self):
self.tmpdir = tempfile.mkdtemp(prefix="misakanet_test_")
self.token_path = os.path.join(self.tmpdir, ".hermes-tokens")
def tearDown(self):
if os.path.exists(self.token_path):
os.remove(self.token_path)
try:
os.rmdir(self.tmpdir)
except OSError:
pass
@mock.patch("hub.master.token_manager.os.path.expanduser")
def test_init_creates_empty_tokens_when_no_file_no_keyring(
self, mock_expanduser
):
"""TokenManager starts with empty tokens when no keyring and no file."""
mock_expanduser.return_value = self.token_path
# Simulate keyring being completely absent
import builtins
orig_import = builtins.__import__
def mock_import(name, *args, **kwargs):
if name == "keyring" or name.startswith("keyring."):
raise ImportError("No module named 'keyring'")
return orig_import(name, *args, **kwargs)
with mock.patch("builtins.__import__", side_effect=mock_import):
with self.assertWarns(UserWarning):
tm = TokenManager(keyring_service="test-nokeyring")
self.assertEqual(tm._tokens, {})
self.assertFalse(tm._keyring_available)
@mock.patch("hub.master.token_manager.os.path.expanduser")
def test_save_and_load_tokens_no_keyring(self, mock_expanduser):
"""Token round-trip works without keyring."""
mock_expanduser.return_value = self.token_path
os.makedirs(self.tmpdir, exist_ok=True)
import builtins
orig_import = builtins.__import__
def mock_import(name, *args, **kwargs):
if name == "keyring" or name.startswith("keyring."):
raise ImportError("No module named 'keyring'")
return orig_import(name, *args, **kwargs)
with mock.patch("builtins.__import__", side_effect=mock_import):
with self.assertWarns(UserWarning):
tm = TokenManager(keyring_service="test-nokeyring")
tm._tokens["test-token-abc"] = {
"created_at": "2026-01-01T00:00:00",
"expires_at": "2099-01-01T00:00:00",
"secret_hash": "abc123",
}
tm._save_tokens()
self.assertTrue(os.path.exists(self.token_path))
mode = os.stat(self.token_path).st_mode & 0o777
if sys.platform == "win32":
self.assertWarnsRegex(
UserWarning,
"cannot guarantee POSIX 0600",
tm._restrict_plaintext_file,
self.token_path,
)
else:
self.assertEqual(mode, 0o600, f"Expected 0600, got {oct(mode)}")
with open(self.token_path) as f:
saved = json.load(f)
self.assertIn("test-token-abc", saved)
@mock.patch("hub.master.token_manager.os.path.expanduser")
def test_revive_from_plaintext_file(self, mock_expanduser):
"""TokenManager can load tokens from a pre-existing plaintext file."""
mock_expanduser.return_value = self.token_path
pre_data = {
"existing-token": {
"created_at": "2026-01-01T00:00:00",
"expires_at": "2099-01-01T00:00:00",
"secret_hash": "def456",
}
}
with open(self.token_path, "w") as f:
json.dump(pre_data, f)
os.chmod(self.token_path, 0o600)
import builtins
orig_import = builtins.__import__
def mock_import(name, *args, **kwargs):
if name == "keyring" or name.startswith("keyring."):
raise ImportError("No module named 'keyring'")
return orig_import(name, *args, **kwargs)
with mock.patch("builtins.__import__", side_effect=mock_import):
with self.assertWarns(UserWarning):
tm = TokenManager(keyring_service="test-nokeyring")
self.assertIn("existing-token", tm._tokens)
self.assertEqual(
tm._tokens["existing-token"]["secret_hash"], "def456"
)
class TestAuditLogger(unittest.TestCase):
"""Test AuditLogger basic functionality."""
def setUp(self):
self.tmpdir = tempfile.mkdtemp(prefix="misakanet_audit_")
self.log_path = os.path.join(self.tmpdir, "audit.jsonl")
def tearDown(self):
if os.path.exists(self.log_path):
os.remove(self.log_path)
try:
os.rmdir(self.tmpdir)
except OSError:
pass
def test_log_creates_file(self):
"""AuditLogger.log() creates the log file and writes an entry."""
logger = AuditLogger(log_path=self.log_path, retention_days=30)
logger.log("TEST_ACTION", "test-actor-12345", {"detail": "value"})
self.assertTrue(os.path.exists(self.log_path))
with open(self.log_path) as f:
lines = f.readlines()
self.assertEqual(len(lines), 1)
entry = json.loads(lines[0])
self.assertEqual(entry["action"], "TEST_ACTION")
self.assertTrue(entry["actor"].endswith("..."))
self.assertEqual(entry["details"]["detail"], "value")
def test_log_missing_directory_creates_it(self):
"""AuditLogger creates parent directory if missing."""
nested_path = os.path.join(self.tmpdir, "deep", "nested", "audit.jsonl")
logger = AuditLogger(log_path=nested_path)
logger.log("INIT", "actor", {})
self.assertTrue(os.path.exists(nested_path))
if __name__ == "__main__":
unittest.main()