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.

103 lines
3.6 KiB

  1. from unittest.mock import patch
  2. from django.core import signing
  3. from django.test.utils import override_settings
  4. from hc.api.models import Channel
  5. from hc.test import BaseTestCase
  6. @override_settings(TELEGRAM_TOKEN="fake-token")
  7. class AddTelegramTestCase(BaseTestCase):
  8. url = "/integrations/add_telegram/"
  9. def test_instructions_work(self):
  10. self.client.login(username="[email protected]", password="password")
  11. r = self.client.get(self.url)
  12. self.assertContains(r, "start@ExampleBot")
  13. @override_settings(TELEGRAM_TOKEN=None)
  14. def test_it_requires_token(self):
  15. payload = signing.dumps((123, "group", "My Group"))
  16. self.client.login(username="[email protected]", password="password")
  17. r = self.client.get(self.url + "?" + payload)
  18. self.assertEqual(r.status_code, 404)
  19. def test_it_shows_confirmation(self):
  20. payload = signing.dumps((123, "group", "My Group"))
  21. self.client.login(username="[email protected]", password="password")
  22. r = self.client.get(self.url + "?" + payload)
  23. self.assertContains(r, "My Group")
  24. def test_it_works(self):
  25. payload = signing.dumps((123, "group", "My Group"))
  26. self.client.login(username="[email protected]", password="password")
  27. form = {"project": str(self.project.code)}
  28. r = self.client.post(self.url + "?" + payload, form)
  29. self.assertRedirects(r, self.channels_url)
  30. c = Channel.objects.get()
  31. self.assertEqual(c.kind, "telegram")
  32. self.assertEqual(c.telegram_id, 123)
  33. self.assertEqual(c.telegram_type, "group")
  34. self.assertEqual(c.telegram_name, "My Group")
  35. self.assertEqual(c.project, self.project)
  36. def test_it_handles_bad_signature(self):
  37. self.client.login(username="[email protected]", password="password")
  38. r = self.client.get(self.url + "?bad-signature")
  39. self.assertContains(r, "Incorrect Link")
  40. self.assertFalse(Channel.objects.exists())
  41. @patch("hc.api.transports.requests.request")
  42. def test_it_sends_invite(self, mock_get):
  43. data = {
  44. "message": {
  45. "chat": {"id": 123, "title": "My Group", "type": "group"},
  46. "text": "/start",
  47. }
  48. }
  49. r = self.client.post(
  50. "/integrations/telegram/bot/", data, content_type="application/json"
  51. )
  52. self.assertEqual(r.status_code, 200)
  53. self.assertTrue(mock_get.called)
  54. @patch("hc.api.transports.requests.request")
  55. def test_bot_handles_bad_message(self, mock_get):
  56. samples = ["", "{}"]
  57. # text is missing
  58. samples.append({"message": {"chat": {"id": 123, "type": "group"}}})
  59. # bad chat type
  60. samples.append(
  61. {"message": {"chat": {"id": 123, "type": "invalid"}, "text": "/start"}}
  62. )
  63. for sample in samples:
  64. r = self.client.post(
  65. "/integrations/telegram/bot/", sample, content_type="application/json"
  66. )
  67. if sample == "":
  68. # Bad JSON payload
  69. self.assertEqual(r.status_code, 400)
  70. else:
  71. # JSON decodes but message structure not recognized
  72. self.assertEqual(r.status_code, 200)
  73. def test_it_requires_rw_access(self):
  74. self.bobs_membership.rw = False
  75. self.bobs_membership.save()
  76. payload = signing.dumps((123, "group", "My Group"))
  77. self.client.login(username="[email protected]", password="password")
  78. form = {"project": str(self.project.code)}
  79. r = self.client.post(self.url + "?" + payload, form)
  80. self.assertEqual(r.status_code, 403)