summaryrefslogtreecommitdiff
path: root/tests/twisted/test-self-alias.py
blob: 6c2e0a80381869aac9f08db3007a704526c09483 (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
#
# Test alias setting for the self handle
#

from sofiatest import exec_test
from servicetest import tp_name_prefix

import dbus

TEXT_TYPE = tp_name_prefix + '.Channel.Type.Text'
ALIASING_INTERFACE = tp_name_prefix + '.Connection.Interface.Aliasing'
CONTACTS_INTERFACE = tp_name_prefix + '.Connection.Interface.Contacts'

def test(q, bus, conn, sip_proxy):
    conn.Connect()
    q.expect('dbus-signal', signal='StatusChanged', args=[0, 1])

    self_handle = conn.GetSelfHandle()

    default_alias = conn.Aliasing.GetAliases([self_handle])[self_handle]

    conn.Aliasing.SetAliases({self_handle: 'foo@bar.baz'})

    event = q.expect('dbus-signal', signal='AliasesChanged',
        args=[[(self_handle, u'foo@bar.baz')]])

    handle = conn.RequestHandles(1, ['sip:user@somewhere.com'])[0]

    assert ALIASING_INTERFACE in \
        conn.Properties.Get(CONTACTS_INTERFACE, "ContactAttributeInterfaces")
    attrs = conn.Contacts.GetContactAttributes([self_handle, handle],
	[ALIASING_INTERFACE], False)
    assert ALIASING_INTERFACE + "/alias" in attrs[self_handle]
    assert attrs[self_handle][ALIASING_INTERFACE + "/alias"] == u'foo@bar.baz'

    conn.RequestChannel(TEXT_TYPE, 1, handle, True)

    event = q.expect('dbus-signal', signal='NewChannel')

    text_iface = dbus.Interface(bus.get_object(conn.bus_name, event.args[0]),
                               TEXT_TYPE)
    text_iface.Send(0, 'Check the display name in From')

    event = q.expect('sip-message')

    self_uri = conn.InspectHandles(1, [self_handle])[0]

    from_header = event.sip_message.headers['from'][0]
    assert from_header.startswith('"foo@bar.baz" <' + self_uri + '>'), from_header

    # Test setting of the default alias
    conn.Aliasing.SetAliases({self_handle: default_alias})
    text_iface.Send(0, 'The display name should be missing in From')
    event = q.expect('sip-message')
    from_header = event.sip_message.headers['from'][0]
    assert from_header.startswith('<' + self_uri + '>'), from_header

    # Test if escaping and whitespace normalization works
    conn.Aliasing.SetAliases({self_handle: 'foo " bar \\\r\n baz\t'})
    text_iface.Send(0, 'Check display name escaping in From')
    event = q.expect('sip-message')
    from_header = event.sip_message.headers['from'][0]
    assert from_header.startswith(r'"foo \" bar \\ baz " <' + self_uri + '>'), from_header

if __name__ == '__main__':
    exec_test(test)