1
0

create_an_agent_spec.rb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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").hover
  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").hover
  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").hover
  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").hover
  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").hover
  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 {
  87. click_on "Save"
  88. }).to have_text("Sorry, there appears to be an error in your JSON input. Please fix it before continuing.")
  89. end
  90. context "displaying the correct information" do
  91. before(:each) do
  92. visit new_agent_path
  93. end
  94. it "shows all options for agents that can be scheduled, create and receive events" do
  95. select_agent_type("Website Agent scrapes")
  96. expect(page).not_to have_content('This type of Agent cannot create events.')
  97. end
  98. it "does not show the target select2 field when the agent can not create events" do
  99. select_agent_type("Email Agent")
  100. expect(page).to have_content('This type of Agent cannot create events.')
  101. end
  102. end
  103. it "allows to click on on the agent name in select2 tags" do
  104. visit new_agent_path
  105. select_agent_type("Website Agent scrapes")
  106. select2("SF Weather", from: 'Sources')
  107. click_on "SF Weather"
  108. expect(page).to have_content "Editing your WeatherAgent"
  109. end
  110. context "clearing unsupported fields of agents" do
  111. before do
  112. visit new_agent_path
  113. end
  114. it "does not send previously configured sources when the current agent does not support them" do
  115. select_agent_type("Website Agent scrapes")
  116. select2("SF Weather", from: 'Sources')
  117. select_agent_type("Webhook Agent")
  118. fill_in(:agent_name, with: "No sources")
  119. click_on "Save"
  120. expect(page).to have_content("No sources")
  121. agent = Agent.find_by(name: "No sources")
  122. expect(agent.sources).to eq([])
  123. end
  124. it "does not send previously configured control targets when the current agent does not support them" do
  125. select_agent_type("Commander Agent")
  126. select2("SF Weather", from: 'Control targets')
  127. select_agent_type("Webhook Agent")
  128. fill_in(:agent_name, with: "No control targets")
  129. click_on "Save"
  130. expect(page).to have_content("No control targets")
  131. agent = Agent.find_by(name: "No control targets")
  132. expect(agent.control_targets).to eq([])
  133. end
  134. it "does not send previously configured receivers when the current agent does not support them" do
  135. select_agent_type("Website Agent scrapes")
  136. sleep 0.5
  137. select2("ZKCD", from: 'Receivers')
  138. select_agent_type("Email Agent")
  139. fill_in(:agent_name, with: "No receivers")
  140. click_on "Save"
  141. expect(page).to have_content("No receivers")
  142. agent = Agent.find_by(name: "No receivers")
  143. expect(agent.receivers).to eq([])
  144. end
  145. end
  146. end