<commit_before>"""Feature test for accounts becoming valid.
"""

import os

import dbus
import dbus.service

from servicetest import EventPattern, tp_name_prefix, tp_path_prefix, \
        call_async, sync_dbus
from mctest import exec_test, SimulatedConnection, create_fakecm_account, \
        make_mc
import constants as cs

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

account1_id = 'fakecm/fakeprotocol/jc_2edenton_40unatco_2eint'
account2_id = 'fakecm/fakeprotocol/jc_2edenton_40example_2ecom'

def preseed():

    accounts_dir = os.environ['MC_ACCOUNT_DIR']

    # The passwords are missing, so the accounts can't connect yet.
    accounts_cfg = open(accounts_dir + '/accounts.cfg', 'w')
    accounts_cfg.write("""# Telepathy accounts
[%s]
manager=fakecm
protocol=fakeprotocol
DisplayName=Work account
NormalizedName=<EMAIL>
param-account=<EMAIL>
Enabled=1
ConnectAutomatically=1
AutomaticPresenceType=2
AutomaticPresenceStatus=available
AutomaticPresenceMessage=My vision is augmented
Nickname=JC
AvatarMime=image/jpeg

[%s]
manager=fakecm
protocol=fakeprotocol
DisplayName=Personal account
NormalizedName=<EMAIL>
param-account=<EMAIL>
Enabled=1
ConnectAutomatically=0
AutomaticPresenceType=2
AutomaticPresenceStatus=available
AutomaticPresenceMessage=My vision is augmented
Nickname=JC
AvatarMime=image/jpeg
""" % (account1_id, account2_id))
    accounts_cfg.close()

    os.makedirs(accounts_dir + '/' + account1_id)
    avatar_bin = open(accounts_dir + '/' + account1_id + '/avatar.bin', 'w')
    avatar_bin.write('Deus Ex')
    avatar_bin.close()

    os.makedirs(accounts_dir + '/' + account2_id)
    avatar_bin = open(accounts_dir + '/' + account2_id + '/avatar.bin', 'w')
    avatar_bin.write('Invisible War')
    avatar_bin.close()

    account_connections_file = open(accounts_dir + '/.mc_connections', 'w')
    account_connections_file.write("")
    account_connections_file.close()

def test(q, bus, unused):
    # make sure RequestConnection doesn't get called yet
    events = [EventPattern('dbus-method-call', method='RequestConnection')]
    q.forbid_events(events)

    # Wait for MC to load
    mc = make_mc(bus, q.append)

    q.expect_many(
            EventPattern('dbus-signal', signal='NameOwnerChanged',
                predicate=lambda e: e.args[0] == cs.AM),
            EventPattern('dbus-signal', signal='NameOwnerChanged',
                predicate=lambda e: e.args[0] == cs.CD),
            )

    # Make account 1 valid: it should connect automatically
<commit_msg>account-manager/make-valid.py: assert that channel requests on an invalid account fail<commit_after>"""Feature test for accounts becoming valid.
"""

import os

import dbus
import dbus.service

from servicetest import EventPattern, tp_name_prefix, tp_path_prefix, \
        call_async, sync_dbus
from mctest import exec_test, SimulatedConnection, create_fakecm_account, \
        make_mc
import constants as cs

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

account1_id = 'fakecm/fakeprotocol/jc_2edenton_40unatco_2eint'
account2_id = 'fakecm/fakeprotocol/jc_2edenton_40example_2ecom'

def preseed():

    accounts_dir = os.environ['MC_ACCOUNT_DIR']

    # The passwords are missing, so the accounts can't connect yet.
    accounts_cfg = open(accounts_dir + '/accounts.cfg', 'w')
    accounts_cfg.write("""# Telepathy accounts
[%s]
manager=fakecm
protocol=fakeprotocol
DisplayName=Work account
NormalizedName=<EMAIL>
param-account=<EMAIL>
Enabled=1
ConnectAutomatically=1
AutomaticPresenceType=2
AutomaticPresenceStatus=available
AutomaticPresenceMessage=My vision is augmented
Nickname=JC
AvatarMime=image/jpeg

[%s]
manager=fakecm
protocol=fakeprotocol
DisplayName=Personal account
NormalizedName=<EMAIL>
param-account=<EMAIL>
Enabled=1
ConnectAutomatically=0
AutomaticPresenceType=2
AutomaticPresenceStatus=available
AutomaticPresenceMessage=My vision is augmented
Nickname=JC
AvatarMime=image/jpeg
""" % (account1_id, account2_id))
    accounts_cfg.close()

    os.makedirs(accounts_dir + '/' + account1_id)
    avatar_bin = open(accounts_dir + '/' + account1_id + '/avatar.bin', 'w')
    avatar_bin.write('Deus Ex')
    avatar_bin.close()

    os.makedirs(accounts_dir + '/' + account2_id)
    avatar_bin = open(accounts_dir + '/' + account2_id + '/avatar.bin', 'w')
    avatar_bin.write('Invisible War')
    avatar_bin.close()

    account_connections_file = open(accounts_dir + '/.mc_connections', 'w')
    account_connections_file.write("")
    account_connections_file.close()

def test(q, bus, unused):
    # make sure RequestConnection doesn't get called yet
    events = [EventPattern('dbus-method-call', method='RequestConnection')]
    q.forbid_events(events)

    # Wait for MC to load
    mc = make_mc(bus, q.append)

    q.expect_many(
            EventPattern('dbus-signal', signal='NameOwnerChanged',
                predicate=lambda e: e.args[0] == cs.AM),
            EventPattern('dbus-signal', signal='NameOwnerChanged',
                predicate=lambda e: e.args[0] == cs.CD),
            )

    # Trying to make a channel on account 1 doesn't work, because it's
    # not valid

    account_path = (cs.tp_path_prefix + '/Account/' + account1_id)

    cd = bus.get_object(cs.CD, cs.CD_PATH)

    user_action_time = dbus.Int64(1238582606)
    request = dbus.Dictionary({
            cs.CHANNEL + '.ChannelType': cs.CHANNEL_TYPE_TEXT,
            cs.CHANNEL + '.TargetHandleType': cs.HT_CONTACT,
            cs.CHANNEL + '.TargetID': 'juliet',
            }, signature='sv')
    call_async(q, cd, 'CreateChannel',
            account_path, request, user_action_time, "",
            dbus_interface=cs.CD)
    ret = q.expect('dbus-return', method='CreateChannel')
    request_path = ret.value[0]

    cr = bus.get_object(cs.CD, request_path)
    request_props = cr.GetAll(cs.CR, dbus_interface=cs.PROPERTIES_IFACE)
    assert request_props['Account'] == account_path
    assert request_props['Requests'] == [request]
    assert request_props['UserActionTime'] == user_action_time
    assert request_props['PreferredHandler'] == ""
    assert request_props['Interfaces'] == []

    sync_dbus(bus, q, mc)

    cr.Proceed(dbus_interface=cs.CR)

    # FIXME: error isn't specified (NotAvailable perhaps?)
    q.expect('dbus-signal', path=cr.object_path,
            interface=cs.CR, signal='Failed')

    # Make account 1 valid: it should connect automatically

{'fraction_non_alphanumeric': 0.11421125249118504, 'fraction_numerical': 0.00996474015023762, 'mean_word_length': 4.252818035426731, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2968078', 'n_tokens_mistral': 2193, 'n_tokens_neox': 2118, 'n_words': 425}