Aucune description http://j1x-huginn.herokuapp.com

webhook_agent_spec.rb 11KB

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