summaryrefslogtreecommitdiff
path: root/tests/twisted/account-manager/gvariant-accounts.py
blob: 3f271be64f9d78392ec51633bc080a6089750683 (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
# Copyright (C) 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
import os
import sys

from servicetest import EventPattern, call_async, assertEquals
from mctest import exec_test, AccountManager, Account, MC
import constants as cs

account_prefix = cs.tp_path_prefix + '/Account/'
account1_id = 'fakecm/fakeprotocol/jc_2edenton_40unatco_2eint'
filename = None

def preseed():
    global filename

    accounts_dir = os.environ['MC_ACCOUNT_DIR']

    escaped = account1_id.replace('/', '_')
    filename = accounts_dir + '/' + escaped
    account = open(filename, 'w')
    account.write("""{
'id': <'%s'>,
'manager': <'fakecm'>,
'protocol': <'fakeprotocol'>,
'DisplayName': <'Work account'>,
'NormalizedName': <'jc.denton@unatco.int'>,
'param-account': <'jc.denton@unatco.int'>,
'param-password': <'ionstorm'>,
'Enabled': <'true'>,
'ConnectAutomatically': <'true'>,
'AutomaticPresenceType': <'2'>,
'AutomaticPresenceMessage': <'My vision is augmented'>,
'Nickname': <'JC'>,
'AvatarMime': <'image/jpeg'>,
'avatar_token': <'Deus Ex'>
}""" % account1_id)
    account.close()

def test(q, bus, unused):
    global filename

    expected_params = {
            'account': 'jc.denton@unatco.int',
            'password': 'ionstorm',
            }

    mc = MC(q, bus)

    am = AccountManager(bus)

    # make sure we have the right accounts
    props = am.GetAll(cs.AM, dbus_interface=dbus.PROPERTIES_IFACE)
    accounts = props['ValidAccounts']

    assertEquals(1, len(accounts))
    path = accounts[0]
    assertEquals(account_prefix + account1_id, path)

    account = Account(bus, path)

    # make sure the account has got the right properties
    props = account.GetAll(cs.ACCOUNT, dbus_interface=dbus.PROPERTIES_IFACE)

    assertEquals('Work account', props['DisplayName'])
    assertEquals('jc.denton@unatco.int', props['NormalizedName'])
    assertEquals(expected_params, props['Parameters'])
    assertEquals(True, props['Enabled'])
    assertEquals(True, props['ConnectAutomatically'])
    assertEquals((cs.PRESENCE_TYPE_AVAILABLE, '', 'My vision is augmented'),
                 props['AutomaticPresence'])
    assertEquals('JC', props['Nickname'])

    props = account.GetAll(cs.ACCOUNT_IFACE_AVATAR, dbus_interface=dbus.PROPERTIES_IFACE)
    avatar = props['Avatar']
    assertEquals('image/jpeg', avatar[1])

    # now delete the account and make sure the file is removed
    assert os.path.exists(filename)

    account.Remove()

    q.expect_many(EventPattern('dbus-signal', signal='Removed'),
                  EventPattern('dbus-signal', signal='AccountRemoved'))

    assert not os.path.exists(filename)

    accounts = am.Get(cs.AM, 'ValidAccounts', dbus_interface=dbus.PROPERTIES_IFACE)
    assertEquals(0, len(accounts))

    # create an account and assert the file is present
    parameters = {
        'account': 'dontdivert@bar.com',
        'password': 'le password'
        }

    accounts_dir = os.environ['MC_ACCOUNT_DIR']
    name = 'fakecm/fakeprotocol/dontdivert_40bar_2ecom0'
    second_filename = os.path.join(accounts_dir, name.replace('/', '_'))
    assert not os.path.exists(second_filename)

    path = am.CreateAccount('fakecm', 'fakeprotocol', 'Display name', parameters, {})

    e = q.expect('dbus-signal', signal='AccountValidityChanged')
    object_path, valid = e.args
    assertEquals(path, object_path)
    assert valid

    assert os.path.exists(second_filename)

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