Fixed never_cache decorator to, well, never cache.
[django-cas-provider.git] / cas_provider / tests.py
index 3dd9791..46ba3ca 100644 (file)
@@ -2,24 +2,32 @@ import StringIO
 import urllib2
 from xml import etree
 from xml.etree import ElementTree
 import urllib2
 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.attribute_formatters import CAS, NSMAP
 from cas_provider.models import ServiceTicket
-from cas_provider.views import _cas2_sucess_response, INVALID_TICKET, _cas2_error_response
-from django.contrib.auth.models import User
+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 django.core.urlresolvers import reverse
 from django.test import TestCase
-from urlparse import urlparse
+from urlparse import urlparse, parse_qsl, parse_qs
 from django.conf import settings
 from django.conf import settings
-import cas_provider
+
+
+
+
+dummy_urlopen_url = None
+
 
 def dummy_urlopen(url):
 
 def dummy_urlopen(url):
+    cas_provider.tests.dummy_urlopen_url = url
     pass
 
     pass
 
-
 class ViewsTest(TestCase):
 
     fixtures = ['cas_users', ]
 
 class ViewsTest(TestCase):
 
     fixtures = ['cas_users', ]
 
+
+
     def setUp(self):
         self.service = 'http://example.com/'
 
     def setUp(self):
         self.service = 'http://example.com/'
 
@@ -41,8 +49,10 @@ class ViewsTest(TestCase):
         self.assertIsNotNone(pgt.text)
         self.assertTrue(pgt.text.startswith('PGTIOU'))
 
         self.assertIsNotNone(pgt.text)
         self.assertTrue(pgt.text.startswith('PGTIOU'))
 
+        pgtId = parse_qs(urlparse(cas_provider.tests.dummy_urlopen_url).query)['pgtId']
+
         #Step 2: Get the actual proxy ticket
         #Step 2: Get the actual proxy ticket
-        proxyTicketResponse = self.client.get(reverse('proxy'), {'targetService': proxyTarget, 'pgt': pgt.text}, follow=False)
+        proxyTicketResponse = self.client.get(reverse('proxy'), {'targetService': proxyTarget, 'pgt': pgtId}, 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))
         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))
@@ -78,8 +88,10 @@ class ViewsTest(TestCase):
         self.assertIsNotNone(pgt_1.text)
         self.assertTrue(pgt_1.text.startswith('PGTIOU'))
 
         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']
+
         #Step 2: Get the actual proxy ticket
         #Step 2: Get the actual proxy ticket
-        proxyTicketResponse_1 = self.client.get(reverse('proxy'), {'targetService': proxyTarget_1, 'pgt': pgt_1.text}, follow=False)
+        proxyTicketResponse_1 = self.client.get(reverse('proxy'), {'targetService': proxyTarget_1, 'pgt': pgtId_1}, follow=False)
         proxyTicketResponseXml_1 = ElementTree.parse(StringIO.StringIO(proxyTicketResponse_1.content))
         self.assertIsNotNone(proxyTicketResponseXml_1.find(CAS + "proxySuccess", namespaces=NSMAP))
         self.assertIsNotNone(proxyTicketResponseXml_1.find(CAS + "proxySuccess/cas:proxyTicket", namespaces=NSMAP))
         proxyTicketResponseXml_1 = ElementTree.parse(StringIO.StringIO(proxyTicketResponse_1.content))
         self.assertIsNotNone(proxyTicketResponseXml_1.find(CAS + "proxySuccess", namespaces=NSMAP))
         self.assertIsNotNone(proxyTicketResponseXml_1.find(CAS + "proxySuccess/cas:proxyTicket", namespaces=NSMAP))
@@ -104,8 +116,10 @@ class ViewsTest(TestCase):
         self.assertIsNotNone(pgt_2.text)
         self.assertTrue(pgt_2.text.startswith('PGTIOU'))
 
         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']
+
         #Step 4: Get the second proxy ticket
         #Step 4: Get the second proxy ticket
-        proxyTicketResponse_2 = self.client.get(reverse('proxy'), {'targetService': proxyTarget_2, 'pgt': pgt_2.text})
+        proxyTicketResponse_2 = self.client.get(reverse('proxy'), {'targetService': proxyTarget_2, 'pgt': pgtId_2})
         proxyTicketResponseXml_2 = ElementTree.parse(StringIO.StringIO(proxyTicketResponse_2.content))
         self.assertIsNotNone(proxyTicketResponseXml_2.find(CAS + "proxySuccess", namespaces=NSMAP))
         self.assertIsNotNone(proxyTicketResponseXml_2.find(CAS + "proxySuccess/cas:proxyTicket", namespaces=NSMAP))
         proxyTicketResponseXml_2 = ElementTree.parse(StringIO.StringIO(proxyTicketResponse_2.content))
         self.assertIsNotNone(proxyTicketResponseXml_2.find(CAS + "proxySuccess", namespaces=NSMAP))
         self.assertIsNotNone(proxyTicketResponseXml_2.find(CAS + "proxySuccess/cas:proxyTicket", namespaces=NSMAP))
@@ -127,30 +141,7 @@ class ViewsTest(TestCase):
 
 
 
 
 
 
-    def test_successful_service_not_matching_in_request_to_proxy(self):
-        urllib2.urlopen = dummy_urlopen # monkey patching urllib2.urlopen so that the testcase doesnt really opens a url
-        proxyTarget_1 = "http://my.sweet.service"
-        proxyTarget_2 = "http://my.malicious.service"
-
-        response = self._login_user('root', '123')
-        response = self._validate_cas2(response, True, proxyTarget_1 )
-
-        # 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_2, 'pgt': pgt.text}, follow=False)
-        proxyTicketResponseXml = ElementTree.parse(StringIO.StringIO(proxyTicketResponse.content))
-        self.assertIsNotNone(proxyTicketResponseXml.find(CAS + "authenticationFailure", namespaces=NSMAP))
-
-
+    
     def test_succeessful_login(self):
         response = self._login_user('root', '123')
         self._validate_cas1(response, True)
     def test_succeessful_login(self):
         response = self._login_user('root', '123')
         self._validate_cas1(response, True)
@@ -257,6 +248,23 @@ class ViewsTest(TestCase):
             self._validate_cas2(response, False)
 
 
             self._validate_cas2(response, False)
 
 
+    def test_generate_proxy_granting_ticket(self):
+        urllib2.urlopen = dummy_urlopen # monkey patching urllib2.urlopen so that the testcase doesnt really opens a url
+        url = 'http://my.call.back/callhere'
+
+        user = User.objects.get(username = 'root')
+        st = ServiceTicket.objects.create(user = user )
+        pgt = generate_proxy_granting_ticket(url, st)
+        self.assertIsNotNone(pgt)
+
+        calledUrl = cas_provider.tests.dummy_urlopen_url
+        parsedUrl = urlparse(calledUrl)
+        params = parse_qs(parsedUrl.query)
+        self.assertIsNotNone(params['pgtId'])
+        self.assertIsNotNone(params['pgtIou'])
+
+
+
     def _fail_login(self, username, password):
         response = self._login_user(username, password)
         self._validate_cas1(response, False)
     def _fail_login(self, username, password):
         response = self._login_user(username, password)
         self._validate_cas1(response, False)