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.

82 lines
2.8 KiB

9 years ago
9 years ago
9 years ago
  1. from django.contrib.auth.models import User
  2. from hc.api.models import Channel, Check
  3. from hc.test import BaseTestCase
  4. class UpdateChannelTestCase(BaseTestCase):
  5. def setUp(self):
  6. super(UpdateChannelTestCase, self).setUp()
  7. self.check = Check(user=self.alice)
  8. self.check.save()
  9. self.channel = Channel(user=self.alice, kind="email")
  10. self.channel.email = "[email protected]"
  11. self.channel.save()
  12. def test_it_works(self):
  13. payload = {
  14. "channel": self.channel.code,
  15. "check-%s" % self.check.code: True
  16. }
  17. self.client.login(username="[email protected]", password="password")
  18. r = self.client.post("/integrations/", data=payload)
  19. self.assertRedirects(r, "/integrations/")
  20. channel = Channel.objects.get(code=self.channel.code)
  21. checks = channel.checks.all()
  22. assert len(checks) == 1
  23. assert checks[0].code == self.check.code
  24. def test_it_checks_channel_user(self):
  25. mallory = User(username="mallory", email="[email protected]")
  26. mallory.set_password("password")
  27. mallory.save()
  28. payload = {"channel": self.channel.code}
  29. self.client.login(username="[email protected]", password="password")
  30. r = self.client.post("/integrations/", data=payload)
  31. # self.channel does not belong to mallory, this should fail--
  32. assert r.status_code == 403
  33. def test_it_checks_check_user(self):
  34. mallory = User(username="mallory", email="[email protected]")
  35. mallory.set_password("password")
  36. mallory.save()
  37. mc = Channel(user=mallory, kind="email")
  38. mc.email = "[email protected]"
  39. mc.save()
  40. payload = {
  41. "channel": mc.code,
  42. "check-%s" % self.check.code: True
  43. }
  44. self.client.login(username="[email protected]", password="password")
  45. r = self.client.post("/integrations/", data=payload)
  46. # mc belongs to mallorym but self.check does not--
  47. assert r.status_code == 403
  48. def test_it_handles_missing_channel(self):
  49. # Correct UUID but there is no channel for it:
  50. payload = {"channel": "6837d6ec-fc08-4da5-a67f-08a9ed1ccf62"}
  51. self.client.login(username="[email protected]", password="password")
  52. r = self.client.post("/integrations/", data=payload)
  53. assert r.status_code == 400
  54. def test_it_handles_missing_check(self):
  55. # check- key has a correct UUID but there's no check object for it
  56. payload = {
  57. "channel": self.channel.code,
  58. "check-6837d6ec-fc08-4da5-a67f-08a9ed1ccf62": True
  59. }
  60. self.client.login(username="[email protected]", password="password")
  61. r = self.client.post("/integrations/", data=payload)
  62. assert r.status_code == 400