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.

81 lines
2.9 KiB

  1. from unittest.mock import patch
  2. from django.test.utils import override_settings
  3. from hc.api.models import Channel
  4. from hc.test import BaseTestCase
  5. @override_settings(LINENOTIFY_CLIENT_ID="t1", LINENOTIFY_CLIENT_SECRET="s1")
  6. class AddLineNotifyCompleteTestCase(BaseTestCase):
  7. url = "/integrations/add_linenotify/"
  8. @patch("hc.front.views.requests")
  9. def test_it_handles_oauth_response(self, mock_requests):
  10. session = self.client.session
  11. session["add_linenotify"] = ("foo", str(self.project.code))
  12. session.save()
  13. mock_requests.post.return_value.json.return_value = {
  14. "status": 200,
  15. "access_token": "test-token",
  16. }
  17. mock_requests.get.return_value.json.return_value = {"target": "Alice"}
  18. url = self.url + "?code=12345678&state=foo"
  19. self.client.login(username="[email protected]", password="password")
  20. r = self.client.get(url, follow=True)
  21. self.assertRedirects(r, self.channels_url)
  22. self.assertContains(r, "The LINE Notify integration has been added!")
  23. ch = Channel.objects.get()
  24. self.assertEqual(ch.value, "test-token")
  25. self.assertEqual(ch.name, "Alice")
  26. self.assertEqual(ch.project, self.project)
  27. # Session should now be clean
  28. self.assertFalse("add_linenotify" in self.client.session)
  29. def test_it_avoids_csrf(self):
  30. session = self.client.session
  31. session["add_linenotify"] = ("foo", str(self.project.code))
  32. session.save()
  33. url = self.url + "?code=12345678&state=bar"
  34. self.client.login(username="[email protected]", password="password")
  35. r = self.client.get(url)
  36. self.assertEqual(r.status_code, 403)
  37. def test_it_handles_denial(self):
  38. session = self.client.session
  39. session["add_linenotify"] = ("foo", str(self.project.code))
  40. session.save()
  41. self.client.login(username="[email protected]", password="password")
  42. r = self.client.get(self.url + "?error=access_denied&state=foo", follow=True)
  43. self.assertRedirects(r, self.channels_url)
  44. self.assertContains(r, "LINE Notify setup was cancelled")
  45. self.assertEqual(Channel.objects.count(), 0)
  46. # Session should now be clean
  47. self.assertFalse("add_linenotify" in self.client.session)
  48. @override_settings(LINENOTIFY_CLIENT_ID=None)
  49. def test_it_requires_client_id(self):
  50. url = self.url + "?code=12345678&state=bar"
  51. self.client.login(username="[email protected]", password="password")
  52. r = self.client.get(url)
  53. self.assertEqual(r.status_code, 404)
  54. def test_it_requires_rw_access(self):
  55. self.bobs_membership.rw = False
  56. self.bobs_membership.save()
  57. url = self.url + "?code=12345678&state=bar"
  58. self.client.login(username="[email protected]", password="password")
  59. r = self.client.get(url)
  60. self.assertEqual(r.status_code, 403)