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.

113 lines
4.2 KiB

7 years ago
  1. from django.test.utils import override_settings
  2. from hc.api.models import Channel
  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.url = "/projects/%s/add_sms/" % self.project.code
  9. def test_instructions_work(self):
  10. self.client.login(username="[email protected]", password="password")
  11. r = self.client.get(self.url)
  12. self.assertContains(r, "Get a SMS message")
  13. @override_settings(USE_PAYMENTS=True)
  14. def test_it_warns_about_limits(self):
  15. self.profile.sms_limit = 0
  16. self.profile.save()
  17. self.client.login(username="[email protected]", password="password")
  18. r = self.client.get(self.url)
  19. self.assertContains(r, "upgrade to a")
  20. def test_it_creates_channel(self):
  21. form = {"label": "My Phone", "phone": "+1234567890", "down": True}
  22. self.client.login(username="[email protected]", password="password")
  23. r = self.client.post(self.url, form)
  24. self.assertRedirects(r, self.channels_url)
  25. c = Channel.objects.get()
  26. self.assertEqual(c.kind, "sms")
  27. self.assertEqual(c.phone_number, "+1234567890")
  28. self.assertEqual(c.name, "My Phone")
  29. self.assertTrue(c.sms_notify_down)
  30. self.assertFalse(c.sms_notify_up)
  31. self.assertEqual(c.project, self.project)
  32. def test_it_rejects_bad_number(self):
  33. for v in ["not a phone number address", False, 15, "+123456789A"]:
  34. form = {"phone": v}
  35. self.client.login(username="[email protected]", password="password")
  36. r = self.client.post(self.url, form)
  37. self.assertContains(r, "Invalid phone number format.")
  38. def test_it_trims_whitespace(self):
  39. form = {"phone": " +1234567890 "}
  40. self.client.login(username="[email protected]", password="password")
  41. self.client.post(self.url, form)
  42. c = Channel.objects.get()
  43. self.assertEqual(c.phone_number, "+1234567890")
  44. @override_settings(TWILIO_AUTH=None)
  45. def test_it_requires_credentials(self):
  46. self.client.login(username="[email protected]", password="password")
  47. r = self.client.get(self.url)
  48. self.assertEqual(r.status_code, 404)
  49. def test_it_requires_rw_access(self):
  50. self.bobs_membership.role = "r"
  51. self.bobs_membership.save()
  52. self.client.login(username="[email protected]", password="password")
  53. r = self.client.get(self.url)
  54. self.assertEqual(r.status_code, 403)
  55. def test_it_strips_invisible_formatting_characters(self):
  56. form = {"label": "My Phone", "phone": "\u202c+1234567890\u202c"}
  57. self.client.login(username="[email protected]", password="password")
  58. r = self.client.post(self.url, form)
  59. self.assertRedirects(r, self.channels_url)
  60. c = Channel.objects.get()
  61. self.assertEqual(c.phone_number, "+1234567890")
  62. def test_it_strips_hyphens(self):
  63. form = {"label": "My Phone", "phone": "+123-4567890"}
  64. self.client.login(username="[email protected]", password="password")
  65. r = self.client.post(self.url, form)
  66. self.assertRedirects(r, self.channels_url)
  67. c = Channel.objects.get()
  68. self.assertEqual(c.phone_number, "+1234567890")
  69. def test_it_strips_spaces(self):
  70. form = {"label": "My Phone", "phone": "+123 45 678 90"}
  71. self.client.login(username="[email protected]", password="password")
  72. r = self.client.post(self.url, form)
  73. self.assertRedirects(r, self.channels_url)
  74. c = Channel.objects.get()
  75. self.assertEqual(c.phone_number, "+1234567890")
  76. def test_it_obeys_up_down_flags(self):
  77. form = {"label": "My Phone", "phone": "+1234567890"}
  78. self.client.login(username="[email protected]", password="password")
  79. r = self.client.post(self.url, form)
  80. self.assertRedirects(r, self.channels_url)
  81. c = Channel.objects.get()
  82. self.assertEqual(c.kind, "sms")
  83. self.assertEqual(c.phone_number, "+1234567890")
  84. self.assertEqual(c.name, "My Phone")
  85. self.assertFalse(c.sms_notify_down)
  86. self.assertFalse(c.sms_notify_up)