twitter_test.rb 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. # encoding: utf-8
  2. # rubocop:disable UselessAssignment
  3. require 'integration_test_helper'
  4. class TwitterTest < ActiveSupport::TestCase
  5. # set system mode to done / to activate
  6. Setting.set('system_init_done', true)
  7. # needed to check correct behavior
  8. Group.create_if_not_exists(
  9. id: 2,
  10. name: 'Twitter',
  11. note: 'All Tweets.',
  12. updated_by_id: 1,
  13. created_by_id: 1
  14. )
  15. # app config
  16. consumer_key = 'd2zoZBmMXmT7KLPgEHSzpw'
  17. consumer_secret = 'QMUrlyDlqjITCkWdrOgsTxMVVLxr4A4IW3DIgtIg'
  18. # user1: armin_theo (is system and is following marion_bauer)
  19. user1_token = '1405469528-WQ6XHizgrbYasUwjp0I0TUONhftNkrfrpgFLrdc'
  20. user1_token_secret = '0LHChGFlQx9jSxM8tkBsuDOMhbJMSXTL2zKJJO5Xk'
  21. # user2: me_bauer (is following armin_theo)
  22. user2_token = '1406098795-XQTjg1Zj5uVW0C11NNpNA4xopyclRJJoriWis0I'
  23. user2_token_secret = 'T8ph5afeSDjGDA9X1ZBlzEvoSiXfN266ZZUMj5UaY'
  24. # add channel
  25. current = Channel.where( adapter: 'Twitter2' )
  26. current.each(&:destroy)
  27. Channel.create(
  28. adapter: 'Twitter2',
  29. area: 'Twitter::Inbound',
  30. options: {
  31. consumer_key: consumer_key,
  32. consumer_secret: consumer_secret,
  33. oauth_token: user1_token,
  34. oauth_token_secret: user1_token_secret,
  35. search: [
  36. {
  37. item: '#citheo42',
  38. group: 'Twitter',
  39. },
  40. {
  41. item: '#citheo24',
  42. group: 'Users',
  43. },
  44. ],
  45. mentions: {
  46. group: 'Twitter',
  47. },
  48. direct_messages: {
  49. group: 'Twitter',
  50. }
  51. },
  52. active: true,
  53. created_by_id: 1,
  54. updated_by_id: 1,
  55. )
  56. test 'new outbound and reply' do
  57. user = User.find(2)
  58. group = Group.where( name: 'Twitter' ).first
  59. state = Ticket::State.where( name: 'new' ).first
  60. priority = Ticket::Priority.where( name: '2 normal' ).first
  61. hash = '#citheo42' + rand(9999).to_s
  62. text = 'Today the weather is really nice... ' + hash
  63. ticket = Ticket.create(
  64. group_id: group.id,
  65. customer_id: user.id,
  66. title: text[0, 40],
  67. state_id: state.id,
  68. priority_id: priority.id,
  69. updated_by_id: 1,
  70. created_by_id: 1,
  71. )
  72. assert( ticket, 'outbound ticket created' )
  73. article = Ticket::Article.create(
  74. ticket_id: ticket.id,
  75. type_id: Ticket::Article::Type.where( name: 'twitter status' ).first.id,
  76. sender_id: Ticket::Article::Sender.where( name: 'Agent' ).first.id,
  77. body: text,
  78. #:from => sender.name,
  79. #:to => to,
  80. #:message_id => tweet.id,
  81. internal: false,
  82. updated_by_id: 1,
  83. created_by_id: 1,
  84. )
  85. assert( article, 'outbound article created' )
  86. assert_equal( article.ticket.articles.count, 1 )
  87. sleep 10
  88. # reply by me_bauer
  89. client = Twitter::REST::Client.new do |config|
  90. config.consumer_key = consumer_key
  91. config.consumer_secret = consumer_secret
  92. config.access_token = user2_token
  93. config.access_token_secret = user2_token_secret
  94. end
  95. client.search(hash, count: 50, result_type: 'recent').collect do |tweet|
  96. assert_equal( tweet.id, article.message_id )
  97. end
  98. reply_hash = '#weather' + rand(9999).to_s
  99. reply_text = '@armin_theo on my side the weather is also nice! 😍😍😍 ' + reply_hash
  100. tweet = client.update(
  101. reply_text,
  102. {
  103. in_reply_to_status_id: article.message_id
  104. }
  105. )
  106. sleep 10
  107. # fetch check system account
  108. Channel.fetch
  109. # check if follow up article has been created
  110. assert_equal( article.ticket.articles.count, 2 )
  111. reply_article = article.ticket.articles.last
  112. assert_equal( reply_article.body, reply_text.utf8_to_3bytesutf8 )
  113. end
  114. test 'new by direct message inbound' do
  115. # cleanup direct messages of system
  116. client = Twitter::REST::Client.new do |config|
  117. config.consumer_key = consumer_key
  118. config.consumer_secret = consumer_secret
  119. config.access_token = user1_token
  120. config.access_token_secret = user1_token_secret
  121. end
  122. dms = client.direct_messages( count: 200 )
  123. dms.each {|dm|
  124. client.destroy_direct_message(dm.id)
  125. }
  126. # direct message to @armin_theo
  127. client = Twitter::REST::Client.new(
  128. consumer_key: consumer_key,
  129. consumer_secret: consumer_secret,
  130. access_token: user2_token,
  131. access_token_secret: user2_token_secret
  132. )
  133. dms = client.direct_messages( count: 200 )
  134. dms.each {|dm|
  135. client.destroy_direct_message(dm.id)
  136. }
  137. sleep 10
  138. hash = '#citheo44' + rand(9999).to_s
  139. text = 'How about the details? ' + hash
  140. dm = client.create_direct_message(
  141. 'armin_theo',
  142. text,
  143. )
  144. assert( dm, "dm with ##{hash} created" )
  145. # fetch check system account
  146. article = nil
  147. (1..4).each {
  148. next if article
  149. sleep 25
  150. Channel.fetch
  151. # check if ticket and article has been created
  152. article = Ticket::Article.where( message_id: dm.id ).last
  153. }
  154. puts '----------------------------------------'
  155. puts 'DM: ' + dm.inspect
  156. puts 'AT: ' + article.inspect
  157. puts '----------------------------------------'
  158. assert( article, 'inbound article created' )
  159. #ticket = Ticket.find( article.ticket.id )
  160. ticket = article.ticket
  161. assert( ticket, 'ticket of inbound article exists' )
  162. assert( ticket.articles, 'ticket.articles exists' )
  163. article_count = ticket.articles.count
  164. assert( article_count )
  165. #assert_equal( ticket.state.name, 'new' )
  166. # reply via ticket
  167. outbound_article = Ticket::Article.create(
  168. ticket_id: ticket.id,
  169. type_id: Ticket::Article::Type.where( name: 'twitter direct-message' ).first.id,
  170. sender_id: Ticket::Article::Sender.where( name: 'Agent' ).first.id,
  171. body: text,
  172. #:from => sender.name,
  173. to: 'me_bauer',
  174. internal: false,
  175. updated_by_id: 1,
  176. created_by_id: 1,
  177. )
  178. assert( outbound_article, 'outbound article created' )
  179. assert_equal( outbound_article.ticket.articles.count, article_count + 1 )
  180. sleep 10
  181. end
  182. end