1
0

twitter_user_agent_spec.rb 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. Event.last(5).each_cons(2) do |t1, t2|
  29. expect(t1.payload[:id]).to be < t2.payload[:id]
  30. end
  31. end
  32. end
  33. describe "#check with starting_at=future date" do
  34. it "should check for changes starting_at a future date, thus not find any" do
  35. opts = @opts.merge({ starting_at: "Jan 01 00:00:01 +0000 2999", })
  36. checker = Agents::TwitterUserAgent.new(name: "tectonic", options: opts)
  37. checker.service = services(:generic)
  38. checker.user = users(:bob)
  39. checker.save!
  40. expect { checker.check }.to change { Event.count }.by(0)
  41. end
  42. end
  43. describe "#check that if choose time line is false then username is required" do
  44. before do
  45. 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(
  46. body: File.read(Rails.root.join("spec/data_fixtures/user_tweets.json")), status: 200
  47. )
  48. end
  49. it 'requires username unless choose_home_time_line is true' do
  50. expect(@checker).to be_valid
  51. @checker.options['username'] = nil
  52. expect(@checker).to_not be_valid
  53. @checker.options['choose_home_time_line'] = 'true'
  54. expect(@checker).to be_valid
  55. end
  56. context "when choose_home_time_line is true" do
  57. before do
  58. @checker.options['choose_home_time_line'] = true
  59. @checker.options.delete('username')
  60. @checker.save!
  61. end
  62. end
  63. it "error messaged added if choose_home_time_line is false and username does not exist" do
  64. opts = @opts.tap { |o| o.delete(:username) }.merge!({ choose_home_time_line: "false" })
  65. checker = Agents::TwitterUserAgent.new(name: "tectonic", options: opts)
  66. checker.service = services(:generic)
  67. checker.user = users(:bob)
  68. expect(checker.save).to eq false
  69. expect(checker.errors.full_messages.first).to eq("username is required")
  70. end
  71. end
  72. end