telegram_agent_spec.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. }
  8. @checker = Agents::TelegramAgent.new name: 'Telegram Tester', options: default_options
  9. @checker.user = users(:bob)
  10. @checker.save!
  11. @sent_messages = []
  12. stub_methods
  13. end
  14. def stub_methods
  15. stub.any_instance_of(Agents::TelegramAgent).send_telegram_message do |method, params|
  16. @sent_messages << { method => params }
  17. end
  18. stub.any_instance_of(Agents::TelegramAgent).load_file do |_url|
  19. :stubbed_file
  20. end
  21. end
  22. def event_with_payload(payload)
  23. event = Event.new
  24. event.agent = agents(:bob_weather_agent)
  25. event.payload = payload
  26. event.save!
  27. event
  28. end
  29. describe 'validation' do
  30. before do
  31. expect(@checker).to be_valid
  32. end
  33. it 'should validate presence of of auth_token' do
  34. @checker.options[:auth_token] = ''
  35. expect(@checker).not_to be_valid
  36. end
  37. it 'should validate presence of of chat_id' do
  38. @checker.options[:chat_id] = ''
  39. expect(@checker).not_to be_valid
  40. end
  41. end
  42. describe '#receive' do
  43. it 'processes multiple events properly' do
  44. event_0 = event_with_payload text: 'Looks like its going to rain'
  45. event_1 = event_with_payload text: 'Another text message'
  46. @checker.receive [event_0, event_1]
  47. expect(@sent_messages).to eq([
  48. { sendMessage: { text: 'Looks like its going to rain' } },
  49. { sendMessage: { text: 'Another text message' } }
  50. ])
  51. end
  52. it 'accepts photo key and uses :send_photo to send the file' do
  53. event = event_with_payload photo: 'https://example.com/image.png'
  54. @checker.receive [event]
  55. expect(@sent_messages).to eq([{ sendPhoto: { photo: :stubbed_file } }])
  56. end
  57. it 'accepts audio key and uses :send_audio to send the file' do
  58. event = event_with_payload audio: 'https://example.com/sound.mp3'
  59. @checker.receive [event]
  60. expect(@sent_messages).to eq([{ sendAudio: { audio: :stubbed_file } }])
  61. end
  62. it 'accepts document key and uses :send_document to send the file' do
  63. event = event_with_payload document: 'https://example.com/document.pdf'
  64. @checker.receive [event]
  65. expect(@sent_messages).to eq([{ sendDocument: { document: :stubbed_file } }])
  66. end
  67. it 'accepts video key and uses :send_video to send the file' do
  68. event = event_with_payload video: 'https://example.com/video.avi'
  69. @checker.receive [event]
  70. expect(@sent_messages).to eq([{ sendVideo: { video: :stubbed_file } }])
  71. end
  72. end
  73. describe '#working?' do
  74. it 'is not working without having received an event' do
  75. expect(@checker).not_to be_working
  76. end
  77. it 'is working after receiving an event without error' do
  78. @checker.last_receive_at = Time.now
  79. expect(@checker).to be_working
  80. end
  81. end
  82. end