|
| 1 | +import unittest |
| 2 | +import sys |
| 3 | +import shutil |
| 4 | +import tempfile |
| 5 | +from io import StringIO |
| 6 | +from pathlib import Path |
| 7 | +from unittest.mock import patch, MagicMock |
| 8 | +from platformdirs import user_config_dir |
| 9 | +from discos_client import scripts |
| 10 | + |
| 11 | + |
| 12 | +class TestKeygen(unittest.TestCase): |
| 13 | + |
| 14 | + def setUp(self): |
| 15 | + self.test_dir = tempfile.mkdtemp() |
| 16 | + self.test_path = Path(self.test_dir) |
| 17 | + self.mock_target_dir = self.test_path / "rpc" / "client" |
| 18 | + self.mock_public = self.mock_target_dir / "identity.key" |
| 19 | + self.mock_secret = self.mock_target_dir / "identity.key_secret" |
| 20 | + |
| 21 | + def tearDown(self): |
| 22 | + shutil.rmtree(self.test_dir) |
| 23 | + |
| 24 | + def test_correct_paths(self): |
| 25 | + config_dir, public, secret = scripts.get_config_paths() |
| 26 | + expected_config_dir = \ |
| 27 | + Path(user_config_dir("discos")) / "rpc" / "client" |
| 28 | + expected_public = expected_config_dir / "identity.key" |
| 29 | + expected_secret = expected_config_dir / "identity.key_secret" |
| 30 | + self.assertEqual(config_dir, expected_config_dir) |
| 31 | + self.assertEqual(public, expected_public) |
| 32 | + self.assertEqual(secret, expected_secret) |
| 33 | + |
| 34 | + @patch("discos_client.scripts.get_config_paths") |
| 35 | + @patch("sys.stdout", new_callable=StringIO) |
| 36 | + @patch.object(sys, "argv", ["discos-keygen"]) |
| 37 | + def test_keygen(self, mock_stdout, mock_paths): |
| 38 | + mock_paths.return_value = ( |
| 39 | + self.mock_target_dir, |
| 40 | + self.mock_public, |
| 41 | + self.mock_secret |
| 42 | + ) |
| 43 | + rc = scripts.keygen() |
| 44 | + self.assertEqual(rc, 0) |
| 45 | + self.assertTrue(self.mock_public.exists()) |
| 46 | + self.assertTrue(self.mock_secret.exists()) |
| 47 | + output = mock_stdout.getvalue() |
| 48 | + self.assertIn("Key pair created in", output) |
| 49 | + |
| 50 | + @patch("discos_client.scripts.get_config_paths") |
| 51 | + @patch("sys.stdout", new_callable=StringIO) |
| 52 | + @patch.object(sys, "argv", ["discos-keygen"]) |
| 53 | + def test_keygen_no_overwrite(self, mock_stdout, mock_paths): |
| 54 | + mock_paths.return_value = ( |
| 55 | + self.mock_target_dir, |
| 56 | + self.mock_public, |
| 57 | + self.mock_secret |
| 58 | + ) |
| 59 | + self.assertEqual(scripts.keygen(), 0) |
| 60 | + self.assertTrue(self.mock_public.exists()) |
| 61 | + self.assertTrue(self.mock_secret.exists()) |
| 62 | + output = mock_stdout.getvalue() |
| 63 | + self.assertIn("Key pair created in", output) |
| 64 | + self.assertEqual(scripts.keygen(), 0) |
| 65 | + output = mock_stdout.getvalue() |
| 66 | + self.assertIn("Kept previously created key pair", output) |
| 67 | + |
| 68 | + @patch("discos_client.scripts.get_config_paths") |
| 69 | + @patch("sys.stdout", new_callable=StringIO) |
| 70 | + def test_print_keys(self, mock_stdout, mock_paths): |
| 71 | + mock_paths.return_value = ( |
| 72 | + self.mock_target_dir, |
| 73 | + self.mock_public, |
| 74 | + self.mock_secret |
| 75 | + ) |
| 76 | + scripts.print_discos_keys() |
| 77 | + output = mock_stdout.getvalue() |
| 78 | + self.assertIn("No key was generated yet.", output) |
| 79 | + |
| 80 | + @patch("discos_client.scripts.get_config_paths") |
| 81 | + @patch("sys.stdout", new_callable=StringIO) |
| 82 | + @patch.object(sys, "argv", ["discos-keygen"]) |
| 83 | + def test_mkdir_error(self, mock_stdout, mock_paths): |
| 84 | + mock_target_dir = MagicMock() |
| 85 | + mock_target_dir.mkdir.side_effect = OSError("Test error") |
| 86 | + mock_paths.return_value = ( |
| 87 | + mock_target_dir, |
| 88 | + self.mock_public, |
| 89 | + self.mock_secret |
| 90 | + ) |
| 91 | + rc = scripts.keygen() |
| 92 | + self.assertEqual(rc, 1) |
| 93 | + output = mock_stdout.getvalue() |
| 94 | + self.assertIn("Error creating the configuration directory", output) |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + unittest.main() |
0 commit comments