Browse Source

Ensure to only send supported agent connections

When switching between Agent types in the in the new Agent form we need to clear the select fields values (targets,
receivers, control targets) when the current Agent does not support the respective method.
Dominik Sander 9 years ago
parent
commit
af1c830d7f

+ 15 - 0
app/assets/javascripts/pages/agent-edit-page.js.coffee

@@ -13,6 +13,15 @@ class @AgentEditPage
           e.preventDefault()
           alert 'Sorry, there appears to be an error in your JSON input. Please fix it before continuing.'
 
+      if $(".link-region").length && $(".link-region").data("can-receive-events") == false
+        $(".link-region .select2-linked-tags option:selected").removeAttr('selected')
+
+      if $(".control-link-region").length && $(".control-link-region").data("can-control-other-agents") == false
+        $(".control-link-region .select2-linked-tags option:selected").removeAttr('selected')
+
+      if $(".event-related-region").length && $(".event-related-region").data("can-create-events") == false
+        $(".event-related-region .select2-linked-tags option:selected").removeAttr('selected')
+
     $("#agent_name").each ->
       # Select the number suffix if this is a cloned agent.
       if matches = this.value.match(/ \(\d+\)$/)
@@ -103,26 +112,32 @@ class @AgentEditPage
     $(".link-region .select2-container").hide()
     $(".link-region .propagate-immediately").hide()
     $(".link-region .cannot-receive-events").show()
+    $(".link-region").data("can-receive-events", false)
 
   showLinks: ->
     $(".link-region .select2-container").show()
     $(".link-region .propagate-immediately").show()
     $(".link-region .cannot-receive-events").hide()
+    $(".link-region").data("can-receive-events", true)
     @showEventDescriptions()
 
   hideControlLinks: ->
     $(".control-link-region").hide()
+    $(".control-link-region").data("can-control-other-agents", false)
 
   showControlLinks: ->
     $(".control-link-region").show()
+    $(".control-link-region").data("can-control-other-agents", true)
 
   hideEventCreation: ->
     $(".event-related-region .select2-container").hide()
     $(".event-related-region .cannot-create-events").show()
+    $(".event-related-region").data("can-create-events", false)
 
   showEventCreation: ->
     $(".event-related-region .select2-container").show()
     $(".event-related-region .cannot-create-events").hide()
+    $(".event-related-region").data("can-create-events", true)
 
   showEventDescriptions: ->
     if $("#agent_source_ids").val()

+ 39 - 0
spec/features/create_an_agent_spec.rb

@@ -56,4 +56,43 @@ describe "Creating a new agent", js: true do
     click_on "SF Weather"
     expect(page).to have_content "Editing your WeatherAgent"
   end
+
+  context "clearing unsupported fields of agents" do
+    before do
+      visit new_agent_path
+    end
+
+    it "does not send previously configured sources when the current agent does not support them" do
+      select2("Website Agent", from: "Type")
+      select2("SF Weather", from: 'Sources')
+      select2("Webhook Agent", from: "Type")
+      fill_in(:agent_name, with: "No sources")
+      click_on "Save"
+      expect(page).to have_content("No sources")
+      agent = Agent.find_by(name: "No sources")
+      expect(agent.sources).to eq([])
+    end
+
+    it "does not send previously configured control targets when the current agent does not support them" do
+      select2("Commander Agent", from: "Type")
+      select2("SF Weather", from: 'Control targets')
+      select2("Webhook Agent", from: "Type")
+      fill_in(:agent_name, with: "No control targets")
+      click_on "Save"
+      expect(page).to have_content("No control targets")
+      agent = Agent.find_by(name: "No control targets")
+      expect(agent.control_targets).to eq([])
+    end
+
+    it "does not send previously configured receivers when the current agent does not support them" do
+      select2("Website Agent", from: "Type")
+      select2("ZKCD", from: 'Receivers')
+      select2("Email Agent", from: "Type")
+      fill_in(:agent_name, with: "No receivers")
+      click_on "Save"
+      expect(page).to have_content("No receivers")
+      agent = Agent.find_by(name: "No receivers")
+      expect(agent.receivers).to eq([])
+    end
+  end
 end

+ 1 - 1
spec/fixtures/agents.yml

@@ -38,7 +38,7 @@ bob_disabled_website_agent:
   user: bob
   events_count: 1
   schedule: "midnight"
-  name: "Disabled ZKCD"
+  name: "Disabled Agent"
   guid: <%= SecureRandom.hex %>
   options: <%= {
                  :url => "http://xkcd.com",