Commit bffeb7ea authored by Nico's avatar Nico
Browse files

Add tests for Contacts.handle_connections_callback

parent 5c0683b5
Pipeline #4983 passed with stage
in 2 minutes and 4 seconds
......@@ -89,14 +89,14 @@ class Contacts(Model):
"""
self._connections_callback = callback
signal_ids = list()
event_callback = self._handle_connections_callback
event_callback = self.handle_connections_callback
for event in self._CONNECTION_EVENTS:
signal_id = self._api.socket_listener.connect(event,
event_callback)
signal_ids.append(signal_id)
return signal_ids
def _handle_connections_callback(self, message):
def handle_connections_callback(self, message):
contact_id = message["data"]["contactId"]
if message["name"] == "ContactConnectedEvent":
self._connections_callback(contact_id, True)
......
......@@ -4,6 +4,7 @@
import json
import pytest
import requests_mock
from briar_wrapper.models.contacts import Contacts
......@@ -12,6 +13,7 @@ from briar_wrapper.models.socket_listener import SocketListener
BASE_HTTP_URL = "http://localhost:7000/v1/contacts/"
TEST_ALIAS = "Alice"
TEST_CONTACT_ID = 42
TEST_CONTACT_FIRST = {
"lastChatActivity": 1
......@@ -109,7 +111,7 @@ def test_get_link(api, request_headers, requests_mock):
assert contacts.get_link() == TEST_LINK
def test_watch_signal_added(api, mocker):
def test_watch_signal_added(api):
contacts = Contacts(api)
contacts._api.socket_listener = SocketListener(api)
contacts._api.socket_listener._highest_signal_id = 136
......@@ -121,12 +123,64 @@ def test_watch_signal_added(api, mocker):
expected_signals = {
137: {
"event": "ContactConnectedEvent",
"callback": contacts._handle_connections_callback
"callback": contacts.handle_connections_callback
},
138: {
"event": "ContactDisconnectedEvent",
"callback": contacts._handle_connections_callback
"callback": contacts.handle_connections_callback
}
}
assert contacts._api.socket_listener._signals == expected_signals
def test_handle_connections_callback_contact_connected(api, mocker):
contacts = Contacts(api)
contacts._connections_callback = mocker.MagicMock()
message = {
"data": {
"contactId": TEST_CONTACT_ID
},
"name": "ContactConnectedEvent"
}
contacts.handle_connections_callback(message)
contacts._connections_callback.assert_called_once_with(
TEST_CONTACT_ID, True
)
def test_handle_connections_callback_contact_disconnected(api, mocker):
contacts = Contacts(api)
contacts._connections_callback = mocker.MagicMock()
message = {
"data": {
"contactId": TEST_CONTACT_ID
},
"name": "ContactDisconnectedEvent"
}
contacts.handle_connections_callback(message)
contacts._connections_callback.assert_called_once_with(
TEST_CONTACT_ID, False
)
def test_handle_connections_callback_wrong_event(api, mocker):
contacts = Contacts(api)
contacts._connections_callback = mocker.MagicMock()
message = {
"data": {
"contactId": TEST_CONTACT_ID
},
"name": "SomeEvent"
}
with pytest.raises(Exception,
match="Wrong event in callback: SomeEvent"):
contacts.handle_connections_callback(message)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment