summaryrefslogtreecommitdiff
path: root/tests/twisted/account-manager/irc.py
blob: 0d0de71f7f29ca8e2f531611556752c827601d05 (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
# encoding: utf-8
#
# Test the odd things about IRC: it has no presence, and nicknames
# are the same thing as identifiers.
#
# Copyright © 2009 Nokia Corporation
# Copyright © 2009-2013 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, call_async, assertEquals, sync_dbus)
from mctest import (exec_test, create_fakecm_account, enable_fakecm_account)
import constants as cs

def test(q, bus, mc):
    params = dbus.Dictionary({"account": "brucewayne",
        "password": "secrecy"}, signature='sv')
    (simulated_cm, account) = create_fakecm_account(q, bus, mc, params)

    account_iface = dbus.Interface(account, cs.ACCOUNT)
    account_props = dbus.Interface(account, cs.PROPERTIES_IFACE)

    call_async(q, account_props, 'Set', cs.ACCOUNT, 'Nickname',
            "BruceWayne")
    q.expect_many(
        EventPattern('dbus-signal',
            path=account.object_path,
            signal='AccountPropertyChanged',
            interface=cs.ACCOUNT,
            args=[{'Nickname': "BruceWayne"}]),
        EventPattern('dbus-return', method='Set'),
        )
    assertEquals("BruceWayne", account_props.Get(cs.ACCOUNT, 'Nickname'))

    expect_after_connect = [
            EventPattern('dbus-method-call',
                interface=cs.CONN_IFACE_CONTACTS,
                predicate=(lambda e: e.method in (
                    'GetContactAttributes', 'GetContactByID'
                    ) and
                    cs.CONN_IFACE_ALIASING in e.args[1]),
                handled=True),
            EventPattern('dbus-method-call',
                interface=cs.CONN_IFACE_ALIASING, method='SetAliases',
                handled=False),
            EventPattern('dbus-signal',
                interface=cs.ACCOUNT,
                predicate=lambda e:
                    e.args[0].get('CurrentPresence') ==
                        (cs.PRESENCE_UNSET, '', '')),
            ]

    conn, get_aliases, set_aliases, _ = enable_fakecm_account(q, bus, mc,
            account, params, has_aliasing=True,
            expect_after_connect=expect_after_connect,
            self_ident=params['account'])

    assert get_aliases.args[0] == [ conn.self_handle ]

    assert set_aliases.args[0] == { conn.self_handle: 'BruceWayne' }
    q.dbus_return(set_aliases.message, signature='')

    # FIXME: fd.o #55666 in telepathy-glib breaks the rest of this test.
    # Reinstate it when we depend on a version that has that fixed.
    return

    # Another client changes our alias remotely, but because this is IRC,
    # that manifests itself as a handle change
    conn.change_self_ident('thebatman')
    conn.change_self_alias('TheBatman')

    get_aliases, _ = q.expect_many(
        EventPattern('dbus-method-call',
            interface=cs.CONN_IFACE_CONTACTS,
            predicate=(lambda e: e.method in (
                'GetContactAttributes', 'GetContactByID'
                ) and
                cs.CONN_IFACE_ALIASING in e.args[1]),
            handled=True),
        EventPattern('dbus-signal', path=account.object_path,
            signal='AccountPropertyChanged', interface=cs.ACCOUNT,
            predicate=(lambda e:
                e.args[0].get('NormalizedName') == 'thebatman')),
        )
    assert get_aliases.args[0] in ([conn.self_handle], conn.self_id)
    q.expect('dbus-signal', path=account.object_path,
            signal='AccountPropertyChanged', interface=cs.ACCOUNT,
            args=[{'Nickname': 'TheBatman'}])

    # We change our nickname back
    call_async(q, account_props, 'Set', cs.ACCOUNT, 'Nickname',
            'BruceWayne')
    _, _, e = q.expect_many(
        EventPattern('dbus-signal',
            path=account.object_path,
            signal='AccountPropertyChanged',
            interface=cs.ACCOUNT,
            predicate=(lambda e: e.args[0].get('Nickname') == 'BruceWayne')),
        EventPattern('dbus-return', method='Set'),
        EventPattern('dbus-method-call',
            interface=cs.CONN_IFACE_ALIASING, method='SetAliases',
            args=[{ conn.self_handle: 'BruceWayne', }],
            handled=False)
        )
    assertEquals('BruceWayne', account_props.Get(cs.ACCOUNT, 'Nickname'))
    conn.change_self_ident('brucewayne')
    conn.change_self_alias('BruceWayne')
    q.dbus_return(e.message, signature='')

    # In response to the self-handle change, we check our nickname again
    get_aliases, _ = q.expect_many(
        EventPattern('dbus-method-call',
            interface=cs.CONN_IFACE_CONTACTS,
            predicate=(lambda e: e.method in (
                'GetContactAttributes', 'GetContactByID'
                ) and
                cs.CONN_IFACE_ALIASING in e.args[1]),
            handled=True),
        EventPattern('dbus-signal', path=account.object_path,
            signal='AccountPropertyChanged', interface=cs.ACCOUNT,
            predicate=(lambda e:
                e.args[0].get('NormalizedName') == 'brucewayne')),
        )
    assert get_aliases.args[0] in ([conn.self_handle], conn.self_id)

    forbidden = [EventPattern('dbus-signal', signal='AccountPropertyChanged',
        predicate=lambda e: 'Nickname' in e.args[0])]
    q.forbid_events(forbidden)
    sync_dbus(bus, q, mc)

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