Ejemplo n.º 1
0
def test_get_contacts_empty():
    agent = make_presence_connected_agent()

    future = agent.start(auto_register=False)
    future.result()

    assert agent.presence.get_contacts() == {}
Ejemplo n.º 2
0
async def test_send_agent(test_client):
    agent = make_presence_connected_agent()
    future = agent.start(auto_register=False)
    future.result()
    agent.stream = MagicMock()
    agent.stream.send = CoroutineMock()
    agent.web.setup_routes()
    client = await test_client(agent.web.app)

    jid = "friend@server"
    item = Item(jid=JID.fromstr(jid))
    agent.presence.roster._update_entry(item)

    msg = "Hello World"

    response = await client.post(f"/spade/agent/{jid}/send/", data={"message": msg})

    assert str(response.url.relative()) == f"/spade/agent/{jid}/"

    sent = agent.traces.all()[0]

    assert sent[1].sent
    assert sent[1].body == "Hello World"

    agent.stop()
Ejemplo n.º 3
0
def test_on_changed(jid):
    agent = make_presence_connected_agent()

    future = agent.start(auto_register=False)
    future.result()

    item = XSOItem(jid=jid)
    item.approved = True
    item.name = "My Friend"

    agent.presence.roster._update_entry(item)

    stanza = Presence(from_=jid, type_=PresenceType.AVAILABLE, show=PresenceShow.CHAT)
    agent.presence.presenceclient.handle_presence(stanza)

    contact = agent.presence.get_contact(jid)
    assert contact["name"] == "My Friend"
    assert contact["presence"].show == PresenceShow.CHAT

    stanza = Presence(from_=jid, type_=PresenceType.AVAILABLE, show=PresenceShow.AWAY)
    agent.presence.presenceclient.handle_presence(stanza)

    contact = agent.presence.get_contact(jid)

    assert contact["name"] == "My Friend"
    assert contact["presence"].show == PresenceShow.AWAY
Ejemplo n.º 4
0
async def test_send_agent(test_client):
    agent = make_presence_connected_agent()
    future = agent.start(auto_register=False)
    future.result()
    agent.stream = MagicMock()
    agent.stream.send = CoroutineMock()
    agent.web.setup_routes()
    client = await test_client(agent.web.app)

    jid = "friend@server"
    item = Item(jid=JID.fromstr(jid))
    agent.presence.roster._update_entry(item)

    msg = "Hello World"

    response = await client.post(f"/spade/agent/{jid}/send/",
                                 data={"message": msg})

    assert str(response.url.relative()) == f"/spade/agent/{jid}/"

    sent = agent.traces.all()[0]

    assert sent[1].sent
    assert sent[1].body == "Hello World"

    agent.stop()
Ejemplo n.º 5
0
def test_get_state_not_available():
    agent = make_presence_connected_agent(available=False, show=PresenceShow.NONE)

    assert type(agent.presence.state) == PresenceState
    assert agent.presence.state.available is False
    assert agent.presence.state.show == PresenceShow.NONE
    assert not agent.presence.is_available()
Ejemplo n.º 6
0
def test_get_contacts_empty():
    agent = make_presence_connected_agent()

    future = agent.start(auto_register=False)
    future.result()

    assert agent.presence.get_contacts() == {}
Ejemplo n.º 7
0
def test_on_changed(jid):
    agent = make_presence_connected_agent()

    future = agent.start(auto_register=False)
    future.result()

    item = XSOItem(jid=jid)
    item.approved = True
    item.name = "My Friend"

    agent.presence.roster._update_entry(item)

    stanza = Presence(from_=jid,
                      type_=PresenceType.AVAILABLE,
                      show=PresenceShow.CHAT)
    agent.presence.presenceclient.handle_presence(stanza)

    contact = agent.presence.get_contact(jid)
    assert contact["name"] == "My Friend"
    assert contact["presence"].show == PresenceShow.CHAT

    stanza = Presence(from_=jid,
                      type_=PresenceType.AVAILABLE,
                      show=PresenceShow.AWAY)
    agent.presence.presenceclient.handle_presence(stanza)

    contact = agent.presence.get_contact(jid)

    assert contact["name"] == "My Friend"
    assert contact["presence"].show == PresenceShow.AWAY
Ejemplo n.º 8
0
async def test_unsubscribe_agent(test_client):
    agent = make_presence_connected_agent()
    future = agent.start(auto_register=False)
    future.result()
    agent.client.enqueue = Mock()

    agent.web.setup_routes()
    client = await test_client(agent.web.app)

    jid = "friend@server"
    jid_ = JID.fromstr(jid)
    item = Item(jid=jid_)

    agent.presence.roster._update_entry(item)

    response = await client.get(f"/spade/agent/{jid}/unsubscribe/")

    assert str(response.url.relative()) == f"/spade/agent/{jid}/"

    assert agent.client.enqueue.mock_calls
    arg = agent.client.enqueue.call_args[0][0]

    assert arg.to == jid_.bare()
    assert arg.type_ == PresenceType.UNSUBSCRIBE

    agent.stop()
Ejemplo n.º 9
0
async def test_unsubscribe_agent(test_client):
    agent = make_presence_connected_agent()
    future = agent.start(auto_register=False)
    future.result()
    agent.client.enqueue = Mock()

    agent.web.setup_routes()
    client = await test_client(agent.web.app)

    jid = "friend@server"
    jid_ = JID.fromstr(jid)
    item = Item(jid=jid_)

    agent.presence.roster._update_entry(item)

    response = await client.get(f"/spade/agent/{jid}/unsubscribe/")

    assert str(response.url.relative()) == f"/spade/agent/{jid}/"

    assert agent.client.enqueue.mock_calls
    arg = agent.client.enqueue.call_args[0][0]

    assert arg.to == jid_.bare()
    assert arg.type_ == PresenceType.UNSUBSCRIBE

    agent.stop()
Ejemplo n.º 10
0
def test_get_status_string():
    agent = make_presence_connected_agent(status="Working")

    future = agent.start(auto_register=False)
    future.result()
    agent.mock_presence()

    assert agent.presence.status == {None: "Working"}
Ejemplo n.º 11
0
def test_get_invalid_str_contact():
    agent = make_presence_connected_agent()

    future = agent.start(auto_register=False)
    future.result()

    with pytest.raises(AttributeError):
        agent.presence.get_contact("invalid@contact")
Ejemplo n.º 12
0
def test_get_status_empty():
    agent = make_presence_connected_agent(status={})

    future = agent.start(auto_register=False)
    future.result()
    agent.mock_presence()

    assert agent.presence.status == {}
Ejemplo n.º 13
0
def test_get_status_empty():
    agent = make_presence_connected_agent(status={})

    future = agent.start(auto_register=False)
    future.result()
    agent.mock_presence()

    assert agent.presence.status == {}
Ejemplo n.º 14
0
def test_get_status_string():
    agent = make_presence_connected_agent(status="Working")

    future = agent.start(auto_register=False)
    future.result()
    agent.mock_presence()

    assert agent.presence.status == {None: "Working"}
Ejemplo n.º 15
0
def test_get_invalid_str_contact():
    agent = make_presence_connected_agent()

    future = agent.start(auto_register=False)
    future.result()

    with pytest.raises(AttributeError):
        agent.presence.get_contact("invalid@contact")
Ejemplo n.º 16
0
def test_get_state_show():
    agent = make_presence_connected_agent(available=True, show=PresenceShow.AWAY)

    future = agent.start(auto_register=False)
    future.result()
    agent.mock_presence()

    assert agent.presence.state.show == PresenceShow.AWAY
Ejemplo n.º 17
0
def test_get_priority():
    agent = make_presence_connected_agent(priority=10)

    future = agent.start(auto_register=False)
    future.result()
    agent.mock_presence()

    assert agent.presence.priority == 10
Ejemplo n.º 18
0
def test_get_invalid_jid_contact():
    agent = make_presence_connected_agent()

    future = agent.start(auto_register=False)
    future.result()

    with pytest.raises(ContactNotFound):
        agent.presence.get_contact(JID.fromstr("invalid@contact"))
Ejemplo n.º 19
0
def test_get_invalid_jid_contact():
    agent = make_presence_connected_agent()

    future = agent.start(auto_register=False)
    future.result()

    with pytest.raises(ContactNotFound):
        agent.presence.get_contact(JID.fromstr("invalid@contact"))
Ejemplo n.º 20
0
def test_get_priority():
    agent = make_presence_connected_agent(priority=10)

    future = agent.start(auto_register=False)
    future.result()
    agent.mock_presence()

    assert agent.presence.priority == 10
Ejemplo n.º 21
0
def test_get_state_available():
    agent = make_presence_connected_agent(available=True)

    future = agent.start(auto_register=False)
    future.result()
    agent.mock_presence()

    assert agent.presence.state.available
    assert agent.presence.is_available()
Ejemplo n.º 22
0
def test_set_unavailable():
    agent = make_presence_connected_agent()

    future = agent.start(auto_register=False)
    future.result()

    agent.presence.set_unavailable()

    assert not agent.presence.is_available()
Ejemplo n.º 23
0
def test_set_presence_available():
    agent = make_presence_connected_agent()

    future = agent.start(auto_register=False)
    future.result()

    agent.presence.set_presence(state=PresenceState(available=True))

    assert agent.presence.is_available()
Ejemplo n.º 24
0
def test_get_state_show():
    agent = make_presence_connected_agent(available=True,
                                          show=PresenceShow.AWAY)

    future = agent.start(auto_register=False)
    future.result()
    agent.mock_presence()

    assert agent.presence.state.show == PresenceShow.AWAY
Ejemplo n.º 25
0
def test_set_presence_status_dict():
    agent = make_presence_connected_agent()

    future = agent.start(auto_register=False)
    future.result()

    agent.presence.set_presence(status={"en": "Lunch"})

    assert agent.presence.status == {"en": "Lunch"}
Ejemplo n.º 26
0
def test_set_presence_available():
    agent = make_presence_connected_agent()

    future = agent.start(auto_register=False)
    future.result()

    agent.presence.set_presence(state=PresenceState(available=True))

    assert agent.presence.is_available()
Ejemplo n.º 27
0
def test_set_presence_status_dict():
    agent = make_presence_connected_agent()

    future = agent.start(auto_register=False)
    future.result()

    agent.presence.set_presence(status={"en": "Lunch"})

    assert agent.presence.status == {"en": "Lunch"}
Ejemplo n.º 28
0
def test_set_unavailable():
    agent = make_presence_connected_agent()

    future = agent.start(auto_register=False)
    future.result()

    agent.presence.set_unavailable()

    assert not agent.presence.is_available()
Ejemplo n.º 29
0
def test_get_state_available():
    agent = make_presence_connected_agent(available=True)

    future = agent.start(auto_register=False)
    future.result()
    agent.mock_presence()

    assert agent.presence.state.available
    assert agent.presence.is_available()
Ejemplo n.º 30
0
def test_set_available_with_show():
    agent = make_presence_connected_agent()

    future = agent.start(auto_register=False)
    future.result()

    agent.presence.set_available(show=PresenceShow.CHAT)

    assert agent.presence.is_available()
    assert agent.presence.state.show == PresenceShow.CHAT
Ejemplo n.º 31
0
def test_get_state_not_available():
    agent = make_presence_connected_agent(available=False, show=PresenceShow.NONE)

    future = agent.start(auto_register=False)
    future.result()

    assert type(agent.presence.state) == PresenceState
    assert agent.presence.state.available is False
    assert agent.presence.state.show == PresenceShow.NONE
    assert not agent.presence.is_available()
Ejemplo n.º 32
0
def test_set_available_with_show():
    agent = make_presence_connected_agent()

    future = agent.start(auto_register=False)
    future.result()

    agent.presence.set_available(show=PresenceShow.CHAT)

    assert agent.presence.is_available()
    assert agent.presence.state.show == PresenceShow.CHAT
Ejemplo n.º 33
0
def test_on_unsubscribed(jid):
    agent = make_presence_connected_agent()
    agent.presence.on_unsubscribed = Mock()

    stanza = Presence(from_=jid, type_=PresenceType.UNSUBSCRIBED)
    agent.presence.roster.handle_unsubscribed(stanza)

    jid_arg = agent.presence.on_unsubscribed.call_args[0][0]

    assert jid_arg == str(jid)
Ejemplo n.º 34
0
def test_get_state_not_available():
    agent = make_presence_connected_agent(available=False,
                                          show=PresenceShow.NONE)

    future = agent.start(auto_register=False)
    future.result()

    assert type(agent.presence.state) == PresenceState
    assert agent.presence.state.available is False
    assert agent.presence.state.show == PresenceShow.NONE
    assert not agent.presence.is_available()
Ejemplo n.º 35
0
def test_set_presence():
    agent = make_presence_connected_agent()

    agent.presence.set_presence(state=PresenceState(True, PresenceShow.PLAIN),
                                status="Lunch",
                                priority=2)

    assert agent.presence.is_available()
    assert agent.presence.state.show == PresenceShow.PLAIN
    assert agent.presence.status == {None: "Lunch"}
    assert agent.presence.priority == 2
Ejemplo n.º 36
0
def test_ignore_self_presence():
    agent = make_presence_connected_agent()
    jid = agent.jid

    stanza = Presence(from_=jid, type_=PresenceType.AVAILABLE, show=PresenceShow.CHAT)
    agent.presence.presenceclient.handle_presence(stanza)

    with pytest.raises(ContactNotFound):
        agent.presence.get_contact(jid)

    assert len(agent.presence.get_contacts()) == 0
Ejemplo n.º 37
0
def test_approve(jid):
    peer_jid = str(jid)
    agent = make_presence_connected_agent()

    agent.aiothread.client.enqueue = Mock()
    agent.presence.approve(peer_jid)

    assert agent.aiothread.client.enqueue.mock_calls
    arg = agent.aiothread.client.enqueue.call_args[0][0]

    assert arg.to == jid.bare()
    assert arg.type_ == PresenceType.SUBSCRIBED
Ejemplo n.º 38
0
def test_on_available(jid):
    agent = make_presence_connected_agent()
    agent.presence.on_available = Mock()

    stanza = Presence(from_=jid, type_=PresenceType.AVAILABLE)
    agent.presence.presenceclient.handle_presence(stanza)

    jid_arg = agent.presence.on_available.call_args[0][0]
    stanza_arg = agent.presence.on_available.call_args[0][1]

    assert jid_arg == str(jid)
    assert stanza_arg.type_ == PresenceType.AVAILABLE
Ejemplo n.º 39
0
def test_on_unsubscribe_approve_all(jid):
    agent = make_presence_connected_agent()
    agent.presence.approve_all = True
    agent.aiothread.client.enqueue = Mock()

    stanza = Presence(from_=jid, type_=PresenceType.UNSUBSCRIBE)
    agent.presence.roster.handle_unsubscribe(stanza)

    assert agent.aiothread.client.enqueue.mock_calls
    arg = agent.aiothread.client.enqueue.call_args[0][0]

    assert arg.to == jid.bare()
    assert arg.type_ == PresenceType.UNSUBSCRIBED
Ejemplo n.º 40
0
def test_on_unavailable(jid):
    agent = make_presence_connected_agent()
    agent.presence.on_unavailable = Mock()
    agent.presence.presenceclient._presences[jid.bare()] = {"home": None}

    stanza = Presence(from_=jid, type_=PresenceType.UNAVAILABLE)
    agent.presence.presenceclient.handle_presence(stanza)

    jid_arg = agent.presence.on_unavailable.call_args[0][0]
    stanza_arg = agent.presence.on_unavailable.call_args[0][1]

    assert jid_arg == str(jid)
    assert stanza_arg.type_ == PresenceType.UNAVAILABLE
Ejemplo n.º 41
0
def test_set_presence():
    agent = make_presence_connected_agent()

    future = agent.start(auto_register=False)
    future.result()

    agent.presence.set_presence(state=PresenceState(True, PresenceShow.PLAIN),
                                status="Lunch",
                                priority=2)

    assert agent.presence.is_available()
    assert agent.presence.state.show == PresenceShow.PLAIN
    assert agent.presence.status == {None: "Lunch"}
    assert agent.presence.priority == 2
Ejemplo n.º 42
0
def test_on_unsubscribed(jid):
    agent = make_presence_connected_agent()

    future = agent.start(auto_register=False)
    future.result()

    agent.presence.on_unsubscribed = Mock()

    stanza = Presence(from_=jid, type_=PresenceType.UNSUBSCRIBED)
    agent.presence.roster.handle_unsubscribed(stanza)

    jid_arg = agent.presence.on_unsubscribed.call_args[0][0]

    assert jid_arg == str(jid)
Ejemplo n.º 43
0
def test_on_unsubscribe(jid):
    agent = make_presence_connected_agent()

    future = agent.start(auto_register=False)
    future.result()

    agent.presence.on_unsubscribe = Mock()

    stanza = Presence(from_=jid, type_=PresenceType.UNSUBSCRIBE)
    agent.presence.roster.handle_unsubscribe(stanza)

    jid_arg = agent.presence.on_unsubscribe.call_args[0][0]

    assert jid_arg == str(jid)
Ejemplo n.º 44
0
def test_approve(jid):
    peer_jid = str(jid)
    agent = make_presence_connected_agent()

    future = agent.start(auto_register=False)
    future.result()

    agent.client.enqueue = Mock()
    agent.presence.approve(peer_jid)

    assert agent.client.enqueue.mock_calls
    arg = agent.client.enqueue.call_args[0][0]

    assert arg.to == jid.bare()
    assert arg.type_ == PresenceType.SUBSCRIBED
Ejemplo n.º 45
0
def test_ignore_self_presence():
    agent = make_presence_connected_agent()

    future = agent.start(auto_register=False)
    future.result()

    jid = agent.jid

    stanza = Presence(from_=jid, type_=PresenceType.AVAILABLE, show=PresenceShow.CHAT)
    agent.presence.presenceclient.handle_presence(stanza)

    with pytest.raises(ContactNotFound):
        agent.presence.get_contact(jid)

    assert len(agent.presence.get_contacts()) == 0
Ejemplo n.º 46
0
def test_approve(jid):
    peer_jid = str(jid)
    agent = make_presence_connected_agent()

    future = agent.start(auto_register=False)
    future.result()

    agent.client.enqueue = Mock()
    agent.presence.approve(peer_jid)

    assert agent.client.enqueue.mock_calls
    arg = agent.client.enqueue.call_args[0][0]

    assert arg.to == jid.bare()
    assert arg.type_ == PresenceType.SUBSCRIBED
Ejemplo n.º 47
0
def test_on_available(jid):
    agent = make_presence_connected_agent()

    future = agent.start(auto_register=False)
    future.result()

    agent.presence.on_available = Mock()

    stanza = Presence(from_=jid, type_=PresenceType.AVAILABLE)
    agent.presence.presenceclient.handle_presence(stanza)

    jid_arg = agent.presence.on_available.call_args[0][0]
    stanza_arg = agent.presence.on_available.call_args[0][1]

    assert jid_arg == str(jid)
    assert stanza_arg.type_ == PresenceType.AVAILABLE
Ejemplo n.º 48
0
def test_set_presence():
    agent = make_presence_connected_agent()

    future = agent.start(auto_register=False)
    future.result()

    agent.presence.set_presence(
        state=PresenceState(True, PresenceShow.PLAIN),
        status="Lunch",
        priority=2
    )

    assert agent.presence.is_available()
    assert agent.presence.state.show == PresenceShow.PLAIN
    assert agent.presence.status == {None: "Lunch"}
    assert agent.presence.priority == 2
Ejemplo n.º 49
0
def test_on_unsubscribe_approve_all(jid):
    agent = make_presence_connected_agent()

    future = agent.start(auto_register=False)
    future.result()

    agent.presence.approve_all = True
    agent.client.enqueue = Mock()

    stanza = Presence(from_=jid, type_=PresenceType.UNSUBSCRIBE)
    agent.presence.roster.handle_unsubscribe(stanza)

    assert agent.client.enqueue.mock_calls
    arg = agent.client.enqueue.call_args[0][0]

    assert arg.to == jid.bare()
    assert arg.type_ == PresenceType.UNSUBSCRIBED
Ejemplo n.º 50
0
def test_get_contact(jid):
    agent = make_presence_connected_agent()

    item = XSOItem(jid=jid)
    item.approved = True
    item.name = "My Friend"

    agent.presence.roster._update_entry(item)

    contact = agent.presence.get_contact(jid)

    assert type(contact) == dict
    assert contact["approved"]
    assert contact["name"] == "My Friend"
    assert contact["subscription"] == 'none'
    assert "ask" not in contact
    assert "groups" not in contact
Ejemplo n.º 51
0
def test_on_subscribe_approve_all(jid):
    agent = make_presence_connected_agent()

    future = agent.start(auto_register=False)
    future.result()

    agent.presence.approve_all = True
    agent.client.enqueue = Mock()

    stanza = Presence(from_=jid, type_=PresenceType.SUBSCRIBE)
    agent.presence.roster.handle_subscribe(stanza)

    assert agent.client.enqueue.mock_calls
    arg = agent.client.enqueue.call_args[0][0]

    assert arg.to == jid.bare()
    assert arg.type_ == PresenceType.SUBSCRIBED
Ejemplo n.º 52
0
def test_get_contacts_with_presence_unavailable(jid):
    agent = make_presence_connected_agent()

    item = XSOItem(jid=jid)
    item.approved = True
    item.name = "My UnAvailable Friend"

    agent.presence.roster._update_entry(item)

    stanza = Presence(from_=jid, type_=PresenceType.UNAVAILABLE)
    agent.presence.presenceclient.handle_presence(stanza)

    contacts = agent.presence.get_contacts()

    assert jid in contacts
    assert contacts[jid]["name"] == "My UnAvailable Friend"

    assert "presence" not in contacts[jid]
Ejemplo n.º 53
0
def test_get_contact(jid):
    agent = make_presence_connected_agent()

    future = agent.start(auto_register=False)
    future.result()

    item = XSOItem(jid=jid)
    item.approved = True
    item.name = "My Friend"

    agent.presence.roster._update_entry(item)

    contact = agent.presence.get_contact(jid)

    assert type(contact) == dict
    assert contact["approved"]
    assert contact["name"] == "My Friend"
    assert contact["subscription"] == 'none'
    assert "ask" not in contact
    assert "groups" not in contact
Ejemplo n.º 54
0
async def test_get_agent(test_client):
    agent = make_presence_connected_agent("jid@server", "password")
    future = agent.start(auto_register=False)
    future.result()

    agent.web.setup_routes()
    client = await test_client(agent.web.app)

    jid = "friend@server"
    item = Item(jid=JID.fromstr(jid))

    agent.presence.roster._update_entry(item)

    response = await client.get(f"/spade/agent/{jid}/")
    response = await response.text()

    sel = Selector(text=response)

    assert sel.css("section.content-header > h1::text").get().strip() == jid

    agent.stop()
Ejemplo n.º 55
0
def test_get_contacts_with_presence_unavailable(jid):
    agent = make_presence_connected_agent()

    future = agent.start(auto_register=False)
    future.result()

    item = XSOItem(jid=jid)
    item.approved = True
    item.name = "My UnAvailable Friend"

    agent.presence.roster._update_entry(item)

    stanza = Presence(from_=jid, type_=PresenceType.UNAVAILABLE)
    agent.presence.presenceclient.handle_presence(stanza)

    contacts = agent.presence.get_contacts()

    bare_jid = jid.bare()
    assert bare_jid in contacts
    assert contacts[bare_jid]["name"] == "My UnAvailable Friend"

    assert "presence" not in contacts[bare_jid]