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])
34 def test_logout(self):
35 response = self._login_user('root', '123')
36 self._validate_cas1(response, True)
38 response = self.client.get(reverse('cas_logout'), follow=False)
39 self.assertEqual(response.status_code, 200)
41 response = self.client.get(reverse('cas_login'), follow=False)
42 self.assertEqual(response.status_code, 200)
43 self.assertEqual(response.context['user'].is_anonymous(), True)
46 def test_broken_pwd(self):
47 self._fail_login('root', '321')
49 def test_broken_username(self):
50 self._fail_login('notroot', '123')
52 def test_nonactive_user_login(self):
53 self._fail_login('nonactive', '123')
55 def test_cas2_success_validate(self):
56 response = self._login_user('root', '123')
57 self._validate_cas2(response, True)
59 def test_cas2_fail_validate(self):
60 for user, pwd in (('root', '321'), ('notroot', '123'), ('nonactive', '123')):
61 response = self._login_user(user, pwd)
62 self._validate_cas2(response, False)
65 def _fail_login(self, username, password):
66 response = self._login_user(username, password)
67 self._validate_cas1(response, False)
69 response = self.client.get(reverse('cas_login'), {'service': self.service}, follow=False)
70 self.assertEqual(response.status_code, 200)
71 response = self.client.get(reverse('cas_login'), follow=False)
72 self.assertEqual(response.status_code, 200)
76 def _login_user(self, username, password):
77 self.username = username
78 response = self.client.get(reverse('cas_login'), {'service': self.service})
79 self.assertEqual(response.status_code, 200)
80 form = response.context['form']
81 service = form['service'].value()
82 return self.client.post(reverse('cas_login'), {
85 'lt': form['lt'].value(),
90 def _validate_cas1(self, response, is_correct=True):
92 self.assertEqual(response.status_code, 302)
93 self.assertTrue(response.has_header('location'))
94 location = urlparse(response['location'])
95 ticket = location.query.split('=')[1]
97 response = self.client.get(reverse('cas_validate'), {'ticket': ticket, 'service': self.service}, follow=False)
98 self.assertEqual(response.status_code, 200)
99 self.assertEqual(unicode(response.content), u'yes\n%s\n' % self.username)
101 self.assertEqual(response.status_code, 200)
102 self.assertEqual(len(response.context['form'].errors), 1)
104 response = self.client.get(reverse('cas_validate'), {'ticket': 'ST-12312312312312312312312', 'service': self.service}, follow=False)
105 self.assertEqual(response.status_code, 200)
106 self.assertEqual(response.content, u'no\n\n')
109 def _validate_cas2(self, response, is_correct=True):
111 self.assertEqual(response.status_code, 302)
112 self.assertTrue(response.has_header('location'))
113 location = urlparse(response['location'])
114 ticket = location.query.split('=')[1]
116 response = self.client.get(reverse('cas_service_validate'), {'ticket': ticket, 'service': self.service}, follow=False)
117 self.assertEqual(response.status_code, 200)
118 self.assertEqual(response.content, _cas2_sucess_response(self.username).content)
120 self.assertEqual(response.status_code, 200)
121 self.assertEqual(len(response.context['form'].errors), 1)
123 response = self.client.get(reverse('cas_service_validate'), {'ticket': 'ST-12312312312312312312312', 'service': self.service}, follow=False)
124 self.assertEqual(response.status_code, 200)
125 self.assertEqual(response.content, _cas2_error_response(INVALID_TICKET).content)
128 class ModelsTestCase(TestCase):
130 fixtures = ['cas_users.json', ]
133 self.user = User.objects.get(username='root')
135 def test_redirects(self):
136 ticket = ServiceTicket.objects.create(service='http://example.com', user=self.user)
137 self.assertEqual(ticket.get_redirect_url(), '%(service)s?ticket=%(ticket)s' % ticket.__dict__)