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
8 class ViewsTest(TestCase):
10 fixtures = ['cas_users.json', ]
13 self.service = 'http://example.com/'
16 def test_succeessful_login(self):
17 response = self._login_user('root', '123')
18 self._validate_cas1(response, True)
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))
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/'))
28 response = self.client.get(response['location'], follow=False)
29 self.assertIn(response.status_code, [302, 200])
32 def test_logout(self):
33 response = self._login_user('root', '123')
34 self._validate_cas1(response, True)
36 response = self.client.get(reverse('cas_logout'), follow=False)
37 self.assertEqual(response.status_code, 200)
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)
44 def test_broken_pwd(self):
45 self._fail_login('root', '321')
47 def test_broken_username(self):
48 self._fail_login('notroot', '123')
50 def test_nonactive_user_login(self):
51 self._fail_login('nonactive', '123')
54 def _fail_login(self, username, password):
55 response = self._login_user(username, password)
56 self._validate_cas1(response, False)
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)
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'), {
74 'lt': form['lt'].value(),
79 def _validate_cas1(self, response, is_correct=True):
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]
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)
90 self.assertEqual(response.status_code, 200)
91 self.assertEqual(len(response.context['form'].errors), 1)
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')
98 class ModelsTestCase(TestCase):
100 fixtures = ['cas_users.json', ]
103 self.user = User.objects.get(username='root')
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__)