def test_vault_get_yaml_fail1(open_exc, filepath, failing_open): """ Test function for EasyVault.get_yaml() that fails because open() on vault file raises an exception. """ password = TEST_VAULT_PASSWORD # Since saved_file() depends on a functioning open(), we need to patch # open() after using saved_file(). with saved_file(filepath): # The following ideally would be a simple counter. In Python 3, that # is possible with the 'nonlocal' statement, but since we support # Python 2, we have the counter in a dict to avoid the rebinding. local_dict = dict() local_dict['count'] = 0 builtins_open = open def new_open(*args, **kwargs): """Mock wrapper for open()""" if local_dict['count'] == failing_open: raise open_exc() local_dict['count'] += 1 return builtins_open(*args, **kwargs) with mock.patch(BUILTINS_OPEN, new_open): vault = EasyVault(filepath, password) with pytest.raises(EasyVaultFileError): # The code to be tested vault.get_yaml()
def test_vault_get_yaml_fail2(yaml_load_exc, filepath): """ Test function for EasyVault.get_yaml() that fails because yaml.safe_load() on vault file raises an exception. """ password = TEST_VAULT_PASSWORD # Since saved_file() depends on a functioning open(), we need to patch # open() after using saved_file(). with saved_file(filepath): with mock.patch.object(yaml, 'safe_load', side_effect=yaml_load_exc): vault = EasyVault(filepath, password) with pytest.raises(EasyVaultYamlError): # The code to be tested vault.get_yaml()
def test_vault_decrypt_fail(patched_exc, patched_function): """ Test function for EasyVault.decrypt() where a function raises an exception. """ filepath = TEST_VAULT_ENCRYPTED password = TEST_VAULT_PASSWORD # Since saved_file() depends on a functioning open(), we need to patch # open() after using saved_file(). with saved_file(filepath): with mock.patch(patched_function, side_effect=patched_exc): vault = EasyVault(filepath, password) with pytest.raises(EasyVaultFileError): # The code to be tested vault.decrypt()
def test_vault_is_encrypted(testcase, init_kwargs, exp_result): """ Test function for EasyVault.is_encrypted() """ vault = EasyVault(**init_kwargs) # The code to be tested act_result = vault.is_encrypted() # Ensure that exceptions raised in the remainder of this function # are not mistaken as expected exceptions assert testcase.exp_exc_types is None, \ "Expected exception not raised: {}". \ format(testcase.exp_exc_types) assert act_result == exp_result
def test_vault_decrypt(testcase, filepath, password, exp_filepath): """ Test function for EasyVault.decrypt() """ vault = EasyVault(filepath, password) with saved_file(filepath): # The code to be tested vault.decrypt() # Ensure that exceptions raised in the remainder of this function # are not mistaken as expected exceptions assert testcase.exp_exc_types is None, \ "Expected exception not raised: {}". \ format(testcase.exp_exc_types) if exp_filepath: assert_files_text_equal(filepath, exp_filepath)
def test_vault_encrypt(testcase, filepath, password): """ Test function for EasyVault.encrypt() """ vault = EasyVault(filepath, password) with saved_file(filepath): # The code to be tested vault.encrypt() # Ensure that exceptions raised in the remainder of this function # are not mistaken as expected exceptions assert testcase.exp_exc_types is None, \ "Expected exception not raised: {}". \ format(testcase.exp_exc_types) # Two encryptions of the same file content with the same password/key # result in different encrypted files, so we cannot verify the result # byte-wise. Instead, we just test whether the file has been encrypted. assert vault.is_encrypted()
def test_vault_get_yaml(testcase, filepath, password, exp_filepath): # pylint: disable=unused-argument """ Test function for EasyVault.get_yaml() """ vault = EasyVault(filepath, password) with saved_file(filepath) as saved_filepath: # The code to be tested act_yaml = vault.get_yaml() # Ensure that exceptions raised in the remainder of this function # are not mistaken as expected exceptions assert testcase.exp_exc_types is None, \ "Expected exception not raised: {}". \ format(testcase.exp_exc_types) # Check that this has not modified the original file assert_files_bytes_equal(filepath, saved_filepath) assert isinstance(act_yaml, (dict, list))
def test_vault_get_text(testcase, filepath, password, exp_filepath): """ Test function for EasyVault.get_text() """ vault = EasyVault(filepath, password) with saved_file(filepath) as saved_filepath: # The code to be tested act_text = vault.get_text() # Ensure that exceptions raised in the remainder of this function # are not mistaken as expected exceptions assert testcase.exp_exc_types is None, \ "Expected exception not raised: {}". \ format(testcase.exp_exc_types) # Check that this has not modified the original file assert_files_bytes_equal(filepath, saved_filepath) assert isinstance(act_text, six.string_types) assert_content_text_equal(act_text, exp_filepath)
def test_vault_init(testcase, init_args, init_kwargs, exp_attrs): """ Test function for EasyVault.__init__() and properties """ # The code to be tested act_obj = EasyVault(*init_args, **init_kwargs) # Ensure that exceptions raised in the remainder of this function # are not mistaken as expected exceptions assert testcase.exp_exc_types is None, \ "Expected exception not raised: {}". \ format(testcase.exp_exc_types) for attr_name in exp_attrs: exp_attr_value = exp_attrs[attr_name] assert hasattr(act_obj, attr_name), \ "Missing attribute {0!r} in returned EasyVault object". \ format(attr_name) act_attr_value = getattr(act_obj, attr_name) assert act_attr_value == exp_attr_value, \ "Unexpected value for attribute {0!r}: Expected {1!r}, got {2!r}".\ format(attr_name, exp_attr_value, act_attr_value)