manual_event_agent_spec.rb 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. require 'rails_helper'
  2. describe Agents::ManualEventAgent do
  3. before do
  4. @checker = Agents::ManualEventAgent.new(name: "My Manual Event Agent")
  5. @checker.user = users(:jane)
  6. @checker.save!
  7. end
  8. describe "#handle_details_post" do
  9. it "emits an event with the given payload" do
  10. expect {
  11. json = { 'foo' => "bar" }.to_json
  12. expect(@checker.handle_details_post({ 'payload' => json })).to eq({ success: true })
  13. }.to change { @checker.events.count }.by(1)
  14. expect(@checker.events.last.payload).to eq({ 'foo' => 'bar' })
  15. end
  16. it "emits multiple events when given a magic 'payloads' key" do
  17. expect {
  18. json = { 'payloads' => [{ 'key' => 'value1' }, { 'key' => 'value2' }] }.to_json
  19. expect(@checker.handle_details_post({ 'payload' => json })).to eq({ success: true })
  20. }.to change { @checker.events.count }.by(2)
  21. events = @checker.events.order('id desc')
  22. expect(events[0].payload).to eq({ 'key' => 'value2' })
  23. expect(events[1].payload).to eq({ 'key' => 'value1' })
  24. end
  25. it "errors when given both payloads and other top-level keys" do
  26. expect {
  27. json = { 'key' => 'value2', 'payloads' => [{ 'key' => 'value1' }] }.to_json
  28. expect(@checker.handle_details_post({ 'payload' => json })).to eq({ success: false, error: "If you provide the 'payloads' key, please do not provide any other keys at the top level." })
  29. }.to_not change { @checker.events.count }
  30. end
  31. it "supports Liquid formatting" do
  32. expect {
  33. json = { 'key' => "{{ 'now' | date: '%Y' }}", 'nested' => { 'lowercase' => "{{ 'uppercase' | upcase }}" } }.to_json
  34. expect(@checker.handle_details_post({ 'payload' => json })).to eq({ success: true })
  35. }.to change { @checker.events.count }.by(1)
  36. expect(@checker.events.last.payload).to eq({ 'key' => Time.now.year.to_s, 'nested' => { 'lowercase' => 'UPPERCASE' } })
  37. end
  38. it "errors when not given a JSON payload" do
  39. expect {
  40. expect(@checker.handle_details_post({ 'foo' =>'bar' })).to eq({ success: false, error: "You must provide a JSON payload" })
  41. }.not_to change { @checker.events.count }
  42. end
  43. end
  44. end