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.

67 lines
2.4 KiB

  1. from datetime import timedelta
  2. from hc.api.models import Channel, Check, Notification
  3. from hc.test import BaseTestCase
  4. class BounceTestCase(BaseTestCase):
  5. def setUp(self):
  6. super(BounceTestCase, self).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. def test_it_works(self):
  16. url = "/api/v1/notifications/%s/bounce" % self.n.code
  17. r = self.client.post(url, "foo", content_type="text/plain")
  18. self.assertEqual(r.status_code, 200)
  19. self.n.refresh_from_db()
  20. self.assertEqual(self.n.error, "foo")
  21. self.channel.refresh_from_db()
  22. self.assertFalse(self.channel.email_verified)
  23. self.assertEqual(self.channel.last_error, "foo")
  24. def test_it_checks_ttl(self):
  25. self.n.created = self.n.created - timedelta(minutes=60)
  26. self.n.save()
  27. url = "/api/v1/notifications/%s/bounce" % self.n.code
  28. r = self.client.post(url, "foo", content_type="text/plain")
  29. self.assertEqual(r.status_code, 403)
  30. def test_it_handles_long_payload(self):
  31. url = "/api/v1/notifications/%s/bounce" % self.n.code
  32. payload = "A" * 500
  33. r = self.client.post(url, payload, content_type="text/plain")
  34. self.assertEqual(r.status_code, 200)
  35. def test_it_handles_missing_notification(self):
  36. fake_code = "07c2f548-9850-4b27-af5d-6c9dc157ec02"
  37. url = "/api/v1/notifications/%s/bounce" % fake_code
  38. r = self.client.post(url, "", content_type="text/plain")
  39. self.assertEqual(r.status_code, 404)
  40. def test_it_requires_post(self):
  41. url = "/api/v1/notifications/%s/bounce" % self.n.code
  42. r = self.client.get(url)
  43. self.assertEqual(r.status_code, 405)
  44. def test_does_not_unsubscribe_transient_bounces(self):
  45. url = "/api/v1/notifications/%s/bounce?type=Transient" % self.n.code
  46. self.client.post(url, "foo", content_type="text/plain")
  47. self.n.refresh_from_db()
  48. self.assertEqual(self.n.error, "foo")
  49. self.channel.refresh_from_db()
  50. self.assertTrue(self.channel.email_verified)