def test_rename_success(self): """Test a successful rename (then rename it back)""" ret = True try: with patch_args( subparser_name="rename", identity="r1", nopassphrase="true", all=True, pwname="test", rename="retest", overwrite="true", ): with mock.patch.object(builtins, "input", lambda _: "y"): Cli().run() with patch_args( subparser_name="rename", identity="r1", nopassphrase="true", all=True, pwname="retest", rename="test", overwrite="true", ): with mock.patch.object(builtins, "input", lambda _: "y"): Cli().run() except DecryptionError: ret = False self.assertTrue(ret)
def test_info_no_pass(self): """Test what happens when pwname is not supplied""" with patch_args( subparser_name="info", identity="r1", nopassphrase="true", pwname=None ): with self.assertRaises(CliArgumentError) as context: Cli().run() self.assertEqual(context.exception.msg, ERROR_MSGS["pwname"])
def test_list_one(self): """test list functionality for one password""" with patch_args(subparser_name="list", identity="r1", nopassphrase="true"): out = "\n".join(Cli().run()) output = yaml.safe_load(out) self.assertDictEqual(output, self.password_list_1)
def test_recipient_not_in_database(self): """test bad recipient functionality""" with self.assertRaises(CliArgumentError) as context: with patch_args(subparser_name="list", identity="bleh", nopassphrase="true"): Cli().run() self.assertEqual(context.exception.msg, ERROR_MSGS["rep"])
def test_info(self): """Test what info shows on a password""" with patch_args( subparser_name="info", identity="r1", nopassphrase="true", pwname="test" ): out = "\n".join(Cli().run()) output = yaml.safe_load(out) del output["Earliest distribute timestamp"] self.assertDictEqual(output, self.check_dict)
def test_create_no_pass(self): """Test what happens with pwname is not supplied""" with self.assertRaises(CliArgumentError) as context: with patch_args(subparser_name="create", identity="r1", nopassphrase="true", pwname=None): with mock.patch.object(builtins, "input", lambda _: "y"): with mock.patch.object(getpass, "getpass", lambda _: "y"): Cli().run() self.assertEqual(context.exception.msg, ERROR_MSGS["pwname"])
def test_recipient_not_in_database(self): """Test what happens with a recipient is not within the appropriate directory""" with self.assertRaises(CliArgumentError) as context: with patch_args( subparser_name="distribute", identity="bleh", nopassphrase="true", pwname="test", ): Cli().run() self.assertEqual(context.exception.msg, ERROR_MSGS["rep"])
def test_distribute_cli_error(self): """Test what happens with pwname is not supplied""" with self.assertRaises(CliArgumentError) as context: with patch_args( subparser_name="distribute", identity="r1", nopassphrase="true", pwname=None, ): Cli().run() self.assertEqual(context.exception.msg, ERROR_MSGS["pwname"])
def test_rename_cli_error(self): """Test what happens when pwname is not supplied""" with self.assertRaises(CliArgumentError) as context: with patch_args( subparser_name="rename", identity="r1", nopassphrase="true", all=True, rename="test", overwrite="true", ): Cli().run() self.assertEqual(context.exception.msg, ERROR_MSGS["pwname"])
def test_show_nopass_error(self): """Test what happens when neither pwname or the all flag are supplied""" ret = False try: with patch_args(subparser_name="show", identity="r1", nopassphrase="true", all=None): "".join(Cli().run()) except KeyError as error: if str(error) == "'pwname'": ret = str(error) self.assertEqual(ret, "'pwname'")
def test_showall_decryption(self): """Test showing all passwords""" ret = True try: with patch_args( subparser_name="show", identity="r1", nopassphrase="true", all=True, pwname="*test*", ): Cli().run() except DecryptionError: ret = False self.assertTrue(ret)
def test_decryption(self): """Test successful decryption""" ret = True try: with patch_args( subparser_name="show", identity="r1", nopassphrase="true", all=None, pwname="test", ): Cli().run() except DecryptionError: ret = False self.assertTrue(ret)
def test_distribute_success(self): """Test a successful distribute""" ret = True try: with patch_args( subparser_name="distribute", identity="r1", nopassphrase="true", pwname="test", ): with mock.patch.object(builtins, "input", lambda _: "y"): Cli().run() except DecryptionError: ret = False self.assertTrue(ret)
def test_modify_success(self): """Test modifying the metadata of a password""" ret = True try: with patch_args( subparser_name="modify", identity="r3", nopassphrase="true", pwname="gentest", ): with mock.patch.object(builtins, "input", lambda _: "y"): Cli().run() except DecryptionError: ret = False self.assertTrue(ret)
def test_generate_success(self): """Test a successful generation of a password""" ret = True try: with patch_args( subparser_name="generate", identity="r3", nopassphrase="true", pwname="gentest", ): with mock.patch.object(builtins, "input", lambda _: "y"): with mock.patch.object(getpass, "getpass", lambda _: "y"): Cli().run() except DecryptionError: ret = False self.assertTrue(ret)
def test_listrecipients(self): """Test a listing of our recipients""" self.maxDiff = None with patch_args( subparser_name="listrecipients", identity="r1", nopassphrase="true", filter="r3", ): out = "\n".join(Cli().run()).replace("\t", " ") output = yaml.safe_load(out) # we remove these things because the actual values depend on creation # moreover, some of the outputs on different operating systems appear # to utilize different delimiters. del output["r3"]["certs"]["enddate"] del output["r3"]["certs"]["subjecthash"] del output["r3"]["certs"]["issuerhash"] del output["r3"]["certs"]["fingerprint"] del output["r3"]["certs"]["subject"] del output["r3"]["certs"]["issuer"] self.assertDictEqual(output, self.out_dict)
from argparse import ArgumentParser from traceback import format_exception_only from libpkpass.errors import PKPassError from libpkpass.commands.cli import Cli if __name__ == "__main__": PARSER = ArgumentParser(add_help=False) # This debug flag exists in both this parser and the main parser # of Cli PARSER.add_argument("--debug", action="store_true") HIGH_LEVEL_ARGS, ARGS = PARSER.parse_known_args() # allow '--debug' to be placed at end of command and not interrupt the subparsers if "--debug" in argv: argv.remove("--debug") try: for mesg in Cli().run(): if mesg: print(mesg) except PKPassError as error: print(f"\n\n{str(type(error).__name__)}: {error.msg}") except KeyboardInterrupt: print("\nExiting") # This is so that users don't see tracebacks, an error will still print out # so that we can investigate # Comment this out for debugging except Exception as err: # pylint: disable=broad-except if HIGH_LEVEL_ARGS.debug: raise err print( f"Generic exception caught: \n\t{format_exception_only(type(err), err)[0]}" )
#!/usr/bin/env python """This Module handles the CLI and any error that comes from it""" from __future__ import print_function from libpkpass.errors import * from libpkpass.commands.cli import Cli try: Cli() except PKPassError as error: print("\n\n%s: %s" % (str(type(error).__name__), error.msg)) except KeyboardInterrupt: print("\nExiting")