~nataliabidart/ubuntuone-control-panel/use-webclient

« back to all changes in this revision

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

  • Committer: Natalia B. Bidart
  • Date: 2012-02-06 15:23:27 UTC
  • Revision ID: natalia.bidart@canonical.com-20120206152327-ws8y5syq0xmbifxg
AllĀ green.

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
 
# Authors: Alejandro J. Cura <alecu@canonical.com>
5
 
#
6
 
# Copyright 2011 Canonical Ltd.
7
 
#
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.
11
 
#
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.
16
 
#
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/>.
19
 
 
20
 
"""Tests for the url signing function."""
21
 
 
22
 
from urlparse import urlparse, parse_qs
23
 
 
24
 
from ubuntuone.controlpanel.tests import TestCase
25
 
from ubuntuone.controlpanel.gui import (
26
 
    UBUNTUONE_FROM_OAUTH,
27
 
)
28
 
 
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'}
34
 
 
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'
40
 
 
41
 
#pylint: disable=E1101
42
 
 
43
 
 
44
 
class SignURLTestCase(TestCase):
45
 
 
46
 
    """Test cases for the URL signing function."""
47
 
 
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)
54
 
 
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)
61
 
 
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)
67
 
 
68
 
        self.assertEqual(parsed_signed.scheme, parsed_sample.scheme)
69
 
 
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)
77
 
 
78
 
        for key in ('next',
79
 
            'oauth_consumer_key',
80
 
            'oauth_signature_method',
81
 
            'oauth_token',
82
 
            'oauth_version'):
83
 
            self.assertEqual("%s=%s" % (key, signed_query[key]),
84
 
            "%s=%s" % (key, sample_query[key]))
85
 
 
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)
91
 
 
92
 
        self.assertEqual(signed_query['next'], ['/blah?foo=bar'])
93
 
 
94
 
    def test_uses_timestamper(self):
95
 
        """Test that the signed url is using the server-relative timestamp."""
96
 
        timestamp = 999
97
 
        signed = sign_url("/blah?foo=bar", TOKEN, timestamp)
98
 
        parsed_signed = urlparse(signed)
99
 
        signed_query = parse_qs(parsed_signed.query)
100
 
 
101
 
        self.assertEqual(signed_query['oauth_timestamp'], [str(timestamp)])