translation_agent_spec.rb 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. require 'spec_helper'
  2. describe Agents::TranslationAgent do
  3. before do
  4. @valid_params = {
  5. :name => "somename",
  6. :options => {
  7. :client_id => "xxxxxx",
  8. :client_secret => "xxxxxx" ,
  9. :to => "fi",
  10. :expected_receive_period_in_days => 1,
  11. :content => {
  12. :text => "{{message}}",
  13. :content => "{{xyz}}"
  14. }
  15. }
  16. }
  17. @checker = Agents::TranslationAgent.new(@valid_params)
  18. @checker.user = users(:jane)
  19. @checker.save!
  20. @event = Event.new
  21. @event.agent = agents(:jane_weather_agent)
  22. @event.payload = {
  23. :message => "somevalue",
  24. :xyz => "someothervalue"
  25. }
  26. stub_request(:any, /microsoft/).to_return(:body => "response", :status => 200)
  27. stub_request(:any, /windows/).to_return(:body => JSON.dump({
  28. :access_token => 'xxx'}), :status => 200)
  29. end
  30. describe "#receive" do
  31. it "checks if it can handle multiple events" do
  32. event1 = Event.new
  33. event1.agent = agents(:bob_weather_agent)
  34. event1.payload = {
  35. :xyz => "value1",
  36. :message => "value2"
  37. }
  38. expect {
  39. @checker.receive([@event,event1])
  40. }.to change { Event.count }.by(2)
  41. end
  42. end
  43. describe "#working?" do
  44. it "checks if events have been received within expected receive period" do
  45. expect(@checker).not_to be_working
  46. Agents::TranslationAgent.async_receive @checker.id, [@event.id]
  47. expect(@checker.reload).to be_working
  48. two_days_from_now = 2.days.from_now
  49. stub(Time).now { two_days_from_now }
  50. expect(@checker.reload).not_to be_working
  51. end
  52. end
  53. describe "validation" do
  54. before do
  55. expect(@checker).to be_valid
  56. end
  57. it "should validate presence of content key" do
  58. @checker.options[:content] = nil
  59. expect(@checker).not_to be_valid
  60. end
  61. it "should validate presence of expected_receive_period_in_days key" do
  62. @checker.options[:expected_receive_period_in_days] = nil
  63. expect(@checker).not_to be_valid
  64. end
  65. it "should validate presence of client_id key" do
  66. @checker.options[:client_id] = ""
  67. expect(@checker).not_to be_valid
  68. end
  69. it "should validate presence of client_secret key" do
  70. @checker.options[:client_secret] = ""
  71. expect(@checker).not_to be_valid
  72. end
  73. it "should validate presence of 'to' key" do
  74. @checker.options[:to] = ""
  75. expect(@checker).not_to be_valid
  76. end
  77. end
  78. end