-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.py
118 lines (102 loc) · 4.81 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import os
import re
import string
import pexpect
import pytest
TIMEOUT = 3
@pytest.fixture(autouse=True)
def setup(tmpdir):
pexpect.run('mypass lock')
os.environ['HOME'] = str(tmpdir.mkdir('home'))
yield
pexpect.run('mypass lock')
def run(cmd, events=[]):
child = pexpect.spawn(cmd, timeout=TIMEOUT)
for expected, response in events:
child.expect(r'^' + re.escape(expected))
if response is not None:
child.sendline(response)
child.expect(r'^\r\n')
child.wait()
assert child.read() == b''
child.close()
assert child.status == 0
def test_cli():
run('mypass add foo.example.com', [('New passphrase: ', 'masterpw'),
('Verify passphrase: ', 'masterpw'),
('Password: ', 'password1')])
run('mypass alias foo.example.com same-as-foo.example.com')
run('mypass add bar.example.com jane', [('Password: ', 'password2')])
run('mypass add bar.example.com john password3')
output, status = pexpect.runu('mypass new bar.example.com jack',
withexitstatus=True, timeout=TIMEOUT)
assert status == 0
assert output.endswith('\r\n')
passwd = output[:-2]
assert len(passwd) == 16
chars = set(passwd)
assert chars.intersection(string.ascii_lowercase)
assert chars.intersection(string.ascii_uppercase)
assert chars.intersection(string.digits)
assert chars.intersection(string.punctuation)
assert chars.issubset(string.ascii_letters +
string.digits +
string.punctuation)
run('mypass lock')
run('mypass list', [('Unlock database: ', 'masterpw'),
('bar.example.com', None),
('foo.example.com', None),
('same-as-foo.example.com', None)])
run('mypass get foo.example.com', [('password1', None)])
run('mypass get bar.example.com', [('jack ' + passwd, None),
('jane password2', None),
('john password3', None)])
run('mypass remove foo.example.com')
run('mypass remove bar.example.com jack')
run('mypass list', [('bar.example.com', None),
('same-as-foo.example.com', None)])
run('mypass get bar.example.com', [('jane password2', None),
('john password3', None)])
run('mypass get same-as-foo.example.com', [('password1', None)])
run('mypass rename bar.example.com --new-context=new.example.com')
run('mypass rename new.example.com jane --new-context=bar.example.com')
run('mypass rename new.example.com john --new-username=jeff')
run('mypass list', [('bar.example.com', None),
('new.example.com', None),
('same-as-foo.example.com', None)])
run('mypass get new.example.com', [('jeff password3', None)])
run('mypass get bar.example.com', [('password2', None)])
run('mypass changepw', [('New passphrase: ', 'masterpw2'),
('Verify passphrase: ', 'masterpw2')])
run('mypass lock')
run('mypass list', [('Unlock database: ', 'masterpw2'),
('bar.example.com', None),
('new.example.com', None),
('same-as-foo.example.com', None)])
def test_bash_completion():
run('mypass add example.com joe pw', [('New passphrase: ', 'masterpw'),
('Verify passphrase: ', 'masterpw')])
shell = pexpect.spawn('bash --norc',
env=dict(os.environ, PS1='$ '),
timeout=TIMEOUT)
shell.sendline('eval "$(register-python-argcomplete --no-defaults mypass)"')
shell.readline()
for input, completed in [('mypass g', 'et'),
('mypass get e', 'xample.com'),
('mypass ad', 'd'),
('mypass add e', 'xample.com'),
('mypass n', 'ew'),
('mypass new e', 'xample.com'),
('mypass rem', 'ove'),
('mypass remove e', 'xample.com'),
('mypass remove example.com j', 'oe'),
('mypass al', 'ias'),
('mypass alias e', 'xample.com'),
('mypass ren', 'ame'),
('mypass rename e', 'xample.com'),
('mypass rename example.com j', 'oe'),
('mypass rename --new-context=e', 'xample.com')]:
shell.send(input + '\t')
shell.expect(r'^\$ {} '.format(re.escape(input + completed)))
shell.sendintr()
shell.expect(r'^\^C\r\n')