root / aai / tests.py @ 8f377cd6
History | View | Annotate | Download (5.2 kB)
1 | 5fb55fba | Georgios Gousios | #
|
---|---|---|---|
2 | 8f377cd6 | Georgios Gousios | # Unit Tests for aai
|
3 | 5fb55fba | Georgios Gousios | #
|
4 | 8f377cd6 | Georgios Gousios | # Provides automated tests for aai module. The tests
|
5 | 5fb55fba | Georgios Gousios | #
|
6 | 5fb55fba | Georgios Gousios | # Copyright 2011 Greek Research and Technology Network
|
7 | 5fb55fba | Georgios Gousios | #
|
8 | 5fb55fba | Georgios Gousios | |
9 | 5fb55fba | Georgios Gousios | from django.test import TestCase |
10 | 5fb55fba | Georgios Gousios | from django.test.client import Client |
11 | faa26af8 | Georgios Gousios | from django.conf import settings |
12 | 5fb55fba | Georgios Gousios | |
13 | 8f377cd6 | Georgios Gousios | from synnefo.aai.shibboleth import Tokens, NoUniqueToken |
14 | dd53338a | Georgios Gousios | from synnefo.db.models import SynnefoUser |
15 | dd53338a | Georgios Gousios | |
16 | faa26af8 | Georgios Gousios | from datetime import datetime, timedelta |
17 | faa26af8 | Georgios Gousios | |
18 | 5fb55fba | Georgios Gousios | class AuthTestCase(TestCase): |
19 | faa26af8 | Georgios Gousios | fixtures = ['api_test_data', 'auth_test_data'] |
20 | dd53338a | Georgios Gousios | apibase = '/api/v1.1'
|
21 | 5fb55fba | Georgios Gousios | |
22 | 5fb55fba | Georgios Gousios | def setUp(self): |
23 | 5fb55fba | Georgios Gousios | self.client = Client()
|
24 | 5fb55fba | Georgios Gousios | |
25 | 1896d262 | Georgios Gousios | def test_shibboleth_correct_request(self): |
26 | 1896d262 | Georgios Gousios | """test request that should succeed and register a user
|
27 | 420f2c20 | Georgios Gousios | """
|
28 | dd53338a | Georgios Gousios | response = self.client.get(self.apibase + '/servers', {}, |
29 | dd53338a | Georgios Gousios | **{Tokens.SIB_GIVEN_NAME: 'Jimmy',
|
30 | dd53338a | Georgios Gousios | Tokens.SIB_EDU_PERSON_PRINCIPAL_NAME: 'jh@gmail.com',
|
31 | 8f377cd6 | Georgios Gousios | Tokens.SIB_DISPLAY_NAME: 'Jimmy Hendrix',
|
32 | 8f377cd6 | Georgios Gousios | 'TEST-AAI' : 'true'}) |
33 | dd53338a | Georgios Gousios | user = None
|
34 | dd53338a | Georgios Gousios | try:
|
35 | dd53338a | Georgios Gousios | user = SynnefoUser.objects.get(uniq = "jh@gmail.com")
|
36 | dd53338a | Georgios Gousios | except SynnefoUser.DoesNotExist:
|
37 | dd53338a | Georgios Gousios | self.assertNotEqual(user, None) |
38 | dd53338a | Georgios Gousios | self.assertNotEqual(user, None) |
39 | 57e59589 | Georgios Gousios | self.assertEquals(response.status_code, 302) |
40 | 57e59589 | Georgios Gousios | self.assertEquals(response['Location'], "http://testserver/") |
41 | 57e59589 | Georgios Gousios | self.assertTrue('X-Auth-Token' in response) |
42 | 57e59589 | Georgios Gousios | self.assertEquals(response['X-Auth-Token'], user.auth_token) |
43 | 420f2c20 | Georgios Gousios | |
44 | 1896d262 | Georgios Gousios | def test_shibboleth_no_uniq_request(self): |
45 | 1896d262 | Georgios Gousios | """test a request with no unique field
|
46 | 1896d262 | Georgios Gousios | """
|
47 | faa26af8 | Georgios Gousios | response = self.client.get(self.apibase + '/servers', {}, |
48 | faa26af8 | Georgios Gousios | **{Tokens.SIB_GIVEN_NAME: 'Jimmy',
|
49 | 8f377cd6 | Georgios Gousios | Tokens.SIB_DISPLAY_NAME: 'Jimmy Hendrix',
|
50 | 8f377cd6 | Georgios Gousios | 'TEST-AAI' : 'true'}) |
51 | faa26af8 | Georgios Gousios | self._test_redirect(response)
|
52 | 1896d262 | Georgios Gousios | |
53 | 1896d262 | Georgios Gousios | def test_shibboleth_wrong_from_request(self): |
54 | 1896d262 | Georgios Gousios | """ test request from wrong host
|
55 | 1896d262 | Georgios Gousios | """
|
56 | faa26af8 | Georgios Gousios | response = self.client.get(self.apibase + '/servers', {}, |
57 | faa26af8 | Georgios Gousios | **{Tokens.SIB_GIVEN_NAME: 'Jimmy',
|
58 | faa26af8 | Georgios Gousios | Tokens.SIB_EDU_PERSON_PRINCIPAL_NAME: 'jh@gmail.com',
|
59 | faa26af8 | Georgios Gousios | Tokens.SIB_DISPLAY_NAME: 'Jimmy Hendrix',
|
60 | faa26af8 | Georgios Gousios | 'REMOTE_ADDR': '1.2.3.4', |
61 | 8f377cd6 | Georgios Gousios | 'SERVER_NAME': 'nohost.nodomain', |
62 | 8f377cd6 | Georgios Gousios | 'TEST-AAI' : 'true'}) |
63 | faa26af8 | Georgios Gousios | self._test_redirect(response)
|
64 | 1896d262 | Georgios Gousios | |
65 | 71a2be7d | Georgios Gousios | def test_shibboleth_expired_token(self): |
66 | 71a2be7d | Georgios Gousios | """ test request from expired token
|
67 | 71a2be7d | Georgios Gousios | """
|
68 | faa26af8 | Georgios Gousios | user = SynnefoUser.objects.get(uniq = "test@synnefo.gr")
|
69 | faa26af8 | Georgios Gousios | self.assertNotEqual(user.auth_token_created, None) |
70 | 25380811 | Georgios Gousios | self._update_user_ts(user)
|
71 | faa26af8 | Georgios Gousios | response = self.client.get(self.apibase + '/servers', {}, |
72 | 8f377cd6 | Georgios Gousios | **{'X-Auth-Token': user.auth_token,
|
73 | 8f377cd6 | Georgios Gousios | 'TEST-AAI' : 'true'}) |
74 | faa26af8 | Georgios Gousios | self._test_redirect(response)
|
75 | 71a2be7d | Georgios Gousios | |
76 | 57e59589 | Georgios Gousios | def test_shibboleth_redirect(self): |
77 | 57e59589 | Georgios Gousios | """ test redirect to Sibboleth page
|
78 | 1896d262 | Georgios Gousios | """
|
79 | 8f377cd6 | Georgios Gousios | response = self.client.get(self.apibase + '/servers', {}, **{'TEST-AAI' : 'true'}) |
80 | 57e59589 | Georgios Gousios | self._test_redirect(response)
|
81 | 57e59589 | Georgios Gousios | |
82 | 57e59589 | Georgios Gousios | def test_shibboleth_auth(self): |
83 | 57e59589 | Georgios Gousios | """ test authentication with X-Auth-Token
|
84 | 57e59589 | Georgios Gousios | """
|
85 | faa26af8 | Georgios Gousios | user = SynnefoUser.objects.get(uniq = "test@synnefo.gr")
|
86 | 57e59589 | Georgios Gousios | response = self.client.get(self.apibase + '/servers', {}, |
87 | 8f377cd6 | Georgios Gousios | **{'X-Auth-Token': user.auth_token,
|
88 | 8f377cd6 | Georgios Gousios | 'TEST-AAI' : 'true'}) |
89 | 57e59589 | Georgios Gousios | self.assertTrue(response.status_code, 200) |
90 | 57e59589 | Georgios Gousios | self.assertTrue('Vary' in response) |
91 | 57e59589 | Georgios Gousios | self.assertTrue('X-Auth-Token' in response['Vary']) |
92 | 1896d262 | Georgios Gousios | |
93 | 1896d262 | Georgios Gousios | def test_fail_oapi_auth(self): |
94 | 1896d262 | Georgios Gousios | """ test authentication from not registered user using OpenAPI
|
95 | 5fb55fba | Georgios Gousios | """
|
96 | dd53338a | Georgios Gousios | response = self.client.get(self.apibase + '/servers', {}, |
97 | dd53338a | Georgios Gousios | **{'X-Auth-User': 'notme', |
98 | 8f377cd6 | Georgios Gousios | 'X-Auth-Key': '0xdeadbabe', |
99 | 8f377cd6 | Georgios Gousios | 'TEST-AAI' : 'true'}) |
100 | 5fb55fba | Georgios Gousios | self.assertEquals(response.status_code, 401) |
101 | 5fb55fba | Georgios Gousios | |
102 | 1896d262 | Georgios Gousios | def test_oapi_auth(self): |
103 | 1896d262 | Georgios Gousios | """authentication with user registration
|
104 | 1896d262 | Georgios Gousios | """
|
105 | dd53338a | Georgios Gousios | response = self.client.get(self.apibase + '/', {}, |
106 | 25380811 | Georgios Gousios | **{'X-Auth-User': 'testdbuser', |
107 | 8f377cd6 | Georgios Gousios | 'X-Auth-Key': 'test@synnefo.gr', |
108 | 8f377cd6 | Georgios Gousios | 'TEST-AAI' : 'true'}) |
109 | 5fb55fba | Georgios Gousios | self.assertEquals(response.status_code, 204) |
110 | 5fb55fba | Georgios Gousios | self.assertNotEqual(response['X-Auth-Token'], None) |
111 | 5fb55fba | Georgios Gousios | self.assertEquals(response['X-Server-Management-Url'], '') |
112 | 5fb55fba | Georgios Gousios | self.assertEquals(response['X-Storage-Url'], '') |
113 | 5fb55fba | Georgios Gousios | self.assertEquals(response['X-CDN-Management-Url'], '') |
114 | 5fb55fba | Georgios Gousios | |
115 | faa26af8 | Georgios Gousios | def _test_redirect(self, response): |
116 | faa26af8 | Georgios Gousios | self.assertEquals(response.status_code, 302) |
117 | 57e59589 | Georgios Gousios | self.assertTrue('Location' in response) |
118 | 25380811 | Georgios Gousios | self.assertEquals(response['Location'], settings.SHIBBOLETH_HOST) |
119 | 25380811 | Georgios Gousios | |
120 | 25380811 | Georgios Gousios | def _update_user_ts(self, user): |
121 | 25380811 | Georgios Gousios | user.auth_token_created = (datetime.now() - |
122 | 25380811 | Georgios Gousios | timedelta(hours = settings.AUTH_TOKEN_DURATION)) |
123 | 25380811 | Georgios Gousios | user.save() |
124 | 8f377cd6 | Georgios Gousios | |
125 | 8f377cd6 | Georgios Gousios |