~dobey/ubuntu/oneiric/ubuntuone-control-panel/release-113

« back to all changes in this revision

Viewing changes to ubuntuone/controlpanel/gui/tests/test_url_sign.py

  • Committer: Sebastien Bacher
  • Date: 2011-07-25 13:17:38 UTC
  • mfrom: (25.1.2 ubuntuone-control-panel)
  • Revision ID: seb128@ubuntu.com-20110725131738-yuevatnd859d1phs
Tags: 1.1.1-0ubuntu1
releasing version 1.1.1-0ubuntu1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
 
 
3
# Authors: Roberto Alsina <roberto.alsina@canonical.com>
 
4
#
 
5
# Copyright 2011 Canonical Ltd.
 
6
#
 
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.
 
10
#
 
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.
 
15
#
 
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/>.
 
18
 
 
19
"""Tests for the url signing function."""
 
20
 
 
21
from urlparse import urlparse, parse_qs
 
22
 
 
23
from ubuntuone.controlpanel.tests import TestCase
 
24
from ubuntuone.controlpanel.gui import sign_url
 
25
 
 
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'}
 
31
 
 
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'
 
37
 
 
38
#pylint: disable=E1101
 
39
 
 
40
 
 
41
class SignURLTestCase(TestCase):
 
42
 
 
43
    """Test cases for the URL signing function."""
 
44
 
 
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)
 
50
 
 
51
        self.assertEqual(parsed_signed.netloc, parsed_sample.netloc)
 
52
 
 
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)
 
58
 
 
59
        self.assertEqual(parsed_signed.scheme, parsed_sample.scheme)
 
60
 
 
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)
 
68
 
 
69
        for key in ('next',
 
70
            'oauth_consumer_key',
 
71
            'oauth_signature_method',
 
72
            'oauth_token',
 
73
            'oauth_version'):
 
74
            self.assertEqual("%s=%s" % (key, signed_query[key]),
 
75
            "%s=%s" % (key, sample_query[key]))
 
76
 
 
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)
 
82
 
 
83
        self.assertEqual(signed_query['next'], ['/blah?foo=bar'])