wunderlist_agent_spec.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. require 'rails_helper'
  2. require 'models/concerns/oauthable'
  3. describe Agents::WunderlistAgent do
  4. it_behaves_like Oauthable
  5. before(:each) do
  6. @valid_params = {
  7. 'list_id' => '12345',
  8. 'title' => '{{title}}: {{url}}',
  9. }
  10. @checker = Agents::WunderlistAgent.new(:name => "somename", :options => @valid_params)
  11. @checker.user = users(:jane)
  12. @checker.service = services(:generic)
  13. @checker.save!
  14. @event = Event.new
  15. @event.agent = agents(:bob_weather_agent)
  16. @event.payload = { title: 'hello', url: 'www.example.com'}
  17. @event.save!
  18. end
  19. describe "validating" do
  20. before do
  21. expect(@checker).to be_valid
  22. end
  23. it "should require the title" do
  24. @checker.options['title'] = nil
  25. expect(@checker).not_to be_valid
  26. end
  27. it "should require the list_id" do
  28. @checker.options['list_id'] = nil
  29. expect(@checker).not_to be_valid
  30. end
  31. end
  32. it "should generate the request_options" do
  33. expect(@checker.send(:request_options)).to eq({:headers=>{"Content-Type"=>"application/json", "User-Agent"=>"Huginn - https://github.com/huginn/huginn", "X-Access-Token"=>"1234token", "X-Client-ID"=>"wunderoauthkey"}})
  34. end
  35. describe "#complete_list_id" do
  36. it "should return a array of hashes" do
  37. stub_request(:get, 'https://a.wunderlist.com/api/v1/lists').to_return(
  38. :body => JSON.dump([{title: 'test', id: 12345}]),
  39. :headers => {"Content-Type" => "text/json"}
  40. )
  41. expect(@checker.complete_list_id).to eq([{:text=>"test (12345)", :id=>12345}])
  42. end
  43. end
  44. describe "#receive" do
  45. it "send a message to the hipchat" do
  46. stub_request(:post, 'https://a.wunderlist.com/api/v1/tasks')
  47. @checker.receive([@event])
  48. expect(WebMock).to have_requested(:post, "https://a.wunderlist.com/api/v1/tasks")
  49. end
  50. end
  51. describe "#working?" do
  52. it "should be working with no entry in the error log" do
  53. expect(@checker).to be_working
  54. end
  55. it "should not be working with a recent entry in the error log" do
  56. @checker.error("test")
  57. @checker.reload
  58. @checker.last_event_at = Time.now
  59. expect(@checker).to_not be_working
  60. end
  61. end
  62. end