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

webhook_agent_spec.rb 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 do not 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. agent.options['response'] = 'That Worked'
  38. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  39. expect(out).to eq(['That Worked', 201])
  40. # Empty string is a valid response
  41. agent.options['response'] = ''
  42. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  43. expect(out).to eq(['', 201])
  44. end
  45. it 'should respond with `Event Created` if the response option is nil or missing' do
  46. agent.options['response'] = nil
  47. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  48. expect(out).to eq(['Event Created', 201])
  49. agent.options.delete('response')
  50. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  51. expect(out).to eq(['Event Created', 201])
  52. end
  53. describe "receiving events" do
  54. context "default settings" do
  55. it "should not accept GET" do
  56. out = nil
  57. expect {
  58. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "get", "text/html")
  59. }.to change { Event.count }.by(0)
  60. expect(out).to eq(['Please use POST requests only', 401])
  61. end
  62. it "should accept POST" do
  63. out = nil
  64. expect {
  65. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  66. }.to change { Event.count }.by(1)
  67. expect(out).to eq(['Event Created', 201])
  68. end
  69. end
  70. context "accepting get and post" do
  71. before { agent.options['verbs'] = 'get,post' }
  72. it "should accept GET" do
  73. out = nil
  74. expect {
  75. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "get", "text/html")
  76. }.to change { Event.count }.by(1)
  77. expect(out).to eq(['Event Created', 201])
  78. end
  79. it "should accept POST" do
  80. out = nil
  81. expect {
  82. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  83. }.to change { Event.count }.by(1)
  84. expect(out).to eq(['Event Created', 201])
  85. end
  86. it "should not accept PUT" do
  87. out = nil
  88. expect {
  89. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "put", "text/html")
  90. }.to change { Event.count }.by(0)
  91. expect(out).to eq(['Please use GET/POST requests only', 401])
  92. end
  93. end
  94. context "accepting only get" do
  95. before { agent.options['verbs'] = 'get' }
  96. it "should accept GET" do
  97. out = nil
  98. expect {
  99. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "get", "text/html")
  100. }.to change { Event.count }.by(1)
  101. expect(out).to eq(['Event Created', 201])
  102. end
  103. it "should not accept POST" do
  104. out = nil
  105. expect {
  106. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  107. }.to change { Event.count }.by(0)
  108. expect(out).to eq(['Please use GET requests only', 401])
  109. end
  110. end
  111. context "accepting only post" do
  112. before { agent.options['verbs'] = 'post' }
  113. it "should not accept GET" do
  114. out = nil
  115. expect {
  116. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "get", "text/html")
  117. }.to change { Event.count }.by(0)
  118. expect(out).to eq(['Please use POST requests only', 401])
  119. end
  120. it "should accept POST" do
  121. out = nil
  122. expect {
  123. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  124. }.to change { Event.count }.by(1)
  125. expect(out).to eq(['Event Created', 201])
  126. end
  127. end
  128. context "accepting only put" do
  129. before { agent.options['verbs'] = 'put' }
  130. it "should accept PUT" do
  131. out = nil
  132. expect {
  133. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "put", "text/html")
  134. }.to change { Event.count }.by(1)
  135. expect(out).to eq(['Event Created', 201])
  136. end
  137. it "should not accept GET" do
  138. out = nil
  139. expect {
  140. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "get", "text/html")
  141. }.to change { Event.count }.by(0)
  142. expect(out).to eq(['Please use PUT requests only', 401])
  143. end
  144. it "should not accept POST" do
  145. out = nil
  146. expect {
  147. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  148. }.to change { Event.count }.by(0)
  149. expect(out).to eq(['Please use PUT requests only', 401])
  150. end
  151. end
  152. context "flaky content with commas" do
  153. before { agent.options['verbs'] = ',, PUT,POST, gEt , ,' }
  154. it "should accept PUT" do
  155. out = nil
  156. expect {
  157. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "put", "text/html")
  158. }.to change { Event.count }.by(1)
  159. expect(out).to eq(['Event Created', 201])
  160. end
  161. it "should accept GET" do
  162. out = nil
  163. expect {
  164. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "get", "text/html")
  165. }.to change { Event.count }.by(1)
  166. expect(out).to eq(['Event Created', 201])
  167. end
  168. it "should accept POST" do
  169. out = nil
  170. expect {
  171. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  172. }.to change { Event.count }.by(1)
  173. expect(out).to eq(['Event Created', 201])
  174. end
  175. it "should not accept DELETE" do
  176. out = nil
  177. expect {
  178. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "delete", "text/html")
  179. }.to change { Event.count }.by(0)
  180. expect(out).to eq(['Please use PUT/POST/GET requests only', 401])
  181. end
  182. end
  183. end
  184. end
  185. end