Exemplo n.º 1
0
 def test_to_python_dict_invalid_ciphertext_base64(self):
     field = SecureField(name='asdf')
     with pytest.raises(ValueError):
         field.to_python(StubConfig(), {
             'method': 'xor',
             'ciphertext': 'hello'
         })
Exemplo n.º 2
0
    def test_to_python_dict_invalid_ciphertext_int(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': 10})
Exemplo n.º 3
0
    def test_to_python_dict_valid(self):
        cfg = StubConfig()
        field = SecureField(name='asdf')

        assert field.to_python(cfg, {
            'method': 'test',
            'ciphertext': 'aGVsbG8='
        }) == 'plaintext'
Exemplo n.º 4
0
 def test_to_basic(self):
     cfg = StubConfig()
     field = SecureField(method='test')
     assert field.to_basic(cfg, 'hello') == {
         'method': 'test',
         'ciphertext': base64.b64encode(b'ciphertext').decode()
     }
     cfg._keyfile.__enter__.assert_called_once_with()
     cfg._keyfile.encrypt.assert_called_once_with('hello', method='test')
     cfg._keyfile.__exit__.assert_called_once_with(None, None, None)
Exemplo n.º 5
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'))
Exemplo n.º 6
0
 def test_to_python_str(self):
     field = SecureField()
     assert field.to_python(StubConfig(), 'hello') == 'hello'
Exemplo n.º 7
0
 def test_to_python_empty_string(self):
     field = SecureField()
     assert field.to_python(None, "") == ""
Exemplo n.º 8
0
 def test_to_python_none(self):
     field = SecureField()
     assert field.to_python(StubConfig(), None) is None
Exemplo n.º 9
0
 def test_to_basic_empty_string(self):
     field = SecureField()
     assert field.to_basic(None, "") is None
Exemplo n.º 10
0
 def test_to_tree_sensitive_mask_empty(self):
     schema = Schema()
     schema.v = SecureField(method='xor', default='asdf')
     config = schema()
     assert config.to_tree(sensitive_mask='') == {'v': ''}
Exemplo n.º 11
0
 def test_to_python_invalid_type(self):
     field = SecureField()
     with pytest.raises(ValueError):
         field.to_python(StubConfig(), 100)
Exemplo n.º 12
0
 def test_to_python_dict_invalid_method(self):
     field = SecureField(name='asdf')
     with pytest.raises(ValueError):
         field.to_python(None, {'method': None, 'ciphertext': b'hello'})
Exemplo n.º 13
0
 def test_to_python_str(self):
     field = SecureField()
     assert field.to_python(None, 'hello') == 'hello'
Exemplo n.º 14
0
 def test_to_python_none(self):
     field = SecureField()
     assert field.to_python(None, None) is None
Exemplo n.º 15
0
 def test_to_basic_none(self):
     field = SecureField()
     assert field.to_basic(None, None) is None
Exemplo n.º 16
0
 def test_to_basic_none(self):
     field = SecureField()
     assert field.to_basic(StubConfig(), None) is None
Exemplo n.º 17
0
 def test_to_tree_empty_mask_secure(self):
     schema = Schema()
     schema.v = SecureField(method='xor', default=None)
     config = schema()
     assert config.to_tree(sensitive_mask='*') == {'v': None}