root / snf-astakos-app / astakos / im / tests / projects.py @ 45112d5a
History | View | Annotate | Download (30.9 kB)
1 |
# Copyright 2011, 2012, 2013 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from astakos.im.tests.common import * |
35 |
from snf_django.utils.testing import assertGreater, assertIn, assertRaises |
36 |
|
37 |
|
38 |
class ProjectAPITest(TestCase): |
39 |
|
40 |
def setUp(self): |
41 |
self.client = Client()
|
42 |
component1 = Component.objects.create(name="comp1")
|
43 |
register.add_service(component1, "service1", "type1", []) |
44 |
# custom service resources
|
45 |
resource11 = {"name": "service1.resource11", |
46 |
"desc": "resource11 desc", |
47 |
"service_type": "type1", |
48 |
"service_origin": "service1", |
49 |
"ui_visible": True} |
50 |
r, _ = register.add_resource(resource11) |
51 |
register.update_resources([(r, 100)])
|
52 |
resource12 = {"name": "service1.resource12", |
53 |
"desc": "resource11 desc", |
54 |
"service_type": "type1", |
55 |
"service_origin": "service1", |
56 |
"unit": "bytes"} |
57 |
r, _ = register.add_resource(resource12) |
58 |
register.update_resources([(r, 1024)])
|
59 |
|
60 |
# create user
|
61 |
self.user1 = get_local_user("test@grnet.gr", moderated=True) |
62 |
quotas.qh_sync_user(self.user1)
|
63 |
self.user2 = get_local_user("test2@grnet.gr", moderated=True) |
64 |
self.user2.uuid = "uuid2" |
65 |
self.user2.save()
|
66 |
quotas.qh_sync_user(self.user2)
|
67 |
self.user3 = get_local_user("test3@grnet.gr", moderated=True) |
68 |
quotas.qh_sync_user(self.user3)
|
69 |
|
70 |
astakos = Component.objects.create(name="astakos")
|
71 |
register.add_service(astakos, "astakos_account", "account", []) |
72 |
# create another service
|
73 |
pending_app = {"name": "astakos.pending_app", |
74 |
"desc": "pend app desc", |
75 |
"service_type": "account", |
76 |
"service_origin": "astakos_account", |
77 |
"ui_visible": False, |
78 |
"api_visible": False} |
79 |
r, _ = register.add_resource(pending_app) |
80 |
register.update_resources([(r, 3)])
|
81 |
accepted = AstakosUser.objects.accepted() |
82 |
quotas.update_base_quota(accepted, r.name, 3)
|
83 |
|
84 |
def create(self, app, headers): |
85 |
dump = json.dumps(app) |
86 |
r = self.client.post(reverse("api_projects"), dump, |
87 |
content_type="application/json", **headers)
|
88 |
body = json.loads(r.content) |
89 |
return r.status_code, body
|
90 |
|
91 |
def modify(self, app, project_id, headers): |
92 |
dump = json.dumps(app) |
93 |
kwargs = {"project_id": project_id}
|
94 |
r = self.client.post(reverse("api_project", kwargs=kwargs), dump, |
95 |
content_type="application/json", **headers)
|
96 |
body = json.loads(r.content) |
97 |
return r.status_code, body
|
98 |
|
99 |
def project_action(self, project_id, action, headers): |
100 |
action = json.dumps({action: "reason"})
|
101 |
r = self.client.post(reverse("api_project_action", |
102 |
kwargs={"project_id": project_id}),
|
103 |
action, content_type="application/json",
|
104 |
**headers) |
105 |
return r.status_code
|
106 |
|
107 |
def app_action(self, app_id, action, headers): |
108 |
action = json.dumps({action: "reason"})
|
109 |
r = self.client.post(reverse("api_application_action", |
110 |
kwargs={"app_id": app_id}),
|
111 |
action, content_type="application/json",
|
112 |
**headers) |
113 |
return r.status_code
|
114 |
|
115 |
def memb_action(self, memb_id, action, headers): |
116 |
action = json.dumps({action: "reason"})
|
117 |
r = self.client.post(reverse("api_membership_action", |
118 |
kwargs={"memb_id": memb_id}), action,
|
119 |
content_type="application/json", **headers)
|
120 |
return r.status_code
|
121 |
|
122 |
def join(self, project_id, headers): |
123 |
action = {"join": {"project": project_id}} |
124 |
req = json.dumps(action) |
125 |
r = self.client.post(reverse("api_memberships"), req, |
126 |
content_type="application/json", **headers)
|
127 |
body = json.loads(r.content) |
128 |
return r.status_code, body
|
129 |
|
130 |
def enroll(self, project_id, user, headers): |
131 |
action = { |
132 |
"enroll": {
|
133 |
"project": project_id,
|
134 |
"user": user.email,
|
135 |
} |
136 |
} |
137 |
req = json.dumps(action) |
138 |
r = self.client.post(reverse("api_memberships"), req, |
139 |
content_type="application/json", **headers)
|
140 |
body = json.loads(r.content) |
141 |
return r.status_code, body
|
142 |
|
143 |
@im_settings(PROJECT_ADMINS=["uuid2"]) |
144 |
def test_projects(self): |
145 |
client = self.client
|
146 |
h_owner = {"HTTP_X_AUTH_TOKEN": self.user1.auth_token} |
147 |
h_admin = {"HTTP_X_AUTH_TOKEN": self.user2.auth_token} |
148 |
h_plain = {"HTTP_X_AUTH_TOKEN": self.user3.auth_token} |
149 |
r = client.get(reverse("api_project", kwargs={"project_id": 1})) |
150 |
self.assertEqual(r.status_code, 401) |
151 |
|
152 |
r = client.get(reverse("api_project", kwargs={"project_id": 1}), |
153 |
**h_owner) |
154 |
self.assertEqual(r.status_code, 404) |
155 |
r = client.get(reverse("api_application", kwargs={"app_id": 1}), |
156 |
**h_owner) |
157 |
self.assertEqual(r.status_code, 404) |
158 |
r = client.get(reverse("api_membership", kwargs={"memb_id": 1}), |
159 |
**h_owner) |
160 |
self.assertEqual(r.status_code, 404) |
161 |
|
162 |
status = self.memb_action(1, "accept", h_admin) |
163 |
self.assertEqual(status, 404) |
164 |
|
165 |
app1 = {"name": "test.pr", |
166 |
"end_date": "2013-5-5T20:20:20Z", |
167 |
"join_policy": "auto", |
168 |
"max_members": 5, |
169 |
"resources": {"service1.resource11": { |
170 |
"member_capacity": 512}} |
171 |
} |
172 |
|
173 |
status, body = self.modify(app1, 1, h_owner) |
174 |
self.assertEqual(status, 404) |
175 |
|
176 |
# Create
|
177 |
status, body = self.create(app1, h_owner)
|
178 |
self.assertEqual(status, 201) |
179 |
project_id = body["id"]
|
180 |
app_id = body["application"]
|
181 |
|
182 |
# Get project
|
183 |
r = client.get(reverse("api_project",
|
184 |
kwargs={"project_id": project_id}),
|
185 |
**h_owner) |
186 |
self.assertEqual(r.status_code, 200) |
187 |
body = json.loads(r.content) |
188 |
self.assertEqual(body["id"], project_id) |
189 |
self.assertEqual(body["application"], app_id) |
190 |
self.assertEqual(body["state"], "pending") |
191 |
self.assertEqual(body["owner"], self.user1.uuid) |
192 |
|
193 |
# Approve forbidden
|
194 |
status = self.app_action(app_id, "approve", h_owner) |
195 |
self.assertEqual(status, 403) |
196 |
|
197 |
# Create another with the same name
|
198 |
status, body = self.create(app1, h_owner)
|
199 |
self.assertEqual(status, 201) |
200 |
project2_id = body["id"]
|
201 |
project2_app_id = body["application"]
|
202 |
|
203 |
# Create yet another, with different name
|
204 |
app_p3 = copy.deepcopy(app1) |
205 |
app_p3["name"] = "new.pr" |
206 |
status, body = self.create(app_p3, h_owner)
|
207 |
self.assertEqual(status, 201) |
208 |
project3_app_id = body["application"]
|
209 |
|
210 |
# No more pending allowed
|
211 |
status, body = self.create(app_p3, h_owner)
|
212 |
self.assertEqual(status, 409) |
213 |
|
214 |
# Cancel
|
215 |
status = self.app_action(project3_app_id, "cancel", h_owner) |
216 |
self.assertEqual(status, 200) |
217 |
|
218 |
# Modify
|
219 |
app2 = {"name": "test.pr", |
220 |
"start_date": "2013-5-5T20:20:20Z", |
221 |
"end_date": "2013-7-5T20:20:20Z", |
222 |
"join_policy": "moderated", |
223 |
"leave_policy": "auto", |
224 |
"max_members": 3, |
225 |
"resources": {"service1.resource11": { |
226 |
"member_capacity": 1024}} |
227 |
} |
228 |
|
229 |
status, body = self.modify(app2, project_id, h_owner)
|
230 |
self.assertEqual(status, 201) |
231 |
self.assertEqual(project_id, body["id"]) |
232 |
app2_id = body["application"]
|
233 |
assertGreater(app2_id, app_id) |
234 |
|
235 |
# Dismiss failed
|
236 |
status = self.app_action(app2_id, "dismiss", h_owner) |
237 |
self.assertEqual(status, 409) |
238 |
|
239 |
# Deny
|
240 |
status = self.app_action(app2_id, "deny", h_admin) |
241 |
self.assertEqual(status, 200) |
242 |
|
243 |
r = client.get(reverse("api_application", kwargs={"app_id": app2_id}), |
244 |
**h_owner) |
245 |
body = json.loads(r.content) |
246 |
self.assertEqual(body["state"], "denied") |
247 |
|
248 |
# Dismiss
|
249 |
status = self.app_action(app2_id, "dismiss", h_owner) |
250 |
self.assertEqual(status, 200) |
251 |
|
252 |
# Resubmit
|
253 |
status, body = self.modify(app2, project_id, h_owner)
|
254 |
self.assertEqual(status, 201) |
255 |
app3_id = body["application"]
|
256 |
|
257 |
# Approve
|
258 |
status = self.app_action(app3_id, "approve", h_admin) |
259 |
self.assertEqual(status, 200) |
260 |
|
261 |
# Get related apps
|
262 |
req = {"body": json.dumps({"project": project_id})} |
263 |
r = client.get(reverse("api_applications"), req, **h_owner)
|
264 |
self.assertEqual(r.status_code, 200) |
265 |
body = json.loads(r.content) |
266 |
self.assertEqual(len(body), 3) |
267 |
|
268 |
# Get apps
|
269 |
r = client.get(reverse("api_applications"), **h_owner)
|
270 |
self.assertEqual(r.status_code, 200) |
271 |
body = json.loads(r.content) |
272 |
self.assertEqual(len(body), 5) |
273 |
|
274 |
# Enroll
|
275 |
status, body = self.enroll(project_id, self.user3, h_owner) |
276 |
self.assertEqual(status, 200) |
277 |
m_plain_id = body["id"]
|
278 |
|
279 |
# Join
|
280 |
status, body = self.join(project_id, h_owner)
|
281 |
self.assertEqual(status, 200) |
282 |
memb_id = body["id"]
|
283 |
|
284 |
# Check memberships
|
285 |
r = client.get(reverse("api_memberships"), **h_plain)
|
286 |
body = json.loads(r.content) |
287 |
self.assertEqual(len(body), 1) |
288 |
m = body[0]
|
289 |
self.assertEqual(m["user"], self.user3.uuid) |
290 |
self.assertEqual(m["state"], "accepted") |
291 |
|
292 |
r = client.get(reverse("api_memberships"), **h_owner)
|
293 |
body = json.loads(r.content) |
294 |
self.assertEqual(len(body), 2) |
295 |
|
296 |
# Check membership
|
297 |
r = client.get(reverse("api_membership", kwargs={"memb_id": memb_id}), |
298 |
**h_admin) |
299 |
m = json.loads(r.content) |
300 |
self.assertEqual(m["user"], self.user1.uuid) |
301 |
self.assertEqual(m["state"], "requested") |
302 |
self.assertEqual(sorted(m["allowed_actions"]), |
303 |
["accept", "cancel", "reject"]) |
304 |
|
305 |
r = client.get(reverse("api_membership", kwargs={"memb_id": memb_id}), |
306 |
**h_plain) |
307 |
self.assertEqual(r.status_code, 403) |
308 |
|
309 |
status = self.memb_action(memb_id, "leave", h_admin) |
310 |
self.assertEqual(status, 409) |
311 |
|
312 |
status = self.memb_action(memb_id, "cancel", h_owner) |
313 |
self.assertEqual(status, 200) |
314 |
|
315 |
status, body = self.join(project_id, h_owner)
|
316 |
self.assertEqual(status, 200) |
317 |
self.assertEqual(memb_id, body["id"]) |
318 |
|
319 |
status = self.memb_action(memb_id, "reject", h_owner) |
320 |
self.assertEqual(status, 200) |
321 |
|
322 |
status, body = self.join(project_id, h_owner)
|
323 |
self.assertEqual(status, 200) |
324 |
self.assertEqual(memb_id, body["id"]) |
325 |
|
326 |
status = self.memb_action(memb_id, "accept", h_owner) |
327 |
self.assertEqual(status, 200) |
328 |
|
329 |
# Enroll fails, already in
|
330 |
status, body = self.enroll(project_id, self.user1, h_owner) |
331 |
self.assertEqual(status, 409) |
332 |
|
333 |
# Remove member
|
334 |
status = self.memb_action(memb_id, "remove", h_owner) |
335 |
self.assertEqual(status, 200) |
336 |
|
337 |
# Enroll a removed member
|
338 |
status, body = self.enroll(project_id, self.user1, h_owner) |
339 |
self.assertEqual(status, 200) |
340 |
|
341 |
# Remove member
|
342 |
status = self.memb_action(memb_id, "remove", h_owner) |
343 |
self.assertEqual(status, 200) |
344 |
|
345 |
# Re-join
|
346 |
status, body = self.join(project_id, h_owner)
|
347 |
self.assertEqual(status, 200) |
348 |
self.assertEqual(memb_id, body["id"]) |
349 |
|
350 |
# Enroll a requested member
|
351 |
status, body = self.enroll(project_id, self.user1, h_owner) |
352 |
self.assertEqual(status, 200) |
353 |
|
354 |
# Enroll fails, already in
|
355 |
status, body = self.enroll(project_id, self.user1, h_owner) |
356 |
self.assertEqual(status, 409) |
357 |
|
358 |
# Enroll fails, project does not exist
|
359 |
status, body = self.enroll(-1, self.user1, h_owner) |
360 |
self.assertEqual(status, 409) |
361 |
|
362 |
# Get projects
|
363 |
## Simple user mode
|
364 |
r = client.get(reverse("api_projects"), **h_plain)
|
365 |
body = json.loads(r.content) |
366 |
self.assertEqual(len(body), 1) |
367 |
p = body[0]
|
368 |
with assertRaises(KeyError): |
369 |
p["pending_application"]
|
370 |
|
371 |
## Owner mode
|
372 |
filters = {"filter": {"state": ["active", "cancelled"]}} |
373 |
req = {"body": json.dumps(filters)}
|
374 |
r = client.get(reverse("api_projects"), req, **h_owner)
|
375 |
body = json.loads(r.content) |
376 |
self.assertEqual(len(body), 2) |
377 |
assertIn("pending_application", body[0]) |
378 |
|
379 |
filters = {"filter": {"state": "pending"}} |
380 |
req = {"body": json.dumps(filters)}
|
381 |
r = client.get(reverse("api_projects"), req, **h_owner)
|
382 |
body = json.loads(r.content) |
383 |
self.assertEqual(len(body), 1) |
384 |
self.assertEqual(body[0]["id"], project2_id) |
385 |
|
386 |
filters = {"filter": {"name": "test.pr"}} |
387 |
req = {"body": json.dumps(filters)}
|
388 |
r = client.get(reverse("api_projects"), req, **h_owner)
|
389 |
body = json.loads(r.content) |
390 |
self.assertEqual(len(body), 2) |
391 |
|
392 |
# Leave failed
|
393 |
status = self.memb_action(m_plain_id, "leave", h_owner) |
394 |
self.assertEqual(status, 403) |
395 |
|
396 |
# Leave
|
397 |
status = self.memb_action(m_plain_id, "leave", h_plain) |
398 |
self.assertEqual(status, 200) |
399 |
|
400 |
# Suspend failed
|
401 |
status = self.project_action(project_id, "suspend", h_owner) |
402 |
self.assertEqual(status, 403) |
403 |
|
404 |
# Unsuspend failed
|
405 |
status = self.project_action(project_id, "unsuspend", h_admin) |
406 |
self.assertEqual(status, 409) |
407 |
|
408 |
# Suspend
|
409 |
status = self.project_action(project_id, "suspend", h_admin) |
410 |
self.assertEqual(status, 200) |
411 |
|
412 |
# Cannot view project
|
413 |
r = client.get(reverse("api_project",
|
414 |
kwargs={"project_id": project_id}), **h_plain)
|
415 |
self.assertEqual(r.status_code, 403) |
416 |
|
417 |
# Unsuspend
|
418 |
status = self.project_action(project_id, "unsuspend", h_admin) |
419 |
self.assertEqual(status, 200) |
420 |
|
421 |
# Cannot approve, project with same name exists
|
422 |
status = self.app_action(project2_app_id, "approve", h_admin) |
423 |
self.assertEqual(status, 409) |
424 |
|
425 |
# Terminate
|
426 |
status = self.project_action(project_id, "terminate", h_admin) |
427 |
self.assertEqual(status, 200) |
428 |
|
429 |
# Join failed
|
430 |
status, _ = self.join(project_id, h_admin)
|
431 |
self.assertEqual(status, 409) |
432 |
|
433 |
# Can approve now
|
434 |
status = self.app_action(project2_app_id, "approve", h_admin) |
435 |
self.assertEqual(status, 200) |
436 |
|
437 |
# Join new project
|
438 |
status, body = self.join(project2_id, h_plain)
|
439 |
self.assertEqual(status, 200) |
440 |
m_project2 = body["id"]
|
441 |
|
442 |
# Get memberships of project
|
443 |
body = {"body": json.dumps({"project": project2_id})} |
444 |
r = client.get(reverse("api_memberships"), body, **h_owner)
|
445 |
body = json.loads(r.content) |
446 |
self.assertEqual(len(body), 1) |
447 |
self.assertEqual(body[0]["id"], m_project2) |
448 |
|
449 |
# Remove member
|
450 |
status = self.memb_action(m_project2, "remove", h_owner) |
451 |
self.assertEqual(status, 200) |
452 |
|
453 |
# Reinstate failed
|
454 |
status = self.project_action(project_id, "reinstate", h_admin) |
455 |
self.assertEqual(status, 409) |
456 |
|
457 |
# Rename
|
458 |
app2_renamed = copy.deepcopy(app2) |
459 |
app2_renamed["name"] = "new.name" |
460 |
status, body = self.modify(app2_renamed, project_id, h_owner)
|
461 |
self.assertEqual(status, 201) |
462 |
app2_renamed_id = body["application"]
|
463 |
|
464 |
# Get project
|
465 |
r = client.get(reverse("api_project",
|
466 |
kwargs={"project_id": project_id}), **h_owner)
|
467 |
body = json.loads(r.content) |
468 |
self.assertEqual(body["application"], app3_id) |
469 |
self.assertEqual(body["pending_application"], app2_renamed_id) |
470 |
self.assertEqual(body["state"], "terminated") |
471 |
assertIn("deactivation_date", body)
|
472 |
|
473 |
# Get application
|
474 |
r = client.get(reverse("api_application",
|
475 |
kwargs={"app_id": app2_renamed_id}), **h_plain)
|
476 |
self.assertEqual(r.status_code, 403) |
477 |
|
478 |
r = client.get(reverse("api_application",
|
479 |
kwargs={"app_id": app2_renamed_id}), **h_owner)
|
480 |
self.assertEqual(r.status_code, 200) |
481 |
body = json.loads(r.content) |
482 |
self.assertEqual(body["state"], "pending") |
483 |
self.assertEqual(body["name"], "new.name") |
484 |
|
485 |
# Approve (automatically reinstates)
|
486 |
action = json.dumps({"approve": ""}) |
487 |
r = client.post(reverse("api_application_action",
|
488 |
kwargs={"app_id": app2_renamed_id}),
|
489 |
action, content_type="application/json", **h_admin)
|
490 |
self.assertEqual(r.status_code, 200) |
491 |
|
492 |
# Bad requests
|
493 |
r = client.head(reverse("api_projects"), **h_admin)
|
494 |
self.assertEqual(r.status_code, 400) |
495 |
|
496 |
r = client.head(reverse("api_project",
|
497 |
kwargs={"project_id": 1}), **h_admin) |
498 |
self.assertEqual(r.status_code, 400) |
499 |
|
500 |
r = client.head(reverse("api_applications"), **h_admin)
|
501 |
self.assertEqual(r.status_code, 400) |
502 |
|
503 |
r = client.head(reverse("api_memberships"), **h_admin)
|
504 |
self.assertEqual(r.status_code, 400) |
505 |
|
506 |
status = self.project_action(1, "nonex", h_owner) |
507 |
self.assertEqual(status, 400) |
508 |
|
509 |
action = json.dumps({"suspend": "", "unsuspend": ""}) |
510 |
r = client.post(reverse("api_project_action",
|
511 |
kwargs={"project_id": 1}), |
512 |
action, content_type="application/json", **h_owner)
|
513 |
self.assertEqual(r.status_code, 400) |
514 |
|
515 |
ap = {"owner": "nonex", |
516 |
"join_policy": "nonex", |
517 |
"leave_policy": "nonex", |
518 |
"start_date": "nonex", |
519 |
"homepage": {},
|
520 |
"max_members": -3, |
521 |
"resources": [],
|
522 |
} |
523 |
|
524 |
status, body = self.create(ap, h_owner)
|
525 |
self.assertEqual(status, 400) |
526 |
self.assertEqual(body["badRequest"]["message"], "User does not exist.") |
527 |
|
528 |
ap["owner"] = self.user1.uuid |
529 |
status, body = self.create(ap, h_owner)
|
530 |
self.assertEqual(status, 400) |
531 |
|
532 |
ap["name"] = "some.name" |
533 |
status, body = self.create(ap, h_owner)
|
534 |
self.assertEqual(status, 400) |
535 |
|
536 |
ap["join_policy"] = "auto" |
537 |
status, body = self.create(ap, h_owner)
|
538 |
self.assertEqual(status, 400) |
539 |
|
540 |
ap["leave_policy"] = "closed" |
541 |
status, body = self.create(ap, h_owner)
|
542 |
self.assertEqual(status, 400) |
543 |
|
544 |
ap["start_date"] = "2013-01-01T0:0Z" |
545 |
status, body = self.create(ap, h_owner)
|
546 |
self.assertEqual(status, 400) |
547 |
|
548 |
ap["end_date"] = "2014-01-01T0:0Z" |
549 |
status, body = self.create(ap, h_owner)
|
550 |
self.assertEqual(status, 400) |
551 |
|
552 |
ap["max_members"] = 0 |
553 |
status, body = self.create(ap, h_owner)
|
554 |
self.assertEqual(status, 400) |
555 |
|
556 |
ap["homepage"] = "a.stri.ng" |
557 |
status, body = self.create(ap, h_owner)
|
558 |
self.assertEqual(status, 400) |
559 |
|
560 |
ap["resources"] = {42: 42} |
561 |
status, body = self.create(ap, h_owner)
|
562 |
self.assertEqual(status, 400) |
563 |
|
564 |
ap["resources"] = {"service1.resource11": {"member_capacity": 512}} |
565 |
status, body = self.create(ap, h_owner)
|
566 |
self.assertEqual(status, 201) |
567 |
|
568 |
ap["name"] = "non_domain_name" |
569 |
status, body = self.create(ap, h_owner)
|
570 |
self.assertEqual(status, 400) |
571 |
|
572 |
ap["name"] = "domain.name" |
573 |
ap.pop("max_members")
|
574 |
status, body = self.create(ap, h_owner)
|
575 |
self.assertEqual(status, 400) |
576 |
|
577 |
filters = {"filter": {"state": "nonex"}} |
578 |
req = {"body": json.dumps(filters)}
|
579 |
r = client.get(reverse("api_projects"), req, **h_owner)
|
580 |
self.assertEqual(r.status_code, 400) |
581 |
|
582 |
filters = {"filter": {"nonex": "nonex"}} |
583 |
req = {"body": json.dumps(filters)}
|
584 |
r = client.get(reverse("api_projects"), req, **h_owner)
|
585 |
self.assertEqual(r.status_code, 400) |
586 |
|
587 |
req = {"body": json.dumps({"project": "nonex"})} |
588 |
r = client.get(reverse("api_applications"), req, **h_owner)
|
589 |
self.assertEqual(r.status_code, 400) |
590 |
|
591 |
req = {"body": json.dumps({"project": "nonex"})} |
592 |
r = client.get(reverse("api_memberships"), req, **h_owner)
|
593 |
self.assertEqual(r.status_code, 400) |
594 |
|
595 |
|
596 |
class TestProjects(TestCase): |
597 |
"""
|
598 |
Test projects.
|
599 |
"""
|
600 |
def setUp(self): |
601 |
# astakos resources
|
602 |
self.resource = Resource.objects.create(name="astakos.pending_app", |
603 |
uplimit=0,
|
604 |
ui_visible=False,
|
605 |
api_visible=False,
|
606 |
service_type="astakos")
|
607 |
|
608 |
# custom service resources
|
609 |
self.resource = Resource.objects.create(name="service1.resource", |
610 |
uplimit=100,
|
611 |
service_type="service1")
|
612 |
self.admin = get_local_user("projects-admin@synnefo.org") |
613 |
self.admin.uuid = 'uuid1' |
614 |
self.admin.save()
|
615 |
|
616 |
self.user = get_local_user("user@synnefo.org") |
617 |
self.member = get_local_user("member@synnefo.org") |
618 |
self.member2 = get_local_user("member2@synnefo.org") |
619 |
|
620 |
self.admin_client = get_user_client("projects-admin@synnefo.org") |
621 |
self.user_client = get_user_client("user@synnefo.org") |
622 |
self.member_client = get_user_client("member@synnefo.org") |
623 |
self.member2_client = get_user_client("member2@synnefo.org") |
624 |
|
625 |
quotas.qh_sync_new_users(AstakosUser.objects.all()) |
626 |
|
627 |
def tearDown(self): |
628 |
Service.objects.all().delete() |
629 |
ProjectApplication.objects.all().delete() |
630 |
Project.objects.all().delete() |
631 |
AstakosUser.objects.all().delete() |
632 |
|
633 |
@im_settings(PROJECT_ADMINS=['uuid1']) |
634 |
def test_application_limit(self): |
635 |
# user cannot create a project
|
636 |
r = self.user_client.get(reverse('project_add'), follow=True) |
637 |
self.assertRedirects(r, reverse('project_list')) |
638 |
self.assertContains(r, "You are not allowed to create a new project") |
639 |
|
640 |
# but admin can
|
641 |
r = self.admin_client.get(reverse('project_add'), follow=True) |
642 |
self.assertRedirects(r, reverse('project_add')) |
643 |
|
644 |
@im_settings(PROJECT_ADMINS=['uuid1']) |
645 |
def test_ui_visible(self): |
646 |
dfrom = datetime.now() |
647 |
dto = datetime.now() + timedelta(days=30)
|
648 |
|
649 |
# astakos.pending_app ui_visible flag is False
|
650 |
# we shouldn't be able to create a project application using this
|
651 |
# resource.
|
652 |
application_data = { |
653 |
'name': 'project.synnefo.org', |
654 |
'homepage': 'https://www.synnefo.org', |
655 |
'start_date': dfrom.strftime("%Y-%m-%d"), |
656 |
'end_date': dto.strftime("%Y-%m-%d"), |
657 |
'member_join_policy': 2, |
658 |
'member_leave_policy': 1, |
659 |
'limit_on_members_number': 5, |
660 |
'service1.resource_uplimit': 100, |
661 |
'is_selected_service1.resource': "1", |
662 |
'astakos.pending_app_uplimit': 100, |
663 |
'is_selected_accounts': "1", |
664 |
'user': self.user.pk |
665 |
} |
666 |
form = forms.ProjectApplicationForm(data=application_data) |
667 |
# form is invalid
|
668 |
self.assertEqual(form.is_valid(), False) |
669 |
|
670 |
del application_data['astakos.pending_app_uplimit'] |
671 |
del application_data['is_selected_accounts'] |
672 |
form = forms.ProjectApplicationForm(data=application_data) |
673 |
self.assertEqual(form.is_valid(), True) |
674 |
|
675 |
@im_settings(PROJECT_ADMINS=['uuid1']) |
676 |
def test_applications(self): |
677 |
# let user have 2 pending applications
|
678 |
quotas.update_base_quota([self.user], 'astakos.pending_app', 2) |
679 |
|
680 |
r = self.user_client.get(reverse('project_add'), follow=True) |
681 |
self.assertRedirects(r, reverse('project_add')) |
682 |
|
683 |
# user fills the project application form
|
684 |
post_url = reverse('project_add') + '?verify=1' |
685 |
dfrom = datetime.now() |
686 |
dto = datetime.now() + timedelta(days=30)
|
687 |
application_data = { |
688 |
'name': 'project.synnefo.org', |
689 |
'homepage': 'https://www.synnefo.org', |
690 |
'start_date': dfrom.strftime("%Y-%m-%d"), |
691 |
'end_date': dto.strftime("%Y-%m-%d"), |
692 |
'member_join_policy': 2, |
693 |
'member_leave_policy': 1, |
694 |
'service1.resource_uplimit': 100, |
695 |
'is_selected_service1.resource': "1", |
696 |
'user': self.user.pk |
697 |
} |
698 |
r = self.user_client.post(post_url, data=application_data, follow=True) |
699 |
self.assertEqual(r.status_code, 200) |
700 |
self.assertEqual(r.context['form'].is_valid(), False) |
701 |
|
702 |
application_data['limit_on_members_number'] = 5 |
703 |
r = self.user_client.post(post_url, data=application_data, follow=True) |
704 |
self.assertEqual(r.status_code, 200) |
705 |
self.assertEqual(r.context['form'].is_valid(), True) |
706 |
|
707 |
# confirm request
|
708 |
post_url = reverse('project_add') + '?verify=0&edit=0' |
709 |
r = self.user_client.post(post_url, data=application_data, follow=True) |
710 |
self.assertContains(r, "The project application has been received") |
711 |
self.assertRedirects(r, reverse('project_list')) |
712 |
self.assertEqual(ProjectApplication.objects.count(), 1) |
713 |
app1 = ProjectApplication.objects.filter().order_by('pk')[0] |
714 |
app1_id = app1.pk |
715 |
project1_id = app1.chain_id |
716 |
|
717 |
# create another one
|
718 |
application_data['name'] = 'project2.synnefo.org' |
719 |
r = self.user_client.post(post_url, data=application_data, follow=True) |
720 |
app2 = ProjectApplication.objects.filter().order_by('pk')[1] |
721 |
project2_id = app2.chain_id |
722 |
|
723 |
# no more applications (LIMIT is 2)
|
724 |
r = self.user_client.get(reverse('project_add'), follow=True) |
725 |
self.assertRedirects(r, reverse('project_list')) |
726 |
self.assertContains(r, "You are not allowed to create a new project") |
727 |
|
728 |
# one project per application
|
729 |
self.assertEqual(Project.objects.count(), 2) |
730 |
|
731 |
# login
|
732 |
self.admin_client.get(reverse("edit_profile")) |
733 |
# admin approves
|
734 |
r = self.admin_client.post(reverse('project_app_approve', |
735 |
kwargs={'application_id': app1_id}),
|
736 |
follow=True)
|
737 |
self.assertEqual(r.status_code, 200) |
738 |
|
739 |
Q_ACTIVE = Project.o_state_q(Project.O_ACTIVE) |
740 |
self.assertEqual(Project.objects.filter(Q_ACTIVE).count(), 1) |
741 |
|
742 |
# login
|
743 |
self.member_client.get(reverse("edit_profile")) |
744 |
# cannot join project2 (not approved yet)
|
745 |
join_url = reverse("project_join", kwargs={'chain_id': project2_id}) |
746 |
r = self.member_client.post(join_url, follow=True) |
747 |
|
748 |
# can join project1
|
749 |
self.member_client.get(reverse("edit_profile")) |
750 |
join_url = reverse("project_join", kwargs={'chain_id': project1_id}) |
751 |
r = self.member_client.post(join_url, follow=True) |
752 |
self.assertEqual(r.status_code, 200) |
753 |
|
754 |
memberships = ProjectMembership.objects.all() |
755 |
self.assertEqual(len(memberships), 1) |
756 |
memb_id = memberships[0].id
|
757 |
|
758 |
reject_member_url = reverse('project_reject_member',
|
759 |
kwargs={'memb_id': memb_id})
|
760 |
accept_member_url = reverse('project_accept_member',
|
761 |
kwargs={'memb_id': memb_id})
|
762 |
|
763 |
# only project owner is allowed to reject
|
764 |
r = self.member_client.post(reject_member_url, follow=True) |
765 |
self.assertContains(r, "You do not have the permissions") |
766 |
self.assertEqual(r.status_code, 200) |
767 |
|
768 |
# user (owns project) rejects membership
|
769 |
r = self.user_client.post(reject_member_url, follow=True) |
770 |
self.assertEqual(ProjectMembership.objects.any_accepted().count(), 0) |
771 |
|
772 |
# user rejoins
|
773 |
self.member_client.get(reverse("edit_profile")) |
774 |
join_url = reverse("project_join", kwargs={'chain_id': project1_id}) |
775 |
r = self.member_client.post(join_url, follow=True) |
776 |
self.assertEqual(r.status_code, 200) |
777 |
self.assertEqual(ProjectMembership.objects.requested().count(), 1) |
778 |
|
779 |
# user (owns project) accepts membership
|
780 |
r = self.user_client.post(accept_member_url, follow=True) |
781 |
self.assertEqual(ProjectMembership.objects.any_accepted().count(), 1) |
782 |
membership = ProjectMembership.objects.get() |
783 |
self.assertEqual(membership.state, ProjectMembership.ACCEPTED)
|
784 |
|
785 |
user_quotas = quotas.get_users_quotas([self.member])
|
786 |
resource = 'service1.resource'
|
787 |
newlimit = user_quotas[self.member.uuid]['system'][resource]['limit'] |
788 |
# 100 from initial uplimit + 100 from project
|
789 |
self.assertEqual(newlimit, 200) |
790 |
|
791 |
remove_member_url = reverse('project_remove_member',
|
792 |
kwargs={'memb_id': membership.id})
|
793 |
r = self.user_client.post(remove_member_url, follow=True) |
794 |
self.assertEqual(r.status_code, 200) |
795 |
|
796 |
user_quotas = quotas.get_users_quotas([self.member])
|
797 |
resource = 'service1.resource'
|
798 |
newlimit = user_quotas[self.member.uuid]['system'][resource]['limit'] |
799 |
# 200 - 100 from project
|
800 |
self.assertEqual(newlimit, 100) |
801 |
|
802 |
# support email gets rendered in emails content
|
803 |
for mail in get_mailbox('user@synnefo.org'): |
804 |
self.assertTrue(settings.CONTACT_EMAIL in |
805 |
mail.message().as_string()) |