1
0

dry_runs_controller_spec.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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")), status: 200)
  54. end
  55. it "does not actually create any agent, event or log" do
  56. expect {
  57. post :create, params: {agent: valid_attributes}
  58. }.not_to change {
  59. [users(:bob).agents.count, users(:bob).events.count, users(:bob).logs.count]
  60. }
  61. results = assigns(:results)
  62. expect(results[:log]).to be_a(String)
  63. expect(results[:log]).to include('Extracting html at')
  64. expect(results[:events]).to be_a(Array)
  65. expect(results[:events].length).to eq(1)
  66. expect(results[:events].map(&:class)).to eq([ActiveSupport::HashWithIndifferentAccess])
  67. expect(results[:memory]).to be_a(Hash)
  68. end
  69. it "does not actually update an agent" do
  70. agent = agents(:bob_weather_agent)
  71. expect {
  72. post :create, params: {agent_id: agent, agent: valid_attributes(name: 'New Name')}
  73. }.not_to change {
  74. [users(:bob).agents.count, users(:bob).events.count, users(:bob).logs.count, agent.name, agent.updated_at]
  75. }
  76. end
  77. it "accepts an event" do
  78. agent = agents(:bob_website_agent)
  79. agent.options['url_from_event'] = '{{ url }}'
  80. agent.save!
  81. url_from_event = "http://xkcd.com/?from_event=1".freeze
  82. expect {
  83. post :create, params: {agent_id: agent, event: { url: url_from_event }.to_json}
  84. }.not_to change {
  85. [users(:bob).agents.count, users(:bob).events.count, users(:bob).logs.count, agent.name, agent.updated_at]
  86. }
  87. results = assigns(:results)
  88. expect(results[:log]).to match(/^\[\d\d:\d\d:\d\d\] INFO -- : Fetching #{Regexp.quote(url_from_event)}$/)
  89. end
  90. it "uses the memory of an existing Agent" do
  91. valid_params = {
  92. :name => "somename",
  93. :options => {
  94. :code => "Agent.check = function() { this.createEvent({ 'message': this.memory('fu') }); };",
  95. }
  96. }
  97. agent = Agents::JavaScriptAgent.new(valid_params)
  98. agent.memory = {fu: "bar"}
  99. agent.user = users(:bob)
  100. agent.save!
  101. post :create, params: {agent_id: agent, agent: valid_params}
  102. results = assigns(:results)
  103. expect(results[:events][0]).to eql({"message" => "bar"})
  104. end
  105. it 'sets created_at of the dry-runned event' do
  106. agent = agents(:bob_formatting_agent)
  107. agent.options['instructions'] = {'created_at' => '{{created_at | date: "%a, %b %d, %y"}}'}
  108. agent.save
  109. post :create, params: {agent_id: agent, event: {test: 1}.to_json}
  110. results = assigns(:results)
  111. expect(results[:events]).to be_a(Array)
  112. expect(results[:events].length).to eq(1)
  113. expect(results[:events].first['created_at']).to eq(Date.today.strftime('%a, %b %d, %y'))
  114. end
  115. end
  116. end