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
|
# Scenario step implementations
IMPLEMENTS GIVEN a users.yaml with user (\S+), password (\S+), scopes (.+)
username = get_next_match()
password = get_next_match()
scopes_string = get_next_match()
scopes = [s.strip() for s in scopes_string.split(',')]
user = {
'salt': 'nacl',
'password': distixapi.encrypt_password('nacl', password),
'cleartext': password,
'scopes': scopes,
}
users = load_users()
users['users'][username] = user
save_users(users)
IMPLEMENTS GIVEN a running backend instance
backend = os.path.join(srcdir, 'distix-backend')
cliapp.runcmd(
['/usr/sbin/daemonize', '-c.', backend, 'pid', 'port', 'users.yaml'])
vars['pid'] = cat('pid').strip()
vars['port'] = cat('port').strip()
IMPLEMENTS FINALLY stop backend instance
import signal
print 'killing process', repr(vars['pid'])
os.kill(int(vars['pid']), signal.SIGTERM)
IMPLEMENTS WHEN user (\S+) makes request GET (\S+)
user = get_next_match()
path = get_next_match()
url = 'http://localhost:{}{}'.format(vars['port'], path)
print 'url:', repr(url)
users = load_users()
print repr(users)
password = users['users'][user]['cleartext']
import requests
r = requests.get(url, auth=(user, password))
vars['http-status'] = r.status_code
vars['http-body'] = r.text
IMPLEMENTS WHEN user (\S+) makes unauthenticated request GET (\S+)
user = get_next_match()
path = get_next_match()
url = 'http://localhost:{}{}'.format(vars['port'], path)
print 'url:', repr(url)
import requests
r = requests.get(url)
vars['http-status'] = r.status_code
vars['http-body'] = r.text
IMPLEMENTS THEN HTTP status code is (\d+)
wanted = int(os.environ['MATCH_1'])
print 'wanted:', repr(wanted)
print 'actual:', repr(vars['http-status'])
assert vars['http-status'] == wanted
IMPLEMENTS THEN result matches (.*)
import json
body = json.loads(vars['http-body'])
pattern = json.loads(os.environ['MATCH_1'])
assert type(body) == type(pattern)
for key in pattern.keys():
assert key in body
assert body[key] == pattern[key]
|