4d4424243d99e7cc86eef98ef4d9635a454bf13f
[django-cas-provider.git] / cas_provider / tests.py
1 from cas_provider.models import ServiceTicket
2 from cas_provider.views import _cas2_sucess_response, _cas2_error_response, \
3     INVALID_TICKET
4 from django.contrib.auth.models import User
5 from django.core.urlresolvers import reverse
6 from django.test import TestCase
7 from urlparse import urlparse
8
9
10 class ViewsTest(TestCase):
11
12     fixtures = ['cas_users.json', ]
13
14     def setUp(self):
15         self.service = 'http://example.com/'
16
17
18     def test_succeessful_login(self):
19         response = self._login_user('root', '123')
20         self._validate_cas1(response, True)
21
22         response = self.client.get(reverse('cas_login'), {'service': self.service}, follow=False)
23         self.assertEqual(response.status_code, 302)
24         self.assertTrue(response['location'].startswith('%s?ticket=' % self.service))
25
26         response = self.client.get(reverse('cas_login'), follow=False)
27         self.assertEqual(response.status_code, 302)
28         self.assertTrue(response['location'].startswith('http://testserver/'))
29
30         response = self.client.get(response['location'], follow=False)
31         self.assertIn(response.status_code, [302, 200])
32
33
34     def test_logout(self):
35         response = self._login_user('root', '123')
36         self._validate_cas1(response, True)
37
38         response = self.client.get(reverse('cas_logout'), follow=False)
39         self.assertEqual(response.status_code, 200)
40
41         response = self.client.get(reverse('cas_login'), follow=False)
42         self.assertEqual(response.status_code, 200)
43         self.assertEqual(response.context['user'].is_anonymous(), True)
44
45
46     def test_broken_pwd(self):
47         self._fail_login('root', '321')
48
49     def test_broken_username(self):
50         self._fail_login('notroot', '123')
51
52     def test_nonactive_user_login(self):
53         self._fail_login('nonactive', '123')
54
55     def test_cas2_success_validate(self):
56         response = self._login_user('root', '123')
57         self._validate_cas2(response, True)
58
59     def test_cas2_fail_validate(self):
60         for user, pwd in (('root', '321'), ('notroot', '123'), ('nonactive', '123')):
61             response = self._login_user(user, pwd)
62             self._validate_cas2(response, False)
63
64
65     def _fail_login(self, username, password):
66         response = self._login_user(username, password)
67         self._validate_cas1(response, False)
68
69         response = self.client.get(reverse('cas_login'), {'service': self.service}, follow=False)
70         self.assertEqual(response.status_code, 200)
71         response = self.client.get(reverse('cas_login'), follow=False)
72         self.assertEqual(response.status_code, 200)
73
74
75
76     def _login_user(self, username, password):
77         self.username = username
78         response = self.client.get(reverse('cas_login'), {'service': self.service})
79         self.assertEqual(response.status_code, 200)
80         form = response.context['form']
81         service = form['service'].value()
82         return self.client.post(reverse('cas_login'), {
83             'username': username,
84             'password': password,
85             'lt': form['lt'].value(),
86             'service': service
87         }, follow=False)
88
89
90     def _validate_cas1(self, response, is_correct=True):
91         if is_correct:
92             self.assertEqual(response.status_code, 302)
93             self.assertTrue(response.has_header('location'))
94             location = urlparse(response['location'])
95             ticket = location.query.split('=')[1]
96
97             response = self.client.get(reverse('cas_validate'), {'ticket': ticket, 'service': self.service}, follow=False)
98             self.assertEqual(response.status_code, 200)
99             self.assertEqual(unicode(response.content), u'yes\n%s\n' % self.username)
100         else:
101             self.assertEqual(response.status_code, 200)
102             self.assertEqual(len(response.context['form'].errors), 1)
103
104             response = self.client.get(reverse('cas_validate'), {'ticket': 'ST-12312312312312312312312', 'service': self.service}, follow=False)
105             self.assertEqual(response.status_code, 200)
106             self.assertEqual(response.content, u'no\n\n')
107
108
109     def _validate_cas2(self, response, is_correct=True):
110         if is_correct:
111             self.assertEqual(response.status_code, 302)
112             self.assertTrue(response.has_header('location'))
113             location = urlparse(response['location'])
114             ticket = location.query.split('=')[1]
115
116             response = self.client.get(reverse('cas_service_validate'), {'ticket': ticket, 'service': self.service}, follow=False)
117             self.assertEqual(response.status_code, 200)
118             self.assertEqual(response.content, _cas2_sucess_response(self.username).content)
119         else:
120             self.assertEqual(response.status_code, 200)
121             self.assertEqual(len(response.context['form'].errors), 1)
122
123             response = self.client.get(reverse('cas_service_validate'), {'ticket': 'ST-12312312312312312312312', 'service': self.service}, follow=False)
124             self.assertEqual(response.status_code, 200)
125             self.assertEqual(response.content, _cas2_error_response(INVALID_TICKET).content)
126
127
128 class ModelsTestCase(TestCase):
129
130     fixtures = ['cas_users.json', ]
131
132     def setUp(self):
133         self.user = User.objects.get(username='root')
134
135     def test_redirects(self):
136         ticket = ServiceTicket.objects.create(service='http://example.com', user=self.user)
137         self.assertEqual(ticket.get_redirect_url(), '%(service)s?ticket=%(ticket)s' % ticket.__dict__)
138