1
1
# -*- coding: utf-8 -*-
3
# test_keyring - tests for ubuntu_sso.keyring
3
# test_linux - tests for ubuntu_sso.keyring under linux
5
5
# Author: Alejandro J. Cura <alecu@canonical.com>
6
6
# Author: Natalia B. Bidart <natalia.bidart@canonical.com>
20
20
# with this program. If not, see <http://www.gnu.org/licenses/>.
21
21
"""Tests for the keyring.py module."""
25
23
from twisted.internet import defer
26
24
from twisted.internet.defer import inlineCallbacks
27
25
from twisted.trial.unittest import TestCase
31
29
from ubuntu_sso.tests import APP_NAME
34
def build_fake_gethostname(fake_hostname):
35
"""Return a fake hostname getter."""
36
return lambda *a: fake_hostname
39
32
class MockItem(object):
40
33
"""An item contains a secret, lookup attributes and has a label."""
116
109
return defer.succeed(self.collections["default"])
119
class TestTokenNameBuilder(TestCase):
120
"""Test the method that builds the token name."""
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"
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)
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"
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)
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"
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)
156
112
class TestKeyring(TestCase):
157
113
"""Test the keyring related functions."""
163
119
self.mock_service = None
164
120
self.service = self.patch(keyring, "SecretService",
165
121
self.get_mock_service)
166
fake_gethostname = build_fake_gethostname("darkstar")
167
self.patch(socket, "gethostname", fake_gethostname)
122
self.patch(common_keyring, "gethostname", lambda: "darkstar")
169
124
def get_mock_service(self):
170
125
"""Create only one instance of the mock service per test."""