def test_interactive_menu_with_file_add_content(monkeypatch): gpg = get_GPG( '{"One": {"user":"******","password":"******","url":"url1","other":"other1"}}', 'secrets_tmp29', '12345') aux = ['1', 'Two', 'user2', 'pass2', 'url2', 'other2'] def mock_raw_input_user(*args, **kwargs): a = aux[0] del aux[0] return a monkeypatch.setattr(__builtin__, 'raw_input', mock_raw_input_user) icmd = InteractiveCMD(gpg) icmd.interactive_menu() json_ex = json.loads( '{"One": {"user":"******","password":"******","url":"url1","other":"other1"},"Two": {"user":"******","password":"******","url":"url2","other":"other2"}}' ) json_gpg = json.loads(str(gpg.decrypt_content())) for i in json_gpg["Two"]: assert json_gpg["Two"][i] == json_ex["Two"][i] for i in json_gpg["One"]: assert json_gpg["One"][i] == json_ex["One"][i] remove_files(['secrets_tmp29'])
def test_interactive_menu_with_file_modify_content_delete(monkeypatch, capsys): gpg = get_GPG( '{"One": {"user":"******","password":"******","url":"url1","other":"other1"},"Two": {"user":"******","password":"******","url":"url2","other":"other2"}}', 'secrets_tmp31', '12345') aux = ['2', 'One', '', '', '', ''] def mock_raw_input_user(*args, **kwargs): a = aux[0] del aux[0] return a monkeypatch.setattr(__builtin__, 'raw_input', mock_raw_input_user) icmd = InteractiveCMD(gpg) icmd.interactive_menu() out, err = capsys.readouterr() json_ex = json.loads( '{"Two": {"user":"******","password":"******","url":"url2","other":"other2"}}' ) json_gpg = json.loads(str(gpg.decrypt_content())) for i in json_gpg["Two"]: assert json_gpg["Two"][i] == json_ex["Two"][i] with pytest.raises(KeyError): json_gpg["One"] assert out == 'Leave all elements without value for delete the entry\nDone! Identifier One has been deleted\n' remove_files(['secrets_tmp31'])
def test_interactive_menu_no_file_exit(monkeypatch): gpg = GPGTools(key='12345') aux = ['2'] def mock_raw_input_user(*args, **kwargs): a = aux[0] del aux[0] return a monkeypatch.setattr(__builtin__, 'raw_input', mock_raw_input_user) icmd = InteractiveCMD(gpg) with pytest.raises(SystemExit): icmd.interactive_menu()
def test_interactive_menu_no_file_exit(monkeypatch): gpg = get_GPG( '{"One": {"user":"******","password":"******","url":"url1","other":"other1"},"Two": {"user":"******","password":"******","url":"url2","other":"other2"}}', 'secrets_tmp38', '12345') aux = ['6'] def mock_raw_input_user(*args, **kwargs): a = aux[0] del aux[0] return a monkeypatch.setattr(__builtin__, 'raw_input', mock_raw_input_user) icmd = InteractiveCMD(gpg) with pytest.raises(SystemExit): icmd.interactive_menu() remove_files(['secrets_tmp38'])
def test_interactive_menu_with_file_print_decrypt_content(monkeypatch, capsys): gpg = get_GPG( '{"One": {"user":"******","password":"******","url":"url1","other":"other1"},"Two": {"user":"******","password":"******","url":"url2","other":"other2"}}', 'secrets_tmp33', '12345') aux = ['4'] def mock_raw_input_user(*args, **kwargs): a = aux[0] del aux[0] return a monkeypatch.setattr(__builtin__, 'raw_input', mock_raw_input_user) icmd = InteractiveCMD(gpg) icmd.interactive_menu() out, err = capsys.readouterr() assert out == 'One\nTwo\n' remove_files(['secrets_tmp33'])
def test_update_keys(monkeypatch): get_files('secrets_tmp34', 'secrets_tmp35') gpg = GPGTools(file='secrets_tmp34', key='12345') aux = ['5'] def mock_raw_input_user(*args, **kwargs): a = aux[0] del aux[0] return a monkeypatch.setattr(__builtin__, 'raw_input', mock_raw_input_user) icmd = InteractiveCMD(gpg) icmd.interactive_menu() file1 = open('secrets_tmp34', 'r') file2 = open('secrets_tmp35', 'r') assert file1.read() != file2.read() remove_files(['secrets_tmp34', 'secrets_tmp35'])
def test_interactive_menu_no_file_add_content(monkeypatch): gpg = GPGTools(key='12345') aux = ['1', 'One', 'user1', 'pass1', 'url1', 'other1'] def mock_raw_input_user(*args, **kwargs): a = aux[0] del aux[0] return a monkeypatch.setattr(__builtin__, 'raw_input', mock_raw_input_user) icmd = InteractiveCMD(gpg) icmd.interactive_menu() json_ex = json.loads( '{"One": {"user":"******","password":"******","url":"url1","other":"other1"}}' ) json_gpg = json.loads(str(gpg.decrypt_content())) for i in json_gpg["One"]: assert json_gpg["One"][i] == json_ex["One"][i] remove_files(['secrets'])
def main(argv): gpg = GPGTools(file=FILE) icmd = InteractiveCMD(gpg) cmdc = CommandControler(gpg) if os.path.isfile(FILE): parser = argparse.ArgumentParser( description='Manager for sensible information under PGP') parser.add_argument( '-i', '--interactive', help='display the interactive menu for pwd-manager', action='store_true') parser.add_argument('-l', '--list', help='list all the stored identifiers', action='store_true') parser.add_argument( '-u', '--user', metavar='identifier', help='return the username for the given identifier') parser.add_argument( '-p', '--password', metavar='identifier', help='return the password for the given identifier') parser.add_argument('-ur', '--url', metavar='identifier', help='return the URL for the given identifier') parser.add_argument( '-o', '--other', metavar='identifier', help='return the other for the given identifier') parser.add_argument( '-a', '--all', metavar='identifier', help='display all values for the given identifier') parser.add_argument( '-ak', '--addkey', nargs=2, metavar=('identifier', '{"user":"******", ...}'), help= 'add element to secrets, second argument must be a valid json string. Allowed keys user, password, url and other' ) parser.add_argument( '-mk', '--modkey', nargs=2, metavar=('identifier', '{"user":"******", ...}'), help= 'modify element to secrets, second argument must be a valid json string. Allowed keys user, password, url and other' ) parser.add_argument('-mi', '--modid', nargs=2, metavar=('old_identifier', 'new_identifier'), help='modify id from an element') parser.add_argument( '-d', '--delete', nargs=1, metavar='identifier', help= 'modify element to secrets, second argument must be a valid json string' ) args = parser.parse_args() if args.interactive: icmd.interactive_menu() elif args.list: cmdc.show_keys() elif args.user: cmdc.get_key_value(args.user, 'user') elif args.password: cmdc.get_key_value(args.password, 'pass') elif args.url: cmdc.get_key_value(args.url, 'url') elif args.other: cmdc.get_key_value(args.other, 'other') elif args.all: cmdc.get_key_value(args.all, 'all') elif args.addkey: cmdc.add_content_id_json(args.addkey[0], str(args.addkey[1:][0])) elif args.modkey: cmdc.modify_content(args.modkey[0], str(args.modkey[1:][0])) elif args.modid: cmdc.modify_id(args.modid[0], args.modid[1]) elif args.delete: cmdc.del_id(args.delete[0]) else: parser.print_help() else: parser = argparse.ArgumentParser( description= '''Manager for sensible information under PGP. Use -i/--interactive or -ak/--addkey to introduce your first key and for more option will be displayed.''' ) parser.add_argument( '-i', '--interactive', help='display the interactive menu for pwd-manager', action='store_true') parser.add_argument( '-ak', '--addkey', nargs=2, metavar=('identifier', '{"user":"******", ...}'), help= 'add element to secrets, second argument must be a valid json string. Allowed keys user, password, url and other' ) args = parser.parse_args() if args.interactive: icmd.interactive_menu() elif args.addkey: cmdc.add_content_id_json(args.addkey[0], str(args.addkey[1:][0])) else: parser.print_help()