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.

76 lines
2.4 KiB

  1. import json
  2. from django.core import signing
  3. from hc.api.models import Channel
  4. from hc.test import BaseTestCase
  5. from mock import patch
  6. class AddTelegramTestCase(BaseTestCase):
  7. url = "/integrations/add_telegram/"
  8. def test_instructions_work(self):
  9. self.client.login(username="[email protected]", password="password")
  10. r = self.client.get(self.url)
  11. self.assertContains(r, "start@ExampleBot")
  12. def test_it_shows_confirmation(self):
  13. payload = signing.dumps((123, "group", "My Group"))
  14. self.client.login(username="[email protected]", password="password")
  15. r = self.client.get(self.url + "?" + payload)
  16. self.assertContains(r, "My Group")
  17. def test_it_works(self):
  18. payload = signing.dumps((123, "group", "My Group"))
  19. self.client.login(username="[email protected]", password="password")
  20. r = self.client.post(self.url + "?" + payload, {})
  21. self.assertRedirects(r, "/integrations/")
  22. c = Channel.objects.get()
  23. self.assertEqual(c.kind, "telegram")
  24. self.assertEqual(c.telegram_id, 123)
  25. self.assertEqual(c.telegram_type, "group")
  26. self.assertEqual(c.telegram_name, "My Group")
  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/", json.dumps(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(json.dumps({
  48. "message": {"chat": {"id": 123, "type": "group"}}
  49. }))
  50. # bad type
  51. samples.append(json.dumps({
  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. self.assertEqual(r.status_code, 400)