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.

87 lines
3.0 KiB

  1. from django.test.utils import override_settings
  2. from hc.api.models import Channel, Check
  3. from hc.test import BaseTestCase
  4. TEST_CREDENTIALS = {
  5. "TWILIO_ACCOUNT": "foo",
  6. "TWILIO_AUTH": "foo",
  7. "TWILIO_FROM": "123",
  8. "TWILIO_USE_WHATSAPP": True,
  9. }
  10. @override_settings(**TEST_CREDENTIALS)
  11. class AddWhatsAppTestCase(BaseTestCase):
  12. def setUp(self):
  13. super().setUp()
  14. self.check = Check.objects.create(project=self.project)
  15. self.url = f"/projects/{self.project.code}/add_whatsapp/"
  16. def test_instructions_work(self):
  17. self.client.login(username="[email protected]", password="password")
  18. r = self.client.get(self.url)
  19. self.assertContains(r, "Add WhatsApp Integration")
  20. self.assertContains(r, "Get a WhatsApp message")
  21. @override_settings(USE_PAYMENTS=True)
  22. def test_it_warns_about_limits(self):
  23. self.profile.sms_limit = 0
  24. self.profile.save()
  25. self.client.login(username="[email protected]", password="password")
  26. r = self.client.get(self.url)
  27. self.assertContains(r, "upgrade to a")
  28. def test_it_creates_channel(self):
  29. form = {
  30. "label": "My Phone",
  31. "phone": "+1234567890",
  32. "down": "true",
  33. "up": "true",
  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. c = Channel.objects.get()
  39. self.assertEqual(c.kind, "whatsapp")
  40. self.assertEqual(c.phone_number, "+1234567890")
  41. self.assertEqual(c.name, "My Phone")
  42. self.assertTrue(c.whatsapp_notify_down)
  43. self.assertTrue(c.whatsapp_notify_up)
  44. self.assertEqual(c.project, self.project)
  45. # Make sure it calls assign_all_checks
  46. self.assertEqual(c.checks.count(), 1)
  47. @override_settings(TWILIO_USE_WHATSAPP=False)
  48. def test_it_obeys_use_whatsapp_flag(self):
  49. self.client.login(username="[email protected]", password="password")
  50. r = self.client.get(self.url)
  51. self.assertEqual(r.status_code, 404)
  52. def test_it_requires_rw_access(self):
  53. self.bobs_membership.role = "r"
  54. self.bobs_membership.save()
  55. self.client.login(username="[email protected]", password="password")
  56. r = self.client.get(self.url)
  57. self.assertEqual(r.status_code, 403)
  58. def test_it_handles_down_false_up_true(self):
  59. form = {"phone": "+1234567890", "up": True}
  60. self.client.login(username="[email protected]", password="password")
  61. self.client.post(self.url, form)
  62. c = Channel.objects.get()
  63. self.assertFalse(c.whatsapp_notify_down)
  64. self.assertTrue(c.whatsapp_notify_up)
  65. def test_it_rejects_unchecked_up_and_down(self):
  66. form = {"phone": "+1234567890"}
  67. self.client.login(username="[email protected]", password="password")
  68. r = self.client.post(self.url, form)
  69. self.assertContains(r, "Please select at least one.")