Statistics
| Branch: | Tag: | Revision:

root / aai / tests.py @ 96b635d9

History | View | Annotate | Download (5.2 kB)

1 48130e66 Georgios Gousios
# Copyright 2011 GRNET S.A. All rights reserved.
2 5fb55fba Georgios Gousios
#
3 48130e66 Georgios Gousios
# Redistribution and use in source and binary forms, with or without
4 48130e66 Georgios Gousios
# modification, are permitted provided that the following conditions
5 48130e66 Georgios Gousios
# are met:
6 5fb55fba Georgios Gousios
#
7 48130e66 Georgios Gousios
#   1. Redistributions of source code must retain the above copyright
8 48130e66 Georgios Gousios
#      notice, this list of conditions and the following disclaimer.
9 48130e66 Georgios Gousios
#
10 48130e66 Georgios Gousios
#  2. Redistributions in binary form must reproduce the above copyright
11 48130e66 Georgios Gousios
#     notice, this list of conditions and the following disclaimer in the
12 48130e66 Georgios Gousios
#     documentation and/or other materials provided with the distribution.
13 5fb55fba Georgios Gousios
#
14 48130e66 Georgios Gousios
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15 48130e66 Georgios Gousios
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 48130e66 Georgios Gousios
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 48130e66 Georgios Gousios
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18 48130e66 Georgios Gousios
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 48130e66 Georgios Gousios
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 48130e66 Georgios Gousios
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 48130e66 Georgios Gousios
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 48130e66 Georgios Gousios
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 48130e66 Georgios Gousios
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 48130e66 Georgios Gousios
# SUCH DAMAGE.
25 5fb55fba Georgios Gousios
#
26 48130e66 Georgios Gousios
# The views and conclusions contained in the software and documentation are
27 48130e66 Georgios Gousios
# those of the authors and should not be interpreted as representing official
28 48130e66 Georgios Gousios
# policies, either expressed or implied, of GRNET S.A.
29 48130e66 Georgios Gousios
30 48130e66 Georgios Gousios
# Provides automated tests for aai module. The tests
31 5fb55fba Georgios Gousios
32 5fb55fba Georgios Gousios
from django.test import TestCase
33 5fb55fba Georgios Gousios
from django.test.client import Client
34 faa26af8 Georgios Gousios
from django.conf import settings
35 5fb55fba Georgios Gousios
36 dd53338a Georgios Gousios
from synnefo.db.models import SynnefoUser
37 dd53338a Georgios Gousios
38 faa26af8 Georgios Gousios
from datetime import datetime, timedelta
39 faa26af8 Georgios Gousios
40 2cd99e7a Georgios Gousios
from synnefo.aai.shibboleth import Tokens
41 2cd99e7a Georgios Gousios
42 1428a464 Georgios Gousios
43 1428a464 Georgios Gousios
class AaiTestCase(TestCase):
44 faa26af8 Georgios Gousios
    fixtures = ['api_test_data', 'auth_test_data']
45 dd53338a Georgios Gousios
    apibase = '/api/v1.1'
46 5fb55fba Georgios Gousios
47 5fb55fba Georgios Gousios
    def setUp(self):
48 5fb55fba Georgios Gousios
        self.client = Client()
49 5fb55fba Georgios Gousios
50 1896d262 Georgios Gousios
    def test_shibboleth_correct_request(self):
51 1896d262 Georgios Gousios
        """test request that should succeed and register a user
52 420f2c20 Georgios Gousios
        """
53 1428a464 Georgios Gousios
        response = self.client.get('/index.html', {},
54 fdc10aee Faidon Liambotis
                                   **{Tokens.SHIB_NAME: 'Jimmy',
55 fdc10aee Faidon Liambotis
                                      Tokens.SHIB_EPPN: 'jh@gmail.com',
56 fdc10aee Faidon Liambotis
                                      Tokens.SHIB_CN: 'Jimmy Hendrix',
57 fdc10aee Faidon Liambotis
                                      Tokens.SHIB_SESSION_ID: '123321',
58 8f377cd6 Georgios Gousios
                                      'TEST-AAI' : 'true'})
59 dd53338a Georgios Gousios
        user = None
60 dd53338a Georgios Gousios
        try:
61 dd53338a Georgios Gousios
            user = SynnefoUser.objects.get(uniq = "jh@gmail.com")
62 dd53338a Georgios Gousios
        except SynnefoUser.DoesNotExist:
63 dd53338a Georgios Gousios
            self.assertNotEqual(user, None)
64 dd53338a Georgios Gousios
        self.assertNotEqual(user, None)
65 57e59589 Georgios Gousios
        self.assertEquals(response.status_code, 302)
66 ac3c3a4b Georgios Gousios
        self.assertEquals(response['Location'], settings.APP_INSTALL_URL)
67 57e59589 Georgios Gousios
        self.assertTrue('X-Auth-Token' in response)
68 57e59589 Georgios Gousios
        self.assertEquals(response['X-Auth-Token'], user.auth_token)
69 c06de383 Georgios Gousios
        #self.assertNotEquals(response.cookies['X-Auth-Token'].find(user.auth_token), -1)
70 420f2c20 Georgios Gousios
71 1896d262 Georgios Gousios
    def test_shibboleth_no_uniq_request(self):
72 1896d262 Georgios Gousios
        """test a request with no unique field
73 1896d262 Georgios Gousios
        """
74 1428a464 Georgios Gousios
        response = self.client.get('/index.html', {},
75 fdc10aee Faidon Liambotis
                               **{Tokens.SHIB_NAME: 'Jimmy',
76 fdc10aee Faidon Liambotis
                                  Tokens.SHIB_CN: 'Jimmy Hendrix',
77 1428a464 Georgios Gousios
                                  'TEST-AAI': 'true'})
78 faa26af8 Georgios Gousios
        self._test_redirect(response)
79 1896d262 Georgios Gousios
80 71a2be7d Georgios Gousios
    def test_shibboleth_expired_token(self):
81 71a2be7d Georgios Gousios
        """ test request from expired token
82 71a2be7d Georgios Gousios
        """
83 1428a464 Georgios Gousios
        user = SynnefoUser.objects.get(uniq="test@synnefo.gr")
84 c9fdfa27 Georgios Gousios
        self.assertNotEqual(user.auth_token_expires, None)
85 c9fdfa27 Georgios Gousios
        user.auth_token_expires = datetime.now()
86 c9fdfa27 Georgios Gousios
        user.save()
87 1428a464 Georgios Gousios
        response = self.client.get('/index.html', {},
88 1428a464 Georgios Gousios
                               **{'X-Auth-Token': user.auth_token,
89 1428a464 Georgios Gousios
                                  'TEST-AAI': 'true'})
90 faa26af8 Georgios Gousios
        self._test_redirect(response)
91 71a2be7d Georgios Gousios
92 57e59589 Georgios Gousios
    def test_shibboleth_redirect(self):
93 57e59589 Georgios Gousios
        """ test redirect to Sibboleth page
94 1896d262 Georgios Gousios
        """
95 1428a464 Georgios Gousios
        response = self.client.get('/index.html', {}, **{'TEST-AAI': 'true'})
96 57e59589 Georgios Gousios
        self._test_redirect(response)
97 57e59589 Georgios Gousios
98 57e59589 Georgios Gousios
    def test_shibboleth_auth(self):
99 57e59589 Georgios Gousios
        """ test authentication with X-Auth-Token
100 57e59589 Georgios Gousios
        """
101 1428a464 Georgios Gousios
        user = SynnefoUser.objects.get(uniq="test@synnefo.gr")
102 1428a464 Georgios Gousios
        response = self.client.get('/index.html', {},
103 1428a464 Georgios Gousios
                               **{'X-Auth-Token': user.auth_token,
104 1428a464 Georgios Gousios
                                  'TEST-AAI': 'true'})
105 57e59589 Georgios Gousios
        self.assertTrue(response.status_code, 200)
106 57e59589 Georgios Gousios
        self.assertTrue('Vary' in response)
107 57e59589 Georgios Gousios
        self.assertTrue('X-Auth-Token' in response['Vary'])
108 1896d262 Georgios Gousios
109 c9fdfa27 Georgios Gousios
    def test_auth_cookie(self):
110 c9fdfa27 Georgios Gousios
        user = SynnefoUser.objects.get(uniq = "test@synnefo.gr")
111 c9fdfa27 Georgios Gousios
        self.client.cookies['X-Auth-Token'] = user.auth_token
112 c9fdfa27 Georgios Gousios
        response = self.client.get('/', {},
113 c9fdfa27 Georgios Gousios
                                   **{'X-Auth-Token': user.auth_token,
114 c9fdfa27 Georgios Gousios
                                      'TEST-AAI' : 'true'})
115 c9fdfa27 Georgios Gousios
        self.assertTrue(response.status_code, 200)
116 c9fdfa27 Georgios Gousios
        self.assertTrue('Vary' in response)
117 c9fdfa27 Georgios Gousios
        self.assertTrue('X-Auth-Token' in response['Vary'])
118 c9fdfa27 Georgios Gousios
119 faa26af8 Georgios Gousios
    def _test_redirect(self, response):
120 faa26af8 Georgios Gousios
        self.assertEquals(response.status_code, 302)
121 57e59589 Georgios Gousios
        self.assertTrue('Location' in response)
122 79d8af70 Georgios Gousios
        self.assertTrue(response['Location'].startswith(settings.LOGIN_URL))