twitter_user_agent_spec.rb 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. require 'rails_helper'
  2. describe Agents::TwitterUserAgent do
  3. before do
  4. # intercept the twitter API request for @tectonic's user profile
  5. stub_request(:any, "https://api.twitter.com/1.1/statuses/user_timeline.json?contributor_details=true&count=200&exclude_replies=false&include_entities=true&include_rts=true&screen_name=tectonic").to_return(:body => File.read(Rails.root.join("spec/data_fixtures/user_tweets.json")), :status => 200)
  6. @opts = {
  7. :username => "tectonic",
  8. :include_retweets => "true",
  9. :exclude_replies => "false",
  10. :expected_update_period_in_days => "2",
  11. :starting_at => "Jan 01 00:00:01 +0000 2000",
  12. :consumer_key => "---",
  13. :consumer_secret => "---",
  14. :oauth_token => "---",
  15. :oauth_token_secret => "---"
  16. }
  17. @checker = Agents::TwitterUserAgent.new(:name => "tectonic", :options => @opts)
  18. @checker.service = services(:generic)
  19. @checker.user = users(:bob)
  20. @checker.save!
  21. end
  22. describe "#check" do
  23. it "should check for changes" do
  24. expect { @checker.check }.to change { Event.count }.by(5)
  25. end
  26. end
  27. describe "#check with starting_at=future date" do
  28. it "should check for changes starting_at a future date, thus not find any" do
  29. opts = @opts.merge({ :starting_at => "Jan 01 00:00:01 +0000 2999", })
  30. checker = Agents::TwitterUserAgent.new(:name => "tectonic", :options => opts)
  31. checker.service = services(:generic)
  32. checker.user = users(:bob)
  33. checker.save!
  34. expect { checker.check }.to change { Event.count }.by(0)
  35. end
  36. end
  37. describe "#check that if choose time line is false then username is required" do
  38. before do
  39. stub_request(:any, "https://api.twitter.com/1.1/statuses/home_timeline.json?contributor_details=true&count=200&exclude_replies=false&include_entities=true&include_rts=true").to_return(:body => File.read(Rails.root.join("spec/data_fixtures/user_tweets.json")), :status => 200)
  40. end
  41. it 'requires username unless choose_home_time_line is true' do
  42. expect(@checker).to be_valid
  43. @checker.options['username'] = nil
  44. expect(@checker).to_not be_valid
  45. @checker.options['choose_home_time_line'] = 'true'
  46. expect(@checker).to be_valid
  47. end
  48. context "when choose_home_time_line is true" do
  49. before do
  50. @checker.options['choose_home_time_line'] = true
  51. @checker.options.delete('username')
  52. @checker.save!
  53. end
  54. end
  55. it "error messaged added if choose_home_time_line is false and username does not exist" do
  56. opts = @opts.tap { |o| o.delete(:username) }.merge!({:choose_home_time_line => "false" })
  57. checker = Agents::TwitterUserAgent.new(:name => "tectonic", :options => opts)
  58. checker.service = services(:generic)
  59. checker.user = users(:bob)
  60. expect(checker.save).to eq false
  61. expect(checker.errors.full_messages.first).to eq("username is required")
  62. end
  63. end
  64. end