Statistics
| Branch: | Tag: | Revision:

root / api / tests_auth.py @ dd53338a

History | View | Annotate | Download (2.5 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

    
12
from synnefo.logic.shibboleth import Tokens
13
from synnefo.db.models import SynnefoUser
14

    
15
class AuthTestCase(TestCase):
16
    fixtures = ['api_test_data']
17
    apibase = '/api/v1.1'
18

    
19
    def setUp(self):
20
        self.client = Client()
21

    
22
    def test_auth_shibboleth(self):
23
        """ test redirect to shibboleth page
24
        """
25
        response = self.client.get(self.apibase + '/servers')
26
        self.assertEquals(response.status_code, 302)
27

    
28
    def test_register_shibboleth_user(self):
29
        """ test registration of sibboleth user upon new incoming request
30
        """
31
        response = self.client.get(self.apibase + '/servers', {},
32
                                   **{Tokens.SIB_GIVEN_NAME: 'Jimmy',
33
                                      Tokens.SIB_EDU_PERSON_PRINCIPAL_NAME: 'jh@gmail.com',
34
                                      Tokens.SIB_DISPLAY_NAME: 'Jimmy Hendrix'})
35

    
36
        user = None
37
        try:
38
            user = SynnefoUser.objects.get(uniq = "jh@gmail.com")
39
        except SynnefoUser.DoesNotExist:
40
            self.assertNotEqual(user, None)
41
        self.assertNotEqual(user, None)
42

    
43
    def test_auth_headers(self):
44
        """ test whether the authentication mechanism sets the correct headers
45
        """
46
        #Check with non-existing user
47
        response = self.client.get(self.apibase + '/servers', {},
48
                                   **{'X-Auth-User': 'notme',
49
                                      'X-Auth-Key': '0xdeadbabe'})
50
        self.assertEquals(response.status_code, 401)
51

    
52
        #Check with existing user
53
        response = self.client.get(self.apibase + '/', {},
54
                                   **{'X-Auth-User': 'testuser',
55
                                      'X-Auth-Key': 'testuserpasswd'})
56
        self.assertEquals(response.status_code, 204)
57
        self.assertNotEqual(response['X-Auth-Token'], None)
58
        self.assertEquals(response['X-Server-Management-Url'], '')
59
        self.assertEquals(response['X-Storage-Url'], '')
60
        self.assertEquals(response['X-CDN-Management-Url'], '')
61

    
62
        #Check access now that we do have an auth token
63
        token = response['X-Auth-Token']
64
        response = self.client.get(self.apibase + '/servers/detail', {},
65
                                   **{'X-Auth-Token': token})
66
        self.assertEquals(response.status_code, 200)