Scott Baker | 3495c5a | 2015-02-18 09:56:21 -0800 | [diff] [blame] | 1 | """ |
| 2 | UserTest - tests whether a user is able to fetch his own user record, |
| 3 | and modify fields. |
| 4 | |
| 5 | All users should be able to set their phone number. |
| 6 | Only admins should be able to set their is_admin bit |
| 7 | """ |
| 8 | |
Scott Baker | 33ad9ad | 2014-10-07 00:10:41 -0700 | [diff] [blame] | 9 | import json |
| 10 | import os |
| 11 | import requests |
| 12 | import sys |
Scott Baker | 3495c5a | 2015-02-18 09:56:21 -0800 | [diff] [blame] | 13 | from urllib import urlencode |
Scott Baker | 33ad9ad | 2014-10-07 00:10:41 -0700 | [diff] [blame] | 14 | |
| 15 | from operator import itemgetter, attrgetter |
| 16 | |
Scott Baker | 3495c5a | 2015-02-18 09:56:21 -0800 | [diff] [blame] | 17 | if (len(sys.argv)!=6): |
| 18 | print "syntax: usertest <hostname> <username> <password> <admin_username> <admin_password>" |
| 19 | sys.exit(-1) |
Scott Baker | 33ad9ad | 2014-10-07 00:10:41 -0700 | [diff] [blame] | 20 | |
Scott Baker | 3495c5a | 2015-02-18 09:56:21 -0800 | [diff] [blame] | 21 | hostname = sys.argv[1] |
| 22 | username = sys.argv[2] |
| 23 | password = sys.argv[3] |
Scott Baker | 33ad9ad | 2014-10-07 00:10:41 -0700 | [diff] [blame] | 24 | |
| 25 | opencloud_auth=(username, password) |
Scott Baker | 3495c5a | 2015-02-18 09:56:21 -0800 | [diff] [blame] | 26 | admin_auth=(sys.argv[4], sys.argv[5]) |
Scott Baker | 33ad9ad | 2014-10-07 00:10:41 -0700 | [diff] [blame] | 27 | |
Scott Baker | 3495c5a | 2015-02-18 09:56:21 -0800 | [diff] [blame] | 28 | REST_API="http://%s:8000/xos/" % hostname |
| 29 | USERS_API = REST_API + "users/" |
Scott Baker | 33ad9ad | 2014-10-07 00:10:41 -0700 | [diff] [blame] | 30 | |
Scott Baker | 3495c5a | 2015-02-18 09:56:21 -0800 | [diff] [blame] | 31 | print "fetching user record for %s:" % username |
| 32 | r = requests.get(USERS_API + "?" + urlencode({"email": username}), auth=opencloud_auth) |
Scott Baker | 33ad9ad | 2014-10-07 00:10:41 -0700 | [diff] [blame] | 33 | for user in r.json(): |
| 34 | print " ", user["email"] |
| 35 | |
| 36 | myself = r.json()[0] |
| 37 | |
| 38 | if myself["phone"] == "123": |
| 39 | myself["phone"] = "456" |
| 40 | else: |
| 41 | myself["phone"] = "123" |
| 42 | |
| 43 | r = requests.put(USERS_API + str(myself["id"]) +"/", data=myself, auth=opencloud_auth) |
| 44 | if r.status_code == 200: |
| 45 | print "I updated my phone to", myself["phone"] |
| 46 | else: |
| 47 | print "I failed to update my phone" |
| 48 | |
| 49 | if myself["is_admin"] == True: |
| 50 | myself["is_admin"] = False |
| 51 | else: |
| 52 | myself["is_admin"] = True |
| 53 | |
| 54 | r = requests.put(USERS_API + str(myself["id"]) +"/", data=myself, auth=opencloud_auth) |
| 55 | if r.status_code == 200: |
| 56 | print "I updated my is_admin to", myself["is_admin"] |
| 57 | else: |
| 58 | print "I failed to update my is_admin" |
| 59 | |
| 60 | r = requests.get(USERS_API + "?email=jhh@cs.arizona.edu", auth=opencloud_auth) |
| 61 | if len(r.json())>0: |
| 62 | print "I was able to read jhh@cs.arizona.edu" |
| 63 | else: |
| 64 | print "I was not able to read jhh@cs.arizona.edu" |
| 65 | |
| 66 | # get john's record using admin, so we can try to update it |
| 67 | r = requests.get(USERS_API + "?email=jhh@cs.arizona.edu", auth=admin_auth) |
| 68 | if len(r.json())>0: |
| 69 | print "Admin was able to read jhh@cs.arizona.edu" |
| 70 | jhh = r.json()[0] |
| 71 | else: |
Scott Baker | 3495c5a | 2015-02-18 09:56:21 -0800 | [diff] [blame] | 72 | print "Admin was not able to read jhh@cs.arizona.edu" |
Scott Baker | 33ad9ad | 2014-10-07 00:10:41 -0700 | [diff] [blame] | 73 | jhh = None |
| 74 | |
| 75 | if jhh: |
| 76 | # try to update john's user record |
| 77 | r = requests.put(USERS_API + str(jhh["id"]) + "/", data=jhh, auth=opencloud_auth) |
| 78 | if r.status_code == 200: |
| 79 | print "I was able to update user", str(jhh["id"]) |
| 80 | else: |
| 81 | print "I was not able to update user", str(jhh["id"]) |