ministryofjustice / operations-engineering

This repository is home to the Operations Engineering's tools and utilities for managing, monitoring, and optimising software development processes at the Ministry of Justice. • This repository is defined and managed in Terraform
https://user-guide.operations-engineering.service.justice.gov.uk/
MIT License
14 stars 5 forks source link

MTA-STS Unit tests #4571

Closed vijaykannan21 closed 2 months ago

vijaykannan21 commented 3 months ago
  1. Rephrased check_mta_sts.py Improved readability by renaming variables and functions. Optimized logic for clarity and performance.

  2. Rephrased s3_service.py Enhanced clarity with better variable and function names. Refactored larger functions into smaller ones for better modularity.

  3. Rephrased in test_test_service_testS3_service.py

    Pretends the file says "enforce" and checks that the method returns True. Pretends the file says "disabled" and checks that the method returns False. Pretends the file doesn't exist and checks that the method returns False.

  4. Created test_check_mta_sts.py Added unit tests to cover core functionalities of check_mta_sts.py. Included test cases for typical and edge scenarios. Used mocking to isolate tests and assertions to verify outcomes.

github-actions[bot] commented 3 months ago

🦙 MegaLinter status: ✅ SUCCESS

Descriptor Linter Files Fixed Errors Elapsed time
✅ PYTHON flake8 4 0 0.6s
✅ PYTHON isort 4 3 0 0.35s
✅ PYTHON pylint 4 0 3.15s
✅ REPOSITORY gitleaks yes no 1.62s

See detailed report in MegaLinter reports _Set VALIDATE_ALL_CODEBASE: true in mega-linter.yml to validate all sources, not only the diff_

_MegaLinter is graciously provided by OX Security_

connormaglynn commented 2 months ago

Some example tests for reference

# A very Basic tests without mocking
class TestAddNumebrs(unittest.TestCase):
    def test_function_adds_numbers(self):
        # Given
        number_1 = 1
        number_2 = 2

        # When
        response = add_numbers(number_1, number_2)

        # Then
        self.assertEqual(response, 3)

# A test for the current code (given it returned a value) where we mock the interaction with the S3Service
@patch("services.s3_service.S3Service.__new__")
class TestMTASTSExample(unittest.TestCase):
    def test_0_failed_domains_when_all_domains_have_mta_sts_enforce(self, mock_s3_service):
        # Given ...setup environment and mocks
        mock_s3_service.return_value.is_well_known_mta_sts_enforce.return_value = True

        # When ...call main()
        failed_domains = main()

        # Then ...add assertions
        self.assertEquals(len(failed_domains), 0)

Hopefully, this helps get a basic structure together for the unit testing! 🧪