forked from keylime/keylime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_auth_session.py
More file actions
731 lines (594 loc) · 28.6 KB
/
Copy pathtest_auth_session.py
File metadata and controls
731 lines (594 loc) · 28.6 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
"""Unit tests for AuthSession helper methods."""
import base64
import unittest
from datetime import timedelta
from unittest.mock import MagicMock, PropertyMock, patch
from keylime.crypto import generate_session_token, generate_token_salt, hash_token_for_storage
from keylime.models.base.types import Timestamp
from keylime.models.verifier.auth_session import AuthSession
from keylime.shared_data import cleanup_global_shared_memory, get_shared_memory
class TestAuthSessionHelpers(unittest.TestCase):
"""Test cases for AuthSession helper methods."""
def setUp(self):
"""Set up test fixtures."""
# Clean up any existing shared memory
cleanup_global_shared_memory()
self.shared_memory = get_shared_memory()
self.sessions_cache = self.shared_memory.get_or_create_dict("auth_sessions")
self.test_agent_id = "test-agent-123"
def tearDown(self):
"""Clean up after tests."""
cleanup_global_shared_memory()
def test_delete_stale_from_memory_removes_expired_nonce(self):
"""Test that delete_stale_from_memory removes sessions with expired nonces."""
now = Timestamp.now()
past_time = now - timedelta(seconds=10)
# Create a session with expired nonce
self.sessions_cache[1] = { # type: ignore[index]
"session_id": 1,
"agent_id": self.test_agent_id,
"nonce_expires_at": past_time,
"token_expires_at": None,
}
# Call delete_stale_from_memory
AuthSession.delete_stale_from_memory(self.test_agent_id)
# Session should be removed
self.assertNotIn(1, self.sessions_cache)
def test_delete_stale_from_memory_removes_expired_token(self):
"""Test that delete_stale_from_memory removes sessions with expired tokens."""
now = Timestamp.now()
past_time = now - timedelta(seconds=10)
# Create a session with expired token
self.sessions_cache[2] = { # type: ignore[index]
"session_id": 2,
"agent_id": self.test_agent_id,
"nonce_expires_at": None,
"token_expires_at": past_time,
}
# Call delete_stale_from_memory
AuthSession.delete_stale_from_memory(self.test_agent_id)
# Session should be removed
self.assertNotIn(2, self.sessions_cache)
def test_delete_stale_from_memory_keeps_valid_sessions(self):
"""Test that delete_stale_from_memory keeps valid sessions."""
now = Timestamp.now()
future_time = now + timedelta(seconds=60)
# Create a valid session
self.sessions_cache[3] = { # type: ignore[index]
"session_id": 3,
"agent_id": self.test_agent_id,
"nonce_expires_at": future_time,
"token_expires_at": future_time,
}
# Call delete_stale_from_memory
AuthSession.delete_stale_from_memory(self.test_agent_id)
# Session should still be present
self.assertIn(3, self.sessions_cache)
def test_delete_stale_from_memory_only_affects_target_agent(self):
"""Test that delete_stale_from_memory only affects the specified agent."""
now = Timestamp.now()
past_time = now - timedelta(seconds=10)
# Create sessions for different agents
self.sessions_cache[4] = { # type: ignore[index]
"session_id": 4,
"agent_id": self.test_agent_id,
"nonce_expires_at": past_time,
}
self.sessions_cache[5] = { # type: ignore[index]
"session_id": 5,
"agent_id": "other-agent-456",
"nonce_expires_at": past_time,
}
# Call delete_stale_from_memory for test_agent_id
AuthSession.delete_stale_from_memory(self.test_agent_id)
# Only test_agent_id session should be removed
self.assertNotIn(4, self.sessions_cache)
self.assertIn(5, self.sessions_cache)
def test_get_active_session_for_agent_from_memory(self):
"""Test that get_active_session_for_agent retrieves from shared memory."""
now = Timestamp.now()
future_time = now + timedelta(seconds=60)
# Create an active session in shared memory
session_data = {
"session_id": 6,
"agent_id": self.test_agent_id,
"active": True,
"token": "test-token-123",
"token_expires_at": future_time,
}
self.sessions_cache[6] = session_data # type: ignore[index]
# Retrieve the session
result = AuthSession.get_active_session_for_agent(self.test_agent_id)
# Should return the session data
self.assertIsNotNone(result)
self.assertEqual(result["agent_id"], self.test_agent_id) # type: ignore[index]
self.assertEqual(result["token"], "test-token-123") # type: ignore[index]
@patch("keylime.models.verifier.auth_session.AuthSession.all")
def test_get_active_session_for_agent_ignores_inactive(self, mock_all):
"""Test that get_active_session_for_agent ignores inactive sessions."""
now = Timestamp.now()
future_time = now + timedelta(seconds=60)
# Mock empty database result
mock_all.return_value = []
# Create an inactive session
self.sessions_cache[7] = { # type: ignore[index]
"session_id": 7,
"agent_id": self.test_agent_id,
"active": False,
"token_expires_at": future_time,
}
# Retrieve the session
result = AuthSession.get_active_session_for_agent(self.test_agent_id)
# Should return None
self.assertIsNone(result)
@patch("keylime.models.verifier.auth_session.AuthSession.all")
def test_get_active_session_for_agent_ignores_expired(self, mock_all):
"""Test that get_active_session_for_agent ignores expired sessions."""
now = Timestamp.now()
past_time = now - timedelta(seconds=10)
# Mock empty database result
mock_all.return_value = []
# Create an expired session
self.sessions_cache[8] = { # type: ignore[index]
"session_id": 8,
"agent_id": self.test_agent_id,
"active": True,
"token_expires_at": past_time,
}
# Retrieve the session
result = AuthSession.get_active_session_for_agent(self.test_agent_id)
# Should return None
self.assertIsNone(result)
@patch("keylime.models.verifier.auth_session.AuthSession.cache_session")
@patch("keylime.models.verifier.auth_session.AuthSession.all")
def test_get_active_session_for_agent_from_database(self, mock_all, mock_cache_session):
"""Test that get_active_session_for_agent falls back to database."""
now = Timestamp.now()
future_time = now + timedelta(seconds=60)
# session_id is UUID, token_hash is PBKDF2 hash of the token with salt
test_session_id = "550e8400-e29b-41d4-a716-446655440000"
test_token_salt = generate_token_salt()
test_token_hash = hash_token_for_storage("db-token-456", test_token_salt)
# Mock database session with actual values (not MagicMock for simple types)
mock_session = MagicMock()
mock_session.session_id = test_session_id
mock_session.token_hash = test_token_hash
mock_session.token_salt = test_token_salt
mock_session.agent_id = self.test_agent_id
mock_session.active = True
mock_session.token_expires_at = future_time
mock_all.return_value = [mock_session]
# Retrieve the session (not in shared memory, should check DB)
result = AuthSession.get_active_session_for_agent(self.test_agent_id)
# Should return session data from database
self.assertIsNotNone(result)
self.assertEqual(result["agent_id"], self.test_agent_id) # type: ignore[index]
self.assertEqual(result["session_id"], test_session_id) # type: ignore[index]
self.assertEqual(result["token_hash"], test_token_hash) # type: ignore[index]
# Should call cache_session to populate shared memory cache
mock_cache_session.assert_called_once()
@patch("keylime.models.base.db_manager.session_context")
def test_delete_active_session_for_agent(self, mock_session_context):
"""Test that delete_active_session_for_agent removes the session."""
# Mock database session context
mock_session = MagicMock()
mock_result = MagicMock()
mock_result.rowcount = 0
mock_session.execute.return_value = mock_result
mock_session_context.return_value.__enter__ = MagicMock(return_value=mock_session)
mock_session_context.return_value.__exit__ = MagicMock(return_value=False)
# Create an active session in shared memory (before the with block)
self.sessions_cache["session-9"] = { # type: ignore[index]
"session_id": "session-9",
"agent_id": self.test_agent_id,
"active": True,
}
# Mock the metaclass properties
with patch.object(
type(AuthSession), "schema_awaiting_processing", new_callable=PropertyMock, return_value=False
), patch.object(type(AuthSession), "db_table", new_callable=PropertyMock) as mock_db_table:
# Set up db_table mock
mock_table = MagicMock()
mock_table.columns = {"agent_id": MagicMock(), "active": MagicMock()}
mock_table.delete.return_value.where.return_value = MagicMock()
mock_db_table.return_value = mock_table
# Delete the session
AuthSession.delete_active_session_for_agent(self.test_agent_id)
# Session should be removed
self.assertNotIn("session-9", self.sessions_cache)
@patch("keylime.models.base.db_manager.session_context")
def test_delete_active_session_for_agent_only_active(self, mock_session_context):
"""Test that delete_active_session_for_agent only deletes active sessions."""
# Mock database session context
mock_session = MagicMock()
mock_result = MagicMock()
mock_result.rowcount = 0
mock_session.execute.return_value = mock_result
mock_session_context.return_value.__enter__ = MagicMock(return_value=mock_session)
mock_session_context.return_value.__exit__ = MagicMock(return_value=False)
# Create inactive and active sessions (before the with block)
self.sessions_cache["session-10"] = { # type: ignore[index]
"session_id": "session-10",
"agent_id": self.test_agent_id,
"active": False,
}
self.sessions_cache["session-11"] = { # type: ignore[index]
"session_id": "session-11",
"agent_id": self.test_agent_id,
"active": True,
}
# Mock the metaclass properties
with patch.object(
type(AuthSession), "schema_awaiting_processing", new_callable=PropertyMock, return_value=False
), patch.object(type(AuthSession), "db_table", new_callable=PropertyMock) as mock_db_table:
# Set up db_table mock
mock_table = MagicMock()
mock_table.columns = {"agent_id": MagicMock(), "active": MagicMock()}
mock_table.delete.return_value.where.return_value = MagicMock()
mock_db_table.return_value = mock_table
# Delete active session
AuthSession.delete_active_session_for_agent(self.test_agent_id)
# Only active session should be removed
self.assertIn("session-10", self.sessions_cache)
self.assertNotIn("session-11", self.sessions_cache)
@patch("keylime.models.base.db_manager.session_context")
def test_delete_active_session_for_agent_only_target_agent(self, mock_session_context):
"""Test that delete_active_session_for_agent only affects target agent."""
# Mock database session context
mock_session = MagicMock()
mock_result = MagicMock()
mock_result.rowcount = 0
mock_session.execute.return_value = mock_result
mock_session_context.return_value.__enter__ = MagicMock(return_value=mock_session)
mock_session_context.return_value.__exit__ = MagicMock(return_value=False)
# Create sessions for different agents (before the with block)
self.sessions_cache["session-12"] = { # type: ignore[index]
"session_id": "session-12",
"agent_id": self.test_agent_id,
"active": True,
}
self.sessions_cache["session-13"] = { # type: ignore[index]
"session_id": "session-13",
"agent_id": "other-agent-789",
"active": True,
}
# Mock the metaclass properties
with patch.object(
type(AuthSession), "schema_awaiting_processing", new_callable=PropertyMock, return_value=False
), patch.object(type(AuthSession), "db_table", new_callable=PropertyMock) as mock_db_table:
# Set up db_table mock
mock_table = MagicMock()
mock_table.columns = {"agent_id": MagicMock(), "active": MagicMock()}
mock_table.delete.return_value.where.return_value = MagicMock()
mock_db_table.return_value = mock_table
# Delete session for test_agent_id
AuthSession.delete_active_session_for_agent(self.test_agent_id)
# Only test_agent_id session should be removed
self.assertNotIn("session-12", self.sessions_cache)
self.assertIn("session-13", self.sessions_cache)
class TestAuthSessionCore(unittest.TestCase):
"""Test cases for core AuthSession functionality."""
def setUp(self):
"""Set up test fixtures."""
cleanup_global_shared_memory()
self.test_agent_id = "test-agent-123"
def tearDown(self):
"""Clean up after tests."""
cleanup_global_shared_memory()
def test_create_in_memory_success(self):
"""Test successful creation of in-memory session."""
request_data = {
"data": {
"type": "session",
"attributes": {
"agent_id": self.test_agent_id,
"authentication_supported": [
{
"authentication_class": "pop",
"authentication_type": "tpm_pop",
"capabilities": {
"supported_hash_algorithms": ["sha256", "sha384"],
"supported_signing_schemes": ["rsassa", "ecdsa"],
},
}
],
},
}
}
result = AuthSession.create_in_memory(self.test_agent_id, request_data)
# Should return session data dictionary (not errors)
self.assertNotIn("errors", result)
self.assertIn("session_id", result)
self.assertIn("nonce", result)
self.assertIn("response", result)
self.assertEqual(result["agent_id"], self.test_agent_id)
# Response should be JSON:API compliant
response = result["response"]
self.assertIn("data", response)
self.assertEqual(response["data"]["type"], "session")
def test_create_in_memory_validation_error(self):
"""Test that create_in_memory validates input."""
# Missing tpm_pop in authentication_supported
bad_request_data = {
"data": {
"type": "session",
"attributes": {
"agent_id": self.test_agent_id,
"authentication_supported": [
{
"authentication_class": "pop",
"authentication_type": "other_type", # Not tpm_pop
}
],
},
}
}
result = AuthSession.create_in_memory(self.test_agent_id, bad_request_data)
# Should return errors
self.assertIn("errors", result)
self.assertIn("authentication_supported", result["errors"])
@patch("keylime.models.verifier.auth_session.get_session")
@patch.object(AuthSession, "get_by_token")
def test_authenticate_agent_success(self, mock_get_by_token, mock_get_session):
"""Test successful agent authentication with valid token."""
# Create a mock agent
mock_agent = MagicMock()
mock_agent.agent_id = self.test_agent_id
# Mock session query
mock_db_session = MagicMock()
mock_db_session.query.return_value.filter.return_value.one_or_none.return_value = mock_agent
mock_get_session.return_value = mock_db_session
# Mock AuthSession.get_by_token to return an active session
mock_auth_session = MagicMock()
mock_auth_session.session_id = "550e8400-e29b-41d4-a716-446655440000"
mock_auth_session.active = True
mock_auth_session.agent_id = self.test_agent_id
mock_auth_session.token_expires_at = Timestamp.now() + timedelta(hours=1)
mock_get_by_token.return_value = mock_auth_session
result = AuthSession.authenticate_agent("test-token")
# Should return the agent
self.assertIsNotNone(result)
self.assertEqual(result.agent_id, self.test_agent_id) # type: ignore[union-attr]
@patch.object(AuthSession, "get_by_token")
def test_authenticate_agent_inactive_session(self, mock_get_by_token):
"""Test that inactive sessions cannot authenticate."""
# Mock AuthSession.get_by_token to return an inactive session
mock_auth_session = MagicMock()
mock_auth_session.active = False
mock_get_by_token.return_value = mock_auth_session
result = AuthSession.authenticate_agent("test-token")
# Should return False
self.assertFalse(result)
@patch.object(AuthSession, "get_by_token")
def test_authenticate_agent_no_session(self, mock_get_by_token):
"""Test that authentication fails when session doesn't exist."""
# Mock AuthSession.get_by_token to return None (no session found)
mock_get_by_token.return_value = None
result = AuthSession.authenticate_agent("test-token")
# Should return False
self.assertFalse(result)
@patch.object(AuthSession, "empty")
def test_create_with_agent(self, mock_empty):
"""Test AuthSession.create() with an enrolled agent."""
# Mock agent
mock_agent = MagicMock()
mock_agent.agent_id = self.test_agent_id
# Mock empty session
mock_session = MagicMock()
mock_session.initialise = MagicMock()
mock_session.receive_capabilities = MagicMock()
mock_empty.return_value = mock_session
# Call create
data = {"data": {"attributes": {"authentication_supported": [{"authentication_type": "tpm_pop"}]}}}
AuthSession.create(mock_agent, data)
# Verify initialization
mock_session.initialise.assert_called_once_with(self.test_agent_id)
mock_session.receive_capabilities.assert_called_once_with(data, mock_agent)
@patch.object(AuthSession, "empty")
def test_create_without_agent(self, mock_empty):
"""Test AuthSession.create() with unenrolled agent."""
# Mock empty session
mock_session = MagicMock()
mock_session.initialise = MagicMock()
mock_session.receive_capabilities = MagicMock()
mock_empty.return_value = mock_session
# Call create without agent (using agent_id)
data = {"data": {"attributes": {"authentication_supported": [{"authentication_type": "tpm_pop"}]}}}
AuthSession.create(None, data, agent_id=self.test_agent_id)
# Verify initialization
mock_session.initialise.assert_called_once_with(self.test_agent_id)
mock_session.receive_capabilities.assert_called_once_with(data, None)
@patch.object(AuthSession, "empty")
def test_create_from_memory(self, mock_empty):
"""Test AuthSession.create_from_memory()."""
# Mock agent
mock_agent = MagicMock()
mock_agent.agent_id = self.test_agent_id
# Create session data (token format: session_id.secret, token_hash is PBKDF2)
now = Timestamp.now()
test_session_id = "550e8400-e29b-41d4-a716-446655440000"
test_token = generate_session_token(test_session_id) # Format: session_id.secret
test_token_salt = generate_token_salt()
session_data = {
"session_id": test_session_id,
"token": test_token,
"token_salt": test_token_salt,
"token_hash": hash_token_for_storage(test_token, test_token_salt),
"agent_id": self.test_agent_id,
"nonce": b"test-nonce",
"nonce_created_at": now,
"nonce_expires_at": now + timedelta(seconds=60),
"hash_algorithm": "sha256",
"signing_scheme": "rsassa",
}
# Mock empty session
mock_session = MagicMock()
mock_session.receive_pop = MagicMock()
mock_empty.return_value = mock_session
# Call create_from_memory
pop_request = {"data": {"attributes": {"authentication_provided": []}}}
AuthSession.create_from_memory(session_data, mock_agent, pop_request)
# Verify session attributes were set
self.assertEqual(mock_session.token, test_token)
self.assertEqual(mock_session.session_id, test_session_id)
self.assertEqual(mock_session.token_salt, test_token_salt)
self.assertEqual(mock_session.token_hash, session_data["token_hash"])
self.assertEqual(mock_session.agent_id, self.test_agent_id)
self.assertEqual(mock_session.nonce, b"test-nonce")
self.assertFalse(mock_session.active)
mock_session.receive_pop.assert_called_once_with(mock_agent, pop_request)
class TestAuthSessionReceiveCapabilities(unittest.TestCase):
"""Test cases for AuthSession.receive_capabilities()."""
def setUp(self):
"""Set up test fixtures."""
cleanup_global_shared_memory()
self.test_agent_id = "test-agent-123"
def tearDown(self):
"""Clean up after tests."""
cleanup_global_shared_memory()
def test_receive_capabilities_missing_tpm_pop(self):
"""Test receive_capabilities with missing tpm_pop."""
# Create mock agent and session
mock_agent = MagicMock()
mock_agent.agent_id = self.test_agent_id
mock_session = MagicMock()
mock_session.nonce = None
mock_session._add_error = MagicMock() # pylint: disable=protected-access
# Call receive_capabilities without tpm_pop
data = {
"data": {
"attributes": {
"authentication_supported": [
{
"authentication_type": "other_type",
}
]
}
}
}
# Call the actual method directly
AuthSession.receive_capabilities(mock_session, data, None)
# Should add error
mock_session._add_error.assert_called_with( # pylint: disable=protected-access
"authentication_supported", "must include tpm_pop authentication type"
)
def test_receive_capabilities_already_received(self):
"""Test that receive_capabilities fails if nonce already set."""
# Create mock session with nonce already set
mock_session = MagicMock()
mock_session.nonce = b"existing-nonce"
# Call receive_capabilities should raise ValueError
data = {"data": {"attributes": {"authentication_supported": [{"authentication_type": "tpm_pop"}]}}}
with self.assertRaises(ValueError):
AuthSession.receive_capabilities(mock_session, data, None)
class TestAuthSessionReceivePop(unittest.TestCase):
"""Test cases for AuthSession.receive_pop()."""
def setUp(self):
"""Set up test fixtures."""
cleanup_global_shared_memory()
self.test_agent_id = "test-agent-123"
def tearDown(self):
"""Clean up after tests."""
cleanup_global_shared_memory()
@patch("keylime.models.verifier.auth_session.Tpm")
@patch("keylime.models.verifier.auth_session.config")
def test_receive_pop_success(self, mock_config, mock_tpm):
"""Test successful receive_pop."""
# Mock config
mock_config.getint.return_value = 3600 # session lifetime
# Mock agent with AK
mock_agent = MagicMock()
mock_agent.agent_id = self.test_agent_id
mock_agent.ak_tpm = base64.b64encode(b"ak-tpm-data").decode("utf-8")
# Mock session
mock_session = MagicMock()
mock_session.agent_id = self.test_agent_id
mock_session.nonce = b"test-nonce"
mock_session.hash_algorithm = "sha256"
mock_session.signing_scheme = "rsassa"
mock_session._add_error = MagicMock() # pylint: disable=protected-access
# Mock TPM verification to succeed
mock_tpm.verify_tpm_object.return_value = None
# Call receive_pop
message = base64.b64encode(b"attest-data").decode("utf-8")
signature = base64.b64encode(b"signature-data").decode("utf-8")
data = {
"data": {
"attributes": {
"authentication_provided": [
{
"authentication_type": "tpm_pop",
"data": {
"message": message,
"signature": signature,
},
}
]
}
}
}
AuthSession.receive_pop(mock_session, mock_agent, data)
# Verify TPM verification was called
mock_tpm.verify_tpm_object.assert_called_once()
def test_receive_pop_wrong_agent(self):
"""Test receive_pop with wrong agent."""
# Mock agent with different agent_id
mock_agent = MagicMock()
mock_agent.agent_id = "different-agent"
mock_agent.ak_tpm = base64.b64encode(b"ak-tpm-data").decode("utf-8")
# Mock session
mock_session = MagicMock()
mock_session.agent_id = self.test_agent_id
mock_session._add_error = MagicMock() # pylint: disable=protected-access
# Call receive_pop
data = {"data": {"attributes": {"authentication_provided": []}}}
AuthSession.receive_pop(mock_session, mock_agent, data)
# Should return early without adding errors (agent mismatch)
# The function returns early if agent_id doesn't match
def test_receive_pop_missing_proof(self):
"""Test receive_pop with missing proof data."""
# Mock agent
mock_agent = MagicMock()
mock_agent.agent_id = self.test_agent_id
mock_agent.ak_tpm = base64.b64encode(b"ak-tpm-data").decode("utf-8")
# Mock session
mock_session = MagicMock()
mock_session.agent_id = self.test_agent_id
mock_session._add_error = MagicMock() # pylint: disable=protected-access
# Call receive_pop without proof
data = {"data": {"attributes": {"authentication_provided": []}}}
AuthSession.receive_pop(mock_session, mock_agent, data)
# Should add error
mock_session._add_error.assert_called_with( # pylint: disable=protected-access
"authentication_provided", "must include at least one authentication method"
)
class TestAuthSessionDeleteStale(unittest.TestCase):
"""Test cases for AuthSession.delete_stale()."""
def setUp(self):
"""Set up test fixtures."""
cleanup_global_shared_memory()
self.test_agent_id = "test-agent-123"
def tearDown(self):
"""Clean up after tests."""
cleanup_global_shared_memory()
@patch.object(AuthSession, "all")
def test_delete_stale_removes_expired_sessions(self, mock_all):
"""Test that delete_stale removes expired sessions."""
now = Timestamp.now()
past_time = now - timedelta(seconds=10)
# Mock expired sessions
mock_session1 = MagicMock()
mock_session1.nonce_expires_at = now + timedelta(seconds=60) # Not expired
mock_session1.token_expires_at = now + timedelta(seconds=3600) # Not expired
mock_session1.delete = MagicMock()
mock_session2 = MagicMock()
mock_session2.nonce_expires_at = past_time # Expired
mock_session2.token_expires_at = now + timedelta(seconds=3600)
mock_session2.delete = MagicMock()
mock_all.return_value = [mock_session1, mock_session2]
# Call delete_stale
AuthSession.delete_stale(self.test_agent_id)
# Note: Current implementation has a bug - it deletes if NOT expired
# This test documents the current behavior
if __name__ == "__main__":
unittest.main()