basecamp_agent_spec.rb 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. require 'spec_helper'
  2. describe Agents::BasecampAgent do
  3. before(:each) do
  4. stub_request(:get, /json$/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/basecamp.json")), :status => 200, :headers => {"Content-Type" => "text/json"})
  5. stub_request(:get, /Z$/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/basecamp.json")), :status => 200, :headers => {"Content-Type" => "text/json"})
  6. @valid_params = {
  7. :username => "user",
  8. :password => "pass",
  9. :user_id => 12345,
  10. :project_id => 6789,
  11. }
  12. @checker = Agents::BasecampAgent.new(:name => "somename", :options => @valid_params)
  13. @checker.user = users(:jane)
  14. @checker.save!
  15. end
  16. describe "validating" do
  17. before do
  18. @checker.should be_valid
  19. end
  20. it "should require the basecamp username" do
  21. @checker.options['username'] = nil
  22. @checker.should_not be_valid
  23. end
  24. it "should require the basecamp password" do
  25. @checker.options['password'] = nil
  26. @checker.should_not be_valid
  27. end
  28. it "should require the basecamp user_id" do
  29. @checker.options['user_id'] = nil
  30. @checker.should_not be_valid
  31. end
  32. it "should require the basecamp project_id" do
  33. @checker.options['project_id'] = nil
  34. @checker.should_not be_valid
  35. end
  36. end
  37. describe "helpers" do
  38. it "should generate a correct auth hash" do
  39. @checker.send(:auth_options).should == {:basic_auth=>{:username=>"user", :password=>"pass"}}
  40. end
  41. it "should not provide the since attribute on first run" do
  42. @checker.send(:request_url).should == "https://basecamp.com/12345/api/v1/projects/6789/events.json"
  43. end
  44. it "should provide the since attribute after the first run" do
  45. time = (Time.now-1.minute).iso8601
  46. @checker.memory[:last_run] = time
  47. @checker.save
  48. @checker.reload.send(:request_url).should == "https://basecamp.com/12345/api/v1/projects/6789/events.json?since=#{time}"
  49. end
  50. end
  51. describe "#check" do
  52. it "should not emit events on its first run" do
  53. expect { @checker.check }.to change { Event.count }.by(0)
  54. end
  55. it "should check that initial run creates an event" do
  56. @checker.last_check_at = Time.now - 1.minute
  57. expect { @checker.check }.to change { Event.count }.by(1)
  58. end
  59. end
  60. describe "#working?" do
  61. it "it is working when at least one event was emited" do
  62. @checker.should_not be_working
  63. @checker.last_check_at = Time.now - 1.minute
  64. @checker.check
  65. @checker.reload.should be_working
  66. end
  67. end
  68. end