summaryrefslogtreecommitdiff
path: root/tests/twisted/account-manager/account-basics.py
blob: 4e643cc9330cce32721a082703cd3cb4d38e8141 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# Copyright (C) 2009 Nokia Corporation
# Copyright (C) 2009-2012 Collabora Ltd.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA

import dbus
import dbus.service

from servicetest import EventPattern, tp_name_prefix, tp_path_prefix, \
        call_async, assertEquals
from mctest import exec_test, create_fakecm_account, get_account_manager
import constants as cs

def test(q, bus, mc):
    # Get the AccountManager interface
    account_manager = get_account_manager(bus)
    account_manager_iface = dbus.Interface(account_manager, cs.AM)

    # Introspect AccountManager for debugging purpose
    account_manager_introspected = account_manager.Introspect(
            dbus_interface=cs.INTROSPECTABLE_IFACE)
    #print account_manager_introspected

    # Check AccountManager has D-Bus property interface
    properties = account_manager.GetAll(cs.AM,
            dbus_interface=cs.PROPERTIES_IFACE)
    assert properties is not None
    assert properties.get('ValidAccounts') == [], \
        properties.get('ValidAccounts')
    assert properties.get('InvalidAccounts') == [], \
        properties.get('InvalidAccounts')
    interfaces = properties.get('Interfaces')

    params = dbus.Dictionary({"account": "someguy@example.com",
        "password": "secrecy"}, signature='sv')
    (simulated_cm, account) = create_fakecm_account(q, bus, mc, params)

    account_path = account.__dbus_object_path__

    # Check the account is correctly created
    properties = account_manager.GetAll(cs.AM,
            dbus_interface=cs.PROPERTIES_IFACE)
    assert properties is not None
    assert properties.get('ValidAccounts') == [account_path], properties
    account_path = properties['ValidAccounts'][0]
    assert isinstance(account_path, dbus.ObjectPath), repr(account_path)
    assert properties.get('InvalidAccounts') == [], properties

    account_iface = dbus.Interface(account, cs.ACCOUNT)
    account_props = dbus.Interface(account, cs.PROPERTIES_IFACE)
    # Introspect Account for debugging purpose
    account_introspected = account.Introspect(
            dbus_interface=cs.INTROSPECTABLE_IFACE)
    #print account_introspected

    # Check Account has D-Bus property interface
    properties = account_props.GetAll(cs.ACCOUNT)
    assert properties is not None

    assert properties.get('DisplayName') == 'fakeaccount', \
        properties.get('DisplayName')
    assert properties.get('Icon') == '', properties.get('Icon')
    assert properties.get('Valid') == True, properties.get('Valid')
    assert properties.get('Enabled') == False, properties.get('Enabled')
    #assert properties.get('Nickname') == 'fakenick', properties.get('Nickname')
    assert properties.get('Parameters') == params, properties.get('Parameters')
    assert properties.get('Connection') == '/', properties.get('Connection')
    assert properties.get('NormalizedName') == '', \
        properties.get('NormalizedName')

    interfaces = properties.get('Interfaces')
    assert cs.ACCOUNT_IFACE_AVATAR in interfaces, interfaces

    # sanity check
    for k in properties:
        assert account_props.Get(cs.ACCOUNT, k) == properties[k], k

    # Alter some miscellaneous r/w properties

    call_async(q, account_props, 'Set', cs.ACCOUNT, 'DisplayName',
            'Work account')
    q.expect_many(
        EventPattern('dbus-signal',
            path=account_path,
            signal='AccountPropertyChanged',
            interface=cs.ACCOUNT,
            args=[{'DisplayName': 'Work account'}]),
        EventPattern('dbus-signal',
            interface=cs.TEST_DBUS_ACCOUNT_PLUGIN_IFACE,
            signal='DeferringSetAttribute',
            args=[account_path, 'DisplayName', 'Work account']),
        EventPattern('dbus-signal',
            interface=cs.TEST_DBUS_ACCOUNT_PLUGIN_IFACE,
            signal='CommittingOne',
            args=[account_path]),
        EventPattern('dbus-method-call',
            interface=cs.TEST_DBUS_ACCOUNT_SERVICE_IFACE,
            method='UpdateAttributes',
            args=[account_path[len(cs.ACCOUNT_PATH_PREFIX):],
                {'DisplayName': 'Work account'},
                {'DisplayName': 0}, # flags
                []],
            ),
        EventPattern('dbus-return', method='Set'),
        )
    assert account_props.Get(cs.ACCOUNT, 'DisplayName') == 'Work account'

    call_async(q, account_props, 'Set', cs.ACCOUNT, 'Icon', 'im-jabber')
    q.expect_many(
        EventPattern('dbus-signal',
            path=account_path,
            signal='AccountPropertyChanged',
            interface=cs.ACCOUNT,
            args=[{'Icon': 'im-jabber'}]),
        EventPattern('dbus-signal',
            interface=cs.TEST_DBUS_ACCOUNT_PLUGIN_IFACE,
            signal='DeferringSetAttribute',
            args=[account_path, 'Icon', 'im-jabber']),
        EventPattern('dbus-signal',
            interface=cs.TEST_DBUS_ACCOUNT_PLUGIN_IFACE,
            signal='CommittingOne',
            args=[account_path]),
        EventPattern('dbus-method-call',
            interface=cs.TEST_DBUS_ACCOUNT_SERVICE_IFACE,
            method='UpdateAttributes',
            args=[account_path[len(cs.ACCOUNT_PATH_PREFIX):],
                {'Icon': 'im-jabber'},
                {'Icon': 0}, # flags
                []],
            ),
        EventPattern('dbus-return', method='Set'),
        )
    assert account_props.Get(cs.ACCOUNT, 'Icon') == 'im-jabber'

    assert account_props.Get(cs.ACCOUNT, 'HasBeenOnline') == False
    call_async(q, account_props, 'Set', cs.ACCOUNT, 'Nickname', 'Joe Bloggs')
    q.expect_many(
        EventPattern('dbus-signal',
            path=account_path,
            signal='AccountPropertyChanged',
            interface=cs.ACCOUNT,
            args=[{'Nickname': 'Joe Bloggs'}]),
        EventPattern('dbus-return', method='Set'),
        EventPattern('dbus-signal',
            interface=cs.TEST_DBUS_ACCOUNT_PLUGIN_IFACE,
            signal='DeferringSetAttribute',
            args=[account_path, 'Nickname', 'Joe Bloggs']),
        EventPattern('dbus-signal',
            interface=cs.TEST_DBUS_ACCOUNT_PLUGIN_IFACE,
            signal='CommittingOne',
            args=[account_path]),
        EventPattern('dbus-method-call',
            interface=cs.TEST_DBUS_ACCOUNT_SERVICE_IFACE,
            method='UpdateAttributes',
            args=[account_path[len(cs.ACCOUNT_PATH_PREFIX):],
                {'Nickname': 'Joe Bloggs'},
                {'Nickname': 0}, # flags
                []],
            ),
        )
    assert account_props.Get(cs.ACCOUNT, 'Nickname') == 'Joe Bloggs'

    assertEquals(dbus.Array(signature='o'),
            account_props.Get(cs.ACCOUNT, 'Supersedes'))
    call_async(q, account_props, 'Set', cs.ACCOUNT, 'Supersedes',
            dbus.Array([cs.ACCOUNT_PATH_PREFIX + 'x/y/z'],
                signature='o'))
    q.expect_many(
        EventPattern('dbus-signal',
            path=account_path,
            signal='AccountPropertyChanged',
            interface=cs.ACCOUNT,
            args=[{'Supersedes': [cs.ACCOUNT_PATH_PREFIX + 'x/y/z']}]),
        EventPattern('dbus-return', method='Set'),
        EventPattern('dbus-signal',
            interface=cs.TEST_DBUS_ACCOUNT_PLUGIN_IFACE,
            signal='DeferringSetAttribute',
            args=[account_path, 'Supersedes',
                [cs.ACCOUNT_PATH_PREFIX + 'x/y/z']]),
        EventPattern('dbus-signal',
            interface=cs.TEST_DBUS_ACCOUNT_PLUGIN_IFACE,
            signal='CommittingOne',
            args=[account_path]),
        EventPattern('dbus-method-call',
            interface=cs.TEST_DBUS_ACCOUNT_SERVICE_IFACE,
            method='UpdateAttributes',
            args=[account_path[len(cs.ACCOUNT_PATH_PREFIX):],
                {'Supersedes': [cs.ACCOUNT_PATH_PREFIX + 'x/y/z']},
                {'Supersedes': 0}, # flags
                []],
            ),
        )
    assertEquals(dbus.Array([cs.ACCOUNT_PATH_PREFIX + 'x/y/z'],
                signature='o'),
            account_props.Get(cs.ACCOUNT, 'Supersedes'))

    # Set some properties to invalidly typed values - this currently succeeds
    # but is a no-op, although in future it should change to raising an
    # exception

    forbidden = [
        EventPattern('dbus-signal',
            interface=cs.TEST_DBUS_ACCOUNT_PLUGIN_IFACE,
            signal='DeferringSetAttribute'),
        EventPattern('dbus-method-call',
            interface=cs.TEST_DBUS_ACCOUNT_SERVICE_IFACE,
            method='UpdateAttributes'),
       ]
    q.forbid_events(forbidden)

    # this variable's D-Bus type must differ from the types of all known
    # properties
    badly_typed = dbus.Struct(('wrongly typed',), signature='s')

    for p in ('DisplayName', 'Icon', 'Enabled', 'Nickname',
            'AutomaticPresence', 'ConnectAutomatically', 'RequestedPresence'):
        try:
            account_props.Set(cs.ACCOUNT, p, badly_typed)
        except dbus.DBusException, e:
            assert e.get_dbus_name() == cs.INVALID_ARGUMENT, \
                    (p, e.get_dbus_name())
        else:
            raise AssertionError('Setting %s with wrong type should fail' % p)

    for p in ('Avatar',):
        try:
            account_props.Set(cs.ACCOUNT_IFACE_AVATAR, p, badly_typed)
        except dbus.DBusException, e:
            assert e.get_dbus_name() == cs.INVALID_ARGUMENT, \
                    (p, e.get_dbus_name())
        else:
            raise AssertionError('Setting %s with wrong type should fail' % p)

    # Make sure MC hasn't crashed yet, and make sure some properties are what
    # we expect them to be

    properties = account_props.GetAll(cs.ACCOUNT)
    assert properties['DisplayName'] == 'Work account'
    assert properties['Icon'] == 'im-jabber'
    properties = account_props.GetAll(cs.ACCOUNT_IFACE_AVATAR)
    assert properties['Avatar'] == ([], '')

    q.unforbid_events(forbidden)

    # Delete the account
    call_async(q, account_iface, 'Remove')
    q.expect_many(
        EventPattern('dbus-return', method='Remove'),
        EventPattern('dbus-signal',
            path=account_path,
            signal='Removed',
            interface=cs.ACCOUNT,
            args=[]
            ),
        EventPattern('dbus-signal',
            path=cs.AM_PATH,
            signal='AccountRemoved',
            interface=cs.AM,
            args=[account_path]
            ),
        EventPattern('dbus-signal',
            interface=cs.TEST_DBUS_ACCOUNT_PLUGIN_IFACE,
            signal='DeferringDelete',
            args=[account_path]),
        EventPattern('dbus-signal',
            interface=cs.TEST_DBUS_ACCOUNT_PLUGIN_IFACE,
            signal='CommittingOne',
            args=[account_path]),
        EventPattern('dbus-method-call',
            interface=cs.TEST_DBUS_ACCOUNT_SERVICE_IFACE,
            method='DeleteAccount',
            args=[account_path[len(cs.ACCOUNT_PATH_PREFIX):]]),
        )

    # Check the account is correctly deleted
    properties = account_manager.GetAll(cs.AM,
            dbus_interface=cs.PROPERTIES_IFACE)
    assert properties is not None
    assert properties.get('ValidAccounts') == [], properties
    assert properties.get('InvalidAccounts') == [], properties


if __name__ == '__main__':
    exec_test(test, {})