1
0

basecamp_agent_spec.rb 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. require 'rails_helper'
  2. require 'models/concerns/oauthable'
  3. describe Agents::BasecampAgent do
  4. it_behaves_like Oauthable
  5. before(:each) do
  6. stub_request(:get, /events.json$/).to_return(
  7. :body => File.read(Rails.root.join("spec/data_fixtures/basecamp.json")),
  8. :status => 200,
  9. :headers => {"Content-Type" => "text/json"}
  10. )
  11. stub_request(:get, /projects.json$/).to_return(
  12. :body => JSON.dump([{name: 'test', id: 1234},{name: 'test1', id: 1235}]),
  13. :status => 200,
  14. :headers => {"Content-Type" => "text/json"}
  15. )
  16. stub_request(:get, /02:00$/).to_return(
  17. :body => File.read(Rails.root.join("spec/data_fixtures/basecamp.json")),
  18. :status => 200,
  19. :headers => {"Content-Type" => "text/json"}
  20. )
  21. @valid_params = { :project_id => 6789 }
  22. @checker = Agents::BasecampAgent.new(:name => "somename", :options => @valid_params)
  23. @checker.service = services(:generic)
  24. @checker.user = users(:jane)
  25. @checker.save!
  26. stub(services(:generic)).refresh_token!
  27. end
  28. describe "validating" do
  29. before do
  30. expect(@checker).to be_valid
  31. end
  32. it "should require the basecamp project_id" do
  33. @checker.options['project_id'] = nil
  34. expect(@checker).not_to be_valid
  35. end
  36. end
  37. describe "helpers" do
  38. it "should generate a correct request options hash" do
  39. expect(@checker.send(:request_options)).to eq({headers: {"User-Agent" => "Huginn - https://github.com/huginn/huginn", "Authorization" => 'Bearer "1234token"'}})
  40. end
  41. it "should generate the correct events url" do
  42. expect(@checker.send(:events_url)).to eq("https://basecamp.com/12345/api/v1/projects/6789/events.json")
  43. end
  44. it "should generate the correct projects url" do
  45. expect(@checker.send(:projects_url)).to eq("https://basecamp.com/12345/api/v1/projects.json")
  46. end
  47. it "should not provide the since attribute on first run" do
  48. expect(@checker.send(:query_parameters)).to eq({})
  49. end
  50. it "should provide the since attribute after the first run" do
  51. time = (Time.now-1.minute).iso8601
  52. @checker.memory[:last_event] = time
  53. @checker.save
  54. expect(@checker.reload.send(:query_parameters)).to eq({:query => {:since => time}})
  55. end
  56. end
  57. describe "#complete_project_id" do
  58. it "should return a array of hashes" do
  59. expect(@checker.complete_project_id).to eq [{text: 'test (1234)', id: 1234}, {text: 'test1 (1235)', id: 1235}]
  60. end
  61. end
  62. describe "#check" do
  63. it "should not emit events on its first run" do
  64. expect { @checker.check }.to change { Event.count }.by(0)
  65. expect(@checker.memory[:last_event]).to eq '2014-04-17T10:25:31.000+02:00'
  66. end
  67. it "should check that initial run creates an event" do
  68. @checker.memory[:last_event] = '2014-04-17T10:25:31.000+02:00'
  69. expect { @checker.check }.to change { Event.count }.by(1)
  70. end
  71. end
  72. describe "#working?" do
  73. it "it is working when at least one event was emitted" do
  74. expect(@checker).not_to be_working
  75. @checker.memory[:last_event] = '2014-04-17T10:25:31.000+02:00'
  76. @checker.check
  77. expect(@checker.reload).to be_working
  78. end
  79. end
  80. end