blob: 6a17b1642d6fdf73aa36143a9acc9dd265157b51 [file] [log] [blame]
Scott Baker134fff72014-10-07 12:54:51 -07001import inspect
2import json
3import os
4import requests
5import sys
6
7from operator import itemgetter, attrgetter
8
9REST_API="http://node43.princeton.vicci.org:8000/plstackapi/"
10USERS_API = REST_API + "users/"
11SLICES_API = REST_API + "slices/"
12SITES_API = REST_API + "sites/"
13SITEPRIV_API = REST_API + "site_privileges/"
14SLICEPRIV_API = REST_API + "slice_memberships/"
15SITEROLE_API = REST_API + "site_roles/"
16
17username = sys.argv[1]
18password = sys.argv[2]
19
20opencloud_auth=(username, password)
21admin_auth=("scott@onlab.us", "letmein")
22
23def fail_unless(x, msg):
24 if not x:
25 (frame, filename, line_number, function_name, lines, index) = inspect.getouterframes(inspect.currentframe())[1]
26 print "FAIL (%s:%d)" % (function_name, line_number), msg
27
28
29print "downloading objects using admin"
30r = requests.get(USERS_API + "?no_hyperlinks=1", auth=admin_auth)
31allUsers = r.json()
32r = requests.get(SLICES_API + "?no_hyperlinks=1", auth=admin_auth)
33allSlices = r.json()
34r = requests.get(SITES_API + "?no_hyperlinks=1", auth=admin_auth)
35allSites = r.json()
36r = requests.get(SITEPRIV_API + "?no_hyperlinks=1", auth=admin_auth)
37allSitePriv = r.json()
38r = requests.get(SLICEPRIV_API + "?no_hyperlinks=1", auth=admin_auth)
39allSlicePriv = r.json()
40r = requests.get(SITEROLE_API + "?no_hyperlinks=1", auth=admin_auth)
41allSiteRole = r.json()
42
43def should_see_user(myself, otherUser):
44 if myself["is_admin"]:
45 return True
46 if myself["id"] == otherUser["id"]:
47 return True
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"]:
52 return True
53 return False
54
55def flip_phone(user):
56 if user["phone"] == "123":
57 user["phone"] = "456"
58 else:
59 user["phone"] = "123"
60
61print " loaded user:%d slice:%d, site:%d, site_priv:%d slice_priv:%d" % (len(allUsers), len(allSlices), len(allSites), len(allSitePriv), len(allSlicePriv))
62
63# get our own user record
64
65r = requests.get(USERS_API + "?email=%s&no_hyperlinks" % username, auth=opencloud_auth)
66fail_unless(r.status_code==200, "failed to get user %s" % username)
67myself = r.json()
68fail_unless(len(myself)==1, "wrong number of results when getting user %s" % username)
69myself = myself[0]
70
71# check to see that we see the users we should be able to
72
73r = requests.get(USERS_API, auth=opencloud_auth)
74myUsers = r.json()
75for user in myUsers:
76 fail_unless(should_see_user(myself, user), "saw user %s but we shouldn't have" % user["email"])
77myUsersIds = [r["id"] for r in myUsers]
78for user in allUsers:
79 if should_see_user(myself, user):
80 fail_unless(user["id"] in myUsersIds, "should have seen user %s but didnt" % user["email"])
81
82# toggle the phone number on the users we should be able to
83
84for user in allUsers:
85 user = requests.get(USERS_API + str(user["id"]) + "/", auth=admin_auth).json()
86 flip_phone(user)
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"])
90 else:
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"])
93
94for user in allUsers:
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"])
100 else:
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"])
103
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)
107
108
109
110
111