X-Git-Url: https://git.mdrn.pl/django-cas-provider.git/blobdiff_plain/2a1924c2ad3c448821641a22d98631dae1c025f2..34efbf37f568d6db6523da7dde3a134c467ed89b:/cas_provider/tests.py diff --git a/cas_provider/tests.py b/cas_provider/tests.py index 4b486de..2165fee 100644 --- a/cas_provider/tests.py +++ b/cas_provider/tests.py @@ -1,21 +1,72 @@ +import StringIO +import urllib2 +from xml import etree +from xml.etree import ElementTree +from cas_provider.attribute_formatters import CAS, NSMAP from cas_provider.models import ServiceTicket -from cas_provider.views import _cas2_sucess_response, _cas2_error_response, \ - INVALID_TICKET +from cas_provider.views import _cas2_sucess_response, INVALID_TICKET, _cas2_error_response from django.contrib.auth.models import User from django.core.urlresolvers import reverse from django.test import TestCase from urlparse import urlparse from django.conf import settings +import cas_provider + +def dummy_urlopen(url): + pass class ViewsTest(TestCase): - fixtures = ['cas_users.json', ] + fixtures = ['cas_users', ] def setUp(self): self.service = 'http://example.com/' + def test_successful_login_with_proxy(self): + urllib2.urlopen = dummy_urlopen # monkey patching urllib2.urlopen so that the testcase doesnt really opens a url + proxyTarget = "http://my.sweet.service" + + response = self._login_user('root', '123') + response = self._validate_cas2(response, True, proxyTarget ) + + # 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)) + auth_success = responseXml.find(CAS + 'authenticationSuccess', namespaces=NSMAP) + pgt = auth_success.find(CAS + "proxyGrantingTicket", namespaces=NSMAP) + user = auth_success.find(CAS + "user", namespaces=NSMAP) + self.assertEqual('root', user.text) + self.assertIsNotNone(pgt.text) + self.assertTrue(pgt.text.startswith('PGTIOU')) + + #Step 2: Get the actual proxy ticket + proxyTicketResponse = self.client.get(reverse('proxy'), {'targetService': proxyTarget, 'pgt': pgt.text}, follow=False) + proxyTicketResponseXml = ElementTree.parse(StringIO.StringIO(proxyTicketResponse.content)) + 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, 'pgtUrl': None}) + proxyValidateResponseXml = ElementTree.parse(StringIO.StringIO(proxyValidateResponse.content)) + + auth_success_2 = proxyValidateResponseXml.find(CAS + 'authenticationSuccess', namespaces=NSMAP) + user_2 = auth_success.find(CAS + "user", namespaces=NSMAP) + proxies = auth_success.find(CAS + "proxies") + self.assertIsNotNone(auth_success_2) + self.assertEqual(user.text, user_2.text) + self.assertIsNotNone(proxies) + + + def test_successful_proxy_chaining(self): + self.assertFalse(True) + + def test_successful_service_not_matching_in_request_to_proxy(self): + self.assertFalse(True) + + def test_succeessful_login(self): response = self._login_user('root', '123') self._validate_cas1(response, True) @@ -73,47 +124,47 @@ class ViewsTest(TestCase): response = self._validate_cas2(response, True) self.assertEqual(response.content, '''''' - '''''' - '''editor''' - '''''' - '''Jasig''' - '''editor''' - '''True''' - '''True''' - '''editor@exapmle.com''' - '''''' - '''''' - '''''') + '''''' + '''editor''' + '''''' + '''Jasig''' + '''editor''' + '''True''' + '''True''' + '''editor@exapmle.com''' + '''''' + '''''' + '''''') self._cas_logout() 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, '''''' - '''''' - '''editor''' - '''RubyCAS''' - '''editor''' - '''True''' - '''True''' - '''editor@exapmle.com''' - '''''' - '''''') + '''''' + '''editor''' + '''RubyCAS''' + '''editor''' + '''True''' + '''True''' + '''editor@exapmle.com''' + '''''' + '''''') self._cas_logout() 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, '''''' - '''''' - '''editor''' - '''''' - '''''' - '''''' - '''''' - '''''' - '''''' - '''''') + '''''' + '''editor''' + '''''' + '''''' + '''''' + '''''' + '''''' + '''''' + '''''') def test_cas2_fail_validate(self): @@ -167,14 +218,16 @@ class ViewsTest(TestCase): self.assertEqual(response.content, u'no\n\n') - def _validate_cas2(self, response, is_correct=True): + def _validate_cas2(self, response, is_correct=True, pgtUrl = None): if is_correct: self.assertEqual(response.status_code, 302) self.assertTrue(response.has_header('location')) location = urlparse(response['location']) ticket = location.query.split('=')[1] - - response = self.client.get(reverse('cas_service_validate'), {'ticket': ticket, 'service': self.service}, follow=False) + if pgtUrl: + response = self.client.get(reverse('cas_service_validate'), {'ticket': ticket, 'service': self.service, 'pgtUrl': pgtUrl}, follow=False) + else: + response = self.client.get(reverse('cas_service_validate'), {'ticket': ticket, 'service': self.service}, follow=False) self.assertEqual(response.status_code, 200) else: self.assertEqual(response.status_code, 200) @@ -185,7 +238,6 @@ class ViewsTest(TestCase): self.assertEqual(response.content, _cas2_error_response(INVALID_TICKET).content) return response - class ModelsTestCase(TestCase): fixtures = ['cas_users.json', ]