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.

97 lines
3.6 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", "value": "+1234567890"}
  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.assertEqual(c.project, self.project)
  30. def test_it_rejects_bad_number(self):
  31. for v in ["not a phone number address", False, 15, "+123456789A"]:
  32. form = {"value": v}
  33. self.client.login(username="[email protected]", password="password")
  34. r = self.client.post(self.url, form)
  35. self.assertContains(r, "Invalid phone number format.")
  36. def test_it_trims_whitespace(self):
  37. form = {"value": " +1234567890 "}
  38. self.client.login(username="[email protected]", password="password")
  39. self.client.post(self.url, form)
  40. c = Channel.objects.get()
  41. self.assertEqual(c.phone_number, "+1234567890")
  42. @override_settings(TWILIO_AUTH=None)
  43. def test_it_requires_credentials(self):
  44. self.client.login(username="[email protected]", password="password")
  45. r = self.client.get(self.url)
  46. self.assertEqual(r.status_code, 404)
  47. def test_it_requires_rw_access(self):
  48. self.bobs_membership.rw = False
  49. self.bobs_membership.save()
  50. self.client.login(username="[email protected]", password="password")
  51. r = self.client.get(self.url)
  52. self.assertEqual(r.status_code, 403)
  53. def test_it_strips_invisible_formatting_characters(self):
  54. form = {"label": "My Phone", "value": "\u202c+1234567890\u202c"}
  55. self.client.login(username="[email protected]", password="password")
  56. r = self.client.post(self.url, form)
  57. self.assertRedirects(r, self.channels_url)
  58. c = Channel.objects.get()
  59. self.assertEqual(c.phone_number, "+1234567890")
  60. def test_it_strips_hyphens(self):
  61. form = {"label": "My Phone", "value": "+123-4567890"}
  62. self.client.login(username="[email protected]", password="password")
  63. r = self.client.post(self.url, form)
  64. self.assertRedirects(r, self.channels_url)
  65. c = Channel.objects.get()
  66. self.assertEqual(c.phone_number, "+1234567890")
  67. def test_it_strips_spaces(self):
  68. form = {"label": "My Phone", "value": "+123 45 678 90"}
  69. self.client.login(username="[email protected]", password="password")
  70. r = self.client.post(self.url, form)
  71. self.assertRedirects(r, self.channels_url)
  72. c = Channel.objects.get()
  73. self.assertEqual(c.phone_number, "+1234567890")