X-Git-Url: https://git.mdrn.pl/django-cas-provider.git/blobdiff_plain/82555c55f77395067231c1c22685597f12706902..243a932dd7165b4dbe5d8938f622450b4b7cdaa3:/cas_provider/tests.py?ds=sidebyside diff --git a/cas_provider/tests.py b/cas_provider/tests.py index 46ba3ca..2f87330 100644 --- a/cas_provider/tests.py +++ b/cas_provider/tests.py @@ -1,26 +1,34 @@ -import StringIO -import urllib2 +from __future__ import unicode_literals +from io import StringIO from xml import etree from xml.etree import ElementTree import cas_provider from cas_provider.attribute_formatters import CAS, NSMAP from cas_provider.models import ServiceTicket +from cas_provider.signals import cas_collect_custom_attributes from cas_provider.views import _cas2_sucess_response, INVALID_TICKET, _cas2_error_response, generate_proxy_granting_ticket from django.contrib.auth.models import User, UserManager from django.core.urlresolvers import reverse from django.test import TestCase -from urlparse import urlparse, parse_qsl, parse_qs from django.conf import settings +try: + from urllib.parse import urlparse, parse_qsl, parse_qs + from urllib.request import install_opener +except: + from urlparse import urlparse, parse_qsl, parse_qs + from urllib2 import install_opener -dummy_urlopen_url = None +class DummyOpener(object): + url = None + + @staticmethod + def open(url, *args, **kwargs): + DummyOpener.url = url -def dummy_urlopen(url): - cas_provider.tests.dummy_urlopen_url = url - pass class ViewsTest(TestCase): @@ -33,7 +41,7 @@ class ViewsTest(TestCase): def test_successful_login_with_proxy(self): - urllib2.urlopen = dummy_urlopen # monkey patching urllib2.urlopen so that the testcase doesnt really opens a url + install_opener(DummyOpener) # Don't really open any URLs proxyTarget = "http://my.sweet.service" response = self._login_user('root', '123') @@ -41,7 +49,7 @@ class ViewsTest(TestCase): # Test: I'm acting as the service that will call another service # Step 1: Get the proxy granting ticket - responseXml = ElementTree.parse(StringIO.StringIO(response.content)) + responseXml = ElementTree.parse(StringIO(response.content.decode('utf-8'))) auth_success = responseXml.find(CAS + 'authenticationSuccess', namespaces=NSMAP) pgt = auth_success.find(CAS + "proxyGrantingTicket", namespaces=NSMAP) user = auth_success.find(CAS + "user", namespaces=NSMAP) @@ -49,18 +57,18 @@ class ViewsTest(TestCase): self.assertIsNotNone(pgt.text) self.assertTrue(pgt.text.startswith('PGTIOU')) - pgtId = parse_qs(urlparse(cas_provider.tests.dummy_urlopen_url).query)['pgtId'] + pgtId = parse_qs(urlparse(DummyOpener.url).query)['pgtId'] #Step 2: Get the actual proxy ticket proxyTicketResponse = self.client.get(reverse('proxy'), {'targetService': proxyTarget, 'pgt': pgtId}, follow=False) - proxyTicketResponseXml = ElementTree.parse(StringIO.StringIO(proxyTicketResponse.content)) + proxyTicketResponseXml = ElementTree.parse(StringIO(proxyTicketResponse.content.decode('utf-8'))) self.assertIsNotNone(proxyTicketResponseXml.find(CAS + "proxySuccess", namespaces=NSMAP)) self.assertIsNotNone(proxyTicketResponseXml.find(CAS + "proxySuccess/cas:proxyTicket", namespaces=NSMAP)) proxyTicket = proxyTicketResponseXml.find(CAS + "proxySuccess/cas:proxyTicket", namespaces=NSMAP); #Step 3: I have the proxy ticket I can talk to some other backend service as the currently logged in user! proxyValidateResponse = self.client.get(reverse('cas_proxy_validate'), {'ticket': proxyTicket.text, 'service': proxyTarget}) - proxyValidateResponseXml = ElementTree.parse(StringIO.StringIO(proxyValidateResponse.content)) + proxyValidateResponseXml = ElementTree.parse(StringIO(proxyValidateResponse.content.decode('utf-8'))) auth_success_2 = proxyValidateResponseXml.find(CAS + 'authenticationSuccess', namespaces=NSMAP) user_2 = auth_success.find(CAS + "user", namespaces=NSMAP) @@ -71,7 +79,7 @@ class ViewsTest(TestCase): def test_successful_proxy_chaining(self): - urllib2.urlopen = dummy_urlopen # monkey patching urllib2.urlopen so that the testcase doesnt really opens a url + install_opener(DummyOpener) # Don't really open any URLs proxyTarget_1 = "http://my.sweet.service_1" proxyTarget_2 = "http://my.sweet.service_2" @@ -80,7 +88,7 @@ class ViewsTest(TestCase): # Test: I'm acting as the service that will call another service # Step 1: Get the proxy granting ticket - responseXml = ElementTree.parse(StringIO.StringIO(response.content)) + responseXml = ElementTree.parse(StringIO(response.content.decode('utf-8'))) auth_success_1 = responseXml.find(CAS + 'authenticationSuccess', namespaces=NSMAP) pgt_1 = auth_success_1.find(CAS + "proxyGrantingTicket", namespaces=NSMAP) user_1 = auth_success_1.find(CAS + "user", namespaces=NSMAP) @@ -88,11 +96,11 @@ class ViewsTest(TestCase): self.assertIsNotNone(pgt_1.text) self.assertTrue(pgt_1.text.startswith('PGTIOU')) - pgtId_1 = parse_qs(urlparse(cas_provider.tests.dummy_urlopen_url).query)['pgtId'] + pgtId_1 = parse_qs(urlparse(DummyOpener.url).query)['pgtId'] #Step 2: Get the actual proxy ticket proxyTicketResponse_1 = self.client.get(reverse('proxy'), {'targetService': proxyTarget_1, 'pgt': pgtId_1}, follow=False) - proxyTicketResponseXml_1 = ElementTree.parse(StringIO.StringIO(proxyTicketResponse_1.content)) + proxyTicketResponseXml_1 = ElementTree.parse(StringIO(proxyTicketResponse_1.content.decode('utf-8'))) self.assertIsNotNone(proxyTicketResponseXml_1.find(CAS + "proxySuccess", namespaces=NSMAP)) self.assertIsNotNone(proxyTicketResponseXml_1.find(CAS + "proxySuccess/cas:proxyTicket", namespaces=NSMAP)) proxyTicket_1 = proxyTicketResponseXml_1.find(CAS + "proxySuccess/cas:proxyTicket", namespaces=NSMAP); @@ -100,7 +108,7 @@ class ViewsTest(TestCase): #Step 3: I'm backend service 1 - I have the proxy ticket - I want to talk to back service 2 # proxyValidateResponse_1 = self.client.get(reverse('cas_proxy_validate'), {'ticket': proxyTicket_1.text, 'service': proxyTarget_1, 'pgtUrl': proxyTarget_2}) - proxyValidateResponseXml_1 = ElementTree.parse(StringIO.StringIO(proxyValidateResponse_1.content)) + proxyValidateResponseXml_1 = ElementTree.parse(StringIO(proxyValidateResponse_1.content.decode('utf-8'))) auth_success_2 = proxyValidateResponseXml_1.find(CAS + 'authenticationSuccess', namespaces=NSMAP) user_2 = auth_success_2.find(CAS + "user", namespaces=NSMAP) @@ -116,17 +124,17 @@ class ViewsTest(TestCase): self.assertIsNotNone(pgt_2.text) self.assertTrue(pgt_2.text.startswith('PGTIOU')) - pgtId_2 = parse_qs(urlparse(cas_provider.tests.dummy_urlopen_url).query)['pgtId'] + pgtId_2 = parse_qs(urlparse(DummyOpener.url).query)['pgtId'] #Step 4: Get the second proxy ticket proxyTicketResponse_2 = self.client.get(reverse('proxy'), {'targetService': proxyTarget_2, 'pgt': pgtId_2}) - proxyTicketResponseXml_2 = ElementTree.parse(StringIO.StringIO(proxyTicketResponse_2.content)) + proxyTicketResponseXml_2 = ElementTree.parse(StringIO(proxyTicketResponse_2.content.decode('utf-8'))) self.assertIsNotNone(proxyTicketResponseXml_2.find(CAS + "proxySuccess", namespaces=NSMAP)) self.assertIsNotNone(proxyTicketResponseXml_2.find(CAS + "proxySuccess/cas:proxyTicket", namespaces=NSMAP)) proxyTicket_2 = proxyTicketResponseXml_2.find(CAS + "proxySuccess/cas:proxyTicket", namespaces=NSMAP) proxyValidateResponse_3 = self.client.get(reverse('cas_proxy_validate'), {'ticket': proxyTicket_2.text, 'service': proxyTarget_2, 'pgtUrl': None}) - proxyValidateResponseXml_3 = ElementTree.parse(StringIO.StringIO(proxyValidateResponse_3.content)) + proxyValidateResponseXml_3 = ElementTree.parse(StringIO(proxyValidateResponse_3.content.decode('utf-8'))) auth_success_3 = proxyValidateResponseXml_3.find(CAS + 'authenticationSuccess', namespaces=NSMAP) user_3 = auth_success_3.find(CAS + "user", namespaces=NSMAP) @@ -193,20 +201,41 @@ class ViewsTest(TestCase): user = User.objects.get(username=self.username) self.assertEqual(response.content, _cas2_sucess_response(user).content) + def test_cas2_validate_twice(self): + response = self._login_user('root', '123') + response2 = self._validate_cas2(response, True) + user = User.objects.get(username=self.username) + self.assertEqual(response2.content, _cas2_sucess_response(user).content) + + response2 = self._validate_cas2(response, True) + self.assertEqual(response2.content, _cas2_error_response(INVALID_TICKET).content) + + def test_cas2_validate_twice_with_pgt(self): + proxyTarget = "http://my.sweet.service_1" + response = self._login_user('root', '123') + response2 = self._validate_cas2(response, True, proxyTarget) + user = User.objects.get(username=self.username) + self.assertEqual(response2.content, _cas2_sucess_response(user).content) + + response2 = self._validate_cas2(response, True, proxyTarget) + self.assertEqual(response2.content, _cas2_error_response(INVALID_TICKET).content) + def test_cas2_custom_attrs(self): - settings.CAS_CUSTOM_ATTRIBUTES_CALLBACK = cas_mapping + cas_collect_custom_attributes.connect(cas_mapping) response = self._login_user('editor', '123') + self.maxDiff=None response = self._validate_cas2(response, True) - self.assertEqual(response.content, '''''' + self.assertEqual(response.content.decode('utf-8'), + '''''' '''''' '''editor''' '''''' '''Jasig''' + '''editor@exapmle.com''' '''editor''' - '''True''' '''True''' - '''editor@exapmle.com''' + '''True''' '''''' '''''' '''''') @@ -215,14 +244,15 @@ class ViewsTest(TestCase): response = self._login_user('editor', '123') settings.CAS_CUSTOM_ATTRIBUTES_FORMATER = 'cas_provider.attribute_formatters.ruby_cas' response = self._validate_cas2(response, True) - self.assertEqual(response.content, '''''' + self.assertEqual(response.content.decode('utf-8'), + '''''' '''''' '''editor''' '''RubyCAS''' + '''editor@exapmle.com''' '''editor''' - '''True''' '''True''' - '''editor@exapmle.com''' + '''True''' '''''' '''''') @@ -230,14 +260,15 @@ class ViewsTest(TestCase): response = self._login_user('editor', '123') settings.CAS_CUSTOM_ATTRIBUTES_FORMATER = 'cas_provider.attribute_formatters.name_value' response = self._validate_cas2(response, True) - self.assertEqual(response.content, '''''' + self.assertEqual(response.content.decode('utf-8'), + '''''' '''''' '''editor''' '''''' + '''''' '''''' - '''''' '''''' - '''''' + '''''' '''''' '''''') @@ -249,7 +280,7 @@ class ViewsTest(TestCase): def test_generate_proxy_granting_ticket(self): - urllib2.urlopen = dummy_urlopen # monkey patching urllib2.urlopen so that the testcase doesnt really opens a url + install_opener(DummyOpener) # Don't really open any URLs url = 'http://my.call.back/callhere' user = User.objects.get(username = 'root') @@ -257,7 +288,7 @@ class ViewsTest(TestCase): pgt = generate_proxy_granting_ticket(url, st) self.assertIsNotNone(pgt) - calledUrl = cas_provider.tests.dummy_urlopen_url + calledUrl = DummyOpener.url parsedUrl = urlparse(calledUrl) params = parse_qs(parsedUrl.query) self.assertIsNotNone(params['pgtId']) @@ -286,7 +317,6 @@ class ViewsTest(TestCase): return self.client.post(reverse('cas_login'), { 'username': username, 'password': password, - 'lt': form['lt'].value(), 'service': service }, follow=False) @@ -300,14 +330,14 @@ class ViewsTest(TestCase): response = self.client.get(reverse('cas_validate'), {'ticket': ticket, 'service': self.service}, follow=False) self.assertEqual(response.status_code, 200) - self.assertEqual(unicode(response.content), u'yes\n%s\n' % self.username) + self.assertEqual(response.content.decode('utf-8'), 'yes\n%s\n' % self.username) else: self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.context['form'].errors), 1) + self.assertEqual(len(response.context['errors']), 1) response = self.client.get(reverse('cas_validate'), {'ticket': 'ST-12312312312312312312312', 'service': self.service}, follow=False) self.assertEqual(response.status_code, 200) - self.assertEqual(response.content, u'no\n\n') + self.assertEqual(response.content.decode('utf-8'), 'no\n\n') def _validate_cas2(self, response, is_correct=True, pgtUrl = None): @@ -323,7 +353,7 @@ class ViewsTest(TestCase): self.assertEqual(response.status_code, 200) else: self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.context['form'].errors), 1) + self.assertEqual(len(response.context['errors']), 1) response = self.client.get(reverse('cas_service_validate'), {'ticket': 'ST-12312312312312312312312', 'service': self.service}, follow=False) self.assertEqual(response.status_code, 200) @@ -342,10 +372,10 @@ class ModelsTestCase(TestCase): self.assertEqual(ticket.get_redirect_url(), '%(service)s?ticket=%(ticket)s' % ticket.__dict__) -def cas_mapping(user): +def cas_mapping(sender, user, **kwargs): return { - 'is_staff': unicode(user.is_staff), - 'is_active': unicode(user.is_active), + 'is_staff': str(user.is_staff), + 'is_active': str(user.is_active), 'email': user.email, 'group': [g.name for g in user.groups.all()] }