1
0

create_an_agent_spec.rb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. require 'rails_helper'
  2. describe "Creating a new agent", js: true do
  3. before(:each) do
  4. login_as(users(:bob))
  5. end
  6. it "creates an agent" do
  7. visit "/"
  8. page.find("a", text: "Agents").trigger(:mouseover)
  9. click_on("New Agent")
  10. select_agent_type("Trigger Agent")
  11. fill_in(:agent_name, with: "Test Trigger Agent")
  12. click_on "Save"
  13. expect(page).to have_text("Test Trigger Agent")
  14. end
  15. context "with associated agents" do
  16. let!(:bob_scheduler_agent) {
  17. Agents::SchedulerAgent.create!(
  18. user: users(:bob),
  19. name: 'Example Scheduler',
  20. options: {
  21. 'action' => 'run',
  22. 'schedule' => '0 * * * *'
  23. },
  24. )
  25. }
  26. let!(:bob_weather_agent) {
  27. agents(:bob_weather_agent)
  28. }
  29. let!(:bob_formatting_agent) {
  30. agents(:bob_formatting_agent).tap { |agent|
  31. # Make this valid
  32. agent.options['instructions']['foo'] = 'bar'
  33. agent.save!
  34. }
  35. }
  36. it "creates an agent with a source and a receiver" do
  37. visit "/"
  38. page.find("a", text: "Agents").trigger(:mouseover)
  39. click_on("New Agent")
  40. select_agent_type("Trigger Agent")
  41. fill_in(:agent_name, with: "Test Trigger Agent")
  42. select2("SF Weather", from: 'Sources')
  43. select2("Formatting Agent", from: 'Receivers')
  44. click_on "Save"
  45. expect(page).to have_text("Test Trigger Agent")
  46. agent = Agent.find_by(name: "Test Trigger Agent")
  47. expect(agent.sources).to eq([bob_weather_agent])
  48. expect(agent.receivers).to eq([bob_formatting_agent])
  49. end
  50. it "creates an agent with a control target" do
  51. visit "/"
  52. page.find("a", text: "Agents").trigger(:mouseover)
  53. click_on("New Agent")
  54. select_agent_type("Scheduler Agent")
  55. fill_in(:agent_name, with: "Test Scheduler Agent")
  56. select2("SF Weather", from: 'Control targets')
  57. click_on "Save"
  58. expect(page).to have_text("Test Scheduler Agent")
  59. agent = Agent.find_by(name: "Test Scheduler Agent")
  60. expect(agent.control_targets).to eq([bob_weather_agent])
  61. end
  62. it "creates an agent with a controller" do
  63. visit "/"
  64. page.find("a", text: "Agents").trigger(:mouseover)
  65. click_on("New Agent")
  66. select_agent_type("Weather Agent")
  67. fill_in(:agent_name, with: "Test Weather Agent")
  68. select2("Example Scheduler", from: 'Controllers')
  69. click_on "Save"
  70. expect(page).to have_text("Test Weather Agent")
  71. agent = Agent.find_by(name: "Test Weather Agent")
  72. expect(agent.controllers).to eq([bob_scheduler_agent])
  73. end
  74. end
  75. it "creates an alert if a new agent with invalid json is submitted" do
  76. visit "/"
  77. page.find("a", text: "Agents").trigger(:mouseover)
  78. click_on("New Agent")
  79. select_agent_type("Trigger Agent")
  80. fill_in(:agent_name, with: "Test Trigger Agent")
  81. click_on("Toggle View")
  82. fill_in(:agent_options, with: '{
  83. "expected_receive_period_in_days": "2"
  84. "keep_event": "false"
  85. }')
  86. expect(get_alert_text_from { click_on "Save" }).to have_text("Sorry, there appears to be an error in your JSON input. Please fix it before continuing.")
  87. end
  88. context "displaying the correct information" do
  89. before(:each) do
  90. visit new_agent_path
  91. end
  92. it "shows all options for agents that can be scheduled, create and receive events" do
  93. select_agent_type("Website Agent scrapes")
  94. expect(page).not_to have_content('This type of Agent cannot create events.')
  95. end
  96. it "does not show the target select2 field when the agent can not create events" do
  97. select_agent_type("Email Agent")
  98. expect(page).to have_content('This type of Agent cannot create events.')
  99. end
  100. end
  101. it "allows to click on on the agent name in select2 tags" do
  102. visit new_agent_path
  103. select_agent_type("Website Agent scrapes")
  104. select2("SF Weather", from: 'Sources')
  105. click_on "SF Weather"
  106. expect(page).to have_content "Editing your WeatherAgent"
  107. end
  108. context "clearing unsupported fields of agents" do
  109. before do
  110. visit new_agent_path
  111. end
  112. it "does not send previously configured sources when the current agent does not support them" do
  113. select_agent_type("Website Agent scrapes")
  114. select2("SF Weather", from: 'Sources')
  115. select_agent_type("Webhook Agent")
  116. fill_in(:agent_name, with: "No sources")
  117. click_on "Save"
  118. expect(page).to have_content("No sources")
  119. agent = Agent.find_by(name: "No sources")
  120. expect(agent.sources).to eq([])
  121. end
  122. it "does not send previously configured control targets when the current agent does not support them" do
  123. select_agent_type("Commander Agent")
  124. select2("SF Weather", from: 'Control targets')
  125. select_agent_type("Webhook Agent")
  126. fill_in(:agent_name, with: "No control targets")
  127. click_on "Save"
  128. expect(page).to have_content("No control targets")
  129. agent = Agent.find_by(name: "No control targets")
  130. expect(agent.control_targets).to eq([])
  131. end
  132. it "does not send previously configured receivers when the current agent does not support them" do
  133. select_agent_type("Website Agent scrapes")
  134. sleep 0.5
  135. select2("ZKCD", from: 'Receivers')
  136. select_agent_type("Email Agent")
  137. fill_in(:agent_name, with: "No receivers")
  138. click_on "Save"
  139. expect(page).to have_content("No receivers")
  140. agent = Agent.find_by(name: "No receivers")
  141. expect(agent.receivers).to eq([])
  142. end
  143. end
  144. end