summaryrefslogtreecommitdiffstats
path: root/authentic2_idp_cas/tests.py
blob: c1750f33111701976ec6eb76e8f2c0a543fcd1da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from xml.etree import ElementTree as ET


from django.test import TestCase
from django.test.client import RequestFactory


from authentic2.compat import get_user_model
from .models import CasTicket
from . import views
from . import constants


class CasTests(TestCase):
    LOGIN = 'test'
    PASSWORD = 'test'

    def setUp(self):
        User = get_user_model()
        self.user = User.objects.create_user(self.LOGIN, password=self.PASSWORD)
        self.factory = RequestFactory()

    def test_service_validate_with_default_attributes(self):
        CasTicket.objects.create(
                ticket_id='ST-xxx',
                service='yyy',
                user=self.user,
                validity=True)
        request = self.factory.get('/idp/cas/serviceValidate',
                {'service': 'yyy', 'ticket': 'ST-xxx'})
        class TestCasProvider(views.CasProvider):
            def get_attributes(self, request, st):
                assert st.service == 'yyy'
                assert st.ticket_id == 'ST-xxx'
                return { 'username': 'bob', 'email': 'bob@example.com' }, 'default'
        provider = TestCasProvider()
        response = provider.service_validate(request)
        print response.content
        root = ET.fromstring(response.content)
        ns_ctx = { 'cas': constants.CAS_NAMESPACE }
        user_elt = root.find('cas:authenticationSuccess/cas:utilisateur', namespaces=ns_ctx)
        assert user_elt is not None

    def test_service_validate_with_custom_attributes(self):
        CasTicket.objects.create(
                ticket_id='ST-xxx',
                service='yyy',
                user=self.user,
                validity=True)
        request = self.factory.get('/idp/cas/serviceValidate',
                {'service': 'yyy', 'ticket': 'ST-xxx'})
        class TestCasProvider(views.CasProvider):
            def get_attributes(self, request, st):
                assert st.service == 'yyy'
                assert st.ticket_id == 'ST-xxx'
                return { 'username': 'bob', 'email': 'bob@example.com' }, 'utilisateur'
        provider = TestCasProvider()
        response = provider.service_validate(request)
        print response.content