Statistics
| Branch: | Tag: | Revision:

root / aai / tests.py @ 53481544

History | View | Annotate | Download (5.3 kB)

1
# Copyright 2011 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or without
4
# modification, are permitted provided that the following conditions
5
# are met:
6
#
7
#   1. Redistributions of source code must retain the above copyright
8
#      notice, this list of conditions and the following disclaimer.
9
#
10
#  2. Redistributions in binary form must reproduce the above copyright
11
#     notice, this list of conditions and the following disclaimer in the
12
#     documentation and/or other materials provided with the distribution.
13
#
14
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
# SUCH DAMAGE.
25
#
26
# The views and conclusions contained in the software and documentation are
27
# those of the authors and should not be interpreted as representing official
28
# policies, either expressed or implied, of GRNET S.A.
29

    
30
# Provides automated tests for aai module. The tests
31

    
32
from django.test import TestCase
33
from django.test.client import Client
34
from django.conf import settings
35

    
36
from synnefo.db.models import SynnefoUser
37

    
38
from datetime import datetime, timedelta
39

    
40
from synnefo.aai.shibboleth import Tokens
41

    
42

    
43
class AaiTestCase(TestCase):
44
    fixtures = ['users', 'api_test_data', 'auth_test_data']
45
    apibase = '/api/v1.1'
46

    
47
    def setUp(self):
48
        self.client = Client()
49

    
50
    def test_shibboleth_correct_request(self):
51
        """test request that should succeed and register a user
52
        """
53
        response = self.client.get('/index.html', {},
54
                                   **{Tokens.SHIB_NAME: 'Jimmy',
55
                                      Tokens.SHIB_EPPN: 'jh@gmail.com',
56
                                      Tokens.SHIB_CN: 'Jimmy Hendrix',
57
                                      Tokens.SHIB_SESSION_ID: '123321',
58
                                      'TEST-AAI' : 'true'})
59
        user = None
60
        try:
61
            user = SynnefoUser.objects.get(uniq = "jh@gmail.com")
62
        except SynnefoUser.DoesNotExist:
63
            self.assertNotEqual(user, None)
64
        self.assertNotEqual(user, None)
65
        self.assertEquals(response.status_code, 302)
66
        self.assertEquals(response['Location'], settings.APP_INSTALL_URL)
67
        self.assertTrue('X-Auth-Token' in response)
68
        self.assertEquals(response['X-Auth-Token'], user.auth_token)
69
        #self.assertNotEquals(response.cookies['X-Auth-Token'].find(user.auth_token), -1)
70

    
71
    def test_shibboleth_no_uniq_request(self):
72
        """test a request with no unique field
73
        """
74
        response = self.client.get('/index.html', {},
75
                               **{Tokens.SHIB_NAME: 'Jimmy',
76
                                  Tokens.SHIB_CN: 'Jimmy Hendrix',
77
                                  'TEST-AAI': 'true'})
78
        self._test_redirect(response)
79

    
80
    def test_shibboleth_expired_token(self):
81
        """ test request from expired token
82
        """
83
        user = SynnefoUser.objects.get(uniq="test@synnefo.gr")
84
        self.assertNotEqual(user.auth_token_expires, None)
85
        user.auth_token_expires = datetime.now()
86
        user.save()
87
        response = self.client.get('/index.html', {},
88
                               **{'X-Auth-Token': user.auth_token,
89
                                  'TEST-AAI': 'true'})
90
        self._test_redirect(response)
91

    
92
    def test_shibboleth_redirect(self):
93
        """ test redirect to Sibboleth page
94
        """
95
        response = self.client.get('/index.html', {}, **{'TEST-AAI': 'true'})
96
        self._test_redirect(response)
97

    
98
    def test_shibboleth_auth(self):
99
        """ test authentication with X-Auth-Token
100
        """
101
        user = SynnefoUser.objects.get(uniq="test@synnefo.gr")
102
        response = self.client.get('/index.html', {},
103
                               **{'X-Auth-Token': user.auth_token,
104
                                  'TEST-AAI': 'true'})
105
        self.assertTrue(response.status_code, 200)
106
        self.assertTrue('Vary' in response)
107
        self.assertTrue('X-Auth-Token' in response['Vary'])
108

    
109
    def test_auth_cookie(self):
110
        user = SynnefoUser.objects.get(uniq = "test@synnefo.gr")
111
        self.client.cookies['X-Auth-Token'] = user.auth_token
112
        response = self.client.get('/', {},
113
                                   **{'X-Auth-Token': user.auth_token,
114
                                      'TEST-AAI' : 'true'})
115
        self.assertTrue(response.status_code, 200)
116
        self.assertTrue('Vary' in response)
117
        self.assertTrue('X-Auth-Token' in response['Vary'])
118

    
119
    def _test_redirect(self, response):
120
        self.assertEquals(response.status_code, 302)
121
        self.assertTrue('Location' in response)
122
        self.assertTrue(response['Location'].startswith(settings.LOGIN_URL))
123