Browse Source

Handle HTTP 429 responses from Matrix server when joining a Matrix room

pull/405/head
Pēteris Caune 4 years ago
parent
commit
f789cad2af
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
3 changed files with 21 additions and 1 deletions
  1. +1
    -0
      CHANGELOG.md
  2. +8
    -1
      hc/front/forms.py
  3. +12
    -0
      hc/front/tests/test_add_matrix.py

+ 1
- 0
CHANGELOG.md View File

@ -15,6 +15,7 @@ All notable changes to this project will be documented in this file.
### Bug Fixes ### Bug Fixes
- Removing Pager Team integration, project appears to be discontinued - Removing Pager Team integration, project appears to be discontinued
- Sending a test notification updates Channel.last_error (#391) - Sending a test notification updates Channel.last_error (#391)
- Handle HTTP 429 responses from Matrix server when joining a Matrix room
## v1.15.0 - 2020-06-04 ## v1.15.0 - 2020-06-04


+ 8
- 1
hc/front/forms.py View File

@ -207,7 +207,14 @@ class AddMatrixForm(forms.Form):
url = settings.MATRIX_HOMESERVER url = settings.MATRIX_HOMESERVER
url += "/_matrix/client/r0/join/%s?" % quote(v) url += "/_matrix/client/r0/join/%s?" % quote(v)
url += urlencode({"access_token": settings.MATRIX_ACCESS_TOKEN}) url += urlencode({"access_token": settings.MATRIX_ACCESS_TOKEN})
doc = requests.post(url, {}).json()
r = requests.post(url, {})
if r.status_code == 429:
raise forms.ValidationError(
"Matrix server returned status code 429 (Too Many Requests), "
"please try again later."
)
doc = r.json()
if "error" in doc: if "error" in doc:
raise forms.ValidationError("Response from Matrix: %s" % doc["error"]) raise forms.ValidationError("Response from Matrix: %s" % doc["error"])


+ 12
- 0
hc/front/tests/test_add_matrix.py View File

@ -1,3 +1,4 @@
from json import JSONDecodeError
from unittest.mock import patch from unittest.mock import patch
from django.test.utils import override_settings from django.test.utils import override_settings
@ -37,3 +38,14 @@ class AddMatrixTestCase(BaseTestCase):
self.client.login(username="[email protected]", password="password") self.client.login(username="[email protected]", password="password")
r = self.client.get(self.url) r = self.client.get(self.url)
self.assertEqual(r.status_code, 404) self.assertEqual(r.status_code, 404)
@patch("hc.front.forms.requests.post")
def test_it_handles_429(self, mock_post):
mock_post.return_value.status_code = 429
form = {"alias": "!foo:example.org"}
self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, form)
self.assertContains(r, "Matrix server returned status code 429")
self.assertFalse(Channel.objects.exists())

Loading…
Cancel
Save