Nessuna descrizione http://j1x-huginn.herokuapp.com

webhook_agent_spec.rb 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. require 'rails_helper'
  2. describe Agents::WebhookAgent do
  3. let(:agent) do
  4. _agent = Agents::WebhookAgent.new(:name => 'webhook',
  5. :options => { 'secret' => 'foobar', 'payload_path' => 'some_key' })
  6. _agent.user = users(:bob)
  7. _agent.save!
  8. _agent
  9. end
  10. let(:payload) { {'people' => [{ 'name' => 'bob' }, { 'name' => 'jon' }] } }
  11. describe 'receive_web_request' do
  12. it 'should create event if secret matches' do
  13. out = nil
  14. expect {
  15. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  16. }.to change { Event.count }.by(1)
  17. expect(out).to eq(['Event Created', 201])
  18. expect(Event.last.payload).to eq(payload)
  19. end
  20. it 'should be able to create multiple events when given an array' do
  21. out = nil
  22. agent.options['payload_path'] = 'some_key.people'
  23. expect {
  24. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  25. }.to change { Event.count }.by(2)
  26. expect(out).to eq(['Event Created', 201])
  27. expect(Event.last.payload).to eq({ 'name' => 'jon' })
  28. end
  29. it 'should not create event if secrets dont match' do
  30. out = nil
  31. expect {
  32. out = agent.receive_web_request({ 'secret' => 'bazbat', 'some_key' => payload }, "post", "text/html")
  33. }.to change { Event.count }.by(0)
  34. expect(out).to eq(['Not Authorized', 401])
  35. end
  36. it 'should respond with customized response message if configured with `response` option' do
  37. out = nil
  38. agent.options['response'] = 'That Worked'
  39. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  40. expect(out).to eq(['That Worked', 201])
  41. end
  42. it 'should respond with `Event Created` if response option is nil' do
  43. out = nil
  44. agent.options['response'] = nil
  45. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  46. expect(out).to eq(['Event Created', 201])
  47. end
  48. describe "receiving events" do
  49. context "default settings" do
  50. it "should not accept GET" do
  51. out = nil
  52. expect {
  53. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "get", "text/html")
  54. }.to change { Event.count }.by(0)
  55. expect(out).to eq(['Please use POST requests only', 401])
  56. end
  57. it "should accept POST" do
  58. out = nil
  59. expect {
  60. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  61. }.to change { Event.count }.by(1)
  62. expect(out).to eq(['Event Created', 201])
  63. end
  64. end
  65. context "accepting get and post" do
  66. before { agent.options['verbs'] = 'get,post' }
  67. it "should accept GET" do
  68. out = nil
  69. expect {
  70. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "get", "text/html")
  71. }.to change { Event.count }.by(1)
  72. expect(out).to eq(['Event Created', 201])
  73. end
  74. it "should accept POST" do
  75. out = nil
  76. expect {
  77. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  78. }.to change { Event.count }.by(1)
  79. expect(out).to eq(['Event Created', 201])
  80. end
  81. it "should not accept PUT" do
  82. out = nil
  83. expect {
  84. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "put", "text/html")
  85. }.to change { Event.count }.by(0)
  86. expect(out).to eq(['Please use GET/POST requests only', 401])
  87. end
  88. end
  89. context "accepting only get" do
  90. before { agent.options['verbs'] = 'get' }
  91. it "should accept GET" do
  92. out = nil
  93. expect {
  94. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "get", "text/html")
  95. }.to change { Event.count }.by(1)
  96. expect(out).to eq(['Event Created', 201])
  97. end
  98. it "should not accept POST" do
  99. out = nil
  100. expect {
  101. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  102. }.to change { Event.count }.by(0)
  103. expect(out).to eq(['Please use GET requests only', 401])
  104. end
  105. end
  106. context "accepting only post" do
  107. before { agent.options['verbs'] = 'post' }
  108. it "should not accept GET" do
  109. out = nil
  110. expect {
  111. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "get", "text/html")
  112. }.to change { Event.count }.by(0)
  113. expect(out).to eq(['Please use POST requests only', 401])
  114. end
  115. it "should accept POST" do
  116. out = nil
  117. expect {
  118. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  119. }.to change { Event.count }.by(1)
  120. expect(out).to eq(['Event Created', 201])
  121. end
  122. end
  123. context "accepting only put" do
  124. before { agent.options['verbs'] = 'put' }
  125. it "should accept PUT" do
  126. out = nil
  127. expect {
  128. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "put", "text/html")
  129. }.to change { Event.count }.by(1)
  130. expect(out).to eq(['Event Created', 201])
  131. end
  132. it "should not accept GET" do
  133. out = nil
  134. expect {
  135. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "get", "text/html")
  136. }.to change { Event.count }.by(0)
  137. expect(out).to eq(['Please use PUT requests only', 401])
  138. end
  139. it "should not accept POST" do
  140. out = nil
  141. expect {
  142. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  143. }.to change { Event.count }.by(0)
  144. expect(out).to eq(['Please use PUT requests only', 401])
  145. end
  146. end
  147. context "flaky content with commas" do
  148. before { agent.options['verbs'] = ',, PUT,POST, gEt , ,' }
  149. it "should accept PUT" do
  150. out = nil
  151. expect {
  152. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "put", "text/html")
  153. }.to change { Event.count }.by(1)
  154. expect(out).to eq(['Event Created', 201])
  155. end
  156. it "should accept GET" do
  157. out = nil
  158. expect {
  159. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "get", "text/html")
  160. }.to change { Event.count }.by(1)
  161. expect(out).to eq(['Event Created', 201])
  162. end
  163. it "should accept POST" do
  164. out = nil
  165. expect {
  166. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  167. }.to change { Event.count }.by(1)
  168. expect(out).to eq(['Event Created', 201])
  169. end
  170. it "should not accept DELETE" do
  171. out = nil
  172. expect {
  173. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "delete", "text/html")
  174. }.to change { Event.count }.by(0)
  175. expect(out).to eq(['Please use PUT/POST/GET requests only', 401])
  176. end
  177. end
  178. end
  179. end
  180. end