basecamp_agent_spec.rb 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 request options hash" do
  39. @checker.send(:request_options).should == {:basic_auth=>{:username=>"user", :password=>"pass"}, :headers => {"User-Agent" => "Huginn (https://github.com/cantino/huginn)"}}
  40. end
  41. it "should generate the currect request url" do
  42. @checker.send(:request_url).should == "https://basecamp.com/12345/api/v1/projects/6789/events.json"
  43. end
  44. it "should not provide the since attribute on first run" do
  45. @checker.send(:query_parameters).should == {}
  46. end
  47. it "should provide the since attribute after the first run" do
  48. time = (Time.now-1.minute).iso8601
  49. @checker.memory[:last_run] = time
  50. @checker.save
  51. @checker.reload.send(:query_parameters).should == {:query => {:since => time}}
  52. end
  53. end
  54. describe "#check" do
  55. it "should not emit events on its first run" do
  56. expect { @checker.check }.to change { Event.count }.by(0)
  57. end
  58. it "should check that initial run creates an event" do
  59. @checker.last_check_at = Time.now - 1.minute
  60. expect { @checker.check }.to change { Event.count }.by(1)
  61. end
  62. end
  63. describe "#working?" do
  64. it "it is working when at least one event was emited" do
  65. @checker.should_not be_working
  66. @checker.last_check_at = Time.now - 1.minute
  67. @checker.check
  68. @checker.reload.should be_working
  69. end
  70. end
  71. end