~ubuntu-branches/ubuntu/precise/landscape-client/precise

« back to all changes in this revision

Viewing changes to landscape/ui/model/configuration/tests/test_mechanism.py

  • Committer: Package Import Robot
  • Author(s): Andreas Hasenack
  • Date: 2012-03-19 09:33:34 UTC
  • mto: This revision was merged to the branch mainline in revision 41.
  • Revision ID: package-import@ubuntu.com-20120319093334-oxjttz163vvfgq8s
Tags: upstream-12.04
ImportĀ upstreamĀ versionĀ 12.04

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import dbus
 
2
 
 
3
from landscape.configuration import LandscapeSetupConfiguration
 
4
from landscape.tests.helpers import LandscapeTest
 
5
from landscape.ui.tests.helpers import (
 
6
    got_gobject_introspection, gobject_skip_message)
 
7
if got_gobject_introspection:
 
8
    from landscape.ui.model.configuration.mechanism import (
 
9
        ConfigurationMechanism, INTERFACE_NAME)
 
10
 
 
11
 
 
12
class MechanismTest(LandscapeTest):
 
13
    """
 
14
    Test that we can use mechanism calls successfully from within a secure
 
15
    context (the easiest to achieve is in-process calls.
 
16
    """
 
17
 
 
18
    def setUp(self):
 
19
        super(MechanismTest, self).setUp()
 
20
        config = "[client]"
 
21
        config += "data_path = /var/lib/landscape/client\n"
 
22
        config += "http_proxy = http://proxy.localdomain:3192\n"
 
23
        config += "tags = a_tag\n"
 
24
        config += "url = https://landscape.canonical.com/message-system\n"
 
25
        config += "account_name = foo\n"
 
26
        config += "registration_password = boink\n"
 
27
        config += "computer_title = baz\n"
 
28
        config += "https_proxy = https://proxy.localdomain:6192\n"
 
29
        config += "ping_url = http://landscape.canonical.com/ping\n"
 
30
        self.config_filename = self.makeFile(config)
 
31
 
 
32
        class MyLandscapeSetupConfiguration(LandscapeSetupConfiguration):
 
33
            default_config_filenames = [self.config_filename]
 
34
 
 
35
        self.config = MyLandscapeSetupConfiguration()
 
36
        bus_name = dbus.service.BusName(INTERFACE_NAME, MechanismTest.bus)
 
37
        self.mechanism = ConfigurationMechanism(self.config, bus_name)
 
38
        self.config.load(["-c", self.config_filename])
 
39
 
 
40
    def tearDown(self):
 
41
        self.mechanism.remove_from_connection()
 
42
        super(MechanismTest, self).tearDown()
 
43
 
 
44
    def test_is_local_call(self):
 
45
        """
 
46
        Test simple mechanism for checking if a call is local does the right
 
47
        thing.  Anything passed to this function that is not L{None} will
 
48
        result in is returning False - this in turn means that bypassing
 
49
        security will not happen, which is the right thing in failure cases
 
50
        too.
 
51
        """
 
52
        self.assertTrue(self.mechanism._is_local_call(None, None))
 
53
        self.assertFalse(self.mechanism._is_local_call(True, True))
 
54
 
 
55
    def test_get_account_name(self):
 
56
        """
 
57
        Test we can get account name from the mechanism.
 
58
        """
 
59
        self.assertEqual("foo", self.mechanism.get("account_name"))
 
60
 
 
61
    def test_set_account_name(self):
 
62
        """
 
63
        Test we can set the account name via the mechanism.
 
64
        """
 
65
        self.mechanism.set("account_name", "bar")
 
66
        self.assertEqual("bar", self.mechanism.get("account_name"))
 
67
 
 
68
    def test_set_account_name_unicode(self):
 
69
        """
 
70
        Non-ascii characters are replaced before passing to underlying config.
 
71
        """
 
72
        self.mechanism.set("account_name", u"unicode\u00a3unicode")
 
73
        self.assertEqual("unicode?unicode", self.mechanism.get("account_name"))
 
74
 
 
75
    def test_no_unicode_to_underlying_config(self):
 
76
        """
 
77
        Non-ascii characters are replaced before passing to underlying config.
 
78
        """
 
79
        class FakeConfig(object):
 
80
            def __init__(self):
 
81
                self.account_name = None
 
82
 
 
83
        fake_config = FakeConfig()
 
84
        self.mechanism._config = fake_config
 
85
        self.mechanism.set("account_name", u"unicode\u00a3unicode")
 
86
        self.assertEqual("unicode?unicode", fake_config.account_name)
 
87
 
 
88
    def test_get_data_path(self):
 
89
        """
 
90
        Test we can get the data path from the mechanism.
 
91
        """
 
92
        self.assertEqual("/var/lib/landscape/client/",
 
93
                         self.mechanism.get("data_path"))
 
94
 
 
95
    def set_data_path(self):
 
96
        """
 
97
        Test we can set the data path via the mechanism.
 
98
        """
 
99
        self.mechanism.set("data_path", "bar")
 
100
        self.assertEqual("bar", self.mechanism.get("data_path"))
 
101
 
 
102
    def test_get_http_proxy(self):
 
103
        """
 
104
        Test that we can get the HTTP proxy from the mechanism.
 
105
        """
 
106
        self.assertEqual("http://proxy.localdomain:3192",
 
107
                         self.mechanism.get("http_proxy"))
 
108
 
 
109
    def test_set_http_proxy(self):
 
110
        """
 
111
        Test that we can set the HTTP proxy via the mechanism.
 
112
        """
 
113
        self.mechanism.set("http_proxy", "bar")
 
114
        self.assertEqual("bar", self.mechanism.get("http_proxy"))
 
115
 
 
116
    def test_get_tags(self):
 
117
        """
 
118
        Test that we can get Tags from the mechanism.
 
119
        """
 
120
        self.assertEqual("a_tag", self.mechanism.get("tags"))
 
121
 
 
122
    def test_set_tags(self):
 
123
        """
 
124
        Test that we can set Tags via the mechanism.
 
125
        """
 
126
        self.mechanism.set("tags", "bar")
 
127
        self.assertEqual("bar", self.mechanism.get("tags"))
 
128
 
 
129
    def test_get_url(self):
 
130
        """
 
131
        Test that we can get URL from the mechanism.
 
132
        """
 
133
        self.assertEqual("https://landscape.canonical.com/message-system",
 
134
                          self.mechanism.get("url"))
 
135
 
 
136
    def test_set_url(self):
 
137
        """
 
138
        Test that we can set the URL via the mechanisms.
 
139
        """
 
140
        self.mechanism.set("url", "bar")
 
141
        self.assertEqual(self.mechanism.get("url"), "bar")
 
142
 
 
143
    def test_get_ping_url(self):
 
144
        """
 
145
        Test that we can get the Ping URL from the mechanism.
 
146
        """
 
147
        self.assertEqual("http://landscape.canonical.com/ping",
 
148
                          self.mechanism.get("ping_url"))
 
149
 
 
150
    def test_set_ping_url(self):
 
151
        """
 
152
        Test that we can set the Ping URL via the mechanism.
 
153
        """
 
154
        self.mechanism.set("ping_url", "bar")
 
155
        self.assertEqual("bar", self.mechanism.get("ping_url"))
 
156
 
 
157
    def test_get_registration_password(self):
 
158
        """
 
159
        Test that we can get the registration password from the mechanism.
 
160
        """
 
161
        self.assertEqual("boink", self.mechanism.get("registration_password"))
 
162
 
 
163
    def test_set_registration_password(self):
 
164
        """
 
165
        Test that we can set the registration password via the mechanism.
 
166
        """
 
167
        self.mechanism.set("registration_password", "bar")
 
168
        self.assertEqual("bar", self.mechanism.get("registration_password"))
 
169
 
 
170
    def test_get_computer_title(self):
 
171
        """
 
172
        Test that we can get the computer title from the mechanism.
 
173
        """
 
174
        self.assertEqual("baz", self.mechanism.get("computer_title"))
 
175
 
 
176
    def test_set_computer_title(self):
 
177
        """
 
178
        Test that we can set the computer title via the mechanism.
 
179
        """
 
180
        self.mechanism.set("computer_title", "bar")
 
181
        self.assertEqual("bar", self.mechanism.get("computer_title"))
 
182
 
 
183
    def test_get_https_proxy(self):
 
184
        """
 
185
        Test that we can get the HTTPS Proxy from the mechanism.
 
186
        """
 
187
        self.assertEqual("https://proxy.localdomain:6192",
 
188
                         self.mechanism.get("https_proxy"))
 
189
 
 
190
    def test_set_https_proxy(self):
 
191
        """
 
192
        Test that we can set the HTTPS Proxy via the mechanism.
 
193
        """
 
194
        self.mechanism.set("https_proxy", "bar")
 
195
        self.assertEqual("bar", self.mechanism.get("https_proxy"))
 
196
 
 
197
    def test_exit(self):
 
198
        """
 
199
        Test that we cause the mechanism to exit.
 
200
        """
 
201
        self.assertRaises(SystemExit, self.mechanism.exit)
 
202
 
 
203
    if not got_gobject_introspection:
 
204
        skip = gobject_skip_message
 
205
    else:
 
206
        dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
 
207
        try:
 
208
            bus = dbus.SessionBus(private=True)
 
209
        except dbus.exceptions.DBusException:
 
210
            skip = "Cannot create private DBus session without X11"