@@ -183,46 +183,48 @@ def test_init_with_invalid_credentials(self, bearer_token_credentials):
183183 with pytest .raises (DXtradeConfigurationError ):
184184 SessionHandler (bearer_token_credentials )
185185
186- def test_init_with_existing_token (self ):
187- """Test initialization with existing session token."""
186+ def test_init_without_token (self ):
187+ """Test initialization without existing session token."""
188188 credentials = SessionCredentials (
189189 username = "test_user" ,
190190 password = "test_password" ,
191- session_token = "existing_token "
191+ domain = "default "
192192 )
193193 handler = SessionHandler (credentials )
194- assert handler ._session_token == "existing_token"
194+ assert handler ._session_token is None
195+ assert handler ._last_login is None
195196
196197 @pytest .mark .asyncio
197198 async def test_authenticate_with_valid_token (self , session_credentials ):
198199 """Test authentication with valid session token."""
199200 handler = SessionHandler (session_credentials )
200201 handler ._session_token = "valid_token"
201202 handler ._token_expires_at = time .time () + 3600 # 1 hour from now
203+ handler ._last_login = time .time () # Just logged in
202204
203205 request = MagicMock (spec = httpx .Request )
204206 request .headers = {}
205207 client = AsyncMock (spec = httpx .AsyncClient )
206208
207209 authenticated_request = await handler .authenticate (request , client )
208210
209- assert authenticated_request .headers ["Authorization" ] == "Bearer valid_token"
211+ assert authenticated_request .headers ["X-Auth-Token" ] == "valid_token"
212+ assert authenticated_request .headers ["Authorization" ] == "DXAPI valid_token"
210213
211214 @pytest .mark .asyncio
212215 async def test_authenticate_requires_login (self , session_credentials ):
213216 """Test authentication that requires login."""
214217 handler = SessionHandler (session_credentials )
215218 # No existing token
216219
217- # Mock successful login response
220+ # Mock successful login response (DXTrade format)
218221 login_response = MagicMock (spec = httpx .Response )
219222 login_response .raise_for_status .return_value = None
220223 login_response .json .return_value = {
221- "success" : True ,
222- "data" : {
223- "token" : "new_session_token" ,
224- "expires_in" : 3600
225- }
224+ "sessionToken" : "new_session_token" ,
225+ "expiresIn" : 3600 ,
226+ "userId" : "user123" ,
227+ "accounts" : ["account1" , "account2" ]
226228 }
227229
228230 client = AsyncMock (spec = httpx .AsyncClient )
@@ -233,27 +235,28 @@ async def test_authenticate_requires_login(self, session_credentials):
233235
234236 authenticated_request = await handler .authenticate (request , client )
235237
236- # Verify login was called
238+ # Verify login was called with domain
237239 client .post .assert_called_once_with (
238- "/auth/ login" ,
239- json = {"username" : "test_user" , "password" : "test_password" }
240+ "/login" ,
241+ json = {"username" : "test_user" , "password" : "test_password" , "domain" : "default" }
240242 )
241243
242244 # Verify token was set
243245 assert handler ._session_token == "new_session_token"
244- assert authenticated_request .headers ["Authorization" ] == "Bearer new_session_token"
246+ assert authenticated_request .headers ["X-Auth-Token" ] == "new_session_token"
247+ assert authenticated_request .headers ["Authorization" ] == "DXAPI new_session_token"
245248
246249 @pytest .mark .asyncio
247250 async def test_authenticate_login_failure (self , session_credentials ):
248251 """Test authentication with login failure."""
249252 handler = SessionHandler (session_credentials )
250253
251- # Mock failed login response
254+ # Mock failed login response (no session token)
252255 login_response = MagicMock (spec = httpx .Response )
253256 login_response .raise_for_status .return_value = None
254257 login_response .json .return_value = {
255- "success" : False ,
256258 "message" : "Invalid credentials"
259+ # No sessionToken field
257260 }
258261
259262 client = AsyncMock (spec = httpx .AsyncClient )
@@ -288,10 +291,17 @@ def test_token_expiration_check(self, session_credentials):
288291
289292 # Expired token
290293 handler ._token_expires_at = time .time () - 3600 # 1 hour ago
294+ handler ._last_login = time .time () - 7200 # 2 hours ago
295+ assert handler ._is_token_expired () is True
296+
297+ # Valid token but session older than 1 hour
298+ handler ._token_expires_at = time .time () + 3600 # 1 hour from now
299+ handler ._last_login = time .time () - 3700 # >1 hour ago
291300 assert handler ._is_token_expired () is True
292301
293- # Valid token
302+ # Valid token and recent session
294303 handler ._token_expires_at = time .time () + 3600 # 1 hour from now
304+ handler ._last_login = time .time () - 1800 # 30 minutes ago
295305 assert handler ._is_token_expired () is False
296306
297307 @pytest .mark .asyncio
@@ -305,10 +315,13 @@ async def test_logout(self, session_credentials):
305315
306316 await handler .logout (client )
307317
308- # Verify logout request was made
318+ # Verify logout request was made with correct headers
309319 client .post .assert_called_once_with (
310- "/auth/logout" ,
311- headers = {"Authorization" : "Bearer test_token" }
320+ "/logout" ,
321+ headers = {
322+ "X-Auth-Token" : "test_token" ,
323+ "Authorization" : "DXAPI test_token"
324+ }
312325 )
313326
314327 # Verify token was cleared
@@ -329,6 +342,7 @@ async def test_logout_network_error(self, session_credentials):
329342
330343 # Token should still be cleared
331344 assert handler ._session_token is None
345+ assert handler ._last_login is None
332346
333347
334348class TestAuthFactory :
0 commit comments