# Implementation of scenario steps ## Server aliases IMPLEMENTS GIVEN server is also known as (\S+) vars['ALIAS'] = get_next_match() ## HTTP requests IMPLEMENTS WHEN user fetches (\S+) address = os.environ['ADDRESS'] url = get_next_match() status, body = http_get(address, url) vars['http_status'] = status vars['http_body'] = body IMPLEMENTS THEN HTTP status is (\d+) expected = vars['http_status'] actual = int(get_next_match()) assertEqual(expected, actual) IMPLEMENTS THEN HTTP body matches "(.*)" body = vars['http_body'] sys.stdout.write('Body:\n{}'.format(repr(body))) pattern = get_next_match() m = re.search(pattern, body) assertNotEqual(m, None) ## Running commands and checking results IMPLEMENTS WHEN user runs Gitano (.*) address = os.environ['ADDRESS'] sshkey = os.environ['SSH_KEY'] cmd = get_next_match() argv = [ 'ssh', '-oIdentitiesOnly=yes', '-i', sshkey, 'git@' + address ] + cmd.split() exit, out, err = cliapp.runcmd_unchecked(argv) vars['argv'] = argv vars['exit'] = int(exit) vars['stdout'] = out vars['stderr'] = err IMPLEMENTS WHEN user runs ssh server (.*) address = os.environ['ADDRESS'] sshkey = os.environ['SSH_KEY'] cmd = get_next_match() argv = [ 'ssh', '-oIdentitiesOnly=yes', '-i', sshkey, address ] + cmd.split() exit, out, err = cliapp.runcmd_unchecked(argv) vars['argv'] = argv vars['exit'] = int(exit) vars['stdout'] = out vars['stderr'] = err IMPLEMENTS WHEN user clones the (\S+) repository over git:// address = os.environ['ADDRESS'] repo = get_next_match() url = 'git://{}/{}'.format(address, repo) argv = ['git', 'clone', url] exit, out, err = cliapp.runcmd_unchecked(argv, cwd=os.environ['DATADIR']) vars['argv'] = argv vars['exit'] = int(exit) vars['stdout'] = out vars['stderr'] = err IMPLEMENTS THEN exit code is (\d+) code = get_next_match() assertEqual(vars['exit'], int(code)) IMPLEMENTS THEN standard output matches "(.+)" pattern = get_next_match() stdout = vars['stdout'] m = re.search(pattern, stdout, re.M) print 'pattern:', repr(pattern) print 'argv:', repr(vars['argv']) print 'stdout:', repr(stdout) assertNotEqual(m, None) IMPLEMENTS THEN standard error matches "(.+)" pattern = get_next_match() stderr = vars['stderr'] m = re.search(pattern, stderr) print 'pattern:', repr(pattern) print 'argv:', repr(vars['argv']) print 'stderr:', repr(stderr) assertNotEqual(m, None) # Mail handling tests ## Generate a random number IMPLEMENTS GIVEN large random number (\S+) varname = get_next_match() mini = 2**32 maxi = 2**64 r = random.randint(mini, maxi) vars[varname] = str(r) ## Send mail over SMTP+TLS IMPLEMENTS WHEN someone sends mail to (\S+) with subject \$(\S+) to_addr = get_next_match() subject_key = get_next_match() subject = vars[subject_key] address = os.environ['ADDRESS'] from_addr = 'someone@example.com' msg = 'From: {}\nTo: {}\nSubject: {}\n\nThis is the body\n'.format( from_addr, to_addr, subject) server = smtplib.SMTP() server.connect(host=address, port=587) server.set_debuglevel(True) server.starttls() server.sendmail(from_addr, to_addr, msg) server.quit() ## Check for a mail in a mailbox IMPLEMENTS THEN mailbox of (\S+) has a mail with subject \$(\S+) user = get_next_match() subject_key = get_next_match() subject = vars[subject_key] print 'Wanted:', subject address = os.environ['ADDRESS'] pass_name = 'pieni.net/{}'.format(user) pass_home = os.environ['PASS_HOME'] password = get_password_with_pass(pass_home, pass_name) got_it = False def match(imapobj, msgid, msg): global got_it print 'Subject:', msg['Subject'] got_it = got_it or (msg['Subject'] == subject) iterate_mails_in_imap_mailbox(address, user, password, match, False) assertTrue(got_it) ## Delete mails IMPLEMENTS THEN mails for (\S+) with subject \$(\S+) get deleted user = get_next_match() subject_key = get_next_match() subject = vars[subject_key] print 'Wanted:', subject address = os.environ['ADDRESS'] pass_name = 'pieni.net/{}'.format(user) pass_home = os.environ['PASS_HOME'] password = get_password_with_pass(pass_home, pass_name) def delete_matching(imapobj, msgid, msg): if msg['Subject'] == subject: imapobj.store(msgid, '+FLAGS', '(\\Deleted)') iterate_mails_in_imap_mailbox( address, user, password, delete_matching, True) ## Run sequence of SMTP commands, check SMTP status IMPLEMENTS WHEN client SMTP authenticates as (\S+) user = get_next_match() vars['smtp_username'] = user IMPLEMENTS WHEN client sends (.*) command = get_next_match() smtp_commands = vars['smtp_commands'] if smtp_commands is None: smtp_commands = [] smtp_commands.append(command) vars['smtp_commands'] = smtp_commands IMPLEMENTS THEN SMTP status code is (\d+) wanted_status = int(get_next_match()) smtp_commands = vars['smtp_commands'] smtp_username = vars['smtp_username'] if smtp_username is not None: pass_home = os.environ['PASS_HOME'] pass_user = 'pieni.net/{}'.format(smtp_username) smtp_password = get_password_with_pass(pass_home, pass_user) address = os.environ['ADDRESS'] server = smtplib.SMTP() print 'connecting to', address server.connect(host=address, port=587) server.set_debuglevel(True) print 'starttls' server.starttls() if smtp_username is not None: print 'Authenticating as', smtp_username print server.login(smtp_username, smtp_password) for cmd in smtp_commands: print 'Command:', cmd actual_status, msg = server.docmd(cmd) server.quit() assertEqual(actual_status, wanted_status)