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.

121 lines
4.5 KiB

7 years ago
  1. from django.test.utils import override_settings
  2. from hc.api.models import Channel, Check
  3. from hc.test import BaseTestCase
  4. @override_settings(TWILIO_ACCOUNT="foo", TWILIO_AUTH="foo", TWILIO_FROM="123")
  5. class AddSmsTestCase(BaseTestCase):
  6. def setUp(self):
  7. super().setUp()
  8. self.check = Check.objects.create(project=self.project)
  9. self.url = f"/projects/{self.project.code}/add_sms/"
  10. def test_instructions_work(self):
  11. self.client.login(username="[email protected]", password="password")
  12. r = self.client.get(self.url)
  13. self.assertContains(r, "Add SMS Integration")
  14. self.assertContains(r, "Get a SMS message")
  15. @override_settings(USE_PAYMENTS=True)
  16. def test_it_warns_about_limits(self):
  17. self.profile.sms_limit = 0
  18. self.profile.save()
  19. self.client.login(username="[email protected]", password="password")
  20. r = self.client.get(self.url)
  21. self.assertContains(r, "upgrade to a")
  22. def test_it_creates_channel(self):
  23. form = {"label": "My Phone", "phone": "+1234567890", "down": True}
  24. self.client.login(username="[email protected]", password="password")
  25. r = self.client.post(self.url, form)
  26. self.assertRedirects(r, self.channels_url)
  27. c = Channel.objects.get()
  28. self.assertEqual(c.kind, "sms")
  29. self.assertEqual(c.phone_number, "+1234567890")
  30. self.assertEqual(c.name, "My Phone")
  31. self.assertTrue(c.sms_notify_down)
  32. self.assertFalse(c.sms_notify_up)
  33. self.assertEqual(c.project, self.project)
  34. # Make sure it calls assign_all_checks
  35. self.assertEqual(c.checks.count(), 1)
  36. def test_it_rejects_bad_number(self):
  37. for v in ["not a phone number", False, 15, "+123456789A"]:
  38. form = {"phone": v}
  39. self.client.login(username="[email protected]", password="password")
  40. r = self.client.post(self.url, form)
  41. self.assertContains(r, "Invalid phone number format.")
  42. def test_it_trims_whitespace(self):
  43. form = {"phone": " +1234567890 ", "down": True}
  44. self.client.login(username="[email protected]", password="password")
  45. self.client.post(self.url, form)
  46. c = Channel.objects.get()
  47. self.assertEqual(c.phone_number, "+1234567890")
  48. @override_settings(TWILIO_AUTH=None)
  49. def test_it_requires_credentials(self):
  50. self.client.login(username="[email protected]", password="password")
  51. r = self.client.get(self.url)
  52. self.assertEqual(r.status_code, 404)
  53. def test_it_requires_rw_access(self):
  54. self.bobs_membership.role = "r"
  55. self.bobs_membership.save()
  56. self.client.login(username="[email protected]", password="password")
  57. r = self.client.get(self.url)
  58. self.assertEqual(r.status_code, 403)
  59. def test_it_strips_invisible_formatting_characters(self):
  60. form = {"phone": "\u202c+1234567890\u202c", "down": True}
  61. self.client.login(username="[email protected]", password="password")
  62. r = self.client.post(self.url, form)
  63. self.assertRedirects(r, self.channels_url)
  64. c = Channel.objects.get()
  65. self.assertEqual(c.phone_number, "+1234567890")
  66. def test_it_strips_hyphens(self):
  67. form = {"phone": "+123-4567890", "down": True}
  68. self.client.login(username="[email protected]", password="password")
  69. r = self.client.post(self.url, form)
  70. self.assertRedirects(r, self.channels_url)
  71. c = Channel.objects.get()
  72. self.assertEqual(c.phone_number, "+1234567890")
  73. def test_it_strips_spaces(self):
  74. form = {"phone": "+123 45 678 90", "down": True}
  75. self.client.login(username="[email protected]", password="password")
  76. r = self.client.post(self.url, form)
  77. self.assertRedirects(r, self.channels_url)
  78. c = Channel.objects.get()
  79. self.assertEqual(c.phone_number, "+1234567890")
  80. def test_it_handles_down_false_up_true(self):
  81. form = {"phone": "+1234567890", "up": True}
  82. self.client.login(username="[email protected]", password="password")
  83. self.client.post(self.url, form)
  84. c = Channel.objects.get()
  85. self.assertFalse(c.sms_notify_down)
  86. self.assertTrue(c.sms_notify_up)
  87. def test_it_rejects_unchecked_up_and_down(self):
  88. form = {"phone": "+1234567890"}
  89. self.client.login(username="[email protected]", password="password")
  90. r = self.client.post(self.url, form)
  91. self.assertContains(r, "Please select at least one.")