|
4 | 4 |
|
5 | 5 | from openstack import exceptions |
6 | 6 |
|
7 | | -from openstack_mcp_server.tools.identity_tools import IdentityTools, Region |
| 7 | +from openstack_mcp_server.tools.identity_tools import IdentityTools |
| 8 | +from openstack_mcp_server.tools.response.identity import Region, Domain |
8 | 9 |
|
9 | 10 |
|
10 | 11 | class TestIdentityTools: |
@@ -334,3 +335,96 @@ def test_get_region_not_found(self, mock_get_openstack_conn_identity): |
334 | 335 | mock_conn.identity.get_region.assert_called_once_with( |
335 | 336 | region="RegionOne", |
336 | 337 | ) |
| 338 | + |
| 339 | + def test_get_domains_success(self, mock_get_openstack_conn_identity): |
| 340 | + """Test getting identity domains successfully.""" |
| 341 | + mock_conn = mock_get_openstack_conn_identity |
| 342 | + |
| 343 | + # Create mock domain objects |
| 344 | + mock_domain1 = Mock() |
| 345 | + mock_domain1.id = "domainone" |
| 346 | + mock_domain1.name = "DomainOne" |
| 347 | + mock_domain1.description = "Domain One description" |
| 348 | + mock_domain1.is_enabled = True |
| 349 | + |
| 350 | + mock_domain2 = Mock() |
| 351 | + mock_domain2.id = "domaintwo" |
| 352 | + mock_domain2.name = "DomainTwo" |
| 353 | + mock_domain2.description = "Domain Two description" |
| 354 | + mock_domain2.is_enabled = False |
| 355 | + |
| 356 | + # Configure mock domain.domains() |
| 357 | + mock_conn.identity.domains.return_value = [mock_domain1, mock_domain2] |
| 358 | + |
| 359 | + # Test get_domains() |
| 360 | + identity_tools = self.get_identity_tools() |
| 361 | + result = identity_tools.get_domains() |
| 362 | + |
| 363 | + # Verify results |
| 364 | + assert result == [ |
| 365 | + Domain(id="domainone", name="DomainOne", description="Domain One description", is_enabled=True), |
| 366 | + Domain(id="domaintwo", name="DomainTwo", description="Domain Two description", is_enabled=False), |
| 367 | + ] |
| 368 | + |
| 369 | + # Verify mock calls |
| 370 | + mock_conn.identity.domains.assert_called_once() |
| 371 | + |
| 372 | + def test_get_domains_empty_list(self, mock_get_openstack_conn_identity): |
| 373 | + """Test getting identity domains when there are no domains.""" |
| 374 | + mock_conn = mock_get_openstack_conn_identity |
| 375 | + |
| 376 | + # Empty domain list |
| 377 | + mock_conn.identity.domains.return_value = [] |
| 378 | + |
| 379 | + # Test get_domains() |
| 380 | + identity_tools = self.get_identity_tools() |
| 381 | + result = identity_tools.get_domains() |
| 382 | + |
| 383 | + # Verify results |
| 384 | + assert result == [] |
| 385 | + |
| 386 | + # Verify mock calls |
| 387 | + mock_conn.identity.domains.assert_called_once() |
| 388 | + |
| 389 | + def test_get_domain_success(self, mock_get_openstack_conn_identity): |
| 390 | + """Test getting a identity domain successfully.""" |
| 391 | + mock_conn = mock_get_openstack_conn_identity |
| 392 | + |
| 393 | + # Create mock domain object |
| 394 | + mock_domain = Mock() |
| 395 | + mock_domain.id = "domainone" |
| 396 | + mock_domain.name = "DomainOne" |
| 397 | + mock_domain.description = "Domain One description" |
| 398 | + mock_domain.is_enabled = True |
| 399 | + |
| 400 | + # Configure mock domain.get_domain() |
| 401 | + mock_conn.identity.get_domain.return_value = mock_domain |
| 402 | + |
| 403 | + # Test get_domain() |
| 404 | + identity_tools = self.get_identity_tools() |
| 405 | + result = identity_tools.get_domain(id="domainone") |
| 406 | + |
| 407 | + # Verify results |
| 408 | + assert result == Domain(id="domainone", name="DomainOne", description="Domain One description", is_enabled=True) |
| 409 | + |
| 410 | + # Verify mock calls |
| 411 | + mock_conn.identity.get_domain.assert_called_once_with(domain="domainone") |
| 412 | + |
| 413 | + def test_get_domain_not_found(self, mock_get_openstack_conn_identity): |
| 414 | + """Test getting a identity domain that does not exist.""" |
| 415 | + mock_conn = mock_get_openstack_conn_identity |
| 416 | + |
| 417 | + # Configure mock to raise NotFoundException |
| 418 | + mock_conn.identity.get_domain.side_effect = exceptions.NotFoundException( |
| 419 | + "Domain 'domainone' not found", |
| 420 | + ) |
| 421 | + |
| 422 | + # Test get_domain() |
| 423 | + identity_tools = self.get_identity_tools() |
| 424 | + |
| 425 | + # Verify exception is raised |
| 426 | + with pytest.raises(exceptions.NotFoundException, match="Domain 'domainone' not found"): |
| 427 | + identity_tools.get_domain(id="domainone") |
| 428 | + |
| 429 | + # Verify mock calls |
| 430 | + mock_conn.identity.get_domain.assert_called_once_with(domain="domainone") |
0 commit comments