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.

56 lines
2.1 KiB

6 years ago
6 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 SwitchChannelTestCase(BaseTestCase):
  5. def setUp(self):
  6. super(SwitchChannelTestCase, self).setUp()
  7. self.check = Check.objects.create(project=self.project)
  8. self.channel = Channel(project=self.project, kind="email")
  9. self.channel.value = "[email protected]"
  10. self.channel.save()
  11. self.url = "/checks/%s/channels/%s/enabled" % (
  12. self.check.code,
  13. self.channel.code,
  14. )
  15. def test_it_enables(self):
  16. self.client.login(username="[email protected]", password="password")
  17. self.client.post(self.url, {"state": "on"})
  18. self.assertTrue(self.channel in self.check.channel_set.all())
  19. def test_it_disables(self):
  20. self.check.channel_set.add(self.channel)
  21. self.client.login(username="[email protected]", password="password")
  22. self.client.post(self.url, {"state": "off"})
  23. self.assertFalse(self.channel in self.check.channel_set.all())
  24. def test_it_checks_ownership(self):
  25. self.client.login(username="[email protected]", password="password")
  26. r = self.client.post(self.url, {"state": "on"})
  27. self.assertEqual(r.status_code, 404)
  28. def test_it_checks_channels_ownership(self):
  29. charlies_project = Project.objects.create(owner=self.charlie)
  30. cc = Check.objects.create(project=charlies_project)
  31. # Charlie will try to assign Alice's channel to his check:
  32. self.url = "/checks/%s/channels/%s/enabled" % (cc.code, self.channel.code)
  33. self.client.login(username="[email protected]", password="password")
  34. r = self.client.post(self.url, {"state": "on"})
  35. self.assertEqual(r.status_code, 400)
  36. def test_it_allows_cross_team_access(self):
  37. self.bobs_profile.current_project = None
  38. self.bobs_profile.save()
  39. self.client.login(username="[email protected]", password="password")
  40. r = self.client.post(self.url, {"state": "on"})
  41. self.assertEqual(r.status_code, 200)