def test_init(self): init_dir = tempfile.mkdtemp() PasswordStore.init( '5C5833E3', path=os.path.join(init_dir, '.password-store') ) self.assertTrue( os.path.isdir(os.path.join(init_dir, '.password-store')) ) self.assertTrue( os.path.isfile( os.path.join(init_dir, '.password-store', '.gpg-id') ) ) self.assertEqual( open( os.path.join(init_dir, '.password-store', '.gpg-id'), 'r' ).read(), '5C5833E3\n' ) shutil.rmtree(init_dir)
def main(): store = PasswordStore() entries = store.get_passwords_list() with open('pass.csv', 'w', newline='') as csvfile: csvwriter = csv.writer(csvfile) for entry in entries: csvwriter.writerow(parse(entry, store))
def test_init_clone(self): origin_dir = tempfile.mkdtemp() destination_dir = tempfile.mkdtemp() subprocess.Popen( [ 'git', '--git-dir=%s' % os.path.join(origin_dir, '.git'), '--work-tree=%s' % origin_dir, 'init', origin_dir ], shell=False ).wait() open(os.path.join(origin_dir, 'test_git_init_clone.gpg'), 'a').close() subprocess.Popen( [ 'git', '--git-dir=%s' % os.path.join(origin_dir, '.git'), '--work-tree=%s' % origin_dir, 'add', 'test_git_init_clone.gpg', ] ).wait() subprocess.Popen( [ 'git', '--git-dir=%s' % os.path.join(origin_dir, '.git'), '--work-tree=%s' % origin_dir, 'commit', '-m', '"testcommit"', ] ).wait() # Init PasswordStore.init( path=destination_dir, clone_url=origin_dir, gpg_id='3CCC3A3A' ) # The key should be imported self.assertTrue( os.path.isfile( os.path.join(destination_dir, 'test_git_init_clone.gpg') ) ) # The gpg-id file should be created self.assertTrue( os.path.isfile( os.path.join(destination_dir, '.gpg-id') ) ) shutil.rmtree(origin_dir) shutil.rmtree(destination_dir)
def test_get_decrypted_password_only_password(self): store = PasswordStore(self.dir) password = '******' store.insert_password('hello.com', password) self.assertEqual( 'ELLO', store.get_decrypted_password('hello.com') )
def test_get_passwords_list(self): store = PasswordStore(self.dir) self.assertListEqual( sorted(store.get_passwords_list()), sorted([ 'test.com', 'linux.ca', 'passwordstore.org', 'Email/email.com', ]))
def test_get_passwords_list(self): store = PasswordStore(self.dir) self.assertListEqual( sorted(store.get_passwords_list()), sorted([ 'test.com', 'linux.ca', 'passwordstore.org', 'Email/email.com', ]) )
def test_constructor(self): # Construct on properly initialized directory store = PasswordStore(self.dir) self.assertEqual(store._get_gpg_id(self.dir), '5C5833E3') self.assertFalse(store.uses_git) self.assertEqual(self.dir, store.path) # Fail gracefully on missing .gpg-id gpg_id_path = os.path.join(self.dir, '.gpg-id') os.remove(gpg_id_path) self.assertRaises(Exception, PasswordStore, self.dir)
def test_encrypt_decrypt(self): self.assertFalse( os.path.isfile(os.path.join(self.dir, 'hello.com.gpg'))) store = PasswordStore(self.dir) password = '******' store.insert_password('hello.com', password) self.assertTrue(os.path.isfile(os.path.join(self.dir, 'hello.com.gpg'))) self.assertEqual(password, store.get_decrypted_password('hello.com'))
def test_generate_password(self): only_letters = PasswordStore.generate_password(digits=False, symbols=False) self.assertTrue(only_letters.isalpha()) alphanum = PasswordStore.generate_password(digits=True, symbols=False) self.assertTrue(alphanum.isalnum()) for char in alphanum: self.assertTrue(char not in string.punctuation) length_100 = PasswordStore.generate_password(length=100) self.assertEqual(len(length_100), 100)
def test_generate_password(self): only_letters = PasswordStore.generate_password( digits=False, symbols=False ) self.assertTrue(only_letters.isalpha()) alphanum = PasswordStore.generate_password(digits=True, symbols=False) self.assertTrue(alphanum.isalnum()) for char in alphanum: self.assertTrue(char not in string.punctuation) length_100 = PasswordStore.generate_password(length=100) self.assertEqual(len(length_100), 100)
def test_encrypt_decrypt(self): self.assertFalse( os.path.isfile(os.path.join(self.dir, 'hello.com.gpg')) ) store = PasswordStore(self.dir) password = '******' store.insert_password('hello.com', password) self.assertTrue( os.path.isfile(os.path.join(self.dir, 'hello.com.gpg')) ) self.assertEqual( password, store.get_decypted_password('hello.com') )
def generate(pass_name, pass_length, no_symbols): symbols = not no_symbols password = PasswordStore.generate_password(digits=True, symbols=symbols, length=pass_length) print(password)
def test_get_decrypted_password_deeply_nested(self): store = PasswordStore(self.dir) self.assertFalse(os.path.isdir(os.path.join(self.dir, 'A', 'B', 'C'))) store.insert_password('A/B/C/D/hello.com', 'Alice') store.insert_password('A/B/C/hello.com', 'Bob') self.assertEqual('Alice', store.get_decrypted_password('A/B/C/D/hello.com')) self.assertEqual('Bob', store.get_decrypted_password('A/B/C/hello.com')) self.assertTrue( os.path.isdir(os.path.join(self.dir, 'A', 'B', 'C', 'D')))
def generate(pass_name, pass_length, no_symbols): symbols = not no_symbols password = PasswordStore.generate_password( digits=True, symbols=symbols, length=pass_length ) print(password)
def test_generate_password_uses_correct_gpg_id(self): store = PasswordStore(self.dir) def get_gpg_ids_used(filename): gpg = subprocess.Popen( [GPG_BIN, '--list-packets', os.path.join(self.dir, filename)], shell=False, stdout=subprocess.PIPE) gpg.wait() pubkeys = [] for line in gpg.stdout.readlines(): line = line.decode() if line.startswith(':pubkey'): pubkeys.append(line.split()[-1]) return pubkeys store.generate_password('should_use_main_key') pubkeys = get_gpg_ids_used('should_use_main_key.gpg') self.assertTrue(len(pubkeys) == 1) self.assertEqual(pubkeys[0], '6C8110881C10BC07') store.generate_password('Email/should_use_secondary_key') pubkeys = get_gpg_ids_used( os.path.join('Email', 'should_use_secondary_key.gpg')) self.assertTrue(len(pubkeys) == 1) self.assertEqual(pubkeys[0], '4B52397C4C1C5D70')
def test_get_decypted_password_specific_entry(self): store = PasswordStore(self.dir) password = '******' store.insert_password('hello.com', password) # When there is no 'password:'******'ELLO', store.get_decypted_password('hello.com', entry=EntryType.password) ) store.insert_password('hello.com', 'sdfsdf\npassword: pwd') self.assertEqual( 'pwd', store.get_decypted_password('hello.com', entry=EntryType.password) ) store.insert_password('hello.com', 'sdf\npassword: pwd\nusername: bob') self.assertEqual( 'bob', store.get_decypted_password('hello.com', entry=EntryType.username) )
def test_generate_in_place(self): store = PasswordStore(self.dir) self.assertRaises(Exception, store.generate_password, 'nope.org', first_line_only=True) store.insert_password('nope.org', 'pw\nremains intact') store.generate_password('nope.org', length=3, first_line_only=True) new_content = store.get_decrypted_password('nope.org') new_password, _, remainder = new_content.partition('\n') self.assertNotEqual(new_password, 'pw') self.assertEqual(remainder, 'remains intact')
def main(ctx, password_store_dir, password_store_git, editor): # init does not need any of this. if ctx.invoked_subcommand == "init": return # Prepare the config file config = { 'password_store': PasswordStore(path=password_store_dir, git_dir=password_store_git), 'editor': editor } ctx.obj = config # By default, invoke ls if ctx.invoked_subcommand is None: ctx.invoke(ls)
def test_get_decrypted_password_deeply_nested(self): store = PasswordStore(self.dir) self.assertFalse( os.path.isdir(os.path.join(self.dir, 'A', 'B', 'C')) ) store.insert_password('A/B/C/D/hello.com', 'Alice') store.insert_password('A/B/C/hello.com', 'Bob') self.assertEqual( 'Alice', store.get_decrypted_password('A/B/C/D/hello.com') ) self.assertEqual( 'Bob', store.get_decrypted_password('A/B/C/hello.com') ) self.assertTrue( os.path.isdir(os.path.join(self.dir, 'A', 'B', 'C', 'D')) )
def test_get_decrypted_password_specific_entry(self): store = PasswordStore(self.dir) password = '******' store.insert_password('hello.com', password) # When there is no 'password:'******'ELLO', store.get_decrypted_password('hello.com', entry=EntryType.password)) store.insert_password('hello.com', 'sdfsdf\npassword: pwd') self.assertEqual( 'pwd', store.get_decrypted_password('hello.com', entry=EntryType.password)) store.insert_password( 'hello', 'sdf\npassword: pwd\nusername: bob\nhost: salut.fr') self.assertEqual( 'bob', store.get_decrypted_password('hello', entry=EntryType.username)) self.assertEqual( 'salut.fr', store.get_decrypted_password('hello', entry=EntryType.hostname))
def init(path, clone, gpg_id): PasswordStore.init(gpg_id, path, clone_url=clone) click.echo("Password store initialized for %s." % gpg_id)
def test_constructor(self): store = PasswordStore(self.dir) self.assertEqual(store.gpg_id, '5C5833E3') self.assertFalse(store.uses_git) self.assertEqual(self.dir, store.path)
def test_generate_password(self): store = PasswordStore(self.dir) store.generate_password('letters.net', digits=False, symbols=False) only_letters = store.get_decrypted_password('letters.net') self.assertTrue(only_letters.isalpha()) store.generate_password('alphanum.co.uk', digits=True, symbols=False) alphanum = store.get_decrypted_password('alphanum.co.uk') self.assertTrue(alphanum.isalnum()) for char in alphanum: self.assertTrue(char not in string.punctuation) store.generate_password('hundred.org', length=100) length_100 = store.get_decrypted_password('hundred.org') self.assertEqual(len(length_100), 100)
def test_get_decrypted_password_doesnt_exist(self): store = PasswordStore(self.dir) self.assertRaises(Exception, store.get_decrypted_password, 'nope.com')