Statistics
| Branch: | Tag: | Revision:

root / api / tests_auth.py @ faa26af8

History | View | Annotate | Download (4.4 kB)

1
#
2
# Unit Tests for api
3
#
4
# Provides automated tests for api module
5
#
6
# Copyright 2011 Greek Research and Technology Network
7
#
8

    
9
from django.test import TestCase
10
from django.test.client import Client
11
from django.conf import settings
12

    
13
from synnefo.logic.shibboleth import Tokens, NoUniqueToken
14
from synnefo.db.models import SynnefoUser
15

    
16
from datetime import datetime, timedelta
17

    
18
class AuthTestCase(TestCase):
19
    fixtures = ['api_test_data', 'auth_test_data']
20
    apibase = '/api/v1.1'
21

    
22
    def setUp(self):
23
        self.client = Client()
24

    
25
    def test_shibboleth_correct_request(self):
26
        """test request that should succeed and register a user
27
        """
28
        response = self.client.get(self.apibase + '/servers', {},
29
                                   **{Tokens.SIB_GIVEN_NAME: 'Jimmy',
30
                                      Tokens.SIB_EDU_PERSON_PRINCIPAL_NAME: 'jh@gmail.com',
31
                                      Tokens.SIB_DISPLAY_NAME: 'Jimmy Hendrix'})
32
        user = None
33
        try:
34
            user = SynnefoUser.objects.get(uniq = "jh@gmail.com")
35
        except SynnefoUser.DoesNotExist:
36
            self.assertNotEqual(user, None)
37
        self.assertNotEqual(user, None)
38
        self.assertTrue('X-Auth-Token' in response.META)
39
        self.assertTrue(len(response['X-Auth-Token']))
40

    
41
    def test_shibboleth_no_uniq_request(self):
42
        """test a request with no unique field
43
        """
44
        response = self.client.get(self.apibase + '/servers', {},
45
                                    **{Tokens.SIB_GIVEN_NAME: 'Jimmy',
46
                                    Tokens.SIB_DISPLAY_NAME: 'Jimmy Hendrix'})
47
        self._test_redirect(response)
48

    
49
    def test_shibboleth_wrong_from_request(self):
50
        """ test request from wrong host
51
        """
52
        response = self.client.get(self.apibase + '/servers', {},
53
                                   **{Tokens.SIB_GIVEN_NAME: 'Jimmy',
54
                                      Tokens.SIB_EDU_PERSON_PRINCIPAL_NAME: 'jh@gmail.com',
55
                                      Tokens.SIB_DISPLAY_NAME: 'Jimmy Hendrix',
56
                                      'REMOTE_ADDR': '1.2.3.4',
57
                                      'SERVER_NAME': 'nohost.nodomain'})
58
        self._test_redirect(response)
59

    
60
    def test_shibboleth_expired_token(self):
61
        """ test request from expired token
62
        """
63
        user = SynnefoUser.objects.get(uniq = "test@synnefo.gr")
64
        self.assertNotEqual(user.auth_token_created, None)
65
        user.auth_token_created = (datetime.now() -
66
                                   timedelta(hours = settings.AUTH_TOKEN_DURATION))
67
        user.save()
68
        response = self.client.get(self.apibase + '/servers', {},
69
                                   **{'X-Auth-Token': user.auth_token})
70
        self._test_redirect(response)
71

    
72
    def test_shibboleth_auth(self):
73
        """ test redirect to shibboleth page
74
        """
75
        response = self.client.get(self.apibase + '/servers')
76
        user = SynnefoUser.objects.get(uniq = "test@synnefo.gr")
77
        self.assertTrue('X-Auth-Token' in response.META)
78

    
79
    def test_fail_oapi_auth(self):
80
        """ test authentication from not registered user using OpenAPI
81
        """
82
        response = self.client.get(self.apibase + '/servers', {},
83
                                   **{'X-Auth-User': 'notme',
84
                                      'X-Auth-Key': '0xdeadbabe'})
85
        self.assertEquals(response.status_code, 401)
86

    
87
    def test_oapi_auth(self):
88
        """authentication with user registration
89
        """
90
        response = self.client.get(self.apibase + '/', {},
91
                                   **{'X-Auth-User': 'testuser',
92
                                      'X-Auth-Key': 'testuserpasswd'})
93
        self.assertEquals(response.status_code, 204)
94
        self.assertNotEqual(response['X-Auth-Token'], None)
95
        self.assertEquals(response['X-Server-Management-Url'], '')
96
        self.assertEquals(response['X-Storage-Url'], '')
97
        self.assertEquals(response['X-CDN-Management-Url'], '')
98

    
99
        #Check access now that we do have an auth token
100
        token = response['X-Auth-Token']
101
        response = self.client.get(self.apibase + '/servers/detail', {},
102
                                   **{'X-Auth-Token': token})
103
        self.assertEquals(response.status_code, 200)
104

    
105
    def _test_redirect(self, response):
106
        self.assertEquals(response.status_code, 302)
107
        self.assertEquals('Location' in response.META)
108
        self.assertEquals(response['Location'], settings.SHIBBOLETH_HOST)