1
# -*- coding: utf-8 -*-
3
# Authors: Roberto Alsina <roberto.alsina@canonical.com>
5
# Copyright 2011 Canonical Ltd.
7
# This program is free software: you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License version 3, as published
9
# by the Free Software Foundation.
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranties of
13
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
14
# PURPOSE. See the GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License along
17
# with this program. If not, see <http://www.gnu.org/licenses/>.
19
"""Tests for the url signing function."""
21
from urlparse import urlparse, parse_qs
23
from ubuntuone.controlpanel.tests import TestCase
24
from ubuntuone.controlpanel.gui import sign_url
26
TOKEN = {u'consumer_key': u'consumer_key',
27
u'consumer_secret': u'consumer_secret',
28
u'token_name': u'test_token',
29
u'token': u'GkInOfSMGwTXAUoVQwLUoPxElEEUdhsLVNTPhxHJDUIeHCPNEo',
30
u'token_secret': u'qFYImEtlczPbsCnYyuwLoPDlPEnvNcIktZphPQklAWrvyfFMV'}
32
SAMPLE_SIGNED = 'https://one.ubuntu.com/api/1.0/from_oauth?oauth_nonce=' \
33
'36886134&oauth_timestamp=1310671062&oauth_consumer_key=consumer_key&' \
34
'oauth_signature_method=HMAC-SHA1&next=%2Fblah&oauth_version=1.0&' \
35
'oauth_token=GkInOfSMGwTXAUoVQwLUoPxElEEUdhsLVNTPhxHJDUIeHCPNEo&' \
36
'oauth_signature=s6h0LRBiWchTADrTJWaJUSuaGpo%3D'
38
#pylint: disable=E1101
41
class SignURLTestCase(TestCase):
43
"""Test cases for the URL signing function."""
45
def test_is_correct_domain(self):
46
"""Test that we are using the right domain."""
47
signed = sign_url("/blah", TOKEN)
48
parsed_signed = urlparse(signed)
49
parsed_sample = urlparse(SAMPLE_SIGNED)
51
self.assertEqual(parsed_signed.netloc, parsed_sample.netloc)
53
def test_is_correct_scheme(self):
54
"""Test that we are using the right scheme."""
55
signed = sign_url("/blah", TOKEN)
56
parsed_signed = urlparse(signed)
57
parsed_sample = urlparse(SAMPLE_SIGNED)
59
self.assertEqual(parsed_signed.scheme, parsed_sample.scheme)
61
def test_correct_query(self):
62
"""Test the invariant parts of the signed URL."""
63
signed = sign_url("/blah", TOKEN)
64
parsed_signed = urlparse(signed)
65
parsed_sample = urlparse(SAMPLE_SIGNED)
66
signed_query = parse_qs(parsed_signed.query)
67
sample_query = parse_qs(parsed_sample.query)
71
'oauth_signature_method',
74
self.assertEqual("%s=%s" % (key, signed_query[key]),
75
"%s=%s" % (key, sample_query[key]))
77
def test_url_with_query(self):
78
"""Test that we are using the right scheme."""
79
signed = sign_url("/blah?foo=bar", TOKEN)
80
parsed_signed = urlparse(signed)
81
signed_query = parse_qs(parsed_signed.query)
83
self.assertEqual(signed_query['next'], ['/blah?foo=bar'])