1
# -*- coding: utf-8 -*-
3
# Authors: Roberto Alsina <roberto.alsina@canonical.com>
4
# Authors: Alejandro J. Cura <alecu@canonical.com>
6
# Copyright 2011 Canonical Ltd.
8
# This program is free software: you can redistribute it and/or modify it
9
# under the terms of the GNU General Public License version 3, as published
10
# by the Free Software Foundation.
12
# This program is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranties of
14
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
15
# PURPOSE. See the GNU General Public License for more details.
17
# You should have received a copy of the GNU General Public License along
18
# with this program. If not, see <http://www.gnu.org/licenses/>.
20
"""Tests for the url signing function."""
22
from urlparse import urlparse, parse_qs
24
from ubuntuone.controlpanel.tests import TestCase
25
from ubuntuone.controlpanel.gui import (
29
TOKEN = {u'consumer_key': u'consumer_key',
30
u'consumer_secret': u'consumer_secret',
31
u'token_name': u'test_token',
32
u'token': u'GkInOfSMGwTXAUoVQwLUoPxElEEUdhsLVNTPhxHJDUIeHCPNEo',
33
u'token_secret': u'qFYImEtlczPbsCnYyuwLoPDlPEnvNcIktZphPQklAWrvyfFMV'}
35
SAMPLE_SIGNED = UBUNTUONE_FROM_OAUTH + '?oauth_nonce=' \
36
'36886134&oauth_timestamp=1310671062&oauth_consumer_key=consumer_key&' \
37
'oauth_signature_method=HMAC-SHA1&next=%2Fblah&oauth_version=1.0&' \
38
'oauth_token=GkInOfSMGwTXAUoVQwLUoPxElEEUdhsLVNTPhxHJDUIeHCPNEo&' \
39
'oauth_signature=s6h0LRBiWchTADrTJWaJUSuaGpo%3D'
41
#pylint: disable=E1101
44
class SignURLTestCase(TestCase):
46
"""Test cases for the URL signing function."""
48
def test_is_correct_domain(self):
49
"""Test that we are using the right domain."""
50
signed = sign_url("/blah", TOKEN)
51
parsed_signed = urlparse(signed)
52
parsed_sample = urlparse(SAMPLE_SIGNED)
53
self.assertEqual(parsed_signed.netloc, parsed_sample.netloc)
55
def test_is_correct_path(self):
56
"""Test that we are using the right path in the URL."""
57
signed = sign_url("/blah", TOKEN)
58
parsed_signed = urlparse(signed)
59
parsed_sample = urlparse(SAMPLE_SIGNED)
60
self.assertEqual(parsed_signed.path, parsed_sample.path)
62
def test_is_correct_scheme(self):
63
"""Test that we are using the right scheme."""
64
signed = sign_url("/blah", TOKEN)
65
parsed_signed = urlparse(signed)
66
parsed_sample = urlparse(SAMPLE_SIGNED)
68
self.assertEqual(parsed_signed.scheme, parsed_sample.scheme)
70
def test_correct_query(self):
71
"""Test the invariant parts of the signed URL."""
72
signed = sign_url("/blah", TOKEN)
73
parsed_signed = urlparse(signed)
74
parsed_sample = urlparse(SAMPLE_SIGNED)
75
signed_query = parse_qs(parsed_signed.query)
76
sample_query = parse_qs(parsed_sample.query)
80
'oauth_signature_method',
83
self.assertEqual("%s=%s" % (key, signed_query[key]),
84
"%s=%s" % (key, sample_query[key]))
86
def test_url_with_query(self):
87
"""Test that we are using the right scheme."""
88
signed = sign_url("/blah?foo=bar", TOKEN)
89
parsed_signed = urlparse(signed)
90
signed_query = parse_qs(parsed_signed.query)
92
self.assertEqual(signed_query['next'], ['/blah?foo=bar'])
94
def test_uses_timestamper(self):
95
"""Test that the signed url is using the server-relative timestamp."""
97
signed = sign_url("/blah?foo=bar", TOKEN, timestamp)
98
parsed_signed = urlparse(signed)
99
signed_query = parse_qs(parsed_signed.query)
101
self.assertEqual(signed_query['oauth_timestamp'], [str(timestamp)])