webhook_agent_spec.rb 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. require 'spec_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. describe "receiving events" do
  37. context "default settings" do
  38. it "should not accept GET" do
  39. out = nil
  40. expect {
  41. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "get", "text/html")
  42. }.to change { Event.count }.by(0)
  43. expect(out).to eq(['Please use POST requests only', 401])
  44. end
  45. it "should accept POST" do
  46. out = nil
  47. expect {
  48. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  49. }.to change { Event.count }.by(1)
  50. expect(out).to eq(['Event Created', 201])
  51. end
  52. end
  53. context "accepting get and post" do
  54. before { agent.options['verbs'] = 'get;post' }
  55. it "should 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(1)
  60. expect(out).to eq(['Event Created', 201])
  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 only get" do
  71. before { agent.options['verbs'] = 'get' }
  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 not 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(0)
  84. expect(out).to eq(['Please use GET requests only', 401])
  85. end
  86. end
  87. context "accepting only post" do
  88. before { agent.options['verbs'] = 'post' }
  89. it "should not accept GET" do
  90. out = nil
  91. expect {
  92. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "get", "text/html")
  93. }.to change { Event.count }.by(0)
  94. expect(out).to eq(['Please use POST requests only', 401])
  95. end
  96. it "should accept POST" do
  97. out = nil
  98. expect {
  99. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
  100. }.to change { Event.count }.by(1)
  101. expect(out).to eq(['Event Created', 201])
  102. end
  103. end
  104. context "accepting only put" do
  105. before { agent.options['verbs'] = 'put' }
  106. it "should accept PUT" do
  107. out = nil
  108. expect {
  109. out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "put", "text/html")
  110. }.to change { Event.count }.by(1)
  111. expect(out).to eq(['Event Created', 201])
  112. end
  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 PUT requests only', 401])
  119. end
  120. it "should not 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(0)
  125. expect(out).to eq(['Please use PUT requests only', 401])
  126. end
  127. end
  128. end
  129. end
  130. end