dry_runs_controller_spec.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. require 'rails_helper'
  2. describe Agents::DryRunsController do
  3. def valid_attributes(options = {})
  4. {
  5. type: "Agents::WebsiteAgent",
  6. name: "Something",
  7. options: agents(:bob_website_agent).options,
  8. source_ids: [agents(:bob_weather_agent).id, ""]
  9. }.merge(options)
  10. end
  11. before do
  12. sign_in users(:bob)
  13. end
  14. describe "GET index" do
  15. it "does not load any events without specifing sources" do
  16. get :index, params: { type: 'Agents::WebsiteAgent', source_ids: [] }
  17. expect(assigns(:events)).to eq([])
  18. end
  19. context "does not load events when the agent is owned by a different user" do
  20. before do
  21. @agent = agents(:jane_website_agent)
  22. @agent.sources << @agent
  23. @agent.save!
  24. expect(@agent.events.count).not_to be(0)
  25. end
  26. it "for new agents" do
  27. get :index, params: { type: 'Agents::WebsiteAgent', source_ids: [@agent.id] }
  28. expect(assigns(:events)).to eq([])
  29. end
  30. it "for existing agents" do
  31. expect(@agent.events.count).not_to be(0)
  32. expect { get :index, params: { agent_id: @agent } }.to raise_error(NoMethodError)
  33. end
  34. end
  35. context "loads the most recent events" do
  36. before do
  37. @agent = agents(:bob_website_agent)
  38. @agent.sources << @agent
  39. @agent.save!
  40. end
  41. it "load the most recent events when providing source ids" do
  42. get :index, params: { type: 'Agents::WebsiteAgent', source_ids: [@agent.id] }
  43. expect(assigns(:events)).to eq([@agent.events.first])
  44. end
  45. it "loads the most recent events for a saved agent" do
  46. get :index, params: { agent_id: @agent }
  47. expect(assigns(:events)).to eq([@agent.events.first])
  48. end
  49. end
  50. end
  51. describe "POST create" do
  52. before do
  53. stub_request(:any, /xkcd/).to_return(body: File.read(Rails.root.join("spec/data_fixtures/xkcd.html")),
  54. status: 200)
  55. end
  56. it "does not actually create any agent, event or log" do
  57. expect {
  58. post :create, params: { agent: valid_attributes }
  59. }.not_to change {
  60. [users(:bob).agents.count, users(:bob).events.count, users(:bob).logs.count]
  61. }
  62. results = assigns(:results)
  63. expect(results[:log]).to be_a(String)
  64. expect(results[:log]).to include('Extracting html at')
  65. expect(results[:events]).to be_a(Array)
  66. expect(results[:events].length).to eq(1)
  67. expect(results[:events].map(&:class)).to eq([ActiveSupport::HashWithIndifferentAccess])
  68. expect(results[:memory]).to be_a(Hash)
  69. end
  70. it "does not actually update an agent" do
  71. agent = agents(:bob_weather_agent)
  72. expect {
  73. post :create, params: { agent_id: agent, agent: valid_attributes(name: 'New Name') }
  74. }.not_to change {
  75. [users(:bob).agents.count, users(:bob).events.count, users(:bob).logs.count, agent.name, agent.updated_at]
  76. }
  77. end
  78. it "accepts an event" do
  79. agent = agents(:bob_website_agent)
  80. agent.options['url_from_event'] = '{{ url }}'
  81. agent.save!
  82. url_from_event = "http://xkcd.com/?from_event=1".freeze
  83. expect {
  84. post :create, params: { agent_id: agent.id, event: { url: url_from_event }.to_json }
  85. }.not_to change {
  86. [users(:bob).agents.count, users(:bob).events.count, users(:bob).logs.count, agent.name, agent.updated_at]
  87. }
  88. results = assigns(:results)
  89. expect(results[:log]).to match(/^\[\d\d:\d\d:\d\d\] INFO -- : Fetching #{Regexp.quote(url_from_event)}$/)
  90. end
  91. it "uses the memory of an existing Agent" do
  92. valid_params = {
  93. name: "somename",
  94. options: {
  95. code: "Agent.check = function() { this.createEvent({ 'message': this.memory('fu') }); };",
  96. }
  97. }
  98. agent = Agents::JavaScriptAgent.new(valid_params)
  99. agent.memory = { fu: "bar" }
  100. agent.user = users(:bob)
  101. agent.save!
  102. post :create, params: { agent_id: agent, agent: valid_params }
  103. results = assigns(:results)
  104. expect(results[:events][0]).to eql({ "message" => "bar" })
  105. end
  106. it 'sets created_at of the dry-runned event' do
  107. agent = agents(:bob_formatting_agent)
  108. agent.options['instructions'] = { 'created_at' => '{{created_at | date: "%a, %b %d, %y"}}' }
  109. agent.save
  110. post :create, params: { agent_id: agent, event: { test: 1 }.to_json }
  111. results = assigns(:results)
  112. expect(results[:events]).to be_a(Array)
  113. expect(results[:events].length).to eq(1)
  114. expect(results[:events].first['created_at']).to eq(Date.today.strftime('%a, %b %d, %y'))
  115. end
  116. end
  117. end