translation_agent_spec.rb 2.9KB

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