telegram_agent_spec.rb 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. require 'rails_helper'
  2. describe Agents::TelegramAgent do
  3. before do
  4. default_options = {
  5. auth_token: 'xxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  6. chat_id: 'xxxxxxxx',
  7. caption: '{{ caption }}',
  8. disable_web_page_preview: '{{ disable_web_page_preview }}',
  9. disable_notification: '{{ silent }}',
  10. long_message: '{{ long }}',
  11. parse_mode: 'html'
  12. }
  13. @checker = Agents::TelegramAgent.new name: 'Telegram Tester', options: default_options
  14. @checker.user = users(:bob)
  15. @checker.save!
  16. end
  17. def event_with_payload(payload)
  18. event = Event.new
  19. event.agent = agents(:bob_weather_agent)
  20. event.payload = payload
  21. event.save!
  22. event
  23. end
  24. def stub_methods
  25. allow_any_instance_of(Agents::TelegramAgent).to receive(:send_message) do |agent, method, params|
  26. @sent_messages << { method => params }
  27. end
  28. end
  29. describe 'validation' do
  30. before do
  31. expect(@checker).to be_valid
  32. end
  33. it 'should validate presence of auth_token' do
  34. @checker.options[:auth_token] = ''
  35. expect(@checker).not_to be_valid
  36. end
  37. it 'should validate presence of chat_id' do
  38. @checker.options[:chat_id] = ''
  39. expect(@checker).not_to be_valid
  40. end
  41. it 'should validate value of caption' do
  42. @checker.options[:caption] = 'a' * 1025
  43. expect(@checker).not_to be_valid
  44. end
  45. it 'should validate value of disable_web_page_preview' do
  46. @checker.options[:disable_web_page_preview] = 'invalid'
  47. expect(@checker).not_to be_valid
  48. end
  49. it 'should validate value of disable_notification' do
  50. @checker.options[:disable_notification] = 'invalid'
  51. expect(@checker).not_to be_valid
  52. end
  53. it 'should validate value of long_message' do
  54. @checker.options[:long_message] = 'invalid'
  55. expect(@checker).not_to be_valid
  56. end
  57. it 'should validate value of parse_mode' do
  58. @checker.options[:parse_mode] = 'invalid'
  59. expect(@checker).not_to be_valid
  60. end
  61. end
  62. describe '#receive' do
  63. before do
  64. stub_methods
  65. @sent_messages = []
  66. end
  67. it 'processes multiple events properly' do
  68. event_0 = event_with_payload silent: 'true', text: 'Looks like it is going to rain'
  69. event_1 = event_with_payload disable_web_page_preview: 'true', long: 'split', text: "#{'a' * 4095} #{'b' * 6}"
  70. event_2 = event_with_payload disable_web_page_preview: 'true', long: 'split', text: "#{'a' * 4096}#{'b' * 6}"
  71. event_3 = event_with_payload long: 'split', text: "#{'a' * 2142} #{'b' * 2142}"
  72. @checker.receive [event_0, event_1, event_2, event_3]
  73. expect(@sent_messages).to eq([
  74. { text: { chat_id: 'xxxxxxxx', disable_notification: 'true', parse_mode: 'html', text: 'Looks like it is going to rain' } },
  75. { text: { chat_id: 'xxxxxxxx', disable_web_page_preview: 'true', parse_mode: 'html', text: 'a' * 4095 } },
  76. { text: { chat_id: 'xxxxxxxx', disable_web_page_preview: 'true', parse_mode: 'html', text: 'b' * 6 } },
  77. { text: { chat_id: 'xxxxxxxx', disable_web_page_preview: 'true', parse_mode: 'html', text: 'a' * 4096 } },
  78. { text: { chat_id: 'xxxxxxxx', disable_web_page_preview: 'true', parse_mode: 'html', text: 'b' * 6 } },
  79. { text: { chat_id: 'xxxxxxxx', parse_mode: 'html', text: 'a' * 2142 } },
  80. { text: { chat_id: 'xxxxxxxx', parse_mode: 'html', text: 'b' * 2142 } }
  81. ])
  82. end
  83. it 'accepts audio key and uses :send_audio to send the file with truncated caption' do
  84. event = event_with_payload audio: 'https://example.com/sound.mp3', caption: 'a' * 1025
  85. @checker.receive [event]
  86. expect(@sent_messages).to eq([{ audio: { audio: 'https://example.com/sound.mp3', caption: 'a'* 1024, chat_id: 'xxxxxxxx' } }])
  87. end
  88. it 'accepts document key and uses :send_document to send the file and the full caption' do
  89. event = event_with_payload caption: "#{'a' * 1023} #{'b' * 6}", document: 'https://example.com/document.pdf', long: 'split'
  90. @checker.receive [event]
  91. expect(@sent_messages).to eq([
  92. { document: { caption: 'a' * 1023, chat_id: 'xxxxxxxx', document: 'https://example.com/document.pdf' } },
  93. { text: { chat_id: 'xxxxxxxx', parse_mode: 'html', text: 'b' * 6 } }
  94. ])
  95. end
  96. it 'accepts photo key and uses :send_photo to send the file' do
  97. event = event_with_payload photo: 'https://example.com/image.png'
  98. @checker.receive [event]
  99. expect(@sent_messages).to eq([{ photo: { chat_id: 'xxxxxxxx', photo: 'https://example.com/image.png' } }])
  100. end
  101. it 'accepts photo key with no caption when long:split is set' do
  102. event = event_with_payload photo: 'https://example.com/image.png', long: 'split', caption: nil
  103. @checker.receive [event]
  104. end
  105. it 'accepts video key and uses :send_video to send the file' do
  106. event = event_with_payload video: 'https://example.com/video.avi'
  107. @checker.receive [event]
  108. expect(@sent_messages).to eq([{ video: { chat_id: 'xxxxxxxx', video: 'https://example.com/video.avi' } }])
  109. end
  110. it 'accepts group key and uses :send_media_group to send the file' do
  111. event = event_with_payload group: [{ type: 'photo', media: 'https://example.com/photo1.jpg' }, { type: 'photo', media: 'https://example.com/photo2.jpg' }]
  112. @checker.receive [event]
  113. expect(@sent_messages).to eq([{ group: { chat_id: 'xxxxxxxx',
  114. media: [{ 'type' => 'photo', 'media' => 'https://example.com/photo1.jpg' },
  115. { 'type' => 'photo', 'media' => 'https://example.com/photo2.jpg' }]
  116. } }])
  117. end
  118. it 'creates a log entry when no key of the received event was useable' do
  119. event = event_with_payload test: '1234'
  120. expect {
  121. @checker.receive [event]
  122. }.to change(AgentLog, :count).by(1)
  123. end
  124. end
  125. it 'creates an error log if the request fails' do
  126. event = event_with_payload text: 'hello'
  127. stub_request(:post, "https://api.telegram.org/botxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/sendMessage").
  128. with(headers: {'Content-type'=>'application/json'}).
  129. to_return(status: 200, body: '{"ok": false}', headers: {'Content-Type' => 'application/json'})
  130. expect {
  131. @checker.receive [event]
  132. }.to change(AgentLog, :count).by(1)
  133. end
  134. describe '#working?' do
  135. it 'is not working without having received an event' do
  136. expect(@checker).not_to be_working
  137. end
  138. it 'is working after receiving an event without error' do
  139. @checker.last_receive_at = Time.now
  140. expect(@checker).to be_working
  141. end
  142. end
  143. describe '#complete_chat_id' do
  144. it 'returns a list of all recents chats, groups and channels' do
  145. stub_request(:post, "https://api.telegram.org/botxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/getUpdates").
  146. to_return(status: 200, body: '{"ok":true,"result":[{"update_id":252965475,"message":{"message_id":15,"from":{"id":97201077,"is_bot":false,"first_name":"Dominik","last_name":"Sander","language_code":"en-US"},"chat":{"id":97201077,"first_name":"Dominik","last_name":"Sander","type":"private"},"date":1506774710,"text":"test"}},{"update_id":252965476,"channel_post":{"message_id":4,"chat":{"id":-1001144599139,"title":"Much channel","type":"channel"},"date":1506782283,"text":"channel"}},{"update_id":252965477,"message":{"message_id":18,"from":{"id":97201077,"is_bot":false,"first_name":"Dominik","last_name":"Sander","language_code":"en-US"},"chat":{"id":-217850512,"title":"Just a test","type":"group","all_members_are_administrators":true},"date":1506782504,"left_chat_participant":{"id":136508315,"is_bot":true,"first_name":"Huginn","username":"HuginnNotificationBot"},"left_chat_member":{"id":136508315,"is_bot":true,"first_name":"Huginn","username":"HuginnNotificationBot"}}}]}', headers: {'Content-Type' => 'application/json'})
  147. expect(@checker.complete_chat_id).to eq([{:id=>97201077, :text=>"Dominik Sander"},
  148. {:id=>-1001144599139, :text=>"Much channel"},
  149. {:id=>-217850512, :text=>"Just a test"}])
  150. end
  151. end
  152. describe '#validate_auth_token' do
  153. it 'returns true if the token is valid' do
  154. stub_request(:post, "https://api.telegram.org/botxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/getMe").
  155. to_return(status: 200, body: '{"ok": true}', headers: {'Content-Type' => 'application/json'})
  156. expect(@checker.validate_auth_token).to be_truthy
  157. end
  158. it 'returns false if the token is invalid' do
  159. stub_request(:post, "https://api.telegram.org/botxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/getMe").
  160. to_return(status: 200, body: "{}")
  161. expect(@checker.validate_auth_token).to be_falsy
  162. end
  163. end
  164. end