-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_kee.py
More file actions
595 lines (472 loc) · 19.2 KB
/
test_kee.py
File metadata and controls
595 lines (472 loc) · 19.2 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
#!/usr/bin/env python3
"""
Unit tests for Kee — AWS CLI profile manager
"""
import json
import os
import tempfile
import unittest
from pathlib import Path
from unittest.mock import Mock, patch
# Import the modules we're testing
from kee import KeeConfig, AWSConfigManager, KeeManager, get_kee_art
class TestKeeArt(unittest.TestCase):
"""Test the ASCII art function."""
def test_get_kee_art_returns_string(self):
"""Test that get_kee_art returns a string."""
art = get_kee_art()
self.assertIsInstance(art, str)
self.assertIn("AWS CLI profile manager", art)
self.assertTrue(len(art) > 0)
class TestKeeConfig(unittest.TestCase):
"""Test the KeeConfig class."""
def setUp(self):
"""Set up test fixtures."""
self.temp_dir = tempfile.mkdtemp()
self.config_dir = Path(self.temp_dir) / ".aws"
self.config_file = self.config_dir / "kee.json"
def tearDown(self):
"""Clean up test fixtures."""
import shutil
shutil.rmtree(self.temp_dir, ignore_errors=True)
@patch("kee.Path.home")
def test_init_creates_config_dir(self, mock_home):
"""Test that KeeConfig creates the .aws directory."""
mock_home.return_value = Path(self.temp_dir)
config = KeeConfig()
self.assertTrue(self.config_dir.exists())
self.assertEqual(config.config_file, self.config_file)
@patch("kee.Path.home")
def test_load_config_default_when_no_file(self, mock_home):
"""Test loading default config when file doesn't exist."""
mock_home.return_value = Path(self.temp_dir)
config = KeeConfig()
result = config.load_config()
expected = {"accounts": {}, "current_account": None}
self.assertEqual(result, expected)
@patch("kee.Path.home")
def test_load_config_from_file(self, mock_home):
"""Test loading config from existing file."""
mock_home.return_value = Path(self.temp_dir)
config = KeeConfig()
# Create test config
test_config = {
"accounts": {"test-account": {"profile_name": "test-account"}},
"current_account": "test-account",
}
# Write test config
self.config_dir.mkdir(parents=True, exist_ok=True)
with open(self.config_file, "w") as f:
json.dump(test_config, f)
result = config.load_config()
self.assertEqual(result, test_config)
@patch("kee.Path.home")
def test_load_config_handles_invalid_json(self, mock_home):
"""Test loading config handles invalid JSON gracefully."""
mock_home.return_value = Path(self.temp_dir)
config = KeeConfig()
# Create invalid JSON file
self.config_dir.mkdir(parents=True, exist_ok=True)
with open(self.config_file, "w") as f:
f.write("invalid json content")
result = config.load_config()
expected = {"accounts": {}, "current_account": None}
self.assertEqual(result, expected)
@patch("kee.Path.home")
def test_save_config(self, mock_home):
"""Test saving config to file."""
mock_home.return_value = Path(self.temp_dir)
config = KeeConfig()
test_config = {
"accounts": {"test-account": {"profile_name": "test-account"}},
"current_account": "test-account",
}
config.save_config(test_config)
# Verify file was created and contains correct data
self.assertTrue(self.config_file.exists())
with open(self.config_file, "r") as f:
saved_config = json.load(f)
self.assertEqual(saved_config, test_config)
class TestAWSConfigManager(unittest.TestCase):
"""Test the AWSConfigManager class."""
def setUp(self):
"""Set up test fixtures."""
self.temp_dir = tempfile.mkdtemp()
self.aws_config_file = Path(self.temp_dir) / ".aws" / "config"
self.aws_config_file.parent.mkdir(parents=True, exist_ok=True)
def tearDown(self):
"""Clean up test fixtures."""
import shutil
shutil.rmtree(self.temp_dir, ignore_errors=True)
@patch("kee.Path.home")
def test_init(self, mock_home):
"""Test AWSConfigManager initialization."""
mock_home.return_value = Path(self.temp_dir)
manager = AWSConfigManager()
expected_path = Path(self.temp_dir) / ".aws" / "config"
self.assertEqual(manager.aws_config_file, expected_path)
@patch("kee.Path.home")
def test_remove_profile_nonexistent_file(self, mock_home):
"""Test removing profile when config file doesn't exist."""
mock_home.return_value = Path(self.temp_dir)
manager = AWSConfigManager()
# Should not raise an exception
manager.remove_profile("test-profile")
@patch("kee.Path.home")
def test_remove_profile_existing(self, mock_home):
"""Test removing an existing profile."""
mock_home.return_value = Path(self.temp_dir)
manager = AWSConfigManager()
# Create test config file
config_content = """[profile test]
sso_role_name = AdministratorAccess
sso_session = test
sso_account_id = 123456789098
output = json
[profile other-profile]
sso_role_name = AdministratorAccess
"""
with open(self.aws_config_file, "w") as f:
f.write(config_content)
manager.remove_profile("test")
# Verify profile was removed
with open(self.aws_config_file, "r") as f:
content = f.read()
self.assertNotIn("profile test", content)
self.assertIn("profile other-profile", content)
@patch("kee.Path.home")
def test_reformat_config_file(self, mock_home):
"""Test reformatting config file."""
mock_home.return_value = Path(self.temp_dir)
manager = AWSConfigManager()
# Create test config file without proper spacing
config_content = """[profile test]
sso_role_name = AdministratorAccess
sso_session = test
[profile other-profile]
sso_role_name = AdministratorAccess"""
with open(self.aws_config_file, "w") as f:
f.write(config_content)
manager.reformat_config_file()
# Verify proper formatting
with open(self.aws_config_file, "r") as f:
content = f.read()
# Should have empty lines between sections
lines = content.split("\n")
profile_indices = [
i for i, line in enumerate(lines) if line.startswith("[profile")
]
# Check that there are empty lines after each profile section
self.assertTrue(any("" in lines for _ in profile_indices))
class TestKeeManager(unittest.TestCase):
"""Test the KeeManager class."""
def setUp(self):
"""Set up test fixtures."""
self.temp_dir = tempfile.mkdtemp()
def tearDown(self):
"""Clean up test fixtures."""
import shutil
shutil.rmtree(self.temp_dir, ignore_errors=True)
@patch("kee.KeeConfig")
@patch("kee.AWSConfigManager")
def test_init(self, mock_aws_config, mock_kee_config):
"""Test KeeManager initialization."""
manager = KeeManager()
mock_kee_config.assert_called_once()
mock_aws_config.assert_called_once()
self.assertIsNotNone(manager.config)
self.assertIsNotNone(manager.aws_config)
@patch("kee.subprocess.run")
@patch("kee.KeeManager._read_profile_info")
@patch("kee.KeeManager._check_credentials")
@patch("kee.KeeConfig")
@patch("kee.AWSConfigManager")
def test_add_account_success(
self,
mock_aws_config,
mock_kee_config,
mock_check_creds,
mock_read_profile,
mock_subprocess,
):
"""Test successful account addition."""
# Setup mocks
mock_subprocess.return_value.returncode = 0
mock_read_profile.return_value = {
"sso_session": "test",
"sso_account_id": "123456789012",
"sso_role_name": "TestRole",
"output": "json",
}
mock_check_creds.return_value = True
mock_config_instance = Mock()
mock_config_instance.load_config.return_value = {
"accounts": {},
"current_account": None,
}
mock_kee_config.return_value = mock_config_instance
mock_aws_config_instance = Mock()
mock_aws_config.return_value = mock_aws_config_instance
manager = KeeManager()
with patch("builtins.print") as mock_print:
result = manager.add_account("test-account")
self.assertTrue(result)
mock_subprocess.assert_called_once()
mock_read_profile.assert_called_once_with("test-account")
mock_check_creds.assert_called_once_with("test-account")
mock_config_instance.save_config.assert_called_once()
# Check that success messages were printed
print_calls = []
for call in mock_print.call_args_list:
if call[0]: # Check if call has positional arguments
print_calls.append(str(call[0][0]))
self.assertTrue(any("[✓]" in msg for msg in print_calls))
@patch("kee.subprocess.run")
@patch("kee.KeeConfig")
@patch("kee.AWSConfigManager")
def test_add_account_sso_failure(
self, mock_aws_config, mock_kee_config, mock_subprocess
):
"""Test account addition when SSO configuration fails."""
mock_subprocess.return_value.returncode = 1
manager = KeeManager()
with patch("builtins.print") as mock_print:
result = manager.add_account("test-account")
self.assertFalse(result)
# Check that failure message was printed
print_calls = []
for call in mock_print.call_args_list:
if call[0]: # Check if call has positional arguments
print_calls.append(str(call[0][0]))
self.assertTrue(any("[X]" in msg and "failed" in msg for msg in print_calls))
@patch("kee.KeeConfig")
@patch("kee.AWSConfigManager")
def test_list_accounts_empty(self, mock_aws_config, mock_kee_config):
"""Test listing accounts when none are configured."""
mock_config_instance = Mock()
mock_config_instance.load_config.return_value = {
"accounts": {},
"current_account": None,
}
mock_kee_config.return_value = mock_config_instance
manager = KeeManager()
with patch("builtins.print") as mock_print:
manager.list_accounts()
mock_print.assert_called_with(
"\n No accounts configured. Use '\x1b[1;37mkee add <account_name>\x1b[0m' to add an account."
)
@patch("kee.KeeConfig")
@patch("kee.AWSConfigManager")
def test_list_accounts_with_data(self, mock_aws_config, mock_kee_config):
"""Test listing accounts with configured accounts."""
test_accounts = {
"test-account": {
"sso_account_id": "123456789012",
"sso_role_name": "TestRole",
}
}
mock_config_instance = Mock()
mock_config_instance.load_config.return_value = {
"accounts": test_accounts,
"current_account": "test-account",
}
mock_kee_config.return_value = mock_config_instance
manager = KeeManager()
with patch("builtins.print") as mock_print:
manager.list_accounts()
# Check that account information was printed
print_calls = []
for call in mock_print.call_args_list:
if call[0]: # Check if call has positional arguments
print_calls.append(call[0][0])
self.assertTrue(any("test-account" in str(msg) for msg in print_calls))
self.assertTrue(any("Current profile" in str(msg) for msg in print_calls))
self.assertTrue(any("123456789012" in str(msg) for msg in print_calls))
@patch("builtins.input")
@patch("kee.KeeConfig")
@patch("kee.AWSConfigManager")
def test_remove_account_not_found(
self, mock_aws_config, mock_kee_config, mock_input
):
"""Test removing account that doesn't exist."""
mock_config_instance = Mock()
mock_config_instance.load_config.return_value = {
"accounts": {},
"current_account": None,
}
mock_kee_config.return_value = mock_config_instance
manager = KeeManager()
with patch("builtins.print") as mock_print:
result = manager.remove_account("nonexistent")
self.assertFalse(result)
mock_print.assert_called_with(
"\n Account '\x1b[1;37mnonexistent\x1b[0m' not found."
)
@patch("builtins.input", return_value="n")
@patch("kee.KeeConfig")
@patch("kee.AWSConfigManager")
def test_remove_account_cancelled(
self, mock_aws_config, mock_kee_config, mock_input
):
"""Test removing account when user cancels."""
test_accounts = {
"test-account": {"profile_name": "test-account", "session_name": ""}
}
mock_config_instance = Mock()
mock_config_instance.load_config.return_value = {
"accounts": test_accounts,
"current_account": None,
}
mock_kee_config.return_value = mock_config_instance
manager = KeeManager()
result = manager.remove_account("test-account")
self.assertFalse(result)
mock_config_instance.save_config.assert_not_called()
@patch("builtins.input", return_value="y")
@patch("kee.KeeConfig")
@patch("kee.AWSConfigManager")
def test_remove_account_success(self, mock_aws_config, mock_kee_config, mock_input):
"""Test successful account removal."""
test_accounts = {
"test-account": {
"profile_name": "test-account",
"session_name": "test-session",
}
}
mock_config_instance = Mock()
mock_config_instance.load_config.return_value = {
"accounts": test_accounts,
"current_account": None,
}
mock_kee_config.return_value = mock_config_instance
mock_aws_config_instance = Mock()
mock_aws_config.return_value = mock_aws_config_instance
manager = KeeManager()
with patch("builtins.print") as mock_print:
result = manager.remove_account("test-account")
self.assertTrue(result)
mock_aws_config_instance.remove_profile.assert_called_once_with("test-account")
mock_config_instance.save_config.assert_called_once()
# Check success message
print_calls = []
for call in mock_print.call_args_list:
if call[0]: # Check if call has positional arguments
print_calls.append(str(call[0][0]))
self.assertTrue(any("[✓]" in msg and "removed" in msg for msg in print_calls))
@patch.dict(
os.environ, {"KEE_ACTIVE_PROFILE": "1", "KEE_CURRENT_ACCOUNT": "existing"}
)
@patch("kee.KeeConfig")
@patch("kee.AWSConfigManager")
def test_use_account_already_in_session(self, mock_aws_config, mock_kee_config):
"""Test using account when already in a session."""
manager = KeeManager()
with patch("builtins.print") as mock_print:
result = manager.use_account("test-account")
self.assertFalse(result)
print_calls = []
for call in mock_print.call_args_list:
if call[0]: # Check if call has positional arguments
print_calls.append(str(call[0][0]))
self.assertTrue(
any(
"You are using a \x1b[1;37mKee\x1b[0m profile: \x1b[1;37mexisting\x1b[0m"
in msg
for msg in print_calls
)
)
@patch("kee.subprocess.run")
@patch("kee.KeeManager._check_credentials")
@patch("kee.KeeManager._sso_login")
@patch("kee.KeeManager._start_subshell")
@patch("kee.KeeConfig")
@patch("kee.AWSConfigManager")
def test_use_account_success(
self,
mock_aws_config,
mock_kee_config,
mock_subshell,
mock_sso_login,
mock_check_creds,
mock_subprocess,
):
"""Test successful account usage."""
test_accounts = {"test-account": {"profile_name": "test-account"}}
mock_config_instance = Mock()
mock_config_instance.load_config.return_value = {
"accounts": test_accounts,
"current_account": None,
}
mock_kee_config.return_value = mock_config_instance
mock_check_creds.return_value = True
manager = KeeManager()
with patch.dict(os.environ, {}, clear=True):
result = manager.use_account("test-account")
self.assertTrue(result)
mock_check_creds.assert_called_once_with("test-account")
mock_subshell.assert_called_once_with("test-account", "test-account")
mock_config_instance.save_config.assert_called()
@patch.dict(
os.environ, {"KEE_ACTIVE_PROFILE": "1", "KEE_CURRENT_ACCOUNT": "test-account"}
)
@patch("kee.KeeConfig")
@patch("kee.AWSConfigManager")
def test_current_account_in_session(self, mock_aws_config, mock_kee_config):
"""Test showing current account when using a profile."""
manager = KeeManager()
with patch("builtins.print") as mock_print:
manager.current_account()
print_calls = []
for call in mock_print.call_args_list:
if call[0]: # Check if call has positional arguments
print_calls.append(str(call[0][0]))
self.assertTrue(
any(
"Current profile: \x1b[1;37mtest-account\x1b[0m" in msg
for msg in print_calls
)
)
@patch("kee.subprocess.run")
@patch("kee.KeeConfig")
@patch("kee.AWSConfigManager")
def test_check_credentials_success(
self, mock_aws_config, mock_kee_config, mock_subprocess
):
"""Test credential checking success."""
mock_subprocess.return_value.returncode = 0
manager = KeeManager()
result = manager._check_credentials("test-profile")
self.assertTrue(result)
mock_subprocess.assert_called_once()
@patch("kee.subprocess.run")
@patch("kee.KeeConfig")
@patch("kee.AWSConfigManager")
def test_check_credentials_failure(
self, mock_aws_config, mock_kee_config, mock_subprocess
):
"""Test credential checking failure."""
mock_subprocess.return_value.returncode = 1
manager = KeeManager()
result = manager._check_credentials("test-profile")
self.assertFalse(result)
@patch("kee.subprocess.run")
@patch("kee.KeeConfig")
@patch("kee.AWSConfigManager")
def test_sso_login_success(self, mock_aws_config, mock_kee_config, mock_subprocess):
"""Test SSO login success."""
mock_subprocess.return_value.returncode = 0
manager = KeeManager()
result = manager._sso_login("test-profile")
self.assertTrue(result)
@patch("kee.subprocess.run")
@patch("kee.KeeConfig")
@patch("kee.AWSConfigManager")
def test_sso_login_failure(self, mock_aws_config, mock_kee_config, mock_subprocess):
"""Test SSO login failure."""
mock_subprocess.return_value.returncode = 1
manager = KeeManager()
result = manager._sso_login("test-profile")
self.assertFalse(result)
if __name__ == "__main__":
# Run tests with verbose output
unittest.main(verbosity=2)