webhook_agent_spec.rb 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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' => 'payload' })
  6. _agent.user = users(:bob)
  7. _agent.save!
  8. _agent
  9. end
  10. let(:payload) { {'some' => 'info'} }
  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', 'payload' => 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 not create event if secrets dont match' do
  21. out = nil
  22. expect {
  23. out = agent.receive_web_request({ 'secret' => 'bazbat', 'payload' => payload }, "post", "text/html")
  24. }.to change { Event.count }.by(0)
  25. expect(out).to eq(['Not Authorized', 401])
  26. end
  27. it "should only accept POSTs" do
  28. out = nil
  29. expect {
  30. out = agent.receive_web_request({ 'secret' => 'foobar', 'payload' => payload }, "get", "text/html")
  31. }.to change { Event.count }.by(0)
  32. expect(out).to eq(['Please use POST requests only', 401])
  33. end
  34. end
  35. end