basecamp_agent_spec.rb 2.5 KB

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