@@ -73,7 +73,9 @@ def test_initialization(self, tmp_path):
7373 assert client .api_key == api_key
7474 assert client .token is None
7575 assert client .token_expiry == 0
76+ assert client .token_api_key is None
7677 assert client .headers ["Authorization" ] == f"Bearer { api_key } "
78+ assert client ._token_path == tmp_path / ".quotient" / f"{ api_key [- 6 :]} _auth_token.json"
7779
7880 def test_handle_jwt_response (self ):
7981 """Test that _handle_response properly processes JWT tokens"""
@@ -111,11 +113,12 @@ def test_save_token(self, tmp_path):
111113 assert client .token_expiry == test_expiry
112114
113115 # Verify token was saved to disk
114- token_file = tmp_path / ".quotient" / "auth_token .json"
116+ token_file = tmp_path / ".quotient" / f" { client . api_key [ - 6 :] } _auth_token .json"
115117 assert token_file .exists ()
116118 stored_data = json .loads (token_file .read_text ())
117119 assert stored_data ["token" ] == test_token
118120 assert stored_data ["expires_at" ] == test_expiry
121+ assert stored_data ["api_key" ] == client .api_key
119122
120123 def test_load_token (self , tmp_path ):
121124 """Test that _load_token reads token data correctly"""
@@ -127,17 +130,19 @@ def test_load_token(self, tmp_path):
127130 # Write a token file
128131 token_dir = tmp_path / ".quotient"
129132 token_dir .mkdir (parents = True )
130- token_file = token_dir / "auth_token .json"
133+ token_file = token_dir / f" { client . api_key [ - 6 :] } _auth_token .json"
131134 token_file .write_text (json .dumps ({
132135 "token" : test_token ,
133- "expires_at" : test_expiry
136+ "expires_at" : test_expiry ,
137+ "api_key" : client .api_key
134138 }))
135139
136140 # Load the token
137141 client ._load_token ()
138142
139143 assert client .token == test_token
140144 assert client .token_expiry == test_expiry
145+ assert client .token_api_key == client .api_key
141146
142147 def test_is_token_valid (self , tmp_path ):
143148 """Test token validity checking"""
@@ -151,16 +156,25 @@ def test_is_token_valid(self, tmp_path):
151156 # Test with expired token
152157 client .token = "expired.token"
153158 client .token_expiry = int (time .time ()) - 3600 # 1 hour ago
159+ client .token_api_key = client .api_key
154160 assert not client ._is_token_valid ()
155161
156162 # Test with valid token
157163 client .token = "valid.token"
158164 client .token_expiry = int (time .time ()) + 3600 # 1 hour from now
165+ client .token_api_key = client .api_key
159166 assert client ._is_token_valid ()
160167
161168 # Test with token about to expire (within 5 minute buffer)
162169 client .token = "about.to.expire"
163170 client .token_expiry = int (time .time ()) + 200 # Less than 5 minutes
171+ client .token_api_key = client .api_key
172+ assert not client ._is_token_valid ()
173+
174+ # Test with mismatched API key
175+ client .token = "valid.token"
176+ client .token_expiry = int (time .time ()) + 3600
177+ client .token_api_key = "different-api-key"
164178 assert not client ._is_token_valid ()
165179
166180 def test_update_auth_header (self , tmp_path ):
@@ -177,19 +191,27 @@ def test_update_auth_header(self, tmp_path):
177191 test_token = "test.jwt.token"
178192 client .token = test_token
179193 client .token_expiry = int (time .time ()) + 3600
194+ client .token_api_key = client .api_key
180195 client ._update_auth_header ()
181196 assert client .headers ["Authorization" ] == f"Bearer { test_token } "
182197
183198 # Should revert to API key when token expires
184199 client .token_expiry = int (time .time ()) - 3600
185200 client ._update_auth_header ()
186201 assert client .headers ["Authorization" ] == f"Bearer { client .api_key } "
202+
203+ # Should revert to API key when API key doesn't match
204+ client .token = test_token
205+ client .token_expiry = int (time .time ()) + 3600
206+ client .token_api_key = "different-api-key"
207+ client ._update_auth_header ()
208+ assert client .headers ["Authorization" ] == f"Bearer { client .api_key } "
187209
188210 def test_token_path_uses_home (self ):
189211 with patch ('pathlib.Path.home' ) as mock_home :
190212 mock_home .return_value = Path ('/home/user' )
191213 client = _BaseQuotientClient ('test-key' )
192- assert client ._token_path == Path ('/home/user/.quotient/auth_token .json' )
214+ assert client ._token_path == Path ('/home/user/.quotient/st-key_auth_token .json' )
193215
194216 def test_token_path_fallback_to_root (self ):
195217 with patch ('pathlib.Path.home' ) as mock_home , \
@@ -200,7 +222,7 @@ def test_token_path_fallback_to_root(self):
200222 mock_exists .return_value = True
201223
202224 client = _BaseQuotientClient ('test-key' )
203- assert client ._token_path == Path ('/root/.quotient/auth_token .json' )
225+ assert client ._token_path == Path ('/root/.quotient/st-key_auth_token .json' )
204226
205227 def test_token_path_fallback_to_cwd (self ):
206228 with patch ('pathlib.Path.home' ) as mock_home , \
@@ -214,7 +236,7 @@ def test_token_path_fallback_to_cwd(self):
214236 mock_cwd .return_value = Path ('/current/dir' )
215237
216238 client = _BaseQuotientClient ('test-key' )
217- assert client ._token_path == Path ('/current/dir/.quotient/auth_token .json' )
239+ assert client ._token_path == Path ('/current/dir/.quotient/st-key_auth_token .json' )
218240
219241 def test_handle_jwt_token_success (self ):
220242 client = _BaseQuotientClient ('test-key' )
0 commit comments