7 from operator import itemgetter, attrgetter
9 REST_API="http://node43.princeton.vicci.org:8000/plstackapi/"
10 USERS_API = REST_API + "users/"
11 SLICES_API = REST_API + "slices/"
12 SITES_API = REST_API + "sites/"
13 SITEPRIV_API = REST_API + "site_privileges/"
14 SLICEPRIV_API = REST_API + "slice_memberships/"
15 SITEROLE_API = REST_API + "site_roles/"
17 username = sys.argv[1]
18 password = sys.argv[2]
20 opencloud_auth=(username, password)
21 admin_auth=("scott@onlab.us", "letmein")
23 def fail_unless(x, msg):
25 (frame, filename, line_number, function_name, lines, index) = inspect.getouterframes(inspect.currentframe())[1]
26 print "FAIL (%s:%d)" % (function_name, line_number), msg
29 print "downloading objects using admin"
30 r = requests.get(USERS_API + "?no_hyperlinks=1", auth=admin_auth)
32 r = requests.get(SLICES_API + "?no_hyperlinks=1", auth=admin_auth)
34 r = requests.get(SITES_API + "?no_hyperlinks=1", auth=admin_auth)
36 r = requests.get(SITEPRIV_API + "?no_hyperlinks=1", auth=admin_auth)
37 allSitePriv = r.json()
38 r = requests.get(SLICEPRIV_API + "?no_hyperlinks=1", auth=admin_auth)
39 allSlicePriv = r.json()
40 r = requests.get(SITEROLE_API + "?no_hyperlinks=1", auth=admin_auth)
41 allSiteRole = r.json()
43 def should_see_user(myself, otherUser):
44 if myself["is_admin"]:
46 if myself["id"] == otherUser["id"]:
48 for sitePriv in allSitePriv:
49 if (sitePriv["user"] == myself["id"]) and (sitePriv["site"] == otherUser["site"]):
50 for role in allSiteRole:
51 if role["role"]=="pi" and role["id"] == sitePriv["role"]:
56 if user["phone"] == "123":
61 print " loaded user:%d slice:%d, site:%d, site_priv:%d slice_priv:%d" % (len(allUsers), len(allSlices), len(allSites), len(allSitePriv), len(allSlicePriv))
63 # get our own user record
65 r = requests.get(USERS_API + "?email=%s&no_hyperlinks" % username, auth=opencloud_auth)
66 fail_unless(r.status_code==200, "failed to get user %s" % username)
68 fail_unless(len(myself)==1, "wrong number of results when getting user %s" % username)
71 # check to see that we see the users we should be able to
73 r = requests.get(USERS_API, auth=opencloud_auth)
76 fail_unless(should_see_user(myself, user), "saw user %s but we shouldn't have" % user["email"])
77 myUsersIds = [r["id"] for r in myUsers]
79 if should_see_user(myself, user):
80 fail_unless(user["id"] in myUsersIds, "should have seen user %s but didnt" % user["email"])
82 # toggle the phone number on the users we should be able to
85 user = requests.get(USERS_API + str(user["id"]) + "/", auth=admin_auth).json()
87 r = requests.put(USERS_API + str(user["id"]) +"/", data=user, auth=opencloud_auth)
88 if should_see_user(myself, user):
89 fail_unless(r.status_code==200, "failed to change phone number on %s" % user["email"])
91 # XXX: this is failing, but for the wrong reason
92 fail_unless(r.status_code!=200, "was able to change phone number on %s but shouldn't have" % user["email"])
95 user = requests.get(USERS_API + str(user["id"]) + "/", auth=admin_auth).json()
96 user["is_staff"] = not user["is_staff"]
97 r = requests.put(USERS_API + str(user["id"]) +"/", data=user, auth=opencloud_auth)
98 if myself["is_admin"]:
99 fail_unless(r.status_code==200, "failed to change is_staff on %s" % user["email"])
101 # XXX: this is failing, but for the wrong reason
102 fail_unless(r.status_code!=200, "was able to change is_staff on %s but shouldn't have" % user["email"])
104 # put it back to false, in case we successfully changed it...
105 user["is_staff"] = False
106 r = requests.put(USERS_API + str(user["id"]) +"/", data=user, auth=opencloud_auth)