Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F91759801
urlutils_unit_tests.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Thu, Nov 14, 04:30
Size
17 KB
Mime Type
text/x-python
Expires
Sat, Nov 16, 04:30 (1 d, 22 h)
Engine
blob
Format
Raw Data
Handle
22319857
Attached To
R3600 invenio-infoscience
urlutils_unit_tests.py
View Options
# -*- coding: utf-8 -*-
##
## This file is part of Invenio.
## Copyright (C) 2009, 2010, 2011 CERN.
##
## Invenio is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation; either version 2 of the
## License, or (at your option) any later version.
##
## Invenio is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Invenio; if not, write to the Free Software Foundation, Inc.,
## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
"""Unit tests for the urlutils library."""
__revision__
=
"$Id$"
import
unittest
from
cgi
import
parse_qs
from
invenio.config
import
CFG_SITE_URL
from
invenio.testutils
import
make_test_suite
,
run_test_suite
from
invenio.urlutils
import
\
create_AWS_request_url
,
\
string_to_numeric_char_reference
,
\
make_canonical_urlargd
,
\
create_html_link
,
\
create_html_mailto
,
\
same_urls_p
,
\
HASHLIB_IMPORTED
,
\
wash_url_argument
,
\
create_url
,
\
create_Indico_request_url
class
TestWashUrlArgument
(
unittest
.
TestCase
):
def
test_wash_url_argument
(
self
):
"""urlutils - washing of URL arguments"""
self
.
assertEqual
(
1
,
wash_url_argument
([
'1'
],
'int'
))
self
.
assertEqual
(
"1"
,
wash_url_argument
([
'1'
],
'str'
))
self
.
assertEqual
([
'1'
],
wash_url_argument
([
'1'
],
'list'
))
self
.
assertEqual
(
0
,
wash_url_argument
(
'ellis'
,
'int'
))
self
.
assertEqual
(
"ellis"
,
wash_url_argument
(
'ellis'
,
'str'
))
self
.
assertEqual
([
"ellis"
],
wash_url_argument
(
'ellis'
,
'list'
))
self
.
assertEqual
(
0
,
wash_url_argument
([
'ellis'
],
'int'
))
self
.
assertEqual
(
"ellis"
,
wash_url_argument
([
'ellis'
],
'str'
))
self
.
assertEqual
([
"ellis"
],
wash_url_argument
([
'ellis'
],
'list'
))
class
TestUrls
(
unittest
.
TestCase
):
"""Tests on URLs"""
def
test_url_creation
(
self
):
"""urlutils - test url creation"""
self
.
assertEqual
(
create_url
(
'http://www.a.com/search'
,
{
'recid'
:
3
,
'of'
:
'hb&'
},
escape_urlargd
=
True
),
'http://www.a.com/search?of=hb%26&recid=3'
)
self
.
assertEqual
(
create_url
(
'http://www.a.com/search'
,
{
'recid'
:
3
,
'of'
:
'hb&'
},
escape_urlargd
=
False
),
'http://www.a.com/search?of=hb&&recid=3'
)
def
test_canonical_urlargd_creation
(
self
):
"""urlutils - test creation of canonical URLs"""
self
.
assertEqual
(
make_canonical_urlargd
({
'a'
:
1
,
'b'
:
'2'
,
'b&'
:
'2='
,
':'
:
'?&'
},
{
'a'
:
(
'int'
,
1
),
'b'
:
(
'str'
,
2
)}),
"?b%26=2%3D&%3A=
%3F
%26&b=2"
)
if
HASHLIB_IMPORTED
:
def
test_signed_aws_request_creation
(
self
):
"""urlutils - test creation of signed AWS requests"""
signed_aws_request_url
=
create_AWS_request_url
(
"http://webservices.amazon.com/onca/xml"
,
{
'AWSAccessKeyId'
:
'00000000000000000000'
,
'Service'
:
'AWSECommerceService'
,
'Operation'
:
'ItemLookup'
,
'ItemId'
:
'0679722769'
,
'ResponseGroup'
:
'ItemAttributes,Offers,Images,Reviews'
,
'Version'
:
'2009-01-06'
},
"1234567890"
,
_timestamp
=
"2009-01-01T12:00:00Z"
)
# Are we at least acccessing correct base url?
self
.
assert_
(
signed_aws_request_url
.
startswith
(
"http://webservices.amazon.com/onca/xml"
))
# Check that parameters with special characters (, :) get correctly
# encoded/decoded
## Note: using parse_qs() url-decodes the string
self
.
assertEqual
(
parse_qs
(
signed_aws_request_url
)[
"ResponseGroup"
],
[
'ItemAttributes,Offers,Images,Reviews'
])
self
.
assert_
(
'ItemAttributes%2COffers%2CImages%2CReviews'
\
in
signed_aws_request_url
)
self
.
assertEqual
(
parse_qs
(
signed_aws_request_url
)[
"Timestamp"
],
[
'2009-01-01T12:00:00Z'
])
# Check signature exists and is correct
self
.
assertEqual
(
parse_qs
(
signed_aws_request_url
)[
"Signature"
],
[
'Nace+U3Az4OhN7tISqgs1vdLBHBEijWcBeCqL5xN9xg='
])
self
.
assert_
(
'Nace%2BU3Az4OhN7tISqgs1vdLBHBEijWcBeCqL5xN9xg%3D&Operation'
\
in
signed_aws_request_url
)
# Continute with an additional request
signed_aws_request_url_2
=
\
create_AWS_request_url
(
"http://ecs.amazonaws.co.uk/onca/xml"
,
{
'AWSAccessKeyId'
:
'00000000000000000000'
,
'Actor'
:
'Johnny Depp'
,
'AssociateTag'
:
'mytag-20'
,
'Operation'
:
'ItemSearch'
,
'ResponseGroup'
:
'ItemAttributes,Offers,Images,Reviews,Variations'
,
'SearchIndex'
:
'DVD'
,
'Service'
:
'AWSECommerceService'
,
'Sort'
:
'salesrank'
,
'Version'
:
'2009-01-01'
},
"1234567890"
,
_timestamp
=
"2009-01-01T12:00:00Z"
)
# Check signature exists and is correct
self
.
assertEqual
(
parse_qs
(
signed_aws_request_url_2
)[
"Signature"
],
[
'TuM6E5L9u/uNqOX09ET03BXVmHLVFfJIna5cxXuHxiU='
])
def
test_signed_Indico_request_creation
(
self
):
"""urlutils - test creation of signed Indico requests"""
signed_Indico_request_url
=
create_Indico_request_url
(
"https://indico.cern.ch"
,
"categ"
,
""
,
[
1
,
7
],
"xml"
,
{
'onlypublic'
:
'yes'
,
'order'
:
'title'
,
'from'
:
'today'
,
'to'
:
'tomorrow'
},
'00000000-0000-0000-0000-000000000000'
,
'00000000-0000-0000-0000-000000000000'
,
_timestamp
=
1234
)
# Are we at least acccessing correct base url?
self
.
assert_
(
signed_Indico_request_url
.
startswith
(
"https://indico.cern.ch/export/categ/1-7.xml?"
))
# Check parameters
self
.
assertEqual
(
parse_qs
(
signed_Indico_request_url
)[
"order"
],
[
'title'
])
self
.
assertEqual
(
parse_qs
(
signed_Indico_request_url
)[
"timestamp"
],
[
'1234'
])
# Check signature exists and is correct
self
.
assertEqual
(
parse_qs
(
signed_Indico_request_url
)[
"signature"
],
[
'e984e0c683e36ce3544372f23a397fd2400f4954'
])
def
test_same_urls_p
(
self
):
"""urlutils - test checking URLs equality"""
self
.
assertEqual
(
same_urls_p
(
CFG_SITE_URL
+
'?a=b&c=d&e=f'
,
CFG_SITE_URL
+
'?e=f&c=d&a=b'
),
True
)
self
.
assertEqual
(
same_urls_p
(
CFG_SITE_URL
+
'?a=b&c=d&e=f&ln=fr'
,
CFG_SITE_URL
+
'?e=f&c=d&a=b&ln=en'
),
False
)
class
TestHtmlLinks
(
unittest
.
TestCase
):
"""Tests on HTML links"""
def
test_html_link_creation
(
self
):
"""urlutils - test creation of HTML links"""
# Check with various encoding and escaping traps
self
.
assertEqual
(
create_html_link
(
'http://www.a.com'
,
{
'a'
:
1
,
'b'
:
'2'
,
'b&'
:
'2='
,
':'
:
'?'
},
'my label > & better than yours'
,
{
'style'
:
'color:#f00'
,
'target'
:
"_blank"
}),
'<a href="http://www.a.com?a=1&%3A=
%3F
&b%26=2%3D&b=2" style="color:#f00" target="_blank">my label > & better than yours</a>'
)
def
test_html_link_creation_no_argument_escaping
(
self
):
"""urlutils - test creation of HTML links, without arguments escaping"""
self
.
assertEqual
(
create_html_link
(
'http://www.a.com'
,
{
'a'
:
1
,
'b'
:
'2'
,
'b&'
:
'2='
,
':'
:
'?'
},
'my label > & better than yours'
,
{
'style'
:
'color:#f00'
,
'target'
:
"_blank"
},
escape_urlargd
=
False
),
'<a href="http://www.a.com?a=1&:=?&b&=2=&b=2" style="color:#f00" target="_blank">my label > & better than yours</a>'
)
def
test_html_link_creation_no_attribute_escaping
(
self
):
"""urlutils - test creation of HTML links, without attributes escaping"""
self
.
assertEqual
(
create_html_link
(
'http://www.a.com'
,
{
'a'
:
1
,
'b'
:
'2'
,
'b&'
:
'2='
,
':'
:
'?'
},
'my label > & better than yours'
,
{
'style'
:
'color:#f00'
,
'target'
:
"_blank"
},
escape_linkattrd
=
False
),
'<a href="http://www.a.com?a=1&%3A=
%3F
&b%26=2%3D&b=2" style="color:#f00" target="_blank">my label > & better than yours</a>'
)
def
test_string_to_numeric_char_reference
(
self
):
"""urlutils - test numeric character conversion from string"""
self
.
assertEqual
(
string_to_numeric_char_reference
(
'abc123'
),
"abc123"
)
self
.
assertEqual
(
string_to_numeric_char_reference
(
'\/&;,#$%~é'
),
"\/&;,#$%~é"
)
class
TestEmailObfuscationMode
(
unittest
.
TestCase
):
"""Tests on HTML mailto links creation and obfuscation modes"""
def
test_html_mailto_obfuscation_mode_minus1
(
self
):
"""urlutils - test creation of HTML "mailto" links, obfuscation mode -1"""
self
.
assertEqual
(
create_html_mailto
(
'juliet@cds.cern.ch'
,
subject
=
'Hey there'
,
body
=
'Lunch at 8pm?
\n
cu!'
,
bcc
=
'romeo@cds.cern.ch'
,
link_label
=
"Date creator"
,
linkattrd
=
{
'style'
:
'text-decoration: blink'
},
email_obfuscation_mode
=-
1
),
''
)
def
test_html_mailto_obfuscation_mode_0
(
self
):
"""urlutils - test creation of HTML "mailto" links, obfuscation mode 0"""
self
.
assertEqual
(
create_html_mailto
(
'juliet@cds.cern.ch'
,
subject
=
'Hey there'
,
body
=
'Lunch at 8pm?
\n
cu!'
,
bcc
=
'romeo@cds.cern.ch'
,
link_label
=
"Date creator"
,
linkattrd
=
{
'style'
:
'text-decoration: blink'
},
email_obfuscation_mode
=
0
),
'<a href="mailto:juliet@cds.cern.ch?body=Lunch%20at%208pm
%3F
%0D%0Acu%21&bcc=romeo
%40c
ds.cern.ch&subject=Hey%20there" style="text-decoration: blink">Date creator</a>'
)
def
test_html_mailto_obfuscation_mode_1
(
self
):
"""urlutils - test creation of HTML "mailto" links, obfuscation mode 1"""
self
.
assertEqual
(
create_html_mailto
(
'juliet@cds.cern.ch'
,
subject
=
'Hey there'
,
body
=
'Lunch at 8pm?
\n
cu!'
,
bcc
=
'romeo@cds.cern.ch'
,
link_label
=
"Date creator"
,
linkattrd
=
{
'style'
:
'text-decoration: blink'
},
email_obfuscation_mode
=
1
),
'<a href="mailto:juliet [at] cds [dot] cern [dot] ch?body=Lunch%20at%208pm
%3F
%0D%0Acu%21&bcc=romeo
%40c
ds.cern.ch&subject=Hey%20there" style="text-decoration: blink">Date creator</a>'
)
def
test_html_mailto_obfuscation_mode_2
(
self
):
"""urlutils - test creation of HTML "mailto" links, obfuscation mode 2"""
self
.
assertEqual
(
create_html_mailto
(
'juliet@cds.cern.ch'
,
subject
=
'Hey there'
,
body
=
'Lunch at 8pm?
\n
cu!'
,
bcc
=
'romeo@cds.cern.ch'
,
link_label
=
"Date creator"
,
linkattrd
=
{
'style'
:
'text-decoration: blink'
},
email_obfuscation_mode
=
2
),
'<a href="mailto:juliet@cds.cern.ch?body=Lunch%20at%208pm
%3F
%0D%0Acu%21&bcc=romeo
%40c
ds.cern.ch&subject=Hey%20there" style="text-decoration: blink">Date creator</a>'
)
def
test_html_mailto_obfuscation_mode_3
(
self
):
"""urlutils - test creation of HTML "mailto" links, obfuscation mode 3"""
self
.
assertEqual
(
create_html_mailto
(
'juliet@cds.cern.ch'
,
subject
=
'Hey there'
,
body
=
'Lunch at 8pm?
\n
cu!'
,
bcc
=
'romeo@cds.cern.ch'
,
link_label
=
"Date creator"
,
linkattrd
=
{
'style'
:
'text-decoration: blink'
},
email_obfuscation_mode
=
3
),
'<script language="JavaScript" type="text/javascript">document.write(
\'
>a/<rotaerc etaD>"knilb :noitaroced-txet"=elyts "ereht02%yeH=tcejbus;pma&hc.nrec.sdc04
%o
emor=ccb;pma&12
%u
cA0%D0
%F
3%mp802%ta02
%hc
nuL=ydob?hc.nrec.sdc@teiluj:otliam"=ferh a<
\'
.split("").reverse().join(""))</script>'
)
def
test_html_mailto_obfuscation_mode_4
(
self
):
"""urlutils - test creation of HTML "mailto" links, obfuscation mode 4"""
self
.
assertEqual
(
create_html_mailto
(
'juliet@cds.cern.ch'
,
subject
=
'Hey there'
,
body
=
'Lunch at 8pm?
\n
cu!'
,
bcc
=
'romeo@cds.cern.ch'
,
link_label
=
"Date creator"
,
linkattrd
=
{
'style'
:
'text-decoration: blink'
},
email_obfuscation_mode
=
4
),
'juliet<img src="
%(CFG_SITE_URL)s
/img/at.gif" alt=" [at] " style="vertical-align:baseline" />cds<img src="
%(CFG_SITE_URL)s
/img/dot.gif" alt=" [dot] " style="vertical-align:bottom" />cern<img src="
%(CFG_SITE_URL)s
/img/dot.gif" alt=" [dot] " style="vertical-align:bottom" />ch'
%
\
{
'CFG_SITE_URL'
:
CFG_SITE_URL
})
TEST_SUITE
=
make_test_suite
(
TestWashUrlArgument
,
TestUrls
,
TestHtmlLinks
,
TestEmailObfuscationMode
)
if
__name__
==
"__main__"
:
run_test_suite
(
TEST_SUITE
)
Event Timeline
Log In to Comment