From a127ab0f0c3d7b1691b98bf9848e066b7454b399 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Wed, 3 Nov 2021 11:22:27 +0200 Subject: [PATCH] Add handling for HTTP 502 response from Matrix server --- hc/front/forms.py | 5 +++++ hc/front/tests/test_add_matrix.py | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/hc/front/forms.py b/hc/front/forms.py index 6f0641ad..44ce79c6 100644 --- a/hc/front/forms.py +++ b/hc/front/forms.py @@ -272,6 +272,11 @@ class AddMatrixForm(forms.Form): "Matrix server returned status code 429 (Too Many Requests), " "please try again later." ) + if r.status_code == 502: + raise forms.ValidationError( + "Matrix server returned status code 502 (Bad Gateway), " + "please try again later." + ) doc = r.json() if "error" in doc: diff --git a/hc/front/tests/test_add_matrix.py b/hc/front/tests/test_add_matrix.py index 69bafcaa..c0e91e11 100644 --- a/hc/front/tests/test_add_matrix.py +++ b/hc/front/tests/test_add_matrix.py @@ -20,6 +20,7 @@ class AddMatrixTestCase(BaseTestCase): @patch("hc.front.forms.requests.post") def test_it_works(self, mock_post): + mock_post.return_value.status_code = 200 mock_post.return_value.json.return_value = {"room_id": "fake-room-id"} form = {"alias": "!foo:example.org"} @@ -49,6 +50,17 @@ class AddMatrixTestCase(BaseTestCase): self.assertContains(r, "Matrix server returned status code 429") self.assertFalse(Channel.objects.exists()) + @patch("hc.front.forms.requests.post") + def test_it_handles_502(self, mock_post): + mock_post.return_value.status_code = 502 + + form = {"alias": "!foo:example.org"} + self.client.login(username="alice@example.org", password="password") + r = self.client.post(self.url, form) + + self.assertContains(r, "Matrix server returned status code 502") + self.assertFalse(Channel.objects.exists()) + def test_it_requires_rw_access(self): self.bobs_membership.role = "r" self.bobs_membership.save()