Fix standard urls. Test for Django up to 5.0.
[django-cas-provider.git] / cas_provider_examples / simple / tests.py
index 2f87330..4e8b05e 100644 (file)
@@ -1,25 +1,17 @@
-from __future__ import unicode_literals
 from io import StringIO
 from io import StringIO
+from urllib.parse import urlparse, parse_qsl, parse_qs
+from urllib.request import install_opener
 from xml import etree
 from xml.etree import ElementTree
 import cas_provider
 from xml import etree
 from xml.etree import ElementTree
 import cas_provider
-from cas_provider.attribute_formatters import CAS, NSMAP
+from cas_provider.attribute_formatters import CAS
 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 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 django.conf import settings
 from django.test import TestCase
 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
-
-
+from django.urls import reverse
 
 
 class DummyOpener(object):
 
 
 class DummyOpener(object):
@@ -50,9 +42,9 @@ 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(response.content.decode('utf-8')))
         # Test: I'm acting as the service that will call another service
         # Step 1: Get the proxy granting ticket
         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)
+        auth_success = responseXml.find(CAS + 'authenticationSuccess')
+        pgt = auth_success.find(CAS + "proxyGrantingTicket")
+        user = auth_success.find(CAS + "user")
         self.assertEqual('root', user.text)
         self.assertIsNotNone(pgt.text)
         self.assertTrue(pgt.text.startswith('PGTIOU'))
         self.assertEqual('root', user.text)
         self.assertIsNotNone(pgt.text)
         self.assertTrue(pgt.text.startswith('PGTIOU'))
@@ -62,16 +54,16 @@ class ViewsTest(TestCase):
         #Step 2: Get the actual proxy ticket
         proxyTicketResponse = self.client.get(reverse('proxy'), {'targetService': proxyTarget, 'pgt': pgtId}, follow=False)
         proxyTicketResponseXml = ElementTree.parse(StringIO(proxyTicketResponse.content.decode('utf-8')))
         #Step 2: Get the actual proxy ticket
         proxyTicketResponse = self.client.get(reverse('proxy'), {'targetService': proxyTarget, 'pgt': pgtId}, follow=False)
         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);
+        self.assertIsNotNone(proxyTicketResponseXml.find(CAS + "proxySuccess"))
+        self.assertIsNotNone(proxyTicketResponseXml.find(CAS + "proxySuccess/" + CAS + "proxyTicket"))
+        proxyTicket = proxyTicketResponseXml.find(CAS + "proxySuccess/" + CAS + "proxyTicket");
 
         #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(proxyValidateResponse.content.decode('utf-8')))
 
 
         #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(proxyValidateResponse.content.decode('utf-8')))
 
-        auth_success_2 = proxyValidateResponseXml.find(CAS + 'authenticationSuccess', namespaces=NSMAP)
-        user_2 = auth_success.find(CAS + "user", namespaces=NSMAP)
+        auth_success_2 = proxyValidateResponseXml.find(CAS + 'authenticationSuccess')
+        user_2 = auth_success.find(CAS + "user")
         proxies_1  = auth_success_2.find(CAS + "proxies")
         self.assertIsNone(proxies_1) # there are no proxies. I am issued by a Service Ticket
         
         proxies_1  = auth_success_2.find(CAS + "proxies")
         self.assertIsNone(proxies_1) # there are no proxies. I am issued by a Service Ticket
         
@@ -89,9 +81,9 @@ 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(response.content.decode('utf-8')))
         # Test: I'm acting as the service that will call another service
         # Step 1: Get the proxy granting ticket
         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)
+        auth_success_1 = responseXml.find(CAS + 'authenticationSuccess')
+        pgt_1 = auth_success_1.find(CAS + "proxyGrantingTicket")
+        user_1 = auth_success_1.find(CAS + "user")
         self.assertEqual('root', user_1.text)
         self.assertIsNotNone(pgt_1.text)
         self.assertTrue(pgt_1.text.startswith('PGTIOU'))
         self.assertEqual('root', user_1.text)
         self.assertIsNotNone(pgt_1.text)
         self.assertTrue(pgt_1.text.startswith('PGTIOU'))
@@ -101,25 +93,25 @@ class ViewsTest(TestCase):
         #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(proxyTicketResponse_1.content.decode('utf-8')))
         #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(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);
+        self.assertIsNotNone(proxyTicketResponseXml_1.find(CAS + "proxySuccess"))
+        self.assertIsNotNone(proxyTicketResponseXml_1.find(CAS + "proxySuccess/" + CAS + "proxyTicket"))
+        proxyTicket_1 = proxyTicketResponseXml_1.find(CAS + "proxySuccess/" + CAS +"proxyTicket");
 
         #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(proxyValidateResponse_1.content.decode('utf-8')))
 
 
         #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(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)
+        auth_success_2 = proxyValidateResponseXml_1.find(CAS + 'authenticationSuccess')
+        user_2 = auth_success_2.find(CAS + "user")
 
         proxies_1  = auth_success_2.find(CAS + "proxies")
         self.assertIsNone(proxies_1) # there are no proxies. I am issued by a Service Ticket
         self.assertIsNotNone(auth_success_2)
         self.assertEqual('root', user_2.text)
 
 
         proxies_1  = auth_success_2.find(CAS + "proxies")
         self.assertIsNone(proxies_1) # there are no proxies. I am issued by a Service Ticket
         self.assertIsNotNone(auth_success_2)
         self.assertEqual('root', user_2.text)
 
-        pgt_2 = auth_success_2.find(CAS + "proxyGrantingTicket", namespaces=NSMAP)
-        user = auth_success_2.find(CAS + "user", namespaces=NSMAP)
+        pgt_2 = auth_success_2.find(CAS + "proxyGrantingTicket")
+        user = auth_success_2.find(CAS + "user")
         self.assertEqual('root', user.text)
         self.assertIsNotNone(pgt_2.text)
         self.assertTrue(pgt_2.text.startswith('PGTIOU'))
         self.assertEqual('root', user.text)
         self.assertIsNotNone(pgt_2.text)
         self.assertTrue(pgt_2.text.startswith('PGTIOU'))
@@ -129,27 +121,25 @@ class ViewsTest(TestCase):
         #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(proxyTicketResponse_2.content.decode('utf-8')))
         #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(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)
+        self.assertIsNotNone(proxyTicketResponseXml_2.find(CAS + "proxySuccess"))
+        self.assertIsNotNone(proxyTicketResponseXml_2.find(CAS + "proxySuccess/" + CAS + "proxyTicket"))
+        proxyTicket_2 = proxyTicketResponseXml_2.find(CAS + "proxySuccess/" + CAS + "proxyTicket")
 
 
-        proxyValidateResponse_3 = self.client.get(reverse('cas_proxy_validate'), {'ticket': proxyTicket_2.text, 'service': proxyTarget_2, 'pgtUrl': None})
+        proxyValidateResponse_3 = self.client.get(reverse('cas_proxy_validate'), {'ticket': proxyTicket_2.text, 'service': proxyTarget_2, 'pgtUrl': ''})
         proxyValidateResponseXml_3 = ElementTree.parse(StringIO(proxyValidateResponse_3.content.decode('utf-8')))
 
         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)
+        auth_success_3 = proxyValidateResponseXml_3.find(CAS + 'authenticationSuccess')
+        user_3 = auth_success_3.find(CAS + "user")
 
         proxies_3  = auth_success_3.find(CAS + "proxies")
         self.assertIsNotNone(proxies_3) # there should be a proxy. I am issued by a Proxy Ticket
 
         proxies_3  = auth_success_3.find(CAS + "proxies")
         self.assertIsNotNone(proxies_3) # there should be a proxy. I am issued by a Proxy Ticket
-        proxy_3 = proxies_3.find(CAS + "proxy", namespaces=NSMAP)
+        proxy_3 = proxies_3.find(CAS + "proxy")
         self.assertEqual(proxyTarget_1, proxy_3.text )
 
         self.assertIsNotNone(auth_success_2)
         self.assertEqual('root', user_3.text)
 
 
         self.assertEqual(proxyTarget_1, proxy_3.text )
 
         self.assertIsNotNone(auth_success_2)
         self.assertEqual('root', user_3.text)
 
 
-
-    
     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)
@@ -160,7 +150,7 @@ class ViewsTest(TestCase):
 
         response = self.client.get(reverse('cas_login'), follow=False)
         self.assertEqual(response.status_code, 302)
 
         response = self.client.get(reverse('cas_login'), follow=False)
         self.assertEqual(response.status_code, 302)
-        self.assertTrue(response['location'].startswith('http://testserver/'))
+        self.assertTrue(response['location'].startswith('/'))
 
         response = self.client.get(response['location'], follow=False)
         self.assertIn(response.status_code, [302, 200])
 
         response = self.client.get(response['location'], follow=False)
         self.assertIn(response.status_code, [302, 200])
@@ -183,7 +173,7 @@ class ViewsTest(TestCase):
 
         response = self.client.get(reverse('cas_login'), follow=False)
         self.assertEqual(response.status_code, 200)
 
         response = self.client.get(reverse('cas_login'), follow=False)
         self.assertEqual(response.status_code, 200)
-        self.assertEqual(response.context['user'].is_anonymous(), True)
+        self.assertTrue(response.context['user'].is_anonymous)
 
 
     def test_broken_pwd(self):
 
 
     def test_broken_pwd(self):