Skip to content

Commit

Permalink
Test the edit command and make it work on Python3.
Browse files Browse the repository at this point in the history
  • Loading branch information
prabi committed May 6, 2019
1 parent 902c679 commit aa74cec
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pypass/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,14 @@ def edit(config, path):
if path in config['password_store'].get_passwords_list():
old_password = config['password_store'].get_decrypted_password(path)
with tempfile.NamedTemporaryFile() as temp_file:
temp_file.write(old_password)
temp_file.write(old_password.encode())
temp_file.flush()

subprocess.call([config['editor'], temp_file.name])
temp_file.seek(0)

config['password_store'].insert_password(
path, temp_file.file.read()
path, temp_file.file.read().decode()
)
click.echo("%s was updated." % path)

Expand Down
39 changes: 39 additions & 0 deletions pypass/tests/mock_editor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env python

#
# Copyright (C) 2019 Peter Rabi <peter.rabi@gmail.com>
#
# This file is part of python-pass.
#
# python-pass is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# python-pass is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with python-pass. If not, see <http://www.gnu.org/licenses/>.
#

'''mock_editor.py is an editor simulator.
One command line argument is expected: a file path.
Upon invocation the file's content gets replaced with the plain text 'edited'.
'''

import sys

if __name__ == '__main__':
if len(sys.argv) != 2:
sys.exit('ERR: Unexpected number of arguments.\n\n{}'.format(__doc__))

file_path = sys.argv[1]
try:
with open(file_path, 'w') as f:
f.write('edited')
except (IOError, OSError) as e:
sys.exit('Couldn\'t edit {}\n{}'.format(file_path, e))
10 changes: 10 additions & 0 deletions pypass/tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ def test_show_clip(self):
xclip.wait()
self.assertEqual(xclip.stdout.read().decode('utf8'), 'clipme999')

def test_edit(self):
store = PasswordStore(self.dir)
store.insert_password('test.com', 'editme')

mock_editor = os.path.join(os.path.dirname(__file__), 'mock_editor.py')
self.run_cli(['--EDITOR', mock_editor, 'edit', 'test.com'])

edited_content = store.get_decrypted_password('test.com')
self.assertEqual(edited_content, 'edited')

def test_edit_not_exist(self):
edit_result = self.run_cli(
['edit', 'woijewoifj.ccc']
Expand Down

0 comments on commit aa74cec

Please sign in to comment.