# 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', datadir, backend, '--pid-file', 'pid', '--port-file', 'port', '--log', 'distix-backend.log', '--users-file', '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]