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_modify_content(monkeypatch, capsys): gpg = get_GPG( '{"One": {"user":"******","password":"******","url":"url1","other":"other1"}}', 'secrets_tmp18', '12345') gpg2 = get_GPG( '{"One": {"user":"******","password":"******","url":"url1","other":"other1"}}', 'secrets_tmp19', '12345') aux = ['One', 'user2', '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.modify_content() out, err = capsys.readouterr() json1 = json.loads(str(gpg.decrypt_content())) json2 = json.loads(str(gpg2.decrypt_content())) assert json1['One']['user'] == json2['One']['user'] assert out == 'Leave all elements without value for delete the entry\nDone! Identifier One has been modified\n' remove_files(['secrets_tmp18', 'secrets_tmp19'])
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_add_content_no_secrets(monkeypatch): gpg = GPGTools(key='12345') gpg2 = get_GPG( '{"One": {"user":"******","password":"******","url":"url1","other":"other1"}}', 'secrets_tmp17', '12345') aux = ['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.add_content() json_gpg = json.loads(str(gpg.decrypt_content())) json_gpg2 = json.loads(str(gpg2.decrypt_content())) assert json_gpg == json_gpg2 remove_files(['secrets', 'secrets_tmp17'])
def test_decrypt_content_copy_clipboard(monkeypatch, capsys): gpg = get_GPG( '{"One": {"user":"******","password":"******","url":"url1","other":"other1"}}', 'secrets_tmp28', '12345') aux = ['One', 'N', 'password'] 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.print_decrypt_content() if platform.system() == 'Darwin': clip_out = pyperclip.paste() assert clip_out == 'pass1' else: out, err = capsys.readouterr() assert out == 'Only Darwin platforms\n' remove_files(['secrets_tmp28'])
def test_show_keys(capsys): gpg = get_GPG('{"One": 1, "Two": 2}', 'secrets_tmp21', '12345') icmd = InteractiveCMD(gpg) icmd.show_keys() out, err = capsys.readouterr() assert out == 'One\nTwo\n' remove_files(['secrets_tmp21'])
def test_update_keys(): get_files('secrets_tmp22', 'secrets_tmp23') gpg = GPGTools(file='secrets_tmp22', key='12345') icmd = InteractiveCMD(gpg) icmd.update_keys() file1 = open('secrets_tmp22', 'r') file2 = open('secrets_tmp23', 'r') assert file1.read() != file2.read() remove_files(['secrets_tmp22', 'secrets_tmp23'])
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_id_or_list(monkeypatch, capsys): gpg = get_GPG( '{"One": {"user":"******","password":"******","url":"url1","other":"other1"}}', 'secrets_tmp14', '12345') aux = ['list', '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.id_or_list() out, err = capsys.readouterr() assert out == 'One\n' remove_files(['secrets_tmp14'])
def test_decrypt_content_fail_id(monkeypatch, capsys): gpg = get_GPG( '{"One": {"user":"******","password":"******","url":"url1","other":"other1"}}', 'secrets_tmp27', '12345') aux = ['Two', 'One', 'Y', 'N'] 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.print_decrypt_content() out, err = capsys.readouterr() assert out == 'user: user1\npassword: pass1\nurl: url1\nother: other1\n' remove_files(['secrets_tmp27'])
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 test_no_update_keys(): ''' This test ensure that the codification works properly in the future this could be moved to the test_gpg_tools ''' get_files('secrets_tmp36', 'secrets_tmp37') gpg = GPGTools(file='secrets_tmp36', key='12345') icmd = InteractiveCMD(gpg) file1 = open('secrets_tmp36', 'r') file2 = open('secrets_tmp37', 'r') assert file1.read() == file2.read() remove_files(['secrets_tmp36', 'secrets_tmp37'])
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()
def test_exit(): gpg = GPGTools() icmd = InteractiveCMD(gpg) with pytest.raises(SystemExit): icmd.exit()