|
| 1 | +#! /usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +# Multicast Python Module (Testing) |
| 5 | +# .................................. |
| 6 | +# Copyright (c) 2025, Mr. Walls |
| 7 | +# .................................. |
| 8 | +# Licensed under MIT (the "License"); |
| 9 | +# you may not use this file except in compliance with the License. |
| 10 | +# You may obtain a copy of the License at |
| 11 | +# .......................................... |
| 12 | +# https://www.github.com/reactive-firewall/multicast/LICENSE.md |
| 13 | +# .......................................... |
| 14 | +# Unless required by applicable law or agreed to in writing, software |
| 15 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | +# See the License for the specific language governing permissions and |
| 18 | +# limitations under the License. |
| 19 | + |
| 20 | +"""Extra Test module for docs.utils functionality. |
| 21 | +
|
| 22 | +This module provides extra test cases for the docs.utils module, focusing on the |
| 23 | +utils.sanitize_url method for url encoding. |
| 24 | +""" |
| 25 | + |
| 26 | + |
| 27 | +__module__ = "tests" |
| 28 | + |
| 29 | + |
| 30 | +try: |
| 31 | + try: |
| 32 | + import context |
| 33 | + except Exception as _: # pragma: no branch |
| 34 | + del _ # skipcq - cleanup any error vars early |
| 35 | + from . import context |
| 36 | + if context.__name__ is None: |
| 37 | + raise ModuleNotFoundError("[CWE-758] Failed to import context") from None |
| 38 | + else: |
| 39 | + from context import unittest |
| 40 | + import docs.utils |
| 41 | +except Exception as err: |
| 42 | + raise ImportError("[CWE-758] Failed to import test context") from err |
| 43 | + |
| 44 | + |
| 45 | +@context.markWithMetaTag("extra", "security") |
| 46 | +class ExtraDocsUtilsTestSuite(context.BasicUsageTestSuite): |
| 47 | + """Test cases for docs.utils module.""" |
| 48 | + |
| 49 | + __module__ = "tests.test_extra" |
| 50 | + |
| 51 | + URL_TEST_FIXTURES = [ |
| 52 | + { |
| 53 | + "input_url": "https://github.com/user/repo", |
| 54 | + "expected": "https://github.com/user/repo", |
| 55 | + }, |
| 56 | + { |
| 57 | + "input_url": "https://github.com/user/repo with spaces", |
| 58 | + "expected": "https://github.com/user/repo%20with%20spaces", |
| 59 | + }, |
| 60 | + { |
| 61 | + "input_url": "https://github.com/user/repo?q=test&sort=desc", |
| 62 | + "expected": "https://github.com/user/repo?q%3Dtest%26sort%3Ddesc", |
| 63 | + }, |
| 64 | + { |
| 65 | + "input_url": "https://github.com/user/repo#section", |
| 66 | + "expected": "https://github.com/user/repo#section", |
| 67 | + }, |
| 68 | + { |
| 69 | + "input_url": "https://github.com/user/repo/<script>alert('xss')</script>", |
| 70 | + "expected": "https://github.com/user/repo/%3Cscript%3Ealert%28%27xss%27%29%3C/script%3E", |
| 71 | + }, |
| 72 | + ] |
| 73 | + |
| 74 | + def test_sanitize_url_GIVEN_raw_url_IS_reliable(self) -> None: |
| 75 | + """Test case 1: Test to ensure reliable URL sanitization.""" |
| 76 | + # Mock _hearstep to return a non-empty response |
| 77 | + for test_params in self.URL_TEST_FIXTURES: |
| 78 | + if test_params["input_url"] == test_params["expected"]: |
| 79 | + continue |
| 80 | + else: |
| 81 | + self.assertNotEqual( |
| 82 | + test_params["input_url"], |
| 83 | + test_params["expected"], |
| 84 | + "Invalid test case. Input and expected URLs were the same, but not skipped.", |
| 85 | + ) |
| 86 | + sanitized_url = docs.utils.sanitize_url(test_params["input_url"]) |
| 87 | + # check for results |
| 88 | + self.assertIsNotNone(sanitized_url) |
| 89 | + # Verify results |
| 90 | + self.assertNotEqual( |
| 91 | + test_params["input_url"], sanitized_url, |
| 92 | + "Invalid test case. Input and output URLs were the same, should be skipped.", |
| 93 | + ) |
| 94 | + self.assertEqual(sanitized_url, test_params["expected"]) |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == '__main__': |
| 98 | + unittest.main() |
0 commit comments