1
0

twitter_user_agent_spec.rb 3.0 KB

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