From a9dc703609231521ed596e6ac9fa7cd746aa6fe1 Mon Sep 17 00:00:00 2001 From: Sebastian Annies Date: Mon, 17 Oct 2011 15:26:16 +0200 Subject: [PATCH] service == service if host, port and path are euqal. Do not mind url params testing the now corrected construction of the proxyCallbackUrl --- cas_provider/models.py | 2 +- cas_provider/tests.py | 35 ++++++++++++++++++++++++++++++----- cas_provider/views.py | 7 +++++-- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/cas_provider/models.py b/cas_provider/models.py index 4ec2e85..c13aa32 100644 --- a/cas_provider/models.py +++ b/cas_provider/models.py @@ -12,7 +12,7 @@ else: # Python <2.6 compatibility from cgi import parse_qs -__all__ = ['ServiceTicket', 'LoginTicket'] +__all__ = ['ServiceTicket', 'LoginTicket', 'ProxyGrantingTicket', 'ProxyTicket', 'ProxyGrantingTicketIOU'] class BaseTicket(models.Model): ticket = models.CharField(_('ticket'), max_length=32) diff --git a/cas_provider/tests.py b/cas_provider/tests.py index 3dd9791..705eef9 100644 --- a/cas_provider/tests.py +++ b/cas_provider/tests.py @@ -2,24 +2,32 @@ import StringIO 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.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 urlparse import urlparse +from urlparse import urlparse, parse_qsl, parse_qs from django.conf import settings -import cas_provider + + + + +dummy_urlopen_url = None + def dummy_urlopen(url): + cas_provider.tests.dummy_urlopen_url = url pass - class ViewsTest(TestCase): fixtures = ['cas_users', ] + + def setUp(self): self.service = 'http://example.com/' @@ -257,6 +265,23 @@ class ViewsTest(TestCase): 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) diff --git a/cas_provider/views.py b/cas_provider/views.py index 2769e52..2f0386f 100644 --- a/cas_provider/views.py +++ b/cas_provider/views.py @@ -135,7 +135,10 @@ def ticket_validate(service, ticket_string, pgtUrl): except ServiceTicket.DoesNotExist: return _cas2_error_response(INVALID_TICKET) - if ticket.service != service: + ticketUrl = urlparse.urlparse(ticket.service) + serviceUrl = urlparse.urlparse(service) + + if not(ticketUrl.hostname == serviceUrl.hostname and ticketUrl.path == serviceUrl.path and ticketUrl.port == serviceUrl.port): return _cas2_error_response(INVALID_SERVICE) pgtIouId = None @@ -196,7 +199,7 @@ def generate_proxy_granting_ticket(pgt_url, ticket): query = dict(urlparse.parse_qsl(uri[4])) query.update(params) - uri[4] = urlencode(query) + uri[3] = urlencode(query) try: response = urllib2.urlopen(urlparse.urlunsplit(uri)) -- 2.20.1