def test_Sensitive(*, capsys): class Foo(Record): name: str phone_number: Sensitive[str] class Bar(Record): alias: str foo: Foo class Baz(Record): alias: str bar: List[Bar] x = Foo(name="Foo", phone_number="631-342-3412") assert Foo._options.has_sensitive_fields assert Foo._options.has_tagged_fields assert not Foo._options.has_secret_fields assert not Foo._options.has_personal_fields assert "phone_number" in Foo._options.sensitive_fields assert "foo" not in Foo._options.sensitive_fields # Model with related model that has sensitive fields, is also sensitive. assert Bar._options.has_sensitive_fields assert "foo" in Bar._options.sensitive_fields assert "alias" not in Bar._options.sensitive_fields assert Baz._options.has_sensitive_fields assert "bar" in Baz._options.sensitive_fields assert "alias" not in Baz._options.sensitive_fields assert x.name == "Foo" with pytest.raises(SecurityError): str(x.phone_number) assert x.phone_number.get_value() == "631-342-3412" with pytest.raises(SecurityError): f"Name={x.name} Phone={x.phone_number}" with pytest.raises(SecurityError): logger.critical("User foo error %s", x.phone_number) def exclaim(x: str) -> str: assert isinstance(x, _FrameLocal) return f"{x}!" with pytest.raises(SecurityError): exclaim(x.phone_number.get_value()) def upper(x: str) -> str: return x.upper() with pytest.raises(SecurityError): upper(x.phone_number.get_value()) with allow_protected_vars(): assert upper(x.phone_number.get_value()) == "631-342-3412"
def test_Sensitive(*, capsys): class Foo(Record): name: str phone_number: Sensitive[str] class Bar(Record): alias: str foo: Foo class Baz(Record): alias: str bar: List[Bar] x = Foo(name='Foo', phone_number='631-342-3412') assert Foo._options.has_sensitive_fields assert Foo._options.has_tagged_fields assert not Foo._options.has_secret_fields assert not Foo._options.has_personal_fields assert 'phone_number' in Foo._options.sensitive_fields assert 'foo' not in Foo._options.sensitive_fields # Model with related model that has sensitive fields, is also sensitive. assert Bar._options.has_sensitive_fields assert 'foo' in Bar._options.sensitive_fields assert 'alias' not in Bar._options.sensitive_fields assert Baz._options.has_sensitive_fields assert 'bar' in Baz._options.sensitive_fields assert 'alias' not in Baz._options.sensitive_fields assert x.name == 'Foo' with pytest.raises(SecurityError): str(x.phone_number) # this raises a securityerror also. # assert x.phone_number.get_value() == '631-342-3412' with pytest.raises(SecurityError): f'Name={x.name} Phone={x.phone_number}' # t his makes pytest think this test failed # logger.critical('User foo error %s', x.phone_number) # stderr_content = capsys.readouterr() # assert 'Logging error' in stderr_content.err # assert 'SecurityError' in stderr_content.err def exclaim(x: str) -> str: assert isinstance(x, _FrameLocal) return f'{x}!' with pytest.raises(SecurityError): exclaim(x.phone_number.get_value()) def upper(x: str) -> str: return x.upper() with pytest.raises(SecurityError): upper(x.phone_number.get_value()) with allow_protected_vars(): assert upper(x.phone_number.get_value()) == '631-342-3412'