Advertisement
Guest User

Test Script for Docassemble Interview

a guest
Jan 12th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.04 KB | None | 0 0
  1. #! /usr/bin/env python
  2. import requests
  3. import json
  4. import sys
  5. import random
  6. import string
  7.  
  8. username = 'user@example.com'
  9. password = 'xxxsecretxxx'
  10. key = 'H3PLMKJKIVATLDPWHJH3AGWEJPFU5GRT'
  11. i = 'docassemble.demo:data/questions/questions.yml'
  12.  
  13. r = requests.get('http://localhost/api/secret', params={'key': key, 'username': username, 'password': password})
  14. if r.status_code != 200:
  15.     sys.exit(r.text)
  16. secret = json.loads(r.text)
  17.  
  18. r = requests.get('http://localhost/api/session/new', params={'key': key, 'i': i, 'secret': secret})
  19. if r.status_code != 200:
  20.     sys.exit(r.text)
  21. info = json.loads(r.text)
  22. session = info['session']
  23.  
  24. r = requests.get('http://localhost/api/session/question', params={'key': key, 'i': i, 'secret': secret, 'session': session})
  25. if r.status_code != 200:
  26.     sys.exit(r.text)
  27. info = json.loads(r.text)
  28.  
  29. print r.text
  30.  
  31. steps = 0
  32. while steps < 1000 and info['questionType'] != 'deadend':
  33.     variables = dict()
  34.     for field in info['fields']:
  35.         if field.get('datatype', None) in ('html', 'note'):
  36.             continue
  37.         elif 'variable_name' not in field:
  38.             if field.get('fieldtype', None) == 'multiple_choice' and 'choices' in field:
  39.                 variables[field['choices'][0]['variable_name']] = field['choices'][0]['value']
  40.             else:
  41.                 sys.exit("Field not recognized:\n" + repr(field))
  42.         elif 'datatype' not in field and 'fieldtype' not in field:
  43.             variables[field['variable_name']] = True
  44.         elif field.get('datatype', None) == 'object':
  45.             if not field.get('required', True):
  46.                 continue
  47.             else:
  48.                 sys.exit("Field not recognized:\n" + repr(field))
  49.         elif field.get('fieldtype', None) == 'multiple_choice' or 'choices' in field:
  50.             variables[field['variable_name']] = field['choices'][0]['value']
  51.         elif field.get('datatype', None) == 'boolean':
  52.             variables[field['variable_name']] = True if random.random() > 0.5 else False
  53.         elif field.get('datatype', None) == 'threestate':
  54.             variables[field['variable_name']] = True if random.random() > 0.66 else (False if random.random() > 0.5 else None)
  55.         elif field.get('datatype', None) in ('text', 'area'):
  56.             variables[field['variable_name']] = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(5))
  57.         elif field.get('datatype', None) == 'email':
  58.             variables[field['variable_name']] = ''.join(random.choice(string.ascii_lowercase) for _ in range(5)) + '@' + ''.join(random.choice(string.ascii_lowercase) for _ in range(5)) + random.choice(['.com', '.net', '.org'])
  59.         elif field.get('datatype', None) == 'currency':
  60.             variables[field['variable_name']] = "{0:.2f}".format(random.random() * 100.0)
  61.         elif field.get('datatype', None) == 'date':
  62.             variables[field['variable_name']] = "2015-04-15"
  63.         elif field.get('datatype', None) == 'range':
  64.             variables[field['variable_name']] = float(field.get('min', 1)) + int(random.random() * (float(field.get('max', 10)) - float(field.get('min', 1))))
  65.         #elif ...
  66.         #elif ...
  67.         #elif ...
  68.         else:
  69.             sys.exit("Field not recognized:\n" + repr(field))
  70.     if len(variables) == 0:
  71.         sys.exit("Fields not recognized:\n" + repr(info['fields']))
  72.     print "Setting variables:\n" + repr(variables)
  73.     data = {'key': key, 'i': i, 'secret': secret, 'session': session, 'variables': json.dumps(variables)}
  74.     if info.get('mandatory', False):
  75.         data['question_name'] = info['questionName']
  76.     r = requests.post('http://localhost/api/session', data=data)
  77.     if r.status_code != 200:
  78.         sys.exit(r.text)
  79.     print "Got question:\n" + r.text
  80.     try:
  81.         info = json.loads(r.text)
  82.         assert isinstance(info, dict)
  83.     except:
  84.         sys.exit(r.text)
  85.     steps += 1
  86.  
  87. r = requests.delete('http://localhost/api/session', params={'key': key, 'i': i, 'session': session})
  88. if r.status_code != 204:
  89.     sys.exit(r.text)
  90.    
  91. sys.exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement