Skip to content
This repository has been archived by the owner on Nov 15, 2022. It is now read-only.

Commit

Permalink
Adding doctests.
Browse files Browse the repository at this point in the history
  • Loading branch information
josiah-wolf-oberholtzer committed Apr 11, 2016
1 parent f49f766 commit 8fd0654
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
58 changes: 55 additions & 3 deletions cas_client/cas_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@


class CASClient(object):
r'''A client for interacting with a remote CAS instance.'''
'''
A client for interacting with a remote CAS instance.
::
>>> from cas_client import CASClient
>>> client = CASClient('https://logmein.com')
'''

def __init__(
self,
Expand Down Expand Up @@ -89,6 +97,15 @@ def get_auth_token_login_url(
def get_login_url(self, service_url=None):
'''
Get the URL for a remote CAS `login` endpoint.
::
>>> from cas_client import CASClient
>>> client = CASClient('https://logmein.com')
>>> service_url = 'http://myservice.net'
>>> client.get_login_url(service_url)
'https://logmein.com/cas/login?service=http://myservice.net'
'''
template = '{server_url}{auth_prefix}/login?service={service_url}'
url = template.format(
Expand All @@ -102,6 +119,15 @@ def get_login_url(self, service_url=None):
def get_logout_url(self, service_url=None):
'''
Get the URL for a remote CAS `logout` endpoint.
::
>>> from cas_client import CASClient
>>> client = CASClient('https://logmein.com')
>>> service_url = 'http://myservice.net'
>>> client.get_logout_url(service_url)
'https://logmein.com/cas/logout?service=http://myservice.net'
'''
template = '{server_url}{auth_prefix}/logout?service={service_url}'
url = template.format(
Expand Down Expand Up @@ -139,6 +165,32 @@ def perform_service_validate(self, ticket=None, service_url=None):
def parse_logout_request(self, message_text):
'''
Parse the contents of a CAS `LogoutRequest` XML message.
::
>>> from cas_client import CASClient
>>> client = CASClient('https://logmein.com')
>>> message_text = """
... <samlp:LogoutRequest
... xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
... xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
... ID="935a2d0c-4026-481e-be3d-20a1b2cdd553"
... Version="2.0"
... IssueInstant="2016-04-08 00:40:55 +0000">
... <saml:NameID>@NOT_USED@</saml:NameID>
... <samlp:SessionIndex>ST-14600760351898-0B3lSFt2jOWSbgQ377B4CtbD9uq0MXR9kG23vAuH</samlp:SessionIndex>
... </samlp:LogoutRequest>
... """
>>> parsed_message = client.parse_logout_request(message_text)
>>> import pprint
>>> pprint.pprint(parsed_message)
{'ID': '935a2d0c-4026-481e-be3d-20a1b2cdd553',
'IssueInstant': '2016-04-08 00:40:55 +0000',
'Version': '2.0',
'session_index': 'ST-14600760351898-0B3lSFt2jOWSbgQ377B4CtbD9uq0MXR9kG23vAuH',
'xmlns:saml': 'urn:oasis:names:tc:SAML:2.0:assertion',
'xmlns:samlp': 'urn:oasis:names:tc:SAML:2.0:protocol'}
'''
result = {}
xml_document = parseString(message_text)
Expand All @@ -149,9 +201,9 @@ def parse_logout_request(self, message_text):
for node in xml_document.getElementsByTagName('samlp:SessionIndex'):
for child in node.childNodes:
if child.nodeType == child.TEXT_NODE:
result['session_index'] = child.nodeValue.strip()
result['session_index'] = str(child.nodeValue.strip())
for key in xml_document.documentElement.attributes.keys():
result[key] = xml_document.documentElement.getAttribute(key)
result[str(key)] = str(xml_document.documentElement.getAttribute(key))
logging.debug('[CAS] LogoutRequest:\n{}'.format(
json.dumps(result, sort_keys=True, indent=4, separators=[',', ': ']),
))
Expand Down
4 changes: 3 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ envlist = py27, py34

[testenv]
deps=pytest
commands=py.test -rf test.py
commands=
py.test -rf test.py
python -m doctest cas_client/cas_client.py

0 comments on commit 8fd0654

Please sign in to comment.