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