|
| 1 | +# -------------------------------------------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | +# -------------------------------------------------------------------------------------------- |
| 5 | + |
| 6 | +import unittest |
| 7 | +import json |
| 8 | +import base64 |
| 9 | +from unittest import mock |
| 10 | + |
| 11 | +from azext_sftp import connectivity_utils |
| 12 | + |
| 13 | + |
| 14 | +class SftpConnectivityUtilsTest(unittest.TestCase): |
| 15 | + """Test suite for SFTP connectivity utilities. |
| 16 | + |
| 17 | + Owner: johnli1 |
| 18 | + """ |
| 19 | + |
| 20 | + def test_format_relay_info_string_success(self): |
| 21 | + """Test format_relay_info_string with valid relay information.""" |
| 22 | + # Arrange |
| 23 | + relay_info = { |
| 24 | + 'namespaceName': 'test-namespace', |
| 25 | + 'namespaceNameSuffix': 'servicebus.windows.net', |
| 26 | + 'hybridConnectionName': 'test-connection', |
| 27 | + 'accessKey': 'test-access-key', |
| 28 | + 'expiresOn': '2025-07-04T10:00:00Z', |
| 29 | + 'serviceConfigurationToken': 'test-token' |
| 30 | + } |
| 31 | + |
| 32 | + # Act |
| 33 | + result = connectivity_utils.format_relay_info_string(relay_info) |
| 34 | + |
| 35 | + # Assert |
| 36 | + # Decode the base64 result to verify the structure |
| 37 | + decoded_bytes = base64.b64decode(result.encode('ascii')) |
| 38 | + decoded_string = decoded_bytes.decode('ascii') |
| 39 | + parsed_result = json.loads(decoded_string) |
| 40 | + |
| 41 | + self.assertIn('relay', parsed_result) |
| 42 | + relay_data = parsed_result['relay'] |
| 43 | + |
| 44 | + self.assertEqual(relay_data['namespaceName'], 'test-namespace') |
| 45 | + self.assertEqual(relay_data['namespaceNameSuffix'], 'servicebus.windows.net') |
| 46 | + self.assertEqual(relay_data['hybridConnectionName'], 'test-connection') |
| 47 | + self.assertEqual(relay_data['accessKey'], 'test-access-key') |
| 48 | + self.assertEqual(relay_data['expiresOn'], '2025-07-04T10:00:00Z') |
| 49 | + self.assertEqual(relay_data['serviceConfigurationToken'], 'test-token') |
| 50 | + |
| 51 | + def test_format_relay_info_string_minimal_data(self): |
| 52 | + """Test format_relay_info_string with minimal required fields.""" |
| 53 | + # Arrange |
| 54 | + relay_info = { |
| 55 | + 'namespaceName': 'minimal', |
| 56 | + 'namespaceNameSuffix': 'suffix', |
| 57 | + 'hybridConnectionName': 'connection', |
| 58 | + 'accessKey': 'key', |
| 59 | + 'expiresOn': '2025-01-01T00:00:00Z', |
| 60 | + 'serviceConfigurationToken': 'token' |
| 61 | + } |
| 62 | + |
| 63 | + # Act |
| 64 | + result = connectivity_utils.format_relay_info_string(relay_info) |
| 65 | + |
| 66 | + # Assert |
| 67 | + self.assertIsInstance(result, str) |
| 68 | + self.assertTrue(len(result) > 0) |
| 69 | + |
| 70 | + # Verify it's valid base64 |
| 71 | + try: |
| 72 | + decoded = base64.b64decode(result.encode('ascii')) |
| 73 | + json.loads(decoded.decode('ascii')) |
| 74 | + except (ValueError, json.JSONDecodeError): |
| 75 | + self.fail("Result is not valid base64-encoded JSON") |
| 76 | + |
| 77 | + def test_format_relay_info_string_special_characters(self): |
| 78 | + """Test format_relay_info_string with special characters in data.""" |
| 79 | + # Arrange |
| 80 | + relay_info = { |
| 81 | + 'namespaceName': 'test-namespace-with-dashes', |
| 82 | + 'namespaceNameSuffix': 'test.suffix.com', |
| 83 | + 'hybridConnectionName': 'connection_with_underscores', |
| 84 | + 'accessKey': 'key+with/special=chars', |
| 85 | + 'expiresOn': '2025-12-31T23:59:59.999Z', |
| 86 | + 'serviceConfigurationToken': 'token-with-various_special.chars' |
| 87 | + } |
| 88 | + |
| 89 | + # Act |
| 90 | + result = connectivity_utils.format_relay_info_string(relay_info) |
| 91 | + |
| 92 | + # Assert |
| 93 | + decoded_bytes = base64.b64decode(result.encode('ascii')) |
| 94 | + decoded_string = decoded_bytes.decode('ascii') |
| 95 | + parsed_result = json.loads(decoded_string) |
| 96 | + |
| 97 | + relay_data = parsed_result['relay'] |
| 98 | + self.assertEqual(relay_data['namespaceName'], 'test-namespace-with-dashes') |
| 99 | + self.assertEqual(relay_data['accessKey'], 'key+with/special=chars') |
| 100 | + |
| 101 | + def test_format_relay_info_string_unicode_characters(self): |
| 102 | + """Test format_relay_info_string with unicode characters.""" |
| 103 | + # Arrange |
| 104 | + relay_info = { |
| 105 | + 'namespaceName': 'test-unicode-αβγ', |
| 106 | + 'namespaceNameSuffix': 'suffix-δεζ', |
| 107 | + 'hybridConnectionName': 'connection-ηθι', |
| 108 | + 'accessKey': 'key-κλμ', |
| 109 | + 'expiresOn': '2025-06-15T12:30:45Z', |
| 110 | + 'serviceConfigurationToken': 'token-νξο' |
| 111 | + } |
| 112 | + |
| 113 | + # Act |
| 114 | + result = connectivity_utils.format_relay_info_string(relay_info) |
| 115 | + |
| 116 | + # Assert |
| 117 | + self.assertIsInstance(result, str) |
| 118 | + |
| 119 | + # Verify roundtrip encoding/decoding works |
| 120 | + decoded_bytes = base64.b64decode(result.encode('ascii')) |
| 121 | + decoded_string = decoded_bytes.decode('ascii') |
| 122 | + parsed_result = json.loads(decoded_string) |
| 123 | + |
| 124 | + relay_data = parsed_result['relay'] |
| 125 | + self.assertEqual(relay_data['namespaceName'], 'test-unicode-αβγ') |
| 126 | + |
| 127 | + def test_format_relay_info_string_empty_values(self): |
| 128 | + """Test format_relay_info_string with empty string values.""" |
| 129 | + # Arrange |
| 130 | + relay_info = { |
| 131 | + 'namespaceName': '', |
| 132 | + 'namespaceNameSuffix': '', |
| 133 | + 'hybridConnectionName': '', |
| 134 | + 'accessKey': '', |
| 135 | + 'expiresOn': '', |
| 136 | + 'serviceConfigurationToken': '' |
| 137 | + } |
| 138 | + |
| 139 | + # Act |
| 140 | + result = connectivity_utils.format_relay_info_string(relay_info) |
| 141 | + |
| 142 | + # Assert |
| 143 | + decoded_bytes = base64.b64decode(result.encode('ascii')) |
| 144 | + decoded_string = decoded_bytes.decode('ascii') |
| 145 | + parsed_result = json.loads(decoded_string) |
| 146 | + |
| 147 | + relay_data = parsed_result['relay'] |
| 148 | + for key in relay_data: |
| 149 | + self.assertEqual(relay_data[key], '') |
| 150 | + |
| 151 | + def test_format_relay_info_string_large_values(self): |
| 152 | + """Test format_relay_info_string with large string values.""" |
| 153 | + # Arrange |
| 154 | + large_string = 'x' * 1000 # 1000 character string |
| 155 | + relay_info = { |
| 156 | + 'namespaceName': large_string, |
| 157 | + 'namespaceNameSuffix': large_string, |
| 158 | + 'hybridConnectionName': large_string, |
| 159 | + 'accessKey': large_string, |
| 160 | + 'expiresOn': '2025-07-04T10:00:00Z', |
| 161 | + 'serviceConfigurationToken': large_string |
| 162 | + } |
| 163 | + |
| 164 | + # Act |
| 165 | + result = connectivity_utils.format_relay_info_string(relay_info) |
| 166 | + |
| 167 | + # Assert |
| 168 | + self.assertIsInstance(result, str) |
| 169 | + decoded_bytes = base64.b64decode(result.encode('ascii')) |
| 170 | + decoded_string = decoded_bytes.decode('ascii') |
| 171 | + parsed_result = json.loads(decoded_string) |
| 172 | + |
| 173 | + relay_data = parsed_result['relay'] |
| 174 | + self.assertEqual(relay_data['namespaceName'], large_string) |
| 175 | + |
| 176 | + @mock.patch('json.dumps') |
| 177 | + def test_format_relay_info_string_json_error_handling(self, mock_dumps): |
| 178 | + """Test format_relay_info_string handles JSON serialization errors.""" |
| 179 | + # Arrange |
| 180 | + mock_dumps.side_effect = TypeError("Object not serializable") |
| 181 | + relay_info = { |
| 182 | + 'namespaceName': 'test', |
| 183 | + 'namespaceNameSuffix': 'test', |
| 184 | + 'hybridConnectionName': 'test', |
| 185 | + 'accessKey': 'test', |
| 186 | + 'expiresOn': 'test', |
| 187 | + 'serviceConfigurationToken': 'test' |
| 188 | + } |
| 189 | + |
| 190 | + # Act & Assert |
| 191 | + with self.assertRaises(TypeError): |
| 192 | + connectivity_utils.format_relay_info_string(relay_info) |
| 193 | + |
| 194 | + def test_format_relay_info_string_structure_validation(self): |
| 195 | + """Test that format_relay_info_string produces the expected JSON structure.""" |
| 196 | + # Arrange |
| 197 | + relay_info = { |
| 198 | + 'namespaceName': 'test-namespace', |
| 199 | + 'namespaceNameSuffix': 'servicebus.windows.net', |
| 200 | + 'hybridConnectionName': 'test-connection', |
| 201 | + 'accessKey': 'test-access-key', |
| 202 | + 'expiresOn': '2025-07-04T10:00:00Z', |
| 203 | + 'serviceConfigurationToken': 'test-token' |
| 204 | + } |
| 205 | + |
| 206 | + # Act |
| 207 | + result = connectivity_utils.format_relay_info_string(relay_info) |
| 208 | + |
| 209 | + # Assert - Verify the exact structure matches expectations |
| 210 | + decoded_bytes = base64.b64decode(result.encode('ascii')) |
| 211 | + decoded_string = decoded_bytes.decode('ascii') |
| 212 | + parsed_result = json.loads(decoded_string) |
| 213 | + |
| 214 | + # Check top-level structure |
| 215 | + self.assertEqual(list(parsed_result.keys()), ['relay']) |
| 216 | + |
| 217 | + # Check relay object has all required fields |
| 218 | + relay_data = parsed_result['relay'] |
| 219 | + expected_keys = { |
| 220 | + 'namespaceName', 'namespaceNameSuffix', 'hybridConnectionName', |
| 221 | + 'accessKey', 'expiresOn', 'serviceConfigurationToken' |
| 222 | + } |
| 223 | + self.assertEqual(set(relay_data.keys()), expected_keys) |
| 224 | + |
| 225 | + |
| 226 | +if __name__ == '__main__': |
| 227 | + unittest.main() |
0 commit comments