Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
briar
Python Briar Wrapper
Commits
bffeb7ea
Commit
bffeb7ea
authored
Oct 10, 2020
by
Nico
Browse files
Add tests for Contacts.handle_connections_callback
parent
5c0683b5
Pipeline
#4983
passed with stage
in 2 minutes and 4 seconds
Changes
2
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
briar_wrapper/models/contacts.py
View file @
bffeb7ea
...
...
@@ -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
)
...
...
tests/briar_wrapper/models/test_contacts.py
View file @
bffeb7ea
...
...
@@ -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
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment