summaryrefslogtreecommitdiff
path: root/tests/twisted/account-manager/create-with-properties.py
blob: d5e9685ed70e8000a1ced62cc95aed6d1e41a7dc (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
# Copyright (C) 2009 Nokia Corporation
# Copyright (C) 2009 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
import dbus.service

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

def test(q, bus, mc):
    account_manager = AccountManager(bus)

    # Check AccountManager has D-Bus property interface
    call_async(q, account_manager.Properties, 'GetAll', cs.AM)
    properties, = q.expect('dbus-return', method='GetAll').value
    assert properties is not None
    assert properties.get('ValidAccounts') == [], \
        properties.get('ValidAccounts')
    assert properties.get('InvalidAccounts') == [], \
        properties.get('InvalidAccounts')
    interfaces = properties.get('Interfaces')
    supported = properties.get('SupportedAccountProperties')

    # assert that current functionality exists
    assert cs.AM_IFACE_NOKIA_QUERY in interfaces, interfaces

    assert (cs.ACCOUNT + '.AutomaticPresence') in supported
    assert (cs.ACCOUNT + '.Enabled') in supported
    assert (cs.ACCOUNT + '.Icon') in supported
    assert (cs.ACCOUNT + '.Nickname') in supported
    assert (cs.ACCOUNT + '.ConnectAutomatically') in supported
    assert (cs.ACCOUNT_IFACE_AVATAR + '.Avatar') in supported
    assert (cs.ACCOUNT_IFACE_NOKIA_COMPAT + '.SecondaryVCardFields') in supported
    assert (cs.ACCOUNT_IFACE_NOKIA_CONDITIONS + '.Condition') in supported

    assert (cs.ACCOUNT + '.RequestedPresence') in supported

    params = dbus.Dictionary({"account": "anarki@example.com",
        "password": "secrecy"}, signature='sv')

    cm_name_ref = dbus.service.BusName(cs.tp_name_prefix +
            '.ConnectionManager.fakecm', bus=bus)

    creation_properties = dbus.Dictionary({
        cs.ACCOUNT + '.Enabled': True,
        cs.ACCOUNT + '.AutomaticPresence': dbus.Struct((
            dbus.UInt32(cs.PRESENCE_TYPE_BUSY),
            'busy', 'Exploding'), signature='uss'),
        cs.ACCOUNT + '.RequestedPresence': dbus.Struct((
            dbus.UInt32(cs.PRESENCE_TYPE_AWAY),
            'away', 'Respawning'), signature='uss'),
        cs.ACCOUNT + '.Icon': 'quake3arena',
        cs.ACCOUNT + '.Nickname': 'AnArKi',
        cs.ACCOUNT + '.ConnectAutomatically': True,
        cs.ACCOUNT_IFACE_AVATAR + '.Avatar': (dbus.ByteArray('foo'),
            'image/jpeg'),
        cs.ACCOUNT_IFACE_NOKIA_COMPAT + '.SecondaryVCardFields':
            dbus.Array(['x-ioquake3', 'x-quake3'], signature='s'),
        cs.ACCOUNT_IFACE_NOKIA_CONDITIONS + '.Condition':
            dbus.Dictionary({ 'has-quad-damage': ':y' }, signature='ss'),
        }, signature='sv')

    call_async(q, account_manager, 'CreateAccount',
            'fakecm',
            'fakeprotocol',
            'fakeaccount',
            params,
            creation_properties)

    # The spec has no order guarantee here.
    # FIXME: MC ought to also introspect the CM and find out that the params
    # are in fact sufficient

    am_signal, ret, rc = q.expect_many(
            EventPattern('dbus-signal', path=cs.AM_PATH,
                signal='AccountValidityChanged', interface=cs.AM),
            EventPattern('dbus-return', method='CreateAccount'),
            EventPattern('dbus-method-call', method='RequestConnection'),
            )
    account_path = ret.value[0]
    assert am_signal.args == [account_path, True], am_signal.args

    assert account_path is not None

    account = bus.get_object(
        cs.tp_name_prefix + '.AccountManager',
        account_path)
    account_props = dbus.Interface(account, cs.PROPERTIES_IFACE)

    properties = account_props.GetAll(cs.ACCOUNT)
    assert properties.get('AutomaticPresence') == (cs.PRESENCE_TYPE_BUSY,
            'busy', 'Exploding'), \
        properties.get('AutomaticPresence')
    assert properties.get('RequestedPresence') == (cs.PRESENCE_TYPE_AWAY,
            'away', 'Respawning'), \
        properties.get('RequestedPresence')
    assert properties.get('ConnectAutomatically') == True, \
        properties.get('ConnectAutomatically')
    assert properties.get('Enabled') == True, \
        properties.get('Enabled')
    assert properties.get('Valid') == True, \
        properties.get('Valid')
    assert properties.get('Icon') == 'quake3arena', \
        properties.get('Icon')
    assert properties.get('Nickname') == 'AnArKi', \
        properties.get('Nickname')

    properties = account_props.GetAll(cs.ACCOUNT_IFACE_AVATAR)
    assert properties.get('Avatar') == ([ord('f'), ord('o'), ord('o')],
            'image/jpeg')

    properties = account_props.GetAll(cs.ACCOUNT_IFACE_NOKIA_COMPAT)
    assert sorted(properties.get('SecondaryVCardFields')) == \
            ['x-ioquake3', 'x-quake3']

    properties = account_props.GetAll(cs.ACCOUNT_IFACE_NOKIA_CONDITIONS)
    assert properties.get('Condition') == {
            'has-quad-damage': ':y',
            }

    # tests for errors when creating an account

    creation_properties2 = creation_properties.copy()
    creation_properties2[cs.ACCOUNT + '.NonExistent'] = 'foo'
    call_async(q, account_manager, 'CreateAccount',
            'fakecm',
            'fakeprotocol',
            'fakeaccount',
            params,
            creation_properties2)
    q.expect('dbus-error', method='CreateAccount')

    params2 = params.copy()
    params2['fake_param'] = 'foo'
    call_async(q, account_manager, 'CreateAccount',
            'fakecm',
            'fakeprotocol',
            'fakeaccount',
            params2,
            creation_properties)
    q.expect('dbus-error', method='CreateAccount')

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