Merge lp:~alecu/ubuntu-sso-client/fix-get-token-name into lp:ubuntu-sso-client

Proposed by Alejandro J. Cura
Status: Merged
Approved by: Alejandro J. Cura
Approved revision: 804
Merged at revision: 804
Proposed branch: lp:~alecu/ubuntu-sso-client/fix-get-token-name
Merge into: lp:ubuntu-sso-client
Diff against target: 215 lines (+101/-50)
3 files modified
ubuntu_sso/keyring/__init__.py (+10/-3)
ubuntu_sso/keyring/tests/test_common.py (+89/-0)
ubuntu_sso/keyring/tests/test_linux.py (+2/-47)
To merge this branch: bzr merge lp:~alecu/ubuntu-sso-client/fix-get-token-name
Reviewer Review Type Date Requested Status
Diego Sarmentero (community) Approve
Natalia Bidart (community) Approve
Review via email: mp+79594@code.launchpad.net

Commit message

The hostname can now contain characters in the system encoding. (LP: #875331)

Description of the change

The hostname can now contain characters in the system encoding. (LP: #875331)

To post a comment you must log in.
Revision history for this message
Natalia Bidart (nataliabidart) wrote :

Looks great!

review: Approve
Revision history for this message
Diego Sarmentero (diegosarmentero) wrote :

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'ubuntu_sso/keyring/__init__.py'
--- ubuntu_sso/keyring/__init__.py 2011-03-03 15:19:29 +0000
+++ ubuntu_sso/keyring/__init__.py 2011-10-17 18:30:32 +0000
@@ -40,20 +40,27 @@
40}40}
4141
4242
43def gethostname():
44 """Get the hostname, encoded in utf-8."""
45 sys_encoding = sys.getfilesystemencoding()
46 hostname = socket.gethostname().decode(sys_encoding)
47 return hostname.encode("utf-8")
48
49
43def get_old_token_name(app_name):50def get_old_token_name(app_name):
44 """Build the token name (old style)."""51 """Build the token name (old style)."""
45 quoted_app_name = urllib.quote(app_name)52 quoted_app_name = urllib.quote(app_name)
46 computer_name = socket.gethostname()53 computer_name = gethostname()
47 quoted_computer_name = urllib.quote(computer_name)54 quoted_computer_name = urllib.quote(computer_name)
48 return "%s - %s" % (quoted_app_name, quoted_computer_name)55 return "%s - %s" % (quoted_app_name, quoted_computer_name)
4956
5057
51def get_token_name(app_name):58def get_token_name(app_name):
52 """Build the token name."""59 """Build the token name."""
53 computer_name = socket.gethostname()60 computer_name = gethostname().decode("utf-8")
54 computer_name = computer_name.replace(TOKEN_SEPARATOR,61 computer_name = computer_name.replace(TOKEN_SEPARATOR,
55 SEPARATOR_REPLACEMENT)62 SEPARATOR_REPLACEMENT)
56 return TOKEN_SEPARATOR.join((app_name, computer_name)).encode('utf-8')63 return TOKEN_SEPARATOR.join((app_name, computer_name)).encode("utf-8")
5764
5865
59@inlineCallbacks66@inlineCallbacks
6067
=== added file 'ubuntu_sso/keyring/tests/test_common.py'
--- ubuntu_sso/keyring/tests/test_common.py 1970-01-01 00:00:00 +0000
+++ ubuntu_sso/keyring/tests/test_common.py 2011-10-17 18:30:32 +0000
@@ -0,0 +1,89 @@
1# -*- coding: utf-8 -*-
2#
3# test_keyring - tests for ubuntu_sso.keyring
4#
5# Author: Alejandro J. Cura <alecu@canonical.com>
6# Author: Natalia B. Bidart <natalia.bidart@canonical.com>
7#
8# Copyright 2010-2011 Canonical Ltd.
9#
10# This program is free software: you can redistribute it and/or modify it
11# under the terms of the GNU General Public License version 3, as published
12# by the Free Software Foundation.
13#
14# This program is distributed in the hope that it will be useful, but
15# WITHOUT ANY WARRANTY; without even the implied warranties of
16# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
17# PURPOSE. See the GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License along
20# with this program. If not, see <http://www.gnu.org/licenses/>.
21"""Tests for the keyring common module."""
22
23from twisted.trial.unittest import TestCase
24
25from ubuntu_sso import keyring
26
27
28class TestGetHostname(TestCase):
29 """Test the function that gets the hostname."""
30
31 def test_get_hostname(self):
32 """The common case."""
33 fake_hostname = "fake hostname"
34 self.patch(keyring.socket, "gethostname", lambda: fake_hostname)
35 self.assertEqual(keyring.gethostname(), fake_hostname)
36
37 def test_get_hostname_uses_filesystem_encoding(self):
38 """The fs encoding is used to decode the name returned by socket."""
39 fake_hostname = u"Привет-ПК"
40 hostname_koi8r = fake_hostname.encode("koi8-r")
41 hostname_utf8 = fake_hostname.encode("utf-8")
42 self.patch(keyring.socket, "gethostname", lambda: hostname_koi8r)
43 self.patch(keyring.sys, "getfilesystemencoding", lambda: "koi8-r")
44 self.assertEqual(keyring.gethostname(), hostname_utf8)
45
46
47class TestTokenNameBuilder(TestCase):
48 """Test the function that builds the token name."""
49
50 def check_build(self, sample_app_name, sample_hostname, expected_result):
51 """Check the build of a given token."""
52 self.patch(keyring, "gethostname", lambda *a: sample_hostname)
53 result = keyring.get_token_name(sample_app_name)
54 self.assertEqual(result, expected_result)
55
56 def test_get_simple_token_name(self):
57 """A simple token name is built right."""
58 sample_app_name = u"UbuntuTwo"
59 sample_hostname = "Darkstar"
60 expected_result = "UbuntuTwo @ Darkstar"
61 self.check_build(sample_app_name, sample_hostname, expected_result)
62
63 def test_get_complex_token_name_for_app_name(self):
64 """A complex token name is built right too."""
65 sample_app_name = u"Ubuntu @ Eleven"
66 sample_hostname = "Mate+Cocido"
67 expected_result = "Ubuntu @ Eleven @ Mate+Cocido"
68 self.check_build(sample_app_name, sample_hostname, expected_result)
69
70 def test_get_complex_token_name_for_hostname(self):
71 """A complex token name is built right too."""
72 sample_app_name = u"Ubuntu Eleven"
73 sample_hostname = "Mate @ Cocido"
74 expected_result = "Ubuntu Eleven @ Mate AT Cocido"
75 self.check_build(sample_app_name, sample_hostname, expected_result)
76
77 def test_get_unicode_appname_token_name(self):
78 """A token name with unicode in the app name."""
79 sample_app_name = u"Ubuntu 四百六十九"
80 sample_hostname = "Darkstar"
81 expected_result = u"Ubuntu 四百六十九 @ Darkstar".encode("utf-8")
82 self.check_build(sample_app_name, sample_hostname, expected_result)
83
84 def test_get_utf8_hostname_token_name(self):
85 """A token name with utf8 in the host name."""
86 sample_app_name = u"Ubuntu Eleven"
87 sample_hostname = u"Привет-ПК".encode("utf-8")
88 expected_result = u"Ubuntu Eleven @ Привет-ПК".encode("utf-8")
89 self.check_build(sample_app_name, sample_hostname, expected_result)
090
=== modified file 'ubuntu_sso/keyring/tests/test_linux.py'
--- ubuntu_sso/keyring/tests/test_linux.py 2011-03-18 09:30:20 +0000
+++ ubuntu_sso/keyring/tests/test_linux.py 2011-10-17 18:30:32 +0000
@@ -1,6 +1,6 @@
1# -*- coding: utf-8 -*-1# -*- coding: utf-8 -*-
2#2#
3# test_keyring - tests for ubuntu_sso.keyring3# test_linux - tests for ubuntu_sso.keyring under linux
4#4#
5# Author: Alejandro J. Cura <alecu@canonical.com>5# Author: Alejandro J. Cura <alecu@canonical.com>
6# Author: Natalia B. Bidart <natalia.bidart@canonical.com>6# Author: Natalia B. Bidart <natalia.bidart@canonical.com>
@@ -20,8 +20,6 @@
20# with this program. If not, see <http://www.gnu.org/licenses/>.20# with this program. If not, see <http://www.gnu.org/licenses/>.
21"""Tests for the keyring.py module."""21"""Tests for the keyring.py module."""
2222
23import socket
24
25from twisted.internet import defer23from twisted.internet import defer
26from twisted.internet.defer import inlineCallbacks24from twisted.internet.defer import inlineCallbacks
27from twisted.trial.unittest import TestCase25from twisted.trial.unittest import TestCase
@@ -31,11 +29,6 @@
31from ubuntu_sso.tests import APP_NAME29from ubuntu_sso.tests import APP_NAME
3230
3331
34def build_fake_gethostname(fake_hostname):
35 """Return a fake hostname getter."""
36 return lambda *a: fake_hostname
37
38
39class MockItem(object):32class MockItem(object):
40 """An item contains a secret, lookup attributes and has a label."""33 """An item contains a secret, lookup attributes and has a label."""
4134
@@ -116,43 +109,6 @@
116 return defer.succeed(self.collections["default"])109 return defer.succeed(self.collections["default"])
117110
118111
119class TestTokenNameBuilder(TestCase):
120 """Test the method that builds the token name."""
121
122 def test_get_simple_token_name(self):
123 """A simple token name is built right."""
124 sample_app_name = "UbuntuTwo"
125 sample_hostname = "Darkstar"
126 expected_result = "UbuntuTwo @ Darkstar"
127
128 fake_gethostname = build_fake_gethostname(sample_hostname)
129 self.patch(socket, "gethostname", fake_gethostname)
130 result = keyring.get_token_name(sample_app_name)
131 self.assertEqual(result, expected_result)
132
133 def test_get_complex_token_name_for_app_name(self):
134 """A complex token name is built right too."""
135 sample_app_name = "Ubuntu @ Eleven"
136 sample_hostname = "Mate+Cocido"
137 expected_result = "Ubuntu @ Eleven @ Mate+Cocido"
138
139 fake_gethostname = build_fake_gethostname(sample_hostname)
140 self.patch(socket, "gethostname", fake_gethostname)
141 result = keyring.get_token_name(sample_app_name)
142 self.assertEqual(result, expected_result)
143
144 def test_get_complex_token_name_for_hostname(self):
145 """A complex token name is built right too."""
146 sample_app_name = "Ubuntu Eleven"
147 sample_hostname = "Mate @ Cocido"
148 expected_result = "Ubuntu Eleven @ Mate AT Cocido"
149
150 fake_gethostname = build_fake_gethostname(sample_hostname)
151 self.patch(socket, "gethostname", fake_gethostname)
152 result = keyring.get_token_name(sample_app_name)
153 self.assertEqual(result, expected_result)
154
155
156class TestKeyring(TestCase):112class TestKeyring(TestCase):
157 """Test the keyring related functions."""113 """Test the keyring related functions."""
158114
@@ -163,8 +119,7 @@
163 self.mock_service = None119 self.mock_service = None
164 self.service = self.patch(keyring, "SecretService",120 self.service = self.patch(keyring, "SecretService",
165 self.get_mock_service)121 self.get_mock_service)
166 fake_gethostname = build_fake_gethostname("darkstar")122 self.patch(common_keyring, "gethostname", lambda: "darkstar")
167 self.patch(socket, "gethostname", fake_gethostname)
168123
169 def get_mock_service(self):124 def get_mock_service(self):
170 """Create only one instance of the mock service per test."""125 """Create only one instance of the mock service per test."""

Subscribers

People subscribed via source and target branches