twitter_test.rb 6.0 KB

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