Skip to content

Commit

Permalink
test case coverage fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
caronc committed Sep 14, 2024
1 parent f9ae7a9 commit c804def
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
8 changes: 8 additions & 0 deletions apprise/plugins/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -1640,6 +1640,14 @@ def server_discovery(self):
# This is an acceptable response; we're done
self.logger.debug(
'Matrix Well-Known Base URI not found at %s', verify_url)

# Clear our keys out for fast recall later on
self.store.set(
self.discovery_base_key, '',
expires=self.discovery_cache_length_sec)
self.store.set(
self.discovery_identity_key, '',
expires=self.discovery_cache_length_sec)
return ''

elif code != requests.codes.ok:
Expand Down
36 changes: 36 additions & 0 deletions test/test_plugin_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,7 @@ def test_plugin_matrix_discovery_service(mock_post, mock_get):
response.status_code = requests.codes.ok
obj.store.clear(
NotifyMatrix.discovery_base_key, NotifyMatrix.discovery_identity_key)

# bad data
_resp['m.homeserver'] = '!garbage!:303'
response.content = dumps(_resp).encode('utf-8')
Expand All @@ -1083,6 +1084,9 @@ def test_plugin_matrix_discovery_service(mock_post, mock_get):
assert NotifyMatrix.discovery_base_key not in obj.store
assert NotifyMatrix.discovery_identity_key not in obj.store

# We fail our discovery and therefore can't send our notification
assert obj.notify('hello world') is False

# bad key
_resp['m.homeserver'] = {}
response.content = dumps(_resp).encode('utf-8')
Expand All @@ -1102,6 +1106,13 @@ def test_plugin_matrix_discovery_service(mock_post, mock_get):
assert obj.base_url == 'https://nuxref.com/base'
assert obj.identity_url == "https://vector.im"

# Verify cache saved
assert NotifyMatrix.discovery_base_key in obj.store
assert NotifyMatrix.discovery_identity_key in obj.store

# Discovery passes so notifications work too
assert obj.notify('hello world') is True

# bad data
_resp['m.identity_server'] = '!garbage!:303'
response.content = dumps(_resp).encode('utf-8')
Expand Down Expand Up @@ -1148,6 +1159,14 @@ def test_plugin_matrix_discovery_service(mock_post, mock_get):
assert obj.base_url == 'https://example.com'
assert obj.identity_url == 'https://example.com'

# Verify cache saved
assert NotifyMatrix.discovery_base_key in obj.store
assert NotifyMatrix.discovery_identity_key in obj.store

# Discovery passes so notifications work too
response.status_code = requests.codes.ok
assert obj.notify('hello world') is True

response.status_code = requests.codes.ok
mock_get.return_value = None
mock_get.side_effect = (response, bad_response)
Expand All @@ -1161,7 +1180,24 @@ def test_plugin_matrix_discovery_service(mock_post, mock_get):
assert NotifyMatrix.discovery_base_key not in obj.store
assert NotifyMatrix.discovery_identity_key not in obj.store

# Test case where ourIdentity URI fails to do it's check
mock_get.side_effect = (response, response, bad_response)
obj.store.clear(
NotifyMatrix.discovery_base_key, NotifyMatrix.discovery_identity_key)

with pytest.raises(MatrixDiscoveryException):
obj.base_url

# Verify cache is not saved
assert NotifyMatrix.discovery_base_key not in obj.store
assert NotifyMatrix.discovery_identity_key not in obj.store

# Enforce cleanup
response.status_code = requests.codes.ok
mock_get.return_value = response
mock_get.side_effect = None
mock_post.return_value = response
mock_post.side_effect = None
del obj


Expand Down

0 comments on commit c804def

Please sign in to comment.