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