CJohnston079 / obfuscator

0 stars 0 forks source link

Untested custom Exception in `obfuscate_fields` #8

Closed CJohnston079 closed 4 months ago

CJohnston079 commented 4 months ago

Issue

Generic Exceptions in obfuscate_fields are raised as an ObfuscationError:

    except Exception as e:
        raise ObfuscationError(e)

However, this line is not covered by the unit test written for it, which only checks that a generic Exception was raised:

    def test_raises_obfuscation_error_for_other_exceptions(self, mocker):
        obfuscate_fields = mocker.patch("src.utils.obfuscate_fields")
        obfuscate_fields.side_effect = Exception

        with pytest.raises(Exception):
            obfuscate_fields("data", "fields")

Updating the test to check an ObfuscationError is raised causes the test to fail:

    def test_raises_obfuscation_error_for_other_exceptions(self, mocker):
        obfuscate_fields = mocker.patch("src.utils.obfuscate_fields")
        obfuscate_fields.side_effect = Exception

        with pytest.raises(ObfuscationError):
            obfuscate_fields("data", "fields")
FAILED test/utils/test_obfuscate_fields.py::TestObfuscateFieldsErrorHandling::test_raises_obfuscation_error_for_other_exceptions - Exception

Proposed resolution

Instead of simulating an Exception in the unit test, obfuscate_fields can be passed an input, that will cause it to raise an Exception. Ideally, inputs that are known to raise an Exception should be handled individually, however, is we pass anException as an argument, this should always reproduce a generic Exception which has not been handled.

obfuscate_fields(Exception, ["name"])
CJohnston079 commented 4 months ago

Resolved in 142a789ce5f1150aa539a5c43adce53980386b49 with proposed resolution.