Ejemplo n.º 1
0
def test_id_empty(logger):
    ('XMLStream.id should return None if there is no stream node stored')

    # Given a connection
    connection = FakeConnection()

    # And a XMLStream
    stream = XMLStream(connection)

    # Then the id is none
    stream.id.should.be.none
Ejemplo n.º 2
0
def test_id(logger):
    ('XMLStream.id should return the id of its stream node')

    # Given a connection
    connection = FakeConnection()

    # And a XMLStream
    stream = XMLStream(connection)
    stream.stream_node = Stream.create(id='what')

    # Then the id is none
    stream.id.should.equal('what')
Ejemplo n.º 3
0
def test_parse_noxml(logger):
    ('XMLStream.parse should return None when failed to parse')

    # Given a connection
    connection = FakeConnection()

    # And a XMLStream
    stream = XMLStream(connection)

    # When I feed it with valid XML
    stream.feed(
        '''<monteque name="Romeo" Loves Julietmonteque'''
    )

    # And call parse
    result = stream.parse()

    result.should.be.none
Ejemplo n.º 4
0
def test_route_nodes_error():
    ('XMLStream.route_nodes() should warn about error nodes')

    # Given a connection
    connection = FakeConnection()

    # And a XMLStream
    stream = XMLStream(connection)

    # And an event handler
    handle_error = EventHandlerMock('on_error')
    stream.on.error(handle_error)

    # When I call route_nodes
    node = ServiceUnavailable.create()
    stream.route_nodes(None, node)

    handle_error.assert_called_once_with(ANY, node)
Ejemplo n.º 5
0
def test_route_nodes_undefined(logger):
    ('XMLStream.route_nodes() should warn about undefined nodes')

    # Given a connection
    connection = FakeConnection()

    # And a XMLStream
    stream = XMLStream(connection)

    # When I call route_nodes with a bare Node
    empty = Node.create(type='character', name='romeo')
    stream.route_nodes(None, empty)

    # Then the logger should have been called appropriately
    logger.warning.assert_called_once_with(
        'no model defined for %s: %r', '<xmpp-unknown name="romeo" type="character" />',
        empty
    )
Ejemplo n.º 6
0
def test_handle_presence():
    ('XMLStream.handle_presence() should forward the `on.presence` event')

    # Given a connection
    connection = FakeConnection()

    # And a XMLStream
    stream = XMLStream(connection)

    # And an event handler
    handle_presence = EventHandlerMock('on_presence')
    stream.on.presence(handle_presence)

    # When I call handle presence
    presence = Presence.create()
    stream.handle_presence(presence)

    handle_presence.assert_called_once_with(ANY, presence)
Ejemplo n.º 7
0
def test_handle_iq_result():
    ('XMLStream.handle_iq() should forward the `on.iq_result` event')

    # Given a connection
    connection = FakeConnection()

    # And a XMLStream
    stream = XMLStream(connection)

    # And an event handler
    handle_iq_result = EventHandlerMock('on_iq_result')
    stream.on.iq_result(handle_iq_result)

    # When I call handle iq
    node = IQ.create(type='result')
    stream.handle_iq(node)

    handle_iq_result.assert_called_once_with(ANY, node)
Ejemplo n.º 8
0
def test_handle_message():
    ('XMLStream.handle_message() should forward the `on.message` event')

    # Given a connection
    connection = FakeConnection()

    # And a XMLStream
    stream = XMLStream(connection)

    # And an event handler
    handle_message = EventHandlerMock('on_message')
    stream.on.message(handle_message)

    # When I call handle message
    message = Message.create()
    stream.route_nodes(None, message)

    handle_message.assert_called_once_with(ANY, message)
Ejemplo n.º 9
0
def test_send_presence_missing_jid():
    ('XMLStream.send_presence() complains '
     'if there is no bound jid and no provided `from` jid')

    # Given a connection
    connection = FakeConnection()

    # And a XMLStream
    stream = XMLStream(connection)

    # When I call send_presence
    stream.send_presence.when.called_with(
        **{
            'to': 'juliet@capulet',
        }
    ).should.have.raised(
        MissingJID, 'Presence cannot be sent when missing the "from" jid'
    )
Ejemplo n.º 10
0
def test_parse_ok(logger):
    ('XMLStream.parse should return a node')

    # Given a connection
    connection = FakeConnection()

    # And a XMLStream
    stream = XMLStream(connection)

    # When I feed it with valid XML
    stream.feed(
        '''<monteque name="Romeo">Loves Juliet</monteque>'''
    )

    # And call parse
    result = stream.parse()

    result.should.be.a(Node)
    result.to_xml().should.look_like('''
    <monteque name="Romeo">Loves Juliet</monteque>
    ''')
Ejemplo n.º 11
0
def test_send_presence():
    ('XMLStream.send_presence when no `from` '
     'defined fallbacks to bound_jid')

    # Given a connection
    connection = FakeConnection()

    # And a XMLStream
    stream = XMLStream(connection)
    stream.handle_bound_jid(
        ResourceBind.create('juliet@capulet')
    )

    # When I call send_presence
    stream.send_presence(
        to='romeo@monteque',
    )

    # Then it should have sent the correct XML
    connection.output.should.equal([
        '<presence from="juliet@capulet" to="romeo@monteque"><priority>10</priority></presence>'
    ])
Ejemplo n.º 12
0
def test_send_message():
    ('XMLStream.send_message() should send a message with body')

    # Given a connection
    connection = FakeConnection()

    # And a XMLStream
    stream = XMLStream(connection)

    # When I call send_presence
    stream.send_message(
        'Hello',
        **{
            'to': 'juliet@capulet',
            'from': 'romeu@monteque',
        }
    )

    # Then it should have sent the correct XML
    connection.output.should.equal([
        '<message from="romeu@monteque" to="juliet@capulet" type="chat"><body>Hello</body></message>'
    ])
Ejemplo n.º 13
0
def test_send_presence_delay():
    ('XMLStream.send_presence should be able to send delay')

    # Given a connection
    connection = FakeConnection()

    # And a XMLStream
    stream = XMLStream(connection)

    # When I call send_presence
    stream.send_presence(
        **{
            'to': 'juliet@capulet',
            'from': 'romeu@monteque',
            'delay': 'foobar'
        }
    )

    # Then it should have sent the correct XML
    connection.output.should.equal([
        '<presence from="romeu@monteque" to="juliet@capulet"><delay from="romeu@monteque" stamp="foobar" xmlns="urn:xmpp:delay" /><priority>10</priority></presence>'
    ])
Ejemplo n.º 14
0
def test_send_presence_jid():
    ('XMLStream.send_presence when no `from` '
     'defined fallbacks')

    # Given a connection
    connection = FakeConnection()

    # And a XMLStream
    stream = XMLStream(connection)

    # When I call send_presence
    stream.send_presence(
        **{
            'to': 'juliet@capulet',
            'from': 'romeu@monteque',
        }
    )

    # Then it should have sent the correct XML
    connection.output.should.equal([
        '<presence from="romeu@monteque" to="juliet@capulet"><priority>10</priority></presence>',
    ])