You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.3 KiB

  1. import json
  2. from django.test.utils import override_settings
  3. from hc.api.models import Channel, Check
  4. from hc.test import BaseTestCase
  5. TEST_CREDENTIALS = {
  6. "TWILIO_ACCOUNT": "foo",
  7. "TWILIO_AUTH": "foo",
  8. "TWILIO_FROM": "123",
  9. "TWILIO_USE_WHATSAPP": True,
  10. }
  11. @override_settings(**TEST_CREDENTIALS)
  12. class EditWhatsAppTestCase(BaseTestCase):
  13. def setUp(self):
  14. super().setUp()
  15. self.check = Check.objects.create(project=self.project)
  16. self.channel = Channel(project=self.project, kind="whatsapp")
  17. self.channel.value = json.dumps(
  18. {"value": "+12345678", "up": True, "down": True}
  19. )
  20. self.channel.save()
  21. self.url = f"/integrations/{self.channel.code}/edit/"
  22. def test_instructions_work(self):
  23. self.client.login(username="[email protected]", password="password")
  24. r = self.client.get(self.url)
  25. self.assertContains(r, "WhatsApp Settings")
  26. self.assertContains(r, "Get a WhatsApp message")
  27. self.assertContains(r, "+12345678")
  28. def test_it_updates_channel(self):
  29. form = {
  30. "label": "My Phone",
  31. "phone": "+1234567890",
  32. "down": "true",
  33. "up": "false",
  34. }
  35. self.client.login(username="[email protected]", password="password")
  36. r = self.client.post(self.url, form)
  37. self.assertRedirects(r, self.channels_url)
  38. self.channel.refresh_from_db()
  39. self.assertEqual(self.channel.phone_number, "+1234567890")
  40. self.assertEqual(self.channel.name, "My Phone")
  41. self.assertTrue(self.channel.whatsapp_notify_down)
  42. self.assertFalse(self.channel.whatsapp_notify_up)
  43. # Make sure it does not call assign_all_checks
  44. self.assertFalse(self.channel.checks.exists())
  45. @override_settings(TWILIO_USE_WHATSAPP=False)
  46. def test_it_obeys_use_whatsapp_flag(self):
  47. self.client.login(username="[email protected]", password="password")
  48. r = self.client.get(self.url)
  49. self.assertEqual(r.status_code, 404)
  50. def test_it_requires_rw_access(self):
  51. self.bobs_membership.role = "r"
  52. self.bobs_membership.save()
  53. self.client.login(username="[email protected]", password="password")
  54. r = self.client.get(self.url)
  55. self.assertEqual(r.status_code, 403)