示例#1
0
    def test_decrypt(self):
        provider = StubProvider()
        kf = KeyFile('asdf.txt')
        kf._KeyFile__key = b'x' * 32
        kf._get_provider = MagicMock(return_value=provider)

        text = kf.decrypt(SecureValue('test', b'hello'))
        provider.decrypt.assert_called_once_with(b'hello')
        kf._get_provider.assert_called_once_with('test')
        assert text == b'plaintext'
示例#2
0
    def test_encrypt(self):
        provider = StubProvider()
        kf = KeyFile('asdf.txt')
        kf._KeyFile__key = b'x' * 32
        kf._get_provider = MagicMock(return_value=provider)

        secret = kf.encrypt('hello', 'test')
        provider.encrypt.assert_called_once_with(b'hello')
        kf._get_provider.assert_called_once_with('test')
        assert secret == SecureValue('test', b'ciphertext')
示例#3
0
    def test_to_python_dict_invalid_ciphertext(self):
        cfg = StubConfig()
        field = SecureField(name='asdf')
        cfg._keyfile.decrypt.side_effect = EncryptionError()
        cfg._keyfile.__exit__.return_value = False

        with pytest.raises(ValueError):
            field.to_python(cfg, {'method': 'test', 'ciphertext': 'aGVsbG8='})

        cfg._keyfile.decrypt.assert_called_once_with(
            SecureValue('test', b'hello'))
示例#4
0
 def __init__(self):
     StubKeyFile.__enter__ = MagicMock(return_value=self)
     StubKeyFile.__exit__ = MagicMock()
     self.encrypt = MagicMock(
         return_value=SecureValue('test', b'ciphertext'))
     self.decrypt = MagicMock(return_value=b'plaintext')