|
| 1 | +"""Unit tests for export/import logic in manage_bitbucket_env.py.""" |
| 2 | +# pylint: disable=duplicate-code |
| 3 | +import unittest |
| 4 | +from unittest.mock import patch, Mock |
| 5 | +import os |
| 6 | +import tempfile |
| 7 | +import json |
| 8 | +from typing import Optional |
| 9 | + |
| 10 | +from manage_bitbucket_env import ( |
| 11 | + export_variables, export_all_variables, export_secure_keys, import_variables |
| 12 | +) |
| 13 | + |
| 14 | +class TestExportImport(unittest.TestCase): |
| 15 | + """Test export and import functionality.""" |
| 16 | + # pylint: disable=attribute-defined-outside-init |
| 17 | + logger: Optional[Mock] = None |
| 18 | + auth: Optional[Mock] = None |
| 19 | + workspace: Optional[str] = None |
| 20 | + repo_slug: Optional[str] = None |
| 21 | + deployment_name: Optional[str] = None |
| 22 | + env_uuid: Optional[str] = None |
| 23 | + |
| 24 | + def setUp(self): |
| 25 | + """Set up test fixtures.""" |
| 26 | + self.logger: Mock = Mock() |
| 27 | + self.auth: Mock = Mock() |
| 28 | + self.workspace: str = 'ws' |
| 29 | + self.repo_slug: str = 'repo' |
| 30 | + self.deployment_name: str = 'env' |
| 31 | + self.env_uuid: str = 'uuid-123' |
| 32 | + |
| 33 | + @patch('manage_bitbucket_env.get_environment_uuid') |
| 34 | + @patch('manage_bitbucket_env.get_variables') |
| 35 | + def test_export_variables(self, mock_get_vars, mock_get_uuid): |
| 36 | + """Test successful export of non-secured variables.""" |
| 37 | + mock_get_uuid.return_value = self.env_uuid |
| 38 | + mock_get_vars.return_value = [ |
| 39 | + {"key": "A", "value": "1", "secured": False}, |
| 40 | + {"key": "B", "value": "2", "secured": True} |
| 41 | + ] |
| 42 | + with tempfile.NamedTemporaryFile(delete=False) as tf: |
| 43 | + fname = tf.name |
| 44 | + try: |
| 45 | + export_variables( |
| 46 | + self.workspace, self.repo_slug, self.deployment_name, |
| 47 | + fname, self.auth, self.logger |
| 48 | + ) |
| 49 | + with open(fname, encoding="utf-8") as f: |
| 50 | + data = json.load(f) |
| 51 | + self.assertEqual(len(data), 1) |
| 52 | + self.assertEqual(data[0]["key"], "A") |
| 53 | + finally: |
| 54 | + os.unlink(fname) |
| 55 | + |
| 56 | + @patch('manage_bitbucket_env.get_environment_uuid') |
| 57 | + @patch('manage_bitbucket_env.get_variables') |
| 58 | + def test_export_all_variables(self, mock_get_vars, mock_get_uuid): |
| 59 | + """Test successful export of all variables.""" |
| 60 | + mock_get_uuid.return_value = self.env_uuid |
| 61 | + mock_get_vars.return_value = [ |
| 62 | + {"key": "A", "value": "1", "secured": False}, |
| 63 | + {"key": "B", "value": "2", "secured": True} |
| 64 | + ] |
| 65 | + with tempfile.NamedTemporaryFile(delete=False) as tf: |
| 66 | + fname = tf.name |
| 67 | + try: |
| 68 | + export_all_variables( |
| 69 | + self.workspace, self.repo_slug, self.deployment_name, |
| 70 | + fname, self.auth, self.logger |
| 71 | + ) |
| 72 | + with open(fname, encoding="utf-8") as f: |
| 73 | + data = json.load(f) |
| 74 | + self.assertEqual(len(data), 2) |
| 75 | + test_var = next(v for v in data if v["key"] == "A") |
| 76 | + self.assertEqual(test_var["value"], "1") |
| 77 | + self.assertFalse(test_var["secured"]) |
| 78 | + secure_var = next(v for v in data if v["key"] == "B") |
| 79 | + self.assertEqual(secure_var["value"], "") |
| 80 | + self.assertTrue(secure_var["secured"]) |
| 81 | + finally: |
| 82 | + os.unlink(fname) |
| 83 | + |
| 84 | + @patch('manage_bitbucket_env.get_environment_uuid') |
| 85 | + @patch('manage_bitbucket_env.get_variables') |
| 86 | + def test_export_secure_keys(self, mock_get_vars, mock_get_uuid): |
| 87 | + """Test successful export of secure keys.""" |
| 88 | + mock_get_uuid.return_value = self.env_uuid |
| 89 | + mock_get_vars.return_value = [ |
| 90 | + {"key": "A", "secured": False}, |
| 91 | + {"key": "B", "secured": True}, |
| 92 | + {"key": "C", "secured": True} |
| 93 | + ] |
| 94 | + with tempfile.NamedTemporaryFile(delete=False) as tf: |
| 95 | + fname = tf.name |
| 96 | + try: |
| 97 | + export_secure_keys( |
| 98 | + self.workspace, self.repo_slug, self.deployment_name, |
| 99 | + fname, self.auth, self.logger |
| 100 | + ) |
| 101 | + with open(fname, encoding="utf-8") as f: |
| 102 | + data = json.load(f) |
| 103 | + self.assertIn("B", data) |
| 104 | + self.assertIn("C", data) |
| 105 | + self.assertNotIn("A", data) |
| 106 | + finally: |
| 107 | + os.unlink(fname) |
| 108 | + |
| 109 | + @patch('manage_bitbucket_env.get_environment_uuid') |
| 110 | + @patch('manage_bitbucket_env.get_variables') |
| 111 | + @patch('manage_bitbucket_env.update_vars') |
| 112 | + def test_import_variables(self, mock_update, mock_get_vars, mock_get_uuid): |
| 113 | + """Test successful import of variables.""" |
| 114 | + mock_get_uuid.return_value = self.env_uuid |
| 115 | + mock_get_vars.return_value = [] |
| 116 | + test_vars = [ |
| 117 | + {"key": "A", "value": "1", "secured": False}, |
| 118 | + {"key": "B", "value": "2", "secured": True} |
| 119 | + ] |
| 120 | + with tempfile.NamedTemporaryFile(delete=False, mode='w', encoding="utf-8") as tf: |
| 121 | + json.dump(test_vars, tf) |
| 122 | + fname = tf.name |
| 123 | + try: |
| 124 | + import_variables( |
| 125 | + self.workspace, self.repo_slug, self.deployment_name, |
| 126 | + fname, False, self.auth, self.logger |
| 127 | + ) |
| 128 | + self.assertEqual(mock_update.call_count, 1) |
| 129 | + call_args = mock_update.call_args[1] |
| 130 | + self.assertEqual(call_args['var']["key"], "A") |
| 131 | + finally: |
| 132 | + os.unlink(fname) |
| 133 | + |
| 134 | +if __name__ == '__main__': |
| 135 | + unittest.main() |
0 commit comments