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.6 KiB

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