1 from django.core.urlresolvers import reverse
2 from django.test import TestCase
3 from urlparse import urlparse
6 class UserTest(TestCase):
8 fixtures = ['cas_users.json', ]
11 self.service = 'http://example.com/'
14 def test_succeessful_login(self):
15 response = self._login_user('root', '123')
16 self._validate_cas1(response, True)
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))
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/'))
26 response = self.client.get(response['location'], follow=False)
27 self.assertIn(response.status_code, [302, 200])
30 def test_logout(self):
31 response = self._login_user('root', '123')
32 self._validate_cas1(response, True)
34 response = self.client.get(reverse('cas_logout'), follow=False)
35 self.assertEqual(response.status_code, 200)
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)
42 def test_broken_pwd(self):
43 self._fail_login('root', '321')
45 def test_broken_username(self):
46 self._fail_login('notroot', '123')
48 def test_nonactive_user_login(self):
49 self._fail_login('nonactive', '123')
52 def _fail_login(self, username, password):
53 response = self._login_user(username, password)
54 self._validate_cas1(response, False)
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)
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'), {
72 'lt': form['lt'].value(),
77 def _validate_cas1(self, response, is_correct=True):
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]
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')
88 self.assertEqual(response.status_code, 200)
89 self.assertGreater(len(response.context['errors']), 0)
90 self.assertEqual(len(response.context['form'].errors), 0)