-
-
Notifications
You must be signed in to change notification settings - Fork 419
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Matrix Server Delegation Support (m.server param)
- Loading branch information
Showing
2 changed files
with
206 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ | |
# POSSIBILITY OF SUCH DAMAGE. | ||
|
||
from unittest import mock | ||
import socket | ||
import os | ||
import requests | ||
import pytest | ||
|
@@ -1012,6 +1013,110 @@ def test_plugin_matrix_attachments_api_v3(mock_post, mock_get, mock_put): | |
del obj | ||
|
||
|
||
@mock.patch('socket.getaddrinfo') | ||
@mock.patch('requests.get') | ||
@mock.patch('requests.post') | ||
def test_plugin_matrix_delegate_server(mock_post, mock_get, mock_getaddrinfo): | ||
""" | ||
NotifyMatrix() Delegate Server | ||
""" | ||
|
||
# Prepare a good response | ||
response = mock.Mock() | ||
response.status_code = requests.codes.ok | ||
response.content = MATRIX_GOOD_RESPONSE.encode('utf-8') | ||
|
||
# Prepare Mock return object | ||
mock_post.return_value = response | ||
mock_get.return_value = response | ||
|
||
# Instantiate our object | ||
obj = Apprise.instantiate( | ||
'matrix://user:[email protected]/#general?v=2&delegate=yes') | ||
|
||
# test throwing exception | ||
mock_getaddrinfo.side_effect = socket.gaierror | ||
|
||
# A failure | ||
assert '__delegate' not in obj.store | ||
assert obj.delegated_server_lookup() is False | ||
assert '__delegate' not in obj.store | ||
|
||
# Test notifications | ||
assert obj.notify( | ||
body='body', title='title', notify_type=NotifyType.INFO, | ||
) is False | ||
|
||
mock_getaddrinfo.side_effect = None | ||
mock_getaddrinfo.return_value = [ | ||
( | ||
socket.AF_INET, # Address family (IPv4) | ||
socket.SOCK_STREAM, # Socket type (TCP) | ||
socket.IPPROTO_TCP, # Protocol (TCP) | ||
'', # Canonical name (empty in this case) | ||
('192.168.1.10', 8448) # Socket address (IP address, port) | ||
), | ||
( | ||
socket.AF_INET, # Address family (IPv4) | ||
socket.SOCK_STREAM, # Socket type (TCP) | ||
socket.IPPROTO_TCP, # Protocol (TCP) | ||
'', # Canonical name (empty in this case) | ||
('192.168.1.11', 8448) # Socket address (IP address, port) | ||
) | ||
] | ||
|
||
# Re-initialize | ||
obj = Apprise.instantiate( | ||
'matrix://user:[email protected]/#general?v=2&delegate=yes') | ||
|
||
assert obj.delegated_server_lookup() == '192.168.1.10:8448' | ||
assert '__delegate' in obj.store | ||
assert obj.store.get('__delegate') == '192.168.1.10:8448' | ||
# A second call uses the cached value | ||
assert obj.delegated_server_lookup() == '192.168.1.10:8448' | ||
|
||
# Test notifications | ||
obj.store.clear('__delegate') | ||
assert obj.notify( | ||
body='body', title='title', notify_type=NotifyType.INFO, | ||
) is True | ||
|
||
# Now clear our cache | ||
mock_getaddrinfo.return_value = [] | ||
obj.store.clear('__delegate') | ||
# No entry found | ||
assert obj.delegated_server_lookup() is None | ||
assert '__delegate' not in obj.store | ||
|
||
# Test notifications | ||
obj.store.clear('__delegate') | ||
assert obj.notify( | ||
body='body', title='title', notify_type=NotifyType.INFO, | ||
) is True | ||
|
||
mock_getaddrinfo.return_value = [ | ||
( | ||
socket.AF_INET6, # Address family (IPv6) | ||
socket.SOCK_STREAM, # Socket type (TCP) | ||
socket.IPPROTO_TCP, # Protocol (TCP) | ||
'', # Canonical name (empty in this case) | ||
# Socket address (IPv6 address, port, flow info, scope ID) | ||
('2001:0db8:85a3:0000:0000:8a2e:0370:7334', 8448, 0, 0) | ||
), | ||
] | ||
obj.store.clear('__delegate') | ||
# No entry found that is IPv4 | ||
assert obj.delegated_server_lookup() is None | ||
assert '__delegate' not in obj.store | ||
|
||
# Test notifications | ||
obj.store.clear('__delegate') | ||
assert obj.notify( | ||
body='body', title='title', notify_type=NotifyType.INFO, | ||
) is True | ||
|
||
|
||
@mock.patch('requests.get') | ||
@mock.patch('requests.post') | ||
def test_plugin_matrix_attachments_api_v2(mock_post, mock_get): | ||
|