Revision a24b5bda snf-astakos-app/astakos/im/tests/api.py
b/snf-astakos-app/astakos/im/tests/api.py | ||
---|---|---|
36 | 36 |
from django.test import TestCase |
37 | 37 |
from django.core.urlresolvers import reverse |
38 | 38 |
|
39 |
|
|
40 |
from urllib import quote |
|
41 |
from urlparse import urlparse, parse_qs |
|
42 | 39 |
#from xml.dom import minidom |
43 | 40 |
|
44 | 41 |
import json |
... | ... | |
553 | 550 |
# body = minidom.parseString(r.content) |
554 | 551 |
# except Exception, e: |
555 | 552 |
# self.fail(e) |
556 |
|
|
557 |
def test_get_endpoints(self): |
|
558 |
client = Client() |
|
559 |
|
|
560 |
# Check in active user token |
|
561 |
inactive_user = AstakosUser.objects.create(email='test3') |
|
562 |
url = '/astakos/api/tokens/%s/endpoints' % quote( |
|
563 |
inactive_user.auth_token) |
|
564 |
r = client.get(url) |
|
565 |
self.assertEqual(r.status_code, 401) |
|
566 |
|
|
567 |
# Check invalid user token in path |
|
568 |
url = '/astakos/api/tokens/nouser/endpoints' |
|
569 |
r = client.get(url) |
|
570 |
self.assertEqual(r.status_code, 401) |
|
571 |
|
|
572 |
# Check forbidden |
|
573 |
url = '/astakos/api/tokens/%s/endpoints' % quote(self.user1.auth_token) |
|
574 |
headers = {'HTTP_X_AUTH_TOKEN': AstakosUser.objects.create( |
|
575 |
email='test4').auth_token} |
|
576 |
r = client.get(url, **headers) |
|
577 |
self.assertEqual(r.status_code, 401) |
|
578 |
|
|
579 |
# Check bad request method |
|
580 |
url = '/astakos/api/tokens/%s/endpoints' % quote(self.user1.auth_token) |
|
581 |
r = client.post(url) |
|
582 |
self.assertEqual(r.status_code, 400) |
|
583 |
|
|
584 |
# Check forbidden |
|
585 |
url = '/astakos/api/tokens/%s/endpoints' % quote(self.user1.auth_token) |
|
586 |
headers = {'HTTP_X_AUTH_TOKEN': self.user2.auth_token} |
|
587 |
r = client.get(url, **headers) |
|
588 |
self.assertEqual(r.status_code, 403) |
|
589 |
|
|
590 |
# Check belongsTo BadRequest |
|
591 |
url = '/astakos/api/tokens/%s/endpoints?belongsTo=%s' % ( |
|
592 |
quote(self.user1.auth_token), quote(self.user2.uuid)) |
|
593 |
headers = {'HTTP_X_AUTH_TOKEN': self.user1.auth_token} |
|
594 |
r = client.get(url, **headers) |
|
595 |
self.assertEqual(r.status_code, 400) |
|
596 |
|
|
597 |
# Check successful request |
|
598 |
url = '/astakos/api/tokens/%s/endpoints' % quote(self.user1.auth_token) |
|
599 |
headers = {'HTTP_X_AUTH_TOKEN': self.user1.auth_token} |
|
600 |
r = client.get(url, **headers) |
|
601 |
self.assertEqual(r.status_code, 200) |
|
602 |
self.assertEqual(r['Content-Type'], 'application/json; charset=UTF-8') |
|
603 |
try: |
|
604 |
body = json.loads(r.content) |
|
605 |
except: |
|
606 |
self.fail('json format expected') |
|
607 |
endpoints = body.get('endpoints') |
|
608 |
self.assertEqual(len(endpoints), 3) |
|
609 |
|
|
610 |
# Check xml serialization |
|
611 |
url = '/astakos/api/tokens/%s/endpoints?format=xml' %\ |
|
612 |
quote(self.user1.auth_token) |
|
613 |
headers = {'HTTP_X_AUTH_TOKEN': self.user1.auth_token} |
|
614 |
r = client.get(url, **headers) |
|
615 |
self.assertEqual(r.status_code, 200) |
|
616 |
self.assertEqual(r['Content-Type'], 'application/xml; charset=UTF-8') |
|
617 |
# try: |
|
618 |
# body = minidom.parseString(r.content) |
|
619 |
# except Exception, e: |
|
620 |
# self.fail('xml format expected') |
|
621 |
endpoints = body.get('endpoints') |
|
622 |
self.assertEqual(len(endpoints), 3) |
|
623 |
|
|
624 |
# Check limit |
|
625 |
url = '/astakos/api/tokens/%s/endpoints?limit=2' %\ |
|
626 |
quote(self.user1.auth_token) |
|
627 |
headers = {'HTTP_X_AUTH_TOKEN': self.user1.auth_token} |
|
628 |
r = client.get(url, **headers) |
|
629 |
self.assertEqual(r.status_code, 200) |
|
630 |
body = json.loads(r.content) |
|
631 |
endpoints = body.get('endpoints') |
|
632 |
self.assertEqual(len(endpoints), 2) |
|
633 |
|
|
634 |
endpoint_link = body.get('endpoint_links', [])[0] |
|
635 |
next = endpoint_link.get('href') |
|
636 |
p = urlparse(next) |
|
637 |
params = parse_qs(p.query) |
|
638 |
self.assertTrue('limit' in params) |
|
639 |
self.assertTrue('marker' in params) |
|
640 |
self.assertEqual(params['marker'][0], '2') |
|
641 |
|
|
642 |
# Check marker |
|
643 |
headers = {'HTTP_X_AUTH_TOKEN': self.user1.auth_token} |
|
644 |
r = client.get(next, **headers) |
|
645 |
self.assertEqual(r.status_code, 200) |
|
646 |
body = json.loads(r.content) |
|
647 |
endpoints = body.get('endpoints') |
|
648 |
self.assertEqual(len(endpoints), 1) |
Also available in: Unified diff