Add error if payloads is used with other top-level keys

Andrew Cantino 8 年之前
父節點
當前提交
fee88f92a4
共有 2 個文件被更改,包括 14 次插入3 次删除
  1. 7 3
      app/models/agents/manual_event_agent.rb
  2. 7 0
      spec/models/agents/manual_event_agent_spec.rb

+ 7 - 3
app/models/agents/manual_event_agent.rb

@@ -19,10 +19,14 @@ module Agents
19 19
     def handle_details_post(params)
20 20
       if params['payload']
21 21
         json = interpolate_options(JSON.parse(params['payload']))
22
-        [json['payloads'] || json].flatten.each do |payload|
23
-          create_event(:payload => payload)
22
+        if json['payloads'] && (json.keys - ['payloads']).length > 0
23
+          { :success => false, :error => "If you provide the 'payloads' key, please do not provide any other keys at the top level." }
24
+        else
25
+          [json['payloads'] || json].flatten.each do |payload|
26
+            create_event(:payload => payload)
27
+          end
28
+          { :success => true }
24 29
         end
25
-        { :success => true }
26 30
       else
27 31
         { :success => false, :error => "You must provide a JSON payload" }
28 32
       end

+ 7 - 0
spec/models/agents/manual_event_agent_spec.rb

@@ -26,6 +26,13 @@ describe Agents::ManualEventAgent do
26 26
       expect(events[1].payload).to eq({ 'key' => 'value1' })
27 27
     end
28 28
 
29
+    it "errors when given both payloads and other top-level keys" do
30
+      expect {
31
+        json = { 'key' => 'value2', 'payloads' => [{ 'key' => 'value1' }] }.to_json
32
+        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." })
33
+      }.to_not change { @checker.events.count }
34
+    end
35
+
29 36
     it "supports Liquid formatting" do
30 37
       expect {
31 38
         json = { 'key' => "{{ 'now' | date: '%Y' }}", 'nested' => { 'lowercase' => "{{ 'uppercase' | upcase }}" } }.to_json