1 from cas_provider.models import ServiceTicket
2 from cas_provider.views import _cas2_sucess_response, _cas2_error_response, \
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
10 class ViewsTest(TestCase):
12 fixtures = ['cas_users.json', ]
15 self.service = 'http://example.com/'
18 def test_succeessful_login(self):
19 response = self._login_user('root', '123')
20 self._validate_cas1(response, True)
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))
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/'))
30 response = self.client.get(response['location'], follow=False)
31 self.assertIn(response.status_code, [302, 200])
33 response = self.client.get(reverse('cas_login'), {'service': self.service, 'warn': True}, follow=False)
34 self.assertEqual(response.status_code, 200)
35 self.assertTemplateUsed(response, 'cas/warn.html')
38 def test_logout(self):
39 response = self._login_user('root', '123')
40 self._validate_cas1(response, True)
42 response = self.client.get(reverse('cas_logout'), follow=False)
43 self.assertEqual(response.status_code, 200)
45 response = self.client.get(reverse('cas_login'), follow=False)
46 self.assertEqual(response.status_code, 200)
47 self.assertEqual(response.context['user'].is_anonymous(), True)
50 def test_broken_pwd(self):
51 self._fail_login('root', '321')
53 def test_broken_username(self):
54 self._fail_login('notroot', '123')
56 def test_nonactive_user_login(self):
57 self._fail_login('nonactive', '123')
59 def test_cas2_success_validate(self):
60 response = self._login_user('root', '123')
61 self._validate_cas2(response, True)
63 def test_cas2_fail_validate(self):
64 for user, pwd in (('root', '321'), ('notroot', '123'), ('nonactive', '123')):
65 response = self._login_user(user, pwd)
66 self._validate_cas2(response, False)
69 def _fail_login(self, username, password):
70 response = self._login_user(username, password)
71 self._validate_cas1(response, False)
73 response = self.client.get(reverse('cas_login'), {'service': self.service}, follow=False)
74 self.assertEqual(response.status_code, 200)
75 response = self.client.get(reverse('cas_login'), follow=False)
76 self.assertEqual(response.status_code, 200)
80 def _login_user(self, username, password):
81 self.username = username
82 response = self.client.get(reverse('cas_login'), {'service': self.service})
83 self.assertEqual(response.status_code, 200)
84 self.assertTemplateUsed(response, 'cas/login.html')
85 form = response.context['form']
86 service = form['service'].value()
87 return self.client.post(reverse('cas_login'), {
90 'lt': form['lt'].value(),
95 def _validate_cas1(self, response, is_correct=True):
97 self.assertEqual(response.status_code, 302)
98 self.assertTrue(response.has_header('location'))
99 location = urlparse(response['location'])
100 ticket = location.query.split('=')[1]
102 response = self.client.get(reverse('cas_validate'), {'ticket': ticket, 'service': self.service}, follow=False)
103 self.assertEqual(response.status_code, 200)
104 self.assertEqual(unicode(response.content), u'yes\n%s\n' % self.username)
106 self.assertEqual(response.status_code, 200)
107 self.assertEqual(len(response.context['form'].errors), 1)
109 response = self.client.get(reverse('cas_validate'), {'ticket': 'ST-12312312312312312312312', 'service': self.service}, follow=False)
110 self.assertEqual(response.status_code, 200)
111 self.assertEqual(response.content, u'no\n\n')
114 def _validate_cas2(self, response, is_correct=True):
116 self.assertEqual(response.status_code, 302)
117 self.assertTrue(response.has_header('location'))
118 location = urlparse(response['location'])
119 ticket = location.query.split('=')[1]
121 response = self.client.get(reverse('cas_service_validate'), {'ticket': ticket, 'service': self.service}, follow=False)
122 self.assertEqual(response.status_code, 200)
123 self.assertEqual(response.content, _cas2_sucess_response(self.username).content)
125 self.assertEqual(response.status_code, 200)
126 self.assertEqual(len(response.context['form'].errors), 1)
128 response = self.client.get(reverse('cas_service_validate'), {'ticket': 'ST-12312312312312312312312', 'service': self.service}, follow=False)
129 self.assertEqual(response.status_code, 200)
130 self.assertEqual(response.content, _cas2_error_response(INVALID_TICKET).content)
133 class ModelsTestCase(TestCase):
135 fixtures = ['cas_users.json', ]
138 self.user = User.objects.get(username='root')
140 def test_redirects(self):
141 ticket = ServiceTicket.objects.create(service='http://example.com', user=self.user)
142 self.assertEqual(ticket.get_redirect_url(), '%(service)s?ticket=%(ticket)s' % ticket.__dict__)