blob: 14cba7ece6fe314a102dd9dc9a88d2143e84b3c8 [file] [log] [blame]
Scott Bakerb13bec22015-02-18 09:56:21 -08001"""
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 Baker438ea112014-10-07 00:10:41 -07009import json
10import os
11import requests
12import sys
Scott Bakerb13bec22015-02-18 09:56:21 -080013from urllib import urlencode
Scott Baker438ea112014-10-07 00:10:41 -070014
15from operator import itemgetter, attrgetter
16
Scott Bakerb13bec22015-02-18 09:56:21 -080017if (len(sys.argv)!=6):
18 print "syntax: usertest <hostname> <username> <password> <admin_username> <admin_password>"
19 sys.exit(-1)
Scott Baker438ea112014-10-07 00:10:41 -070020
Scott Bakerb13bec22015-02-18 09:56:21 -080021hostname = sys.argv[1]
22username = sys.argv[2]
23password = sys.argv[3]
Scott Baker438ea112014-10-07 00:10:41 -070024
25opencloud_auth=(username, password)
Scott Bakerb13bec22015-02-18 09:56:21 -080026admin_auth=(sys.argv[4], sys.argv[5])
Scott Baker438ea112014-10-07 00:10:41 -070027
Scott Bakerb13bec22015-02-18 09:56:21 -080028REST_API="http://%s:8000/xos/" % hostname
29USERS_API = REST_API + "users/"
Scott Baker438ea112014-10-07 00:10:41 -070030
Scott Bakerb13bec22015-02-18 09:56:21 -080031print "fetching user record for %s:" % username
32r = requests.get(USERS_API + "?" + urlencode({"email": username}), auth=opencloud_auth)
Scott Baker438ea112014-10-07 00:10:41 -070033for user in r.json():
34 print " ", user["email"]
35
36myself = r.json()[0]
37
38if myself["phone"] == "123":
39 myself["phone"] = "456"
40else:
41 myself["phone"] = "123"
42
43r = requests.put(USERS_API + str(myself["id"]) +"/", data=myself, auth=opencloud_auth)
44if r.status_code == 200:
45 print "I updated my phone to", myself["phone"]
46else:
47 print "I failed to update my phone"
48
49if myself["is_admin"] == True:
50 myself["is_admin"] = False
51else:
52 myself["is_admin"] = True
53
54r = requests.put(USERS_API + str(myself["id"]) +"/", data=myself, auth=opencloud_auth)
55if r.status_code == 200:
56 print "I updated my is_admin to", myself["is_admin"]
57else:
58 print "I failed to update my is_admin"
59
60r = requests.get(USERS_API + "?email=jhh@cs.arizona.edu", auth=opencloud_auth)
61if len(r.json())>0:
62 print "I was able to read jhh@cs.arizona.edu"
63else:
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
67r = requests.get(USERS_API + "?email=jhh@cs.arizona.edu", auth=admin_auth)
68if len(r.json())>0:
69 print "Admin was able to read jhh@cs.arizona.edu"
70 jhh = r.json()[0]
71else:
Scott Bakerb13bec22015-02-18 09:56:21 -080072 print "Admin was not able to read jhh@cs.arizona.edu"
Scott Baker438ea112014-10-07 00:10:41 -070073 jhh = None
74
75if 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"])