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.

84 lines
2.7 KiB

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