channel_spec.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. require 'rails_helper'
  2. RSpec.describe Channel do
  3. describe '#fetch', use_vcr: :with_oauth_headers do
  4. context 'for Twitter driver' do
  5. context 'with valid token' do
  6. subject(:twitter_channel) { create(:twitter_channel) }
  7. # travel back in time when VCR was recorded
  8. before { travel_to '2020-02-06 13:37 +0100' }
  9. it 'returns true' do
  10. expect(twitter_channel.fetch(true)).to be(true)
  11. end
  12. it 'sets successful status attributes' do
  13. expect { twitter_channel.fetch(true) }
  14. .to change { twitter_channel.reload.attributes }
  15. .to hash_including(
  16. 'status_in' => 'ok',
  17. 'last_log_in' => '',
  18. 'status_out' => nil,
  19. 'last_log_out' => nil
  20. )
  21. end
  22. it 'adds tickets based on config parameters (mention/DM/search)' do
  23. expect { twitter_channel.fetch(true) }
  24. .to change(Ticket, :count).by(21)
  25. expect(Ticket.last.attributes).to include(
  26. 'title' => 'RT @BarackObama: Kobe was a legend on the court and just getting started in what...',
  27. 'preferences' => { 'channel_id' => twitter_channel.id,
  28. 'channel_screen_name' => twitter_channel.options[:user][:screen_name] },
  29. 'customer_id' => User.find_by(firstname: 'Zammad', lastname: 'Ali').id
  30. )
  31. end
  32. context 'and legacy "import_older_tweets" option' do
  33. subject(:twitter_channel) { create(:twitter_channel, :legacy) }
  34. it 'adds tickets based on config parameters (mention/DM/search)' do
  35. expect { twitter_channel.fetch(true) }
  36. .to change(Ticket, :count).by(26)
  37. expect(Ticket.last.attributes).to include(
  38. 'title' => 'Wir haben unsere DMs deaktiviert. ' \
  39. 'Leider können wir dank der neuen Twitter API k...',
  40. 'preferences' => { 'channel_id' => twitter_channel.id,
  41. 'channel_screen_name' => twitter_channel.options[:user][:screen_name] },
  42. 'customer_id' => User.find_by(firstname: 'Ccc', lastname: 'Event Logistics').id
  43. )
  44. end
  45. end
  46. end
  47. context 'with invalid token' do
  48. subject(:twitter_channel) { create(:twitter_channel, :invalid) }
  49. it 'returns false' do
  50. expect(twitter_channel.fetch(true)).to be(false)
  51. end
  52. it 'sets error/nil status attributes' do
  53. expect { twitter_channel.fetch(true) }
  54. .to change { twitter_channel.reload.attributes }
  55. .to hash_including(
  56. 'status_in' => 'error',
  57. 'last_log_in' => "Can't use Channel::Driver::Twitter: " \
  58. '#<Twitter::Error::Unauthorized: Invalid or expired token.>',
  59. 'status_out' => nil,
  60. 'last_log_out' => nil
  61. )
  62. end
  63. end
  64. end
  65. end
  66. end