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
3.3 KiB

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