Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-cas
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
django-cas
Commits
7fa47f44
Commit
7fa47f44
authored
Nov 30, 2010
by
Ed Crewe
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
wrap up as a test case
parent
135ac60b
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
70 additions
and
42 deletions
+70
-42
django_cas/tests/cas_tests.py
+70
-42
No files found.
django_cas/tests/cas_tests.py
View file @
7fa47f44
...
@@ -7,12 +7,14 @@
...
@@ -7,12 +7,14 @@
# python proxy_test.py username password
# python proxy_test.py username password
# You will need to edit the constants below to match your setup ...
# You will need to edit the constants below to match your setup ...
import
unittest
import
sys
import
sys
import
getpass
import
getpass
import
urllib2
import
urllib2
import
urllib
import
urllib
from
urlparse
import
urljoin
from
urlparse
import
urljoin
import
cookielib
import
cookielib
from
xml.dom
import
minidom
# Add in a separate test_config file if you wish of the following format
# Add in a separate test_config file if you wish of the following format
try
:
try
:
...
@@ -31,40 +33,69 @@ except:
...
@@ -31,40 +33,69 @@ except:
'submit'
:
'Login'
# login submit button
'submit'
:
'Login'
# login submit button
}
}
def
get_auth
():
class
TestCAS
(
unittest
.
TestCase
):
""" A class for testing a CAS setup both for standard and proxy authentication """
opener
=
None
auth
=
{}
def
setUp
(
self
):
cj
=
cookielib
.
CookieJar
()
opener
=
urllib2
.
build_opener
(
urllib2
.
HTTPCookieProcessor
(
cj
))
urllib2
.
install_opener
(
opener
)
self
.
opener
=
opener
self
.
get_auth
()
def
test_cas
(
self
):
""" Test ordinary and proxy CAS login
NB cant put these into separate tests since tickets
are required to be passed between tests
"""
print
'Test ordinary CAS login'
print
'-----------------------'
self
.
ticket
=
self
.
login
()
self
.
get_restricted
()
print
'Test proxy CAS login'
print
'--------------------'
iou
=
self
.
get_proxy
()
if
iou
:
print
'Got IOU:
%
s'
%
iou
else
:
print
'Proxy CAS login failed, no IOU'
def
get_auth
(
self
):
""" Get authentication by passing to this script on the command line """
""" Get authentication by passing to this script on the command line """
if
len
(
sys
.
argv
)
>
1
:
if
len
(
sys
.
argv
)
>
1
:
AUTH
[
'username'
]
=
sys
.
argv
[
1
]
self
.
auth
[
'username'
]
=
sys
.
argv
[
1
]
else
:
else
:
AUTH
[
'username'
]
=
getpass
.
getuser
()
self
.
auth
[
'username'
]
=
getpass
.
getuser
()
AUTH
[
'password'
]
=
getpass
.
getpass
(
'CAS Password for user
%
s:'
%
AUTH
[
'username'
])
self
.
auth
[
'password'
]
=
getpass
.
getpass
(
'CAS Password for user
%
s:'
%
AUTH
[
'username'
])
return
AUTH
return
def
get_token
(
opener
,
url
,
token
=
TOKEN
):
def
get_token
(
self
,
url
,
token
=
TOKEN
):
""" Get CSRF token """
""" Get CSRF token """
r
=
opener
.
open
(
url
)
r
=
self
.
opener
.
open
(
url
)
page
=
r
.
read
()
page
=
r
.
read
()
starts
=
[
'<input type="hidden" name="
%
s"'
%
token
,
starts
=
[
'<input type="hidden" name="
%
s"'
%
token
,
'value="'
]
'value="'
]
return
find_in_page
(
page
,
starts
,
'"'
)
return
self
.
find_in_page
(
page
,
starts
,
'"'
)
def
get_ticket
(
page
,
app_url
):
def
get_ticket
(
self
,
page
,
app_url
):
""" Get CSRF token """
""" Get CSRF token """
starts
=
[
app_url
,
'?ticket='
]
starts
=
[
app_url
,
'?ticket='
]
return
find_in_page
(
page
,
starts
,
'"'
)
return
self
.
find_in_page
(
page
,
starts
,
'"'
)
def
find_in_dom
(
page
,
nesting
=
[
'body'
,
'form'
]):
def
find_in_dom
(
self
,
page
,
nesting
=
[
'body'
,
'form'
]):
""" Use dom to get values from page """
""" Use dom to get values from XML or page """
from
xml.dom
import
minidom
dom
=
minidom
.
parseString
(
page
)
dom
=
minidom
.
parseString
(
page
)
for
level
in
nesting
:
for
level
in
nesting
:
try
:
try
:
dom
=
dom
.
getElementsByTagn
ame
(
level
)[
0
]
dom
=
dom
.
getElementsByTagN
ame
(
level
)[
0
]
except
:
except
:
break
break
return
dom
.
getAttribute
(
'data'
)
return
dom
.
childNodes
[
0
]
.
nodeValue
def
find_in_page
(
page
,
starts
,
stop
):
def
find_in_page
(
self
,
page
,
starts
,
stop
):
""" Replace this with find_in_dom ?
""" Replace this with find_in_dom ?
Although without knowing the CAS login page this
Although without knowing the CAS login page this
is probably more generic.
is probably more generic.
...
@@ -75,33 +106,33 @@ def find_in_page(page, starts, stop):
...
@@ -75,33 +106,33 @@ def find_in_page(page, starts, stop):
found
=
page
[
start
:
end
]
found
=
page
[
start
:
end
]
return
found
return
found
def
login
(
opener
,
auth
):
def
login
(
self
):
""" Login to CAS server """
""" Login to CAS server """
url
=
'
%
s/login?service=
%
s'
%
(
CAS_SERVER_URL
,
APP_URL
)
url
=
'
%
s/login?service=
%
s'
%
(
CAS_SERVER_URL
,
APP_URL
)
ticket
=
''
ticket
=
''
token
=
get_token
(
opener
,
url
)
token
=
self
.
get_token
(
url
)
if
token
:
if
token
:
auth
[
TOKEN
]
=
token
self
.
auth
[
TOKEN
]
=
token
else
:
else
:
print
'FAILED CSRF Token could not be found on page'
print
'FAILED CSRF Token could not be found on page'
return
ticket
return
ticket
auth
[
'service'
]
=
APP_URL
self
.
auth
[
'service'
]
=
APP_URL
data
=
urllib
.
urlencode
(
auth
)
data
=
urllib
.
urlencode
(
self
.
auth
)
sso_resp
=
opener
.
open
(
url
,
data
)
sso_resp
=
self
.
opener
.
open
(
url
,
data
)
sso_page
=
sso_resp
.
read
()
sso_page
=
sso_resp
.
read
()
found
=
sso_page
.
find
(
CAS_SUCCESS
)
>
-
1
found
=
sso_page
.
find
(
CAS_SUCCESS
)
>
-
1
sso_resp
.
close
()
sso_resp
.
close
()
if
found
:
if
found
:
ticket
=
get_ticket
(
sso_page
,
APP_URL
)
ticket
=
self
.
get_ticket
(
sso_page
,
APP_URL
)
print
'PASS CAS logged in to
%
s'
%
url
print
'PASS CAS logged in to
%
s'
%
url
else
:
else
:
print
'FAILED CAS login to
%
s'
%
url
print
'FAILED CAS login to
%
s'
%
url
return
ticket
return
ticket
def
get_restricted
(
opener
):
def
get_restricted
(
self
):
""" Access a restricted URL and see if its accessible """
""" Access a restricted URL and see if its accessible """
url
=
APP_URL
+
APP_RESTRICTED
url
=
APP_URL
+
APP_RESTRICTED
app_resp
=
opener
.
open
(
url
)
app_resp
=
self
.
opener
.
open
(
url
)
ok
=
app_resp
.
code
==
200
ok
=
app_resp
.
code
==
200
app_resp
.
close
()
app_resp
.
close
()
if
ok
:
if
ok
:
...
@@ -110,22 +141,19 @@ def get_restricted(opener):
...
@@ -110,22 +141,19 @@ def get_restricted(opener):
print
'FAILED to log in to restricted app at
%
s'
%
url
print
'FAILED to log in to restricted app at
%
s'
%
url
return
return
def
get_proxy
(
opener
,
ticket
):
def
get_proxy
(
self
):
""" Use login ticket to get proxy """
""" Use login ticket to get proxy """
url_args
=
(
CAS_SERVER_URL
,
ticket
,
APP_URL
,
PROXY_URL
)
url_args
=
(
CAS_SERVER_URL
,
self
.
ticket
,
APP_URL
,
PROXY_URL
)
iou_url
=
'
%
s/serviceValidate?ticket=
%
s&service=
%
s&pgtUrl=
%
s'
%
url_args
iou_url
=
'
%
s/serviceValidate?ticket=
%
s&service=
%
s&pgtUrl=
%
s'
%
url_args
iou
=
opener
.
open
(
iou_url
)
iou
=
self
.
opener
.
open
(
iou_url
)
print
iou
.
read
()
page
=
iou
.
read
()
if
page
.
find
(
'cas:authenticationSuccess'
)
>
-
1
:
cj
=
cookielib
.
CookieJar
()
iou_ticket
=
self
.
find_in_dom
(
page
,[
'cas:serviceResponse'
,
opener
=
urllib2
.
build_opener
(
urllib2
.
HTTPCookieProcessor
(
cj
))
'cas:authenticationSuccess'
,
urllib2
.
install_opener
(
opener
)
'cas:proxyGrantingTicket'
])
auth
=
get_auth
()
return
iou_ticket
print
'Test ordinary CAS login'
print
page
print
'-----------------------'
return
None
ticket
=
login
(
opener
,
auth
)
get_restricted
(
opener
)
if
__name__
==
'__main__'
:
print
''
unittest
.
main
()
print
'Test proxy CAS login'
print
'--------------------'
get_proxy
(
opener
,
ticket
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment