|
21 | 21 | # Functions from content_health_utils |
22 | 22 | check_server_reachable = content_health_utils.check_server_reachable |
23 | 23 | extract_error_details = content_health_utils.extract_error_details |
| 24 | +extract_guid = content_health_utils.extract_guid |
24 | 25 | format_error_message = content_health_utils.format_error_message |
25 | 26 | get_content = content_health_utils.get_content |
26 | 27 | get_env_var = content_health_utils.get_env_var |
@@ -250,6 +251,67 @@ def test_format_error_message_client_error_invalid_json(self): |
250 | 251 | assert result == error_text |
251 | 252 |
|
252 | 253 |
|
| 254 | +# Tests for extract_guid function |
| 255 | +class TestExtractGuid: |
| 256 | + |
| 257 | + def test_extract_guid_with_valid_guid(self): |
| 258 | + """Test extract_guid with a valid GUID string""" |
| 259 | + # Setup - Valid GUID string |
| 260 | + input_string = "1d97c1ff-e56c-4074-906f-cb3557685b75" |
| 261 | + |
| 262 | + # Execute |
| 263 | + result, error_message = extract_guid(input_string) |
| 264 | + |
| 265 | + # Assert |
| 266 | + assert result == input_string |
| 267 | + assert error_message is None |
| 268 | + |
| 269 | + def test_extract_guid_with_valid_guid_in_url(self): |
| 270 | + """Test extract_guid with a URL containing a valid GUID""" |
| 271 | + # Setup - URL with GUID |
| 272 | + guid = "1d97c1ff-e56c-4074-906f-cb3557685b75" |
| 273 | + input_string = f"https://connect.example.com/content/{guid}/" |
| 274 | + |
| 275 | + # Execute |
| 276 | + result, error_message = extract_guid(input_string) |
| 277 | + |
| 278 | + # Assert |
| 279 | + assert result == guid |
| 280 | + assert error_message is None |
| 281 | + |
| 282 | + def test_extract_guid_with_url_no_guid(self): |
| 283 | + """Test extract_guid with a URL that does not contain a GUID""" |
| 284 | + # Setup - URL without GUID |
| 285 | + input_string = "https://connect.example.com/content/dashboard" |
| 286 | + |
| 287 | + # Execute |
| 288 | + result, error_message = extract_guid(input_string) |
| 289 | + |
| 290 | + # Assert |
| 291 | + assert result == input_string |
| 292 | + assert error_message is not None |
| 293 | + assert "The URL provided in <code>MONITORED_CONTENT_GUID</code> does not contain a valid GUID" in error_message |
| 294 | + assert "The URL should contain a GUID like: <code>1d97c1ff-e56c-4074-906f-cb3557685b75</code>" in error_message |
| 295 | + assert f"<a href=\"{input_string}\" target=\"_blank\" rel=\"noopener noreferrer\">" in error_message |
| 296 | + assert "Please update your environment variable with a valid GUID or a URL containing a GUID" in error_message |
| 297 | + |
| 298 | + def test_extract_guid_with_plain_text(self): |
| 299 | + """Test extract_guid with plain text that is not a URL and does not contain a GUID""" |
| 300 | + # Setup - Plain text input |
| 301 | + input_string = "some-content-name" |
| 302 | + |
| 303 | + # Execute |
| 304 | + result, error_message = extract_guid(input_string) |
| 305 | + |
| 306 | + # Assert |
| 307 | + assert result == input_string |
| 308 | + assert error_message is not None |
| 309 | + assert "The value provided in <code>MONITORED_CONTENT_GUID</code> is not a valid GUID" in error_message |
| 310 | + assert "A valid GUID looks like: <code>1d97c1ff-e56c-4074-906f-cb3557685b75</code>" in error_message |
| 311 | + assert f"The provided value was: <code>{input_string}</code>" in error_message |
| 312 | + assert "Please update your environment variable with a valid GUID or a URL containing a GUID" in error_message |
| 313 | + |
| 314 | + |
253 | 315 | # Tests for get_content and get_user functions |
254 | 316 | class TestContentAndUserFunctions: |
255 | 317 |
|
@@ -488,6 +550,36 @@ def test_validate_missing_content_url(self, mock_client, valid_content_response, |
488 | 550 | mock_get.assert_called_once() |
489 | 551 | args, kwargs = mock_get.call_args |
490 | 552 | assert args[0] == f"{connect_test_server}/content/{guid}" |
| 553 | + |
| 554 | + def test_validate_missing_content_url_with_trailing_slash(self, mock_client, valid_content_response, valid_user_response, |
| 555 | + mock_response, api_test_key): |
| 556 | + """Test validate when content_url is missing and connect_server has a trailing slash""" |
| 557 | + # Setup - content details without content_url |
| 558 | + guid = valid_content_response["guid"] |
| 559 | + content_without_url = valid_content_response.copy() |
| 560 | + del content_without_url["content_url"] |
| 561 | + mock_client.content.get.return_value = content_without_url |
| 562 | + |
| 563 | + # Setup - owner details |
| 564 | + mock_client.users.get.return_value = valid_user_response |
| 565 | + |
| 566 | + # Setup - connect_server with trailing slash |
| 567 | + connect_server_with_trailing_slash = "https://connect.example.com/" |
| 568 | + |
| 569 | + # Setup - HTTP request |
| 570 | + with patch('requests.get', return_value=mock_response) as mock_get: |
| 571 | + # Execute |
| 572 | + result = validate(mock_client, guid, connect_server_with_trailing_slash, api_test_key) |
| 573 | + |
| 574 | + # Assert |
| 575 | + assert result["guid"] == guid |
| 576 | + assert result["status"] == STATUS_PASS |
| 577 | + |
| 578 | + # Verify HTTP request was made with correctly constructed URL - no double slash |
| 579 | + mock_get.assert_called_once() |
| 580 | + args, kwargs = mock_get.call_args |
| 581 | + assert args[0] == "https://connect.example.com/content/{0}".format(guid) |
| 582 | + assert "//" not in args[0].replace("https://", "") |
491 | 583 |
|
492 | 584 | def test_validate_no_title_fallback_to_name(self, mock_client, valid_user_response, mock_response, |
493 | 585 | connect_test_server, api_test_key): |
|
0 commit comments