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.

106 lines
3.9 KiB

  1. from datetime import timedelta
  2. from hc.api.models import Channel, Check, Notification
  3. from hc.test import BaseTestCase
  4. class NotificationStatusTestCase(BaseTestCase):
  5. def setUp(self):
  6. super().setUp()
  7. self.check = Check(project=self.project, status="up")
  8. self.check.save()
  9. self.channel = Channel(project=self.project, kind="email")
  10. self.channel.value = "[email protected]"
  11. self.channel.email_verified = True
  12. self.channel.save()
  13. self.n = Notification(owner=self.check, channel=self.channel)
  14. self.n.save()
  15. self.url = "/api/v1/notifications/%s/status" % self.n.code
  16. def test_it_handles_twilio_failed_status(self):
  17. r = self.csrf_client.post(self.url, {"MessageStatus": "failed"})
  18. self.assertEqual(r.status_code, 200)
  19. self.n.refresh_from_db()
  20. self.assertEqual(self.n.error, "Delivery failed (status=failed).")
  21. self.channel.refresh_from_db()
  22. self.assertEqual(self.channel.last_error, "Delivery failed (status=failed).")
  23. self.assertTrue(self.channel.email_verified)
  24. def test_it_handles_twilio_undelivered_status(self):
  25. r = self.csrf_client.post(self.url, {"MessageStatus": "undelivered"})
  26. self.assertEqual(r.status_code, 200)
  27. self.n.refresh_from_db()
  28. self.assertEqual(self.n.error, "Delivery failed (status=undelivered).")
  29. self.channel.refresh_from_db()
  30. self.assertIn("status=undelivered", self.channel.last_error)
  31. def test_it_handles_twilio_delivered_status(self):
  32. r = self.csrf_client.post(self.url, {"MessageStatus": "delivered"})
  33. self.assertEqual(r.status_code, 200)
  34. self.n.refresh_from_db()
  35. self.assertEqual(self.n.error, "")
  36. self.channel.refresh_from_db()
  37. self.assertEqual(self.channel.last_error, "")
  38. def test_it_checks_ttl(self):
  39. self.n.created = self.n.created - timedelta(minutes=61)
  40. self.n.save()
  41. r = self.csrf_client.post(self.url, {"MessageStatus": "failed"})
  42. self.assertEqual(r.status_code, 200)
  43. # The notification should not have the error field set:
  44. self.n.refresh_from_db()
  45. self.assertEqual(self.n.error, "")
  46. def test_it_handles_missing_notification(self):
  47. fake_code = "07c2f548-9850-4b27-af5d-6c9dc157ec02"
  48. url = f"/api/v1/notifications/{fake_code}/status"
  49. r = self.csrf_client.post(url, {"MessageStatus": "failed"})
  50. self.assertEqual(r.status_code, 200)
  51. def test_it_requires_post(self):
  52. r = self.csrf_client.get(self.url)
  53. self.assertEqual(r.status_code, 405)
  54. def test_it_handles_error_key(self):
  55. r = self.csrf_client.post(self.url, {"error": "Something went wrong."})
  56. self.assertEqual(r.status_code, 200)
  57. self.n.refresh_from_db()
  58. self.assertEqual(self.n.error, "Something went wrong.")
  59. self.channel.refresh_from_db()
  60. self.assertEqual(self.channel.last_error, "Something went wrong.")
  61. self.assertTrue(self.channel.email_verified)
  62. def test_it_handles_mark_not_verified_key(self):
  63. payload = {"error": "Received complaint.", "mark_not_verified": "1"}
  64. r = self.csrf_client.post(self.url, payload)
  65. self.assertEqual(r.status_code, 200)
  66. self.channel.refresh_from_db()
  67. self.assertEqual(self.channel.last_error, "Received complaint.")
  68. self.assertFalse(self.channel.email_verified)
  69. def test_it_handles_twilio_call_status_failed(self):
  70. r = self.csrf_client.post(self.url, {"CallStatus": "failed"})
  71. self.assertEqual(r.status_code, 200)
  72. self.n.refresh_from_db()
  73. self.assertEqual(self.n.error, "Delivery failed (status=failed).")
  74. self.channel.refresh_from_db()
  75. self.assertEqual(self.channel.last_error, "Delivery failed (status=failed).")
  76. self.assertTrue(self.channel.email_verified)