|
6 | 6 | import textwrap |
7 | 7 | from tempfile import NamedTemporaryFile |
8 | 8 |
|
| 9 | +import requests |
| 10 | +import requests_mock |
| 11 | + |
9 | 12 | from .utils import BaseCliTest |
10 | 13 |
|
11 | 14 |
|
@@ -67,3 +70,241 @@ def test_admin_save_config_and_show(self): |
67 | 70 | ) |
68 | 71 |
|
69 | 72 | os.unlink(file_location.name) |
| 73 | + |
| 74 | + def test_auto_configure_url_only(self): |
| 75 | + """Test auto-configure with --url parameter""" |
| 76 | + file_location = NamedTemporaryFile(mode="w+", delete=False) |
| 77 | + |
| 78 | + # Mock the HTTP responses for config endpoints |
| 79 | + mock_config = { |
| 80 | + "cognito_install": "auth.console.dev.stacklet.dev", |
| 81 | + "cognito_user_pool_region": "us-east-2", |
| 82 | + "cognito_user_pool_id": "us-east-2_test123", |
| 83 | + "cognito_user_pool_client_id": "test-client-id", |
| 84 | + "cubejs_domain": "cubejs.dev.stacklet.dev", |
| 85 | + "saml_providers": [{"name": "TestIDP", "idp_id": "test-idp-id"}], |
| 86 | + } |
| 87 | + |
| 88 | + with requests_mock.Mocker() as m: |
| 89 | + # Mock both config endpoints |
| 90 | + m.get("https://console.dev.stacklet.dev/config/cognito.json", json=mock_config) |
| 91 | + m.get("https://console.dev.stacklet.dev/config/cubejs.json", json={}) |
| 92 | + |
| 93 | + res = self.runner.invoke( |
| 94 | + self.cli, |
| 95 | + [ |
| 96 | + "auto-configure", |
| 97 | + "--url=console.dev.stacklet.dev", |
| 98 | + f"--location={file_location.name}", |
| 99 | + ], |
| 100 | + ) |
| 101 | + |
| 102 | + self.assertEqual(res.exit_code, 0) |
| 103 | + |
| 104 | + # Verify the config was saved correctly |
| 105 | + with open(file_location.name, "r") as f: |
| 106 | + config = json.load(f) |
| 107 | + |
| 108 | + self.assertEqual(config["api"], "https://api.dev.stacklet.dev") |
| 109 | + self.assertEqual(config["auth_url"], "https://auth.console.dev.stacklet.dev") |
| 110 | + self.assertEqual(config["cognito_client_id"], "test-client-id") |
| 111 | + self.assertEqual(config["cognito_user_pool_id"], "us-east-2_test123") |
| 112 | + self.assertEqual(config["region"], "us-east-2") |
| 113 | + self.assertEqual(config["cubejs"], "https://cubejs.dev.stacklet.dev") |
| 114 | + self.assertEqual(config["idp_id"], "test-idp-id") |
| 115 | + |
| 116 | + os.unlink(file_location.name) |
| 117 | + |
| 118 | + def test_auto_configure_prefix_only(self): |
| 119 | + """Test auto-configure with --prefix parameter""" |
| 120 | + file_location = NamedTemporaryFile(mode="w+", delete=False) |
| 121 | + |
| 122 | + mock_config = { |
| 123 | + "cognito_install": "auth.console.myorg.stacklet.io", |
| 124 | + "cognito_user_pool_region": "us-west-2", |
| 125 | + "cognito_user_pool_id": "us-west-2_test456", |
| 126 | + "cognito_user_pool_client_id": "test-client-id-2", |
| 127 | + "cubejs_domain": "cubejs.myorg.stacklet.io", |
| 128 | + "saml_providers": [{"name": "OrgIDP", "idp_id": "org-idp-id"}], |
| 129 | + } |
| 130 | + |
| 131 | + with requests_mock.Mocker() as m: |
| 132 | + m.get("https://console.myorg.stacklet.io/config/cognito.json", json=mock_config) |
| 133 | + m.get("https://console.myorg.stacklet.io/config/cubejs.json", json={}) |
| 134 | + |
| 135 | + res = self.runner.invoke( |
| 136 | + self.cli, |
| 137 | + [ |
| 138 | + "auto-configure", |
| 139 | + "--prefix=myorg", |
| 140 | + f"--location={file_location.name}", |
| 141 | + ], |
| 142 | + ) |
| 143 | + |
| 144 | + self.assertEqual(res.exit_code, 0) |
| 145 | + |
| 146 | + with open(file_location.name, "r") as f: |
| 147 | + config = json.load(f) |
| 148 | + |
| 149 | + self.assertEqual(config["api"], "https://api.myorg.stacklet.io") |
| 150 | + self.assertEqual(config["auth_url"], "https://auth.console.myorg.stacklet.io") |
| 151 | + self.assertEqual(config["cubejs"], "https://cubejs.myorg.stacklet.io") |
| 152 | + |
| 153 | + os.unlink(file_location.name) |
| 154 | + |
| 155 | + def test_auto_configure_url_without_console_prefix(self): |
| 156 | + """Test auto-configure with URL that needs console prefix added""" |
| 157 | + file_location = NamedTemporaryFile(mode="w+", delete=False) |
| 158 | + |
| 159 | + mock_config = { |
| 160 | + "cognito_install": "auth.console.example.stacklet.io", |
| 161 | + "cognito_user_pool_region": "eu-west-1", |
| 162 | + "cognito_user_pool_id": "eu-west-1_test789", |
| 163 | + "cognito_user_pool_client_id": "test-client-id-3", |
| 164 | + "cubejs_domain": "cubejs.example.stacklet.io", |
| 165 | + "saml_providers": [{"name": "ExampleIDP", "idp_id": "example-idp-id"}], |
| 166 | + } |
| 167 | + |
| 168 | + with requests_mock.Mocker() as m: |
| 169 | + m.get("https://console.example.stacklet.io/config/cognito.json", json=mock_config) |
| 170 | + m.get("https://console.example.stacklet.io/config/cubejs.json", json={}) |
| 171 | + |
| 172 | + res = self.runner.invoke( |
| 173 | + self.cli, |
| 174 | + [ |
| 175 | + "auto-configure", |
| 176 | + "--url=example.stacklet.io", # Missing console prefix |
| 177 | + f"--location={file_location.name}", |
| 178 | + ], |
| 179 | + ) |
| 180 | + |
| 181 | + self.assertEqual(res.exit_code, 0) |
| 182 | + |
| 183 | + # Verify it added the console prefix and connected correctly |
| 184 | + with open(file_location.name, "r") as f: |
| 185 | + config = json.load(f) |
| 186 | + |
| 187 | + self.assertEqual(config["region"], "eu-west-1") |
| 188 | + self.assertEqual(config["cognito_user_pool_id"], "eu-west-1_test789") |
| 189 | + |
| 190 | + os.unlink(file_location.name) |
| 191 | + |
| 192 | + def test_auto_configure_both_url_and_prefix_error(self): |
| 193 | + """Test that providing both --url and --prefix results in an error""" |
| 194 | + res = self.runner.invoke( |
| 195 | + self.cli, |
| 196 | + [ |
| 197 | + "auto-configure", |
| 198 | + "--url=console.dev.stacklet.dev", |
| 199 | + "--prefix=dev", |
| 200 | + ], |
| 201 | + ) |
| 202 | + |
| 203 | + self.assertEqual(res.exit_code, 1) # Command should exit with error code |
| 204 | + self.assertIn("Cannot specify both --url and --prefix", res.output) |
| 205 | + |
| 206 | + def test_auto_configure_neither_url_nor_prefix_error(self): |
| 207 | + """Test that providing neither --url nor --prefix results in an error""" |
| 208 | + res = self.runner.invoke( |
| 209 | + self.cli, |
| 210 | + [ |
| 211 | + "auto-configure", |
| 212 | + ], |
| 213 | + ) |
| 214 | + |
| 215 | + self.assertEqual(res.exit_code, 1) # Command should exit with error code |
| 216 | + self.assertIn("Must specify either --url or --prefix", res.output) |
| 217 | + |
| 218 | + def test_auto_configure_connection_error(self): |
| 219 | + """Test auto-configure behavior when connection fails""" |
| 220 | + file_location = NamedTemporaryFile(mode="w+", delete=False) |
| 221 | + |
| 222 | + with requests_mock.Mocker() as m: |
| 223 | + # Mock connection error |
| 224 | + m.get( |
| 225 | + "https://console.nonexistent.stacklet.dev/config/cognito.json", |
| 226 | + exc=requests.exceptions.ConnectionError, |
| 227 | + ) |
| 228 | + |
| 229 | + res = self.runner.invoke( |
| 230 | + self.cli, |
| 231 | + [ |
| 232 | + "auto-configure", |
| 233 | + "--url=console.nonexistent.stacklet.dev", |
| 234 | + f"--location={file_location.name}", |
| 235 | + ], |
| 236 | + ) |
| 237 | + |
| 238 | + self.assertEqual(res.exit_code, 1) # Command should exit with error code |
| 239 | + self.assertIn("Unable to connect to", res.output) |
| 240 | + |
| 241 | + os.unlink(file_location.name) |
| 242 | + |
| 243 | + def test_auto_configure_multiple_idps_with_selection(self): |
| 244 | + """Test auto-configure with multiple IDPs and explicit selection""" |
| 245 | + file_location = NamedTemporaryFile(mode="w+", delete=False) |
| 246 | + |
| 247 | + mock_config = { |
| 248 | + "cognito_install": "auth.console.multi.stacklet.dev", |
| 249 | + "cognito_user_pool_region": "us-east-1", |
| 250 | + "cognito_user_pool_id": "us-east-1_multi123", |
| 251 | + "cognito_user_pool_client_id": "multi-client-id", |
| 252 | + "cubejs_domain": "cubejs.multi.stacklet.dev", |
| 253 | + "saml_providers": [ |
| 254 | + {"name": "IDP1", "idp_id": "idp-1"}, |
| 255 | + {"name": "IDP2", "idp_id": "idp-2"}, |
| 256 | + ], |
| 257 | + } |
| 258 | + |
| 259 | + with requests_mock.Mocker() as m: |
| 260 | + m.get("https://console.multi.stacklet.dev/config/cognito.json", json=mock_config) |
| 261 | + m.get("https://console.multi.stacklet.dev/config/cubejs.json", json={}) |
| 262 | + |
| 263 | + res = self.runner.invoke( |
| 264 | + self.cli, |
| 265 | + [ |
| 266 | + "auto-configure", |
| 267 | + "--url=console.multi.stacklet.dev", |
| 268 | + "--idp=IDP2", |
| 269 | + f"--location={file_location.name}", |
| 270 | + ], |
| 271 | + ) |
| 272 | + |
| 273 | + self.assertEqual(res.exit_code, 0) |
| 274 | + |
| 275 | + with open(file_location.name, "r") as f: |
| 276 | + config = json.load(f) |
| 277 | + |
| 278 | + self.assertEqual(config["idp_id"], "idp-2") # Should use the selected IDP |
| 279 | + |
| 280 | + os.unlink(file_location.name) |
| 281 | + |
| 282 | + def test_auto_configure_multiple_idps_no_selection_error(self): |
| 283 | + """Test auto-configure with multiple IDPs but no selection results in error""" |
| 284 | + mock_config = { |
| 285 | + "cognito_install": "auth.console.multi.stacklet.dev", |
| 286 | + "cognito_user_pool_region": "us-east-1", |
| 287 | + "cognito_user_pool_id": "us-east-1_multi123", |
| 288 | + "cognito_user_pool_client_id": "multi-client-id", |
| 289 | + "cubejs_domain": "cubejs.multi.stacklet.dev", |
| 290 | + "saml_providers": [ |
| 291 | + {"name": "IDP1", "idp_id": "idp-1"}, |
| 292 | + {"name": "IDP2", "idp_id": "idp-2"}, |
| 293 | + ], |
| 294 | + } |
| 295 | + |
| 296 | + with requests_mock.Mocker() as m: |
| 297 | + m.get("https://console.multi.stacklet.dev/config/cognito.json", json=mock_config) |
| 298 | + m.get("https://console.multi.stacklet.dev/config/cubejs.json", json={}) |
| 299 | + |
| 300 | + res = self.runner.invoke( |
| 301 | + self.cli, |
| 302 | + [ |
| 303 | + "auto-configure", |
| 304 | + "--url=console.multi.stacklet.dev", |
| 305 | + # No --idp specified |
| 306 | + ], |
| 307 | + ) |
| 308 | + |
| 309 | + self.assertEqual(res.exit_code, 1) # Command should exit with error code |
| 310 | + self.assertIn("Multiple identity providers available", res.output) |
0 commit comments