Revision 3472d390
b/snf-astakos-app/astakos/im/tests/api.py | ||
---|---|---|
423 | 423 |
|
424 | 424 |
def test_authenticate(self): |
425 | 425 |
client = Client() |
426 |
url = reverse('astakos.api.tokens.authenticate') |
|
426 | 427 |
|
427 | 428 |
# Check not allowed method |
428 |
url = reverse('astakos.api.tokens.authenticate') |
|
429 | 429 |
r = client.get(url, post_data={}) |
430 | 430 |
self.assertEqual(r.status_code, 400) |
431 | 431 |
|
... | ... | |
442 | 442 |
self.assertTrue('serviceCatalog' in body.get('access')) |
443 | 443 |
|
444 | 444 |
# Check unsupported xml input |
445 |
url = reverse('astakos.api.tokens.authenticate') |
|
446 | 445 |
post_data = """ |
447 | 446 |
<?xml version="1.0" encoding="UTF-8"?> |
448 | 447 |
<auth xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
... | ... | |
458 | 457 |
"Unsupported Content-type: 'application/xml'") |
459 | 458 |
|
460 | 459 |
# Check malformed request: missing password |
461 |
url = reverse('astakos.api.tokens.authenticate') |
|
462 | 460 |
post_data = """{"auth":{"passwordCredentials":{"username":"%s"}, |
463 | 461 |
"tenantName":"%s"}}""" % ( |
464 | 462 |
self.user1.uuid, self.user1.uuid) |
... | ... | |
469 | 467 |
startswith('Malformed request')) |
470 | 468 |
|
471 | 469 |
# Check malformed request: missing username |
472 |
url = reverse('astakos.api.tokens.authenticate') |
|
473 | 470 |
post_data = """{"auth":{"passwordCredentials":{"password":"%s"}, |
474 | 471 |
"tenantName":"%s"}}""" % ( |
475 | 472 |
self.user1.auth_token, self.user1.uuid) |
... | ... | |
480 | 477 |
startswith('Malformed request')) |
481 | 478 |
|
482 | 479 |
# Check invalid pass |
483 |
url = reverse('astakos.api.tokens.authenticate') |
|
484 | 480 |
post_data = """{"auth":{"passwordCredentials":{"username":"%s", |
485 | 481 |
"password":"%s"}, |
486 | 482 |
"tenantName":"%s"}}""" % ( |
... | ... | |
492 | 488 |
'Invalid token') |
493 | 489 |
|
494 | 490 |
# Check inconsistent pass |
495 |
url = reverse('astakos.api.tokens.authenticate') |
|
496 | 491 |
post_data = """{"auth":{"passwordCredentials":{"username":"%s", |
497 | 492 |
"password":"%s"}, |
498 | 493 |
"tenantName":"%s"}}""" % ( |
... | ... | |
504 | 499 |
'Invalid credentials') |
505 | 500 |
|
506 | 501 |
# Check invalid json data |
507 |
url = reverse('astakos.api.tokens.authenticate') |
|
508 | 502 |
r = client.post(url, "not json", content_type='application/json') |
509 | 503 |
self.assertEqual(r.status_code, 400) |
510 | 504 |
body = json.loads(r.content) |
511 | 505 |
self.assertEqual(body['badRequest']['message'], 'Invalid JSON data') |
512 | 506 |
|
513 | 507 |
# Check auth with token |
514 |
url = reverse('astakos.api.tokens.authenticate') |
|
515 | 508 |
post_data = """{"auth":{"token": {"id":"%s"}, |
516 | 509 |
"tenantName":"%s"}}""" % ( |
517 | 510 |
self.user1.auth_token, self.user1.uuid) |
... | ... | |
524 | 517 |
self.fail(e) |
525 | 518 |
|
526 | 519 |
# Check malformed request: missing token |
527 |
url = reverse('astakos.api.tokens.authenticate') |
|
528 | 520 |
post_data = """{"auth":{"auth_token":{"id":"%s"}, |
529 | 521 |
"tenantName":"%s"}}""" % ( |
530 | 522 |
self.user1.auth_token, self.user1.uuid) |
... | ... | |
535 | 527 |
startswith('Malformed request')) |
536 | 528 |
|
537 | 529 |
# Check bad request: inconsistent tenant |
538 |
url = reverse('astakos.api.tokens.authenticate') |
|
539 | 530 |
post_data = """{"auth":{"token":{"id":"%s"}, |
540 | 531 |
"tenantName":"%s"}}""" % ( |
541 | 532 |
self.user1.auth_token, self.user2.uuid) |
... | ... | |
546 | 537 |
'Not conforming tenantName') |
547 | 538 |
|
548 | 539 |
# Check bad request: inconsistent tenant |
549 |
url = reverse('astakos.api.tokens.authenticate') |
|
550 | 540 |
post_data = """{"auth":{"token":{"id":"%s"}, |
551 | 541 |
"tenantName":""}}""" % ( |
552 | 542 |
self.user1.auth_token) |
... | ... | |
554 | 544 |
self.assertEqual(r.status_code, 200) |
555 | 545 |
|
556 | 546 |
# Check successful json response |
557 |
url = reverse('astakos.api.tokens.authenticate') |
|
558 | 547 |
post_data = """{"auth":{"passwordCredentials":{"username":"%s", |
559 | 548 |
"password":"%s"}, |
560 | 549 |
"tenantName":"%s"}}""" % ( |
... | ... | |
579 | 568 |
self.assertEqual(len(service_catalog), 3) |
580 | 569 |
|
581 | 570 |
# Check successful xml response |
582 |
url = reverse('astakos.api.tokens.authenticate') |
|
583 | 571 |
headers = {'HTTP_ACCEPT': 'application/xml'} |
584 | 572 |
post_data = """{"auth":{"passwordCredentials":{"username":"%s", |
585 | 573 |
"password":"%s"}, |
... | ... | |
594 | 582 |
# except Exception, e: |
595 | 583 |
# self.fail(e) |
596 | 584 |
|
597 |
# Check belongsTo BadRequest |
|
598 |
url = '/astakos/api/tokens/%s/endpoints?belongsTo=%s' % ( |
|
599 |
quote(self.user1.auth_token), quote(self.user2.uuid)) |
|
600 |
headers = {'HTTP_X_AUTH_TOKEN': self.user1.auth_token} |
|
601 |
r = client.get(url, **headers) |
|
602 |
self.assertEqual(r.status_code, 400) |
|
603 |
|
|
604 |
# Check successful request |
|
605 |
url = '/astakos/api/tokens/%s/endpoints' % quote(self.user1.auth_token) |
|
606 |
headers = {'HTTP_X_AUTH_TOKEN': self.user1.auth_token} |
|
607 |
r = client.get(url, **headers) |
|
608 |
self.assertEqual(r.status_code, 200) |
|
609 |
self.assertEqual(r['Content-Type'], 'application/json; charset=UTF-8') |
|
610 |
try: |
|
611 |
body = json.loads(r.content) |
|
612 |
except: |
|
613 |
self.fail('json format expected') |
|
614 |
endpoints = body.get('endpoints') |
|
615 |
self.assertEqual(len(endpoints), 3) |
|
616 |
|
|
617 |
# Check xml serialization |
|
618 |
url = '/astakos/api/tokens/%s/endpoints?format=xml' %\ |
|
619 |
quote(self.user1.auth_token) |
|
620 |
headers = {'HTTP_X_AUTH_TOKEN': self.user1.auth_token} |
|
621 |
r = client.get(url, **headers) |
|
622 |
self.assertEqual(r.status_code, 200) |
|
623 |
self.assertEqual(r['Content-Type'], 'application/xml; charset=UTF-8') |
|
624 |
# try: |
|
625 |
# body = minidom.parseString(r.content) |
|
626 |
# except Exception, e: |
|
627 |
# self.fail('xml format expected') |
|
628 |
endpoints = body.get('endpoints') |
|
629 |
self.assertEqual(len(endpoints), 3) |
|
630 |
|
|
631 |
# Check limit |
|
632 |
url = '/astakos/api/tokens/%s/endpoints?limit=2' %\ |
|
633 |
quote(self.user1.auth_token) |
|
634 |
headers = {'HTTP_X_AUTH_TOKEN': self.user1.auth_token} |
|
635 |
r = client.get(url, **headers) |
|
636 |
self.assertEqual(r.status_code, 200) |
|
637 |
body = json.loads(r.content) |
|
638 |
endpoints = body.get('endpoints') |
|
639 |
self.assertEqual(len(endpoints), 2) |
|
640 |
|
|
641 |
endpoint_link = body.get('endpoint_links', [])[0] |
|
642 |
next = endpoint_link.get('href') |
|
643 |
p = urlparse(next) |
|
644 |
params = parse_qs(p.query) |
|
645 |
self.assertTrue('limit' in params) |
|
646 |
self.assertTrue('marker' in params) |
|
647 |
self.assertEqual(params['marker'][0], '2') |
|
648 |
|
|
649 |
# Check marker |
|
650 |
headers = {'HTTP_X_AUTH_TOKEN': self.user1.auth_token} |
|
651 |
r = client.get(next, **headers) |
|
652 |
self.assertEqual(r.status_code, 200) |
|
653 |
body = json.loads(r.content) |
|
654 |
endpoints = body.get('endpoints') |
|
655 |
self.assertEqual(len(endpoints), 1) |
|
656 |
|
|
657 | 585 |
|
658 | 586 |
class WrongPathAPITest(TestCase): |
659 | 587 |
def test_catch_wrong_account_paths(self, *args): |
Also available in: Unified diff