post_agent_spec.rb 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. require 'spec_helper'
  2. describe Agents::PostAgent do
  3. before do
  4. @valid_params = {
  5. :name => "somename",
  6. :options => {
  7. 'post_url' => "http://www.example.com",
  8. 'expected_receive_period_in_days' => 1,
  9. 'payload' => {
  10. 'default' => 'value'
  11. }
  12. }
  13. }
  14. @checker = Agents::PostAgent.new(@valid_params)
  15. @checker.user = users(:jane)
  16. @checker.save!
  17. @event = Event.new
  18. @event.agent = agents(:jane_weather_agent)
  19. @event.payload = {
  20. 'somekey' => 'somevalue',
  21. 'someotherkey' => {
  22. 'somekey' => 'value'
  23. }
  24. }
  25. @sent_posts = []
  26. @sent_gets = []
  27. stub.any_instance_of(Agents::PostAgent).post_data { |data| @sent_posts << data }
  28. stub.any_instance_of(Agents::PostAgent).get_data { |data| @sent_gets << data }
  29. end
  30. describe "#receive" do
  31. it "can handle multiple events and merge the payloads with options['payload']" do
  32. event1 = Event.new
  33. event1.agent = agents(:bob_weather_agent)
  34. event1.payload = {
  35. 'xyz' => 'value1',
  36. 'message' => 'value2',
  37. 'default' => 'value2'
  38. }
  39. lambda {
  40. lambda {
  41. @checker.receive([@event, event1])
  42. }.should change { @sent_posts.length }.by(2)
  43. }.should_not change { @sent_gets.length }
  44. @sent_posts[0].should == @event.payload.merge('default' => 'value')
  45. @sent_posts[1].should == event1.payload
  46. end
  47. it "can make GET requests" do
  48. @checker.options['method'] = 'get'
  49. lambda {
  50. lambda {
  51. @checker.receive([@event])
  52. }.should change { @sent_gets.length }.by(1)
  53. }.should_not change { @sent_posts.length }
  54. @sent_gets[0].should == @event.payload.merge('default' => 'value')
  55. end
  56. end
  57. describe "#check" do
  58. it "sends options['payload'] as a POST request" do
  59. lambda {
  60. @checker.check
  61. }.should change { @sent_posts.length }.by(1)
  62. @sent_posts[0].should == @checker.options['payload']
  63. end
  64. it "sends options['payload'] as a GET request" do
  65. @checker.options['method'] = 'get'
  66. lambda {
  67. lambda {
  68. @checker.check
  69. }.should change { @sent_gets.length }.by(1)
  70. }.should_not change { @sent_posts.length }
  71. @sent_gets[0].should == @checker.options['payload']
  72. end
  73. end
  74. describe "#working?" do
  75. it "checks if events have been received within expected receive period" do
  76. @checker.should_not be_working
  77. Agents::PostAgent.async_receive @checker.id, [@event.id]
  78. @checker.reload.should be_working
  79. two_days_from_now = 2.days.from_now
  80. stub(Time).now { two_days_from_now }
  81. @checker.reload.should_not be_working
  82. end
  83. end
  84. describe "validation" do
  85. before do
  86. @checker.should be_valid
  87. end
  88. it "should validate presence of post_url" do
  89. @checker.options['post_url'] = ""
  90. @checker.should_not be_valid
  91. end
  92. it "should validate presence of expected_receive_period_in_days" do
  93. @checker.options['expected_receive_period_in_days'] = ""
  94. @checker.should_not be_valid
  95. end
  96. it "should validate method as post or get, defaulting to post" do
  97. @checker.options['method'] = ""
  98. @checker.method.should == "post"
  99. @checker.should be_valid
  100. @checker.options['method'] = "POST"
  101. @checker.method.should == "post"
  102. @checker.should be_valid
  103. @checker.options['method'] = "get"
  104. @checker.method.should == "get"
  105. @checker.should be_valid
  106. @checker.options['method'] = "wut"
  107. @checker.method.should == "wut"
  108. @checker.should_not be_valid
  109. end
  110. it "should validate payload as a hash, if present" do
  111. @checker.options['payload'] = ""
  112. @checker.should be_valid
  113. @checker.options['payload'] = "hello"
  114. @checker.should_not be_valid
  115. @checker.options['payload'] = ["foo", "bar"]
  116. @checker.should_not be_valid
  117. @checker.options['payload'] = { 'this' => 'that' }
  118. @checker.should be_valid
  119. end
  120. it "requires headers to be a hash, if present" do
  121. @checker.options['headers'] = [1,2,3]
  122. @checker.should_not be_valid
  123. @checker.options['headers'] = "hello world"
  124. @checker.should_not be_valid
  125. @checker.options['headers'] = ""
  126. @checker.should be_valid
  127. @checker.options['headers'] = {}
  128. @checker.should be_valid
  129. @checker.options['headers'] = { "Authorization" => "foo bar" }
  130. @checker.should be_valid
  131. end
  132. end
  133. describe "#generate_uri" do
  134. it "merges params with any in the post_url" do
  135. @checker.options['post_url'] = "http://example.com/a/path?existing_param=existing_value"
  136. uri = @checker.generate_uri("some_param" => "some_value", "another_param" => "another_value")
  137. uri.request_uri.should == "/a/path?existing_param=existing_value&some_param=some_value&another_param=another_value"
  138. end
  139. it "works fine with urls that do not have a query" do
  140. @checker.options['post_url'] = "http://example.com/a/path"
  141. uri = @checker.generate_uri("some_param" => "some_value", "another_param" => "another_value")
  142. uri.request_uri.should == "/a/path?some_param=some_value&another_param=another_value"
  143. end
  144. it "just returns the post_uri when no params are given" do
  145. @checker.options['post_url'] = "http://example.com/a/path?existing_param=existing_value"
  146. uri = @checker.generate_uri
  147. uri.request_uri.should == "/a/path?existing_param=existing_value"
  148. end
  149. end
  150. end