~ahasenack/landscape-client/landscape-client-1.5.5-0ubuntu0.9.04.0

« back to all changes in this revision

Viewing changes to landscape/broker/tests/test_registration.py

  • Committer: Bazaar Package Importer
  • Author(s): Rick Clark
  • Date: 2008-09-08 16:35:57 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080908163557-l3ixzj5dxz37wnw2
Tags: 1.0.18-0ubuntu1
New upstream release 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import logging
 
2
 
 
3
from landscape.broker.registration import (
 
4
    RegistrationHandler, Identity, InvalidCredentialsError)
 
5
 
 
6
from landscape.tests.helpers import LandscapeTest, ExchangeHelper
 
7
 
 
8
 
 
9
class RegistrationTest(LandscapeTest):
 
10
 
 
11
    helpers = [ExchangeHelper]
 
12
 
 
13
    def setUp(self):
 
14
        super(RegistrationTest, self).setUp()
 
15
        self.config = self.broker_service.config
 
16
        self.identity = self.broker_service.identity
 
17
        self.handler = self.broker_service.registration
 
18
        logging.getLogger().setLevel(logging.INFO)
 
19
 
 
20
    def mock_gethostname(self, replay=True):
 
21
        gethostname_mock = self.mocker.replace("socket.gethostname")
 
22
        gethostname_mock()
 
23
        self.mocker.result("ooga")
 
24
        if replay:
 
25
            self.mocker.replay()
 
26
 
 
27
    def check_persist_property(self, attr, persist_name):
 
28
        value = "VALUE"
 
29
        self.assertEquals(getattr(self.identity, attr), None,
 
30
                          "%r attribute should default to None, not %r" %
 
31
                          (attr, getattr(self.identity, attr)))
 
32
        setattr(self.identity, attr, value)
 
33
        self.assertEquals(getattr(self.identity, attr), value,
 
34
                          "%r attribute should be %r, not %r" %
 
35
                          (attr, value, getattr(self.identity, attr)))
 
36
        self.assertEquals(self.persist.get(persist_name), value,
 
37
                          "%r not set to %r in persist" % (persist_name, value))
 
38
 
 
39
    def check_config_property(self, attr):
 
40
        value = "VALUE"
 
41
        setattr(self.config, attr, value)
 
42
        self.assertEquals(getattr(self.identity, attr), value,
 
43
                          "%r attribute should be %r, not %r" %
 
44
                          (attr, value, getattr(self.identity, attr)))
 
45
 
 
46
    def test_secure_id(self):
 
47
        self.check_persist_property("secure_id",
 
48
                                    "registration.secure-id")
 
49
 
 
50
    def test_insecure_id(self):
 
51
        self.check_persist_property("insecure_id",
 
52
                                    "registration.insecure-id")
 
53
 
 
54
    def test_computer_title(self):
 
55
        self.check_config_property("computer_title")
 
56
 
 
57
    def test_account_name(self):
 
58
        self.check_config_property("account_name")
 
59
 
 
60
    def test_registration_password(self):
 
61
        self.check_config_property("registration_password")
 
62
 
 
63
    def test_server_initiated_id_changing(self):
 
64
        """
 
65
        The server must be able to ask a client to change its secure
 
66
        and insecure ids even if no requests were sent.
 
67
        """
 
68
        self.reactor.fire("message",
 
69
                          {"type": "set-id", "id": "abc", "insecure-id": "def"})
 
70
        self.assertEquals(self.identity.secure_id, "abc")
 
71
        self.assertEquals(self.identity.insecure_id, "def")
 
72
 
 
73
    def test_registration_done_event(self):
 
74
        """
 
75
        When new ids are received from the server, a "registration-done"
 
76
        event is fired.
 
77
        """
 
78
        reactor_mock = self.mocker.patch(self.reactor)
 
79
        reactor_mock.fire("registration-done")
 
80
        self.mocker.replay()
 
81
        self.reactor.fire("message",
 
82
                          {"type": "set-id", "id": "abc", "insecure-id": "def"})
 
83
 
 
84
    def test_unknown_id(self):
 
85
        self.identity.secure_id = "old_id"
 
86
        self.identity.insecure_id = "old_id"
 
87
        self.mstore.set_accepted_types(["register"])
 
88
        self.reactor.fire("message", {"type": "unknown-id"})
 
89
        self.assertEquals(self.identity.secure_id, None)
 
90
        self.assertEquals(self.identity.insecure_id, None)
 
91
 
 
92
    def test_should_register(self):
 
93
        self.mstore.set_accepted_types(["register"])
 
94
        self.config.computer_title = "Computer Title"
 
95
        self.config.account_name = "account_name"
 
96
        self.assertTrue(self.handler.should_register())
 
97
 
 
98
    def test_should_register_with_existing_id(self):
 
99
        self.mstore.set_accepted_types(["register"])
 
100
        self.identity.secure_id = "secure"
 
101
        self.config.computer_title = "Computer Title"
 
102
        self.config.account_name = "account_name"
 
103
        self.assertFalse(self.handler.should_register())
 
104
 
 
105
    def test_should_register_without_computer_title(self):
 
106
        self.mstore.set_accepted_types(["register"])
 
107
        self.config.computer_title = None
 
108
        self.assertFalse(self.handler.should_register())
 
109
 
 
110
    def test_should_register_without_account_name(self):
 
111
        self.mstore.set_accepted_types(["register"])
 
112
        self.config.account_name = None
 
113
        self.assertFalse(self.handler.should_register())
 
114
 
 
115
    def test_should_register_with_unaccepted_message(self):
 
116
        self.assertFalse(self.handler.should_register())
 
117
 
 
118
    def test_queue_message_on_exchange(self):
 
119
        """
 
120
        When a computer_title and account_name are available, no
 
121
        secure_id is set, and an exchange is about to happen,
 
122
        queue a registration message.
 
123
        """
 
124
        self.mock_gethostname()
 
125
        self.mstore.set_accepted_types(["register"])
 
126
        self.config.computer_title = "Computer Title"
 
127
        self.config.account_name = "account_name"
 
128
        self.reactor.fire("pre-exchange")
 
129
        self.assertMessages(self.mstore.get_pending_messages(),
 
130
                            [{"type": "register",
 
131
                              "computer_title": "Computer Title",
 
132
                              "account_name": "account_name",
 
133
                              "registration_password": None,
 
134
                              "hostname": "ooga"}
 
135
                            ])
 
136
        self.assertEquals(self.logfile.getvalue().strip(),
 
137
                          "INFO: Queueing message to register with account "
 
138
                          "'account_name' without a password.")
 
139
 
 
140
    def test_queue_message_on_exchange_with_password(self):
 
141
        """If a registration password is available, we pass it on!"""
 
142
        self.mock_gethostname()
 
143
        self.mstore.set_accepted_types(["register"])
 
144
        self.config.computer_title = "Computer Title"
 
145
        self.config.account_name = "account_name"
 
146
        self.config.registration_password = "SEKRET"
 
147
        self.reactor.fire("pre-exchange")
 
148
        self.assertMessages(self.mstore.get_pending_messages(),
 
149
                            [{"type": "register",
 
150
                              "computer_title": "Computer Title",
 
151
                              "account_name": "account_name",
 
152
                              "registration_password": "SEKRET",
 
153
                              "hostname": "ooga"}
 
154
                            ])
 
155
        self.assertEquals(self.logfile.getvalue().strip(),
 
156
                          "INFO: Queueing message to register with account "
 
157
                          "'account_name' with a password.")
 
158
 
 
159
    def test_queueing_registration_message_resets_message_store(self):
 
160
        """
 
161
        When a registration message is queued, the store is reset
 
162
        entirely, since everything else that was queued is meaningless
 
163
        now that we're trying to register again.
 
164
        """
 
165
        self.mstore.set_accepted_types(["register", "test"])
 
166
        self.mstore.add({"type": "test"})
 
167
        self.config.computer_title = "Computer Title"
 
168
        self.config.account_name = "account_name"
 
169
        self.reactor.fire("pre-exchange")
 
170
        messages = self.mstore.get_pending_messages()
 
171
        self.assertEquals(len(messages), 1)
 
172
        self.assertEquals(messages[0]["type"], "register")
 
173
 
 
174
    def test_no_message_when_should_register_is_false(self):
 
175
        """If we already have a secure id, do not queue a register message.
 
176
        """
 
177
        handler_mock = self.mocker.patch(self.handler)
 
178
        handler_mock.should_register()
 
179
        self.mocker.result(False)
 
180
 
 
181
        self.mstore.set_accepted_types(["register"])
 
182
        self.config.computer_title = "Computer Title"
 
183
        self.config.account_name = "account_name"
 
184
 
 
185
        # If we didn't fake it, it'd work.  We do that to ensure that
 
186
        # all the needed data is in place, and that this method is
 
187
        # really what decides if a message is sent or not.  This way
 
188
        # we can test it individually.
 
189
        self.assertTrue(self.handler.should_register())
 
190
 
 
191
        # Now let's see.
 
192
        self.mocker.replay()
 
193
 
 
194
        self.reactor.fire("pre-exchange")
 
195
        self.assertMessages(self.mstore.get_pending_messages(), [])
 
196
 
 
197
    def test_registration_failed_event(self):
 
198
        """
 
199
        The deferred returned by a registration request should fail
 
200
        with L{InvalidCredentialsError} if the server responds with a
 
201
        failure message.
 
202
        """
 
203
        reactor_mock = self.mocker.patch(self.reactor)
 
204
        reactor_mock.fire("registration-failed")
 
205
        self.mocker.replay()
 
206
        self.reactor.fire("message",
 
207
                          {"type": "registration", "info": "unknown-account"})
 
208
 
 
209
    def test_registration_failed_event_not_fired_when_uncertain(self):
 
210
        """
 
211
        If the data in the registration message isn't what we expect,
 
212
        the event isn't fired.
 
213
        """
 
214
        reactor_mock = self.mocker.patch(self.reactor)
 
215
        reactor_mock.fire("registration-failed")
 
216
        self.mocker.count(0)
 
217
        self.mocker.replay()
 
218
        self.reactor.fire("message",
 
219
                          {"type": "registration", "info": "blah-blah"})
 
220
 
 
221
    def test_register_resets_ids(self):
 
222
        self.identity.secure_id = "foo"
 
223
        self.identity.insecure_id = "bar"
 
224
        self.handler.register()
 
225
        self.assertEquals(self.identity.secure_id, None)
 
226
        self.assertEquals(self.identity.insecure_id, None)
 
227
 
 
228
    def test_register_calls_urgent_exchange(self):
 
229
        exchanger_mock = self.mocker.patch(self.exchanger)
 
230
        exchanger_mock.exchange()
 
231
        self.mocker.passthrough()
 
232
        self.mocker.replay()
 
233
        self.handler.register()
 
234
 
 
235
    def test_register_deferred_called_on_done(self):
 
236
        # We don't want informational messages.
 
237
        self.logger.setLevel(logging.WARNING)
 
238
 
 
239
        calls = [0]
 
240
        d = self.handler.register()
 
241
        def add_call(result):
 
242
            self.assertEquals(result, None)
 
243
            calls[0] += 1
 
244
        d.addCallback(add_call)
 
245
 
 
246
        # This should somehow callback the deferred.
 
247
        self.reactor.fire("message",
 
248
                          {"type": "set-id", "id": "abc", "insecure-id": "def"})
 
249
 
 
250
        self.assertEquals(calls, [1])
 
251
 
 
252
        # Doing it again to ensure that the deferred isn't called twice.
 
253
        self.reactor.fire("message",
 
254
                          {"type": "set-id", "id": "abc", "insecure-id": "def"})
 
255
 
 
256
        self.assertEquals(calls, [1])
 
257
 
 
258
        self.assertEquals(self.logfile.getvalue(), "")
 
259
 
 
260
    def test_resynchronize_fired_when_registration_done(self):
 
261
 
 
262
        results = []
 
263
        def append():
 
264
            results.append(True)
 
265
        self.reactor.call_on("resynchronize-clients", append)
 
266
 
 
267
        self.handler.register()
 
268
 
 
269
        # This should somehow callback the deferred.
 
270
        self.reactor.fire("message",
 
271
                          {"type": "set-id", "id": "abc", "insecure-id": "def"})
 
272
 
 
273
        self.assertEquals(results, [True])
 
274
 
 
275
    def test_register_deferred_called_on_failed(self):
 
276
        # We don't want informational messages.
 
277
        self.logger.setLevel(logging.WARNING)
 
278
 
 
279
        calls = [0]
 
280
        d = self.handler.register()
 
281
        def add_call(failure):
 
282
            exception = failure.value
 
283
            self.assertTrue(isinstance(exception, InvalidCredentialsError))
 
284
            calls[0] += 1
 
285
        d.addErrback(add_call)
 
286
 
 
287
        # This should somehow callback the deferred.
 
288
        self.reactor.fire("message",
 
289
                          {"type": "registration", "info": "unknown-account"})
 
290
 
 
291
        self.assertEquals(calls, [1])
 
292
 
 
293
        # Doing it again to ensure that the deferred isn't called twice.
 
294
        self.reactor.fire("message",
 
295
                          {"type": "registration", "info": "unknown-account"})
 
296
 
 
297
        self.assertEquals(calls, [1])
 
298
 
 
299
        self.assertEquals(self.logfile.getvalue(), "")
 
300
 
 
301
    def test_exchange_done_calls_exchange(self):
 
302
        exchanger_mock = self.mocker.patch(self.exchanger)
 
303
        exchanger_mock.exchange()
 
304
        self.mocker.passthrough()
 
305
        self.mocker.replay()
 
306
 
 
307
        self.mstore.set_accepted_types(["register"])
 
308
        self.config.computer_title = "Computer Title"
 
309
        self.config.account_name = "account_name"
 
310
        self.reactor.fire("exchange-done")
 
311
 
 
312
    def test_exchange_done_wont_call_exchange_when_just_tried(self):
 
313
        exchanger_mock = self.mocker.patch(self.exchanger)
 
314
        exchanger_mock.exchange()
 
315
        self.mocker.count(0)
 
316
        self.mocker.replay()
 
317
 
 
318
        self.mstore.set_accepted_types(["register"])
 
319
        self.config.computer_title = "Computer Title"
 
320
        self.config.account_name = "account_name"
 
321
        self.reactor.fire("pre-exchange")
 
322
        self.reactor.fire("exchange-done")