<commit_before>"""test OLPC bundle. We shouldn't announce OLPC features until we use the OLPC
interface"""
import base64
import errno
import os

import dbus

# must come before the twisted imports due to side-effects
from gabbletest import go, make_result_iq
from servicetest import call_async, lazy, match, tp_name_prefix, unwrap, Event

from twisted.internet.protocol import Factory, Protocol
from twisted.words.protocols.jabber.client import IQ
from twisted.words.xish import domish, xpath
from twisted.internet import reactor

NS_OLPC_BUDDY_PROPS_NOTIFY = "http://laptop.org/xmpp/buddy-properties+notify"
NS_OLPC_ACTIVITIES_NOTIFY = "http://laptop.org/xmpp/activities+notify"
NS_OLPC_CURRENT_ACTIVITY_NOTIFY = "http://laptop.org/xmpp/current-activity+notify"
NS_OLPC_ACTIVITY_PROPS_NOTIFY = "http://laptop.org/xmpp/activity-properties+notify"

olpc_features = set([NS_OLPC_BUDDY_PROPS_NOTIFY, NS_OLPC_ACTIVITIES_NOTIFY,
        NS_OLPC_CURRENT_ACTIVITY_NOTIFY, NS_OLPC_ACTIVITY_PROPS_NOTIFY])

@match('dbus-signal', signal='StatusChanged', args=[0, 1])
def expect_connected(event, data):

    # send diso request
    m = domish.Element(('', 'iq'))
    m['from'] = '<EMAIL>'
    m['id'] = '1'
    query = m.addElement('query')
    query['xmlns'] = 'http://jabber.org/protocol/disco#info'
    data['stream'].send(m)

    return True

@match('stream-iq', iq_type='result',
    query_ns='http://jabber.org/protocol/disco#info',
    to='<EMAIL>')
def expect_first_disco_reply(event, data):
    if event.stanza['id'] != '1':
        return False

    features = set([str(f['var']) for f in xpath.queryForNodes('/iq/query/feature',
        event.stanza)])

    # OLPC NS aren't announced
    assert len(olpc_features.intersection(features)) == 0

    # Use OLPC interface
    data['olpc_iface'] = dbus.Interface(data['conn'],
            'org.laptop.Telepathy.BuddyInfo')
    call_async(data['test'], data['olpc_iface'], 'SetProperties',
            {'color': '#ff0000,#0000ff'})

    return True

@match('stream-presence')
def expect_bundle_presence(event, data):

    c_nodes = xpath.queryForNodes('/presence/c', event.stanza)
    assert c_nodes is not None
    assert len(c_nodes) == 1

    c_node = c_nodes[0]
    assert c_node['ext'] == 'olpc'

    # send diso request
    m = domish.Element(('', 'iq'))
    m['from'] = '<EMAIL>'
    m['id'] = '2'
    query = m.addElement('query')
    query['xmlns'] = 'http://jabber.org/protocol/disco#info'
    data['stream'].send(m)

    return True

@match('stream-iq', iq_type='result',
    query_ns='http://jabber.org/protocol/disco#info',
    to='<EMAIL>')
def expect_snd_disco_reply(event, data):
    if event.stanza['id'] != '2':
        return False

    # OLPC NS are now announced
    features = set([str(f['var']) for f in xpath.queryForNodes('/iq/query/feature',
        event.stanza)])

    assert olpc_features.issubset(features)

    return True

if __name__ == '__main__':
    go()
<commit_msg>test-olpc-bundle.py: updated to use new test API<commit_after>"""test OLPC bundle. We shouldn't announce OLPC features until we use the OLPC
interface"""
import base64
import errno
import os

import dbus

from servicetest import call_async, EventPattern
from gabbletest import exec_test, make_result_iq, acknowledge_iq

from twisted.words.xish import domish, xpath

NS_OLPC_BUDDY_PROPS_NOTIFY = "http://laptop.org/xmpp/buddy-properties+notify"
NS_OLPC_ACTIVITIES_NOTIFY = "http://laptop.org/xmpp/activities+notify"
NS_OLPC_CURRENT_ACTIVITY_NOTIFY = "http://laptop.org/xmpp/current-activity+notify"
NS_OLPC_ACTIVITY_PROPS_NOTIFY = "http://laptop.org/xmpp/activity-properties+notify"

olpc_features = set([NS_OLPC_BUDDY_PROPS_NOTIFY, NS_OLPC_ACTIVITIES_NOTIFY,
        NS_OLPC_CURRENT_ACTIVITY_NOTIFY, NS_OLPC_ACTIVITY_PROPS_NOTIFY])

def test(q, bus, conn, stream):
    conn.Connect()

    _, iq_event = q.expect_many(
        EventPattern('dbus-signal', signal='StatusChanged', args=[0, 1]),
        EventPattern('stream-iq', to=None, query_ns='vcard-temp',
            query_name='vCard'))

    acknowledge_iq(stream, iq_event.stanza)

    # send diso request
    m = domish.Element(('', 'iq'))
    m['from'] = '<EMAIL>'
    m['id'] = '1'
    query = m.addElement('query')
    query['xmlns'] = 'http://jabber.org/protocol/disco#info'
    stream.send(m)

    # wait for disco response
    event = q.expect('stream-iq', iq_type='result',
            query_ns='http://jabber.org/protocol/disco#info',
            to='<EMAIL>')

    features = set([str(f['var']) for f in xpath.queryForNodes('/iq/query/feature',
        event.stanza)])

    # OLPC NS aren't announced
    assert len(olpc_features.intersection(features)) == 0

    # Use OLPC interface
    buddy_info_iface = dbus.Interface(conn, 'org.laptop.Telepathy.BuddyInfo')
    call_async(q, buddy_info_iface, 'SetProperties',
            {'color': '#ff0000,#0000ff'})

    # wait for <presence> stanza
    event = q.expect('stream-presence')
    c_nodes = xpath.queryForNodes('/presence/c', event.stanza)
    assert c_nodes is not None
    assert len(c_nodes) == 1

    c_node = c_nodes[0]
    assert c_node['ext'] == 'olpc'

    # send diso request
    m = domish.Element(('', 'iq'))
    m['from'] = '<EMAIL>'
    m['id'] = '2'
    query = m.addElement('query')
    query['xmlns'] = 'http://jabber.org/protocol/disco#info'
    stream.send(m)

    # wait for disco response
    event = q.expect('stream-iq', iq_type='result',
        query_ns='http://jabber.org/protocol/disco#info',
        to='<EMAIL>')
    assert event.stanza['id'] == '2'

    # OLPC NS are now announced
    features = set([str(f['var']) for f in xpath.queryForNodes('/iq/query/feature',
        event.stanza)])

    assert olpc_features.issubset(features)

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

{'fraction_non_alphanumeric': 0.1467128027681661, 'fraction_numerical': 0.006401384083044983, 'mean_word_length': 3.9537275064267354, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '2942156', 'n_tokens_mistral': 2125, 'n_tokens_neox': 2037, 'n_words': 399}