basecamp_agent_spec.rb 2.3KB

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, /Z$/).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. @checker.should be_valid
  17. end
  18. it "should require the basecamp project_id" do
  19. @checker.options['project_id'] = nil
  20. @checker.should_not be_valid
  21. end
  22. end
  23. describe "helpers" do
  24. it "should generate a correct request options hash" do
  25. @checker.send(:request_options).should == {:headers => {"User-Agent" => "Huginn (https://github.com/cantino/huginn)", "Authorization" => 'Bearer "1234token"'}}
  26. end
  27. it "should generate the currect request url" do
  28. @checker.send(:request_url).should == "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. @checker.send(:query_parameters).should == {}
  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_run] = time
  36. @checker.save
  37. @checker.reload.send(:query_parameters).should == {: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. end
  44. it "should check that initial run creates an event" do
  45. @checker.last_check_at = Time.now - 1.minute
  46. expect { @checker.check }.to change { Event.count }.by(1)
  47. end
  48. end
  49. describe "#working?" do
  50. it "it is working when at least one event was emited" do
  51. @checker.should_not be_working
  52. @checker.last_check_at = Time.now - 1.minute
  53. @checker.check
  54. @checker.reload.should be_working
  55. end
  56. end
  57. end