Revision faa26af8 api/tests_auth.py

b/api/tests_auth.py
8 8

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

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

  
16
from datetime import datetime, timedelta
17

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

  
19 22
    def setUp(self):
......
32 35
        except SynnefoUser.DoesNotExist:
33 36
            self.assertNotEqual(user, None)
34 37
        self.assertNotEqual(user, None)
38
        self.assertTrue('X-Auth-Token' in response.META)
39
        self.assertTrue(len(response['X-Auth-Token']))
35 40

  
36 41
    def test_shibboleth_no_uniq_request(self):
37 42
        """test a request with no unique field
38 43
        """
39
        try :
40
            response = self.client.get(self.apibase + '/servers', {},
41
                                   **{Tokens.SIB_GIVEN_NAME: 'Jimmy',
42
                                      Tokens.SIB_DISPLAY_NAME: 'Jimmy Hendrix'})
43
            self.assertEqual(True, True)
44
        except NoUniqueToken:
45
            self.assertEqual(True, True)
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)
46 48

  
47 49
    def test_shibboleth_wrong_from_request(self):
48 50
        """ test request from wrong host
49 51
        """
50
        #TODO: Test request from wrong host
51
        #self.client
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'})
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)
56 59

  
57 60
    def test_shibboleth_expired_token(self):
58 61
        """ test request from expired token
59 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)
60 71

  
61
        #response = self.client.get(self.apibase + '/servers', {},
62
        #                           **{Tokens.SIB_GIVEN_NAME: 'Jimmy',
63
        #                              Tokens.SIB_EDU_PERSON_PRINCIPAL_NAME: 'jh@gmail.com',
64
        #                              Tokens.SIB_DISPLAY_NAME: 'Jimmy Hendrix'})
65

  
66
    def test_auth_shibboleth(self):
72
    def test_shibboleth_auth(self):
67 73
        """ test redirect to shibboleth page
68 74
        """
69 75
        response = self.client.get(self.apibase + '/servers')
70
        self.assertEquals(response.status_code, 302)
76
        user = SynnefoUser.objects.get(uniq = "test@synnefo.gr")
77
        self.assertTrue('X-Auth-Token' in response.META)
71 78

  
72 79
    def test_fail_oapi_auth(self):
73 80
        """ test authentication from not registered user using OpenAPI
......
94 101
        response = self.client.get(self.apibase + '/servers/detail', {},
95 102
                                   **{'X-Auth-Token': token})
96 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)

Also available in: Unified diff