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.

65 lines
2.0 KiB

  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("/channels/", 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("/channels/", 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("/channels/", data=payload)
  48. # mc belongs to mallorym but self.check does not--
  49. assert r.status_code == 403