Нет описания http://j1x-huginn.herokuapp.com

webhook_agent_spec.rb 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 interpolated response message if configured with `response` option' do
  46. agent.options['response'] = '{{some_key.people[1].name}}'
  47. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  48. expect(out).to eq(['jon', 201])
  49. end
  50. it 'should respond with `Event Created` if the response option is nil or missing' do
  51. agent.options['response'] = nil
  52. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  53. expect(out).to eq(['Event Created', 201])
  54. agent.options.delete('response')
  55. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  56. expect(out).to eq(['Event Created', 201])
  57. end
  58. it 'should respond with customized response code if configured with `code` option' do
  59. agent.options['code'] = '200'
  60. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  61. expect(out).to eq(['Event Created', 200])
  62. end
  63. it 'should respond with `201` if the code option is empty, nil or missing' do
  64. agent.options['code'] = ''
  65. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  66. expect(out).to eq(['Event Created', 201])
  67. agent.options['code'] = nil
  68. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  69. expect(out).to eq(['Event Created', 201])
  70. agent.options.delete('code')
  71. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  72. expect(out).to eq(['Event Created', 201])
  73. end
  74. describe "receiving events" do
  75. context "default settings" do
  76. it "should not accept GET" do
  77. out = nil
  78. expect {
  79. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "get", "text/html")
  80. }.to change { Event.count }.by(0)
  81. expect(out).to eq(['Please use POST requests only', 401])
  82. end
  83. it "should accept POST" do
  84. out = nil
  85. expect {
  86. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  87. }.to change { Event.count }.by(1)
  88. expect(out).to eq(['Event Created', 201])
  89. end
  90. end
  91. context "accepting get and post" do
  92. before { agent.options['verbs'] = 'get,post' }
  93. it "should accept GET" do
  94. out = nil
  95. expect {
  96. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "get", "text/html")
  97. }.to change { Event.count }.by(1)
  98. expect(out).to eq(['Event Created', 201])
  99. end
  100. it "should accept POST" do
  101. out = nil
  102. expect {
  103. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  104. }.to change { Event.count }.by(1)
  105. expect(out).to eq(['Event Created', 201])
  106. end
  107. it "should not accept PUT" do
  108. out = nil
  109. expect {
  110. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "put", "text/html")
  111. }.to change { Event.count }.by(0)
  112. expect(out).to eq(['Please use GET/POST requests only', 401])
  113. end
  114. end
  115. context "accepting only get" do
  116. before { agent.options['verbs'] = 'get' }
  117. it "should accept GET" do
  118. out = nil
  119. expect {
  120. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "get", "text/html")
  121. }.to change { Event.count }.by(1)
  122. expect(out).to eq(['Event Created', 201])
  123. end
  124. it "should not accept POST" do
  125. out = nil
  126. expect {
  127. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  128. }.to change { Event.count }.by(0)
  129. expect(out).to eq(['Please use GET requests only', 401])
  130. end
  131. end
  132. context "accepting only post" do
  133. before { agent.options['verbs'] = 'post' }
  134. it "should not accept GET" do
  135. out = nil
  136. expect {
  137. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "get", "text/html")
  138. }.to change { Event.count }.by(0)
  139. expect(out).to eq(['Please use POST requests only', 401])
  140. end
  141. it "should accept POST" do
  142. out = nil
  143. expect {
  144. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  145. }.to change { Event.count }.by(1)
  146. expect(out).to eq(['Event Created', 201])
  147. end
  148. end
  149. context "accepting only put" do
  150. before { agent.options['verbs'] = 'put' }
  151. it "should accept PUT" do
  152. out = nil
  153. expect {
  154. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "put", "text/html")
  155. }.to change { Event.count }.by(1)
  156. expect(out).to eq(['Event Created', 201])
  157. end
  158. it "should not accept GET" do
  159. out = nil
  160. expect {
  161. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "get", "text/html")
  162. }.to change { Event.count }.by(0)
  163. expect(out).to eq(['Please use PUT requests only', 401])
  164. end
  165. it "should not accept POST" do
  166. out = nil
  167. expect {
  168. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  169. }.to change { Event.count }.by(0)
  170. expect(out).to eq(['Please use PUT requests only', 401])
  171. end
  172. end
  173. context "flaky content with commas" do
  174. before { agent.options['verbs'] = ',, PUT,POST, gEt , ,' }
  175. it "should accept PUT" do
  176. out = nil
  177. expect {
  178. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "put", "text/html")
  179. }.to change { Event.count }.by(1)
  180. expect(out).to eq(['Event Created', 201])
  181. end
  182. it "should accept GET" do
  183. out = nil
  184. expect {
  185. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "get", "text/html")
  186. }.to change { Event.count }.by(1)
  187. expect(out).to eq(['Event Created', 201])
  188. end
  189. it "should accept POST" do
  190. out = nil
  191. expect {
  192. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  193. }.to change { Event.count }.by(1)
  194. expect(out).to eq(['Event Created', 201])
  195. end
  196. it "should not accept DELETE" do
  197. out = nil
  198. expect {
  199. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "delete", "text/html")
  200. }.to change { Event.count }.by(0)
  201. expect(out).to eq(['Please use PUT/POST/GET requests only', 401])
  202. end
  203. end
  204. context "with reCAPTCHA" do
  205. it "should not check a reCAPTCHA response unless recaptcha_secret is set" do
  206. checked = false
  207. out = nil
  208. stub_request(:any, /verify/).to_return { |request|
  209. checked = true
  210. { status: 200, body: '{"success":false}' }
  211. }
  212. expect {
  213. out= agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  214. }.not_to change { checked }
  215. expect(out).to eq(["Event Created", 201])
  216. end
  217. it "should reject a request if recaptcha_secret is set but g-recaptcha-response is not given" do
  218. agent.options['recaptcha_secret'] = 'supersupersecret'
  219. checked = false
  220. out = nil
  221. stub_request(:any, /verify/).to_return { |request|
  222. checked = true
  223. { status: 200, body: '{"success":false}' }
  224. }
  225. expect {
  226. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  227. }.not_to change { checked }
  228. expect(out).to eq(["Not Authorized", 401])
  229. end
  230. it "should reject a request if recaptcha_secret is set and g-recaptcha-response given is not verified" do
  231. agent.options['recaptcha_secret'] = 'supersupersecret'
  232. checked = false
  233. out = nil
  234. stub_request(:any, /verify/).to_return { |request|
  235. checked = true
  236. { status: 200, body: '{"success":false}' }
  237. }
  238. expect {
  239. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload, 'g-recaptcha-response' => 'somevalue' }, "post", "text/html")
  240. }.to change { checked }
  241. expect(out).to eq(["Not Authorized", 401])
  242. end
  243. it "should accept a request if recaptcha_secret is set and g-recaptcha-response given is verified" do
  244. agent.options['payload_path'] = '.'
  245. agent.options['recaptcha_secret'] = 'supersupersecret'
  246. checked = false
  247. out = nil
  248. stub_request(:any, /verify/).to_return { |request|
  249. checked = true
  250. { status: 200, body: '{"success":true}' }
  251. }
  252. expect {
  253. out = agent.receive_web_request(payload.merge({ 'secret' => 'foobar', 'g-recaptcha-response' => 'somevalue' }), "post", "text/html")
  254. }.to change { checked }
  255. expect(out).to eq(["Event Created", 201])
  256. expect(Event.last.payload).to eq(payload)
  257. end
  258. end
  259. end
  260. end
  261. end