twitter_test.rb 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. # encoding: utf-8
  2. require 'integration_test_helper'
  3. class TwitterTest < ActiveSupport::TestCase
  4. # set system mode to done / to activate
  5. Setting.set('system_init_done', true)
  6. # needed to check correct behavior
  7. Group.create_if_not_exists(
  8. id: 2,
  9. name: 'Twitter',
  10. note: 'All Tweets.',
  11. updated_by_id: 1,
  12. created_by_id: 1
  13. )
  14. # app config
  15. if !ENV['TWITTER_CONSUMER_KEY']
  16. raise "ERROR: Need TWITTER_CONSUMER_KEY - hint TWITTER_CONSUMER_KEY='1234'"
  17. end
  18. if !ENV['TWITTER_CONSUMER_SECRET']
  19. raise "ERROR: Need TWITTER_CONSUMER_SECRET - hint TWITTER_CONSUMER_SECRET='1234'"
  20. end
  21. consumer_key = ENV['TWITTER_CONSUMER_KEY']
  22. consumer_secret = ENV['TWITTER_CONSUMER_SECRET']
  23. # armin_theo (is system and is following marion_bauer)
  24. if !ENV['TWITTER_SYSTEM_LOGIN']
  25. raise "ERROR: Need TWITTER_SYSTEM_LOGIN - hint TWITTER_SYSTEM_LOGIN='@system'"
  26. end
  27. if !ENV['TWITTER_SYSTEM_TOKEN']
  28. raise "ERROR: Need TWITTER_SYSTEM_TOKEN - hint TWITTER_SYSTEM_TOKEN='1234'"
  29. end
  30. if !ENV['TWITTER_SYSTEM_TOKEN_SECRET']
  31. raise "ERROR: Need TWITTER_SYSTEM_TOKEN_SECRET - hint TWITTER_SYSTEM_TOKEN_SECRET='1234'"
  32. end
  33. system_login = ENV['TWITTER_SYSTEM_LOGIN']
  34. system_login_without_at = system_login[1, system_login.length]
  35. system_token = ENV['TWITTER_SYSTEM_TOKEN']
  36. system_token_secret = ENV['TWITTER_SYSTEM_TOKEN_SECRET']
  37. # me_bauer (is customer and is following armin_theo)
  38. if !ENV['TWITTER_CUSTOMER_LOGIN']
  39. raise "ERROR: Need CUSTOMER_LOGIN - hint TWITTER_CUSTOMER_LOGIN='@customer'"
  40. end
  41. if !ENV['TWITTER_CUSTOMER_TOKEN']
  42. raise "ERROR: Need CUSTOMER_TOKEN - hint TWITTER_CUSTOMER_TOKEN='1234'"
  43. end
  44. if !ENV['TWITTER_CUSTOMER_TOKEN_SECRET']
  45. raise "ERROR: Need CUSTOMER_TOKEN_SECRET - hint TWITTER_CUSTOMER_TOKEN_SECRET='1234'"
  46. end
  47. customer_login = ENV['TWITTER_CUSTOMER_LOGIN']
  48. customer_token = ENV['TWITTER_CUSTOMER_TOKEN']
  49. customer_token_secret = ENV['TWITTER_CUSTOMER_TOKEN_SECRET']
  50. # add channel
  51. current = Channel.where(area: 'Twitter::Account')
  52. current.each(&:destroy)
  53. channel = Channel.create(
  54. area: 'Twitter::Account',
  55. options: {
  56. adapter: 'twitter',
  57. auth: {
  58. consumer_key: consumer_key,
  59. consumer_secret: consumer_secret,
  60. oauth_token: system_token,
  61. oauth_token_secret: system_token_secret,
  62. },
  63. user: {
  64. screen_name: system_login,
  65. id: '1234',
  66. },
  67. sync: {
  68. search: [
  69. {
  70. term: '#citheo42',
  71. group_id: 2,
  72. },
  73. {
  74. term: '#zarepl24',
  75. group_id: 1,
  76. },
  77. ],
  78. mentions: {
  79. group_id: 2,
  80. },
  81. direct_messages: {
  82. group_id: 2,
  83. }
  84. }
  85. },
  86. active: true,
  87. created_by_id: 1,
  88. updated_by_id: 1,
  89. )
  90. test 'a new outbound and reply' do
  91. hash = '#citheo42' + rand(999_999).to_s
  92. user = User.find(2)
  93. text = "Today the weather is really #{rand_word}... #{hash}"
  94. ticket = Ticket.create(
  95. title: text[0, 40],
  96. customer_id: user.id,
  97. group_id: 2,
  98. state: Ticket::State.find_by(name: 'new'),
  99. priority: Ticket::Priority.find_by(name: '2 normal'),
  100. preferences: {
  101. channel_id: channel.id,
  102. },
  103. updated_by_id: 1,
  104. created_by_id: 1,
  105. )
  106. assert(ticket, "outbound ticket created, text: #{text}")
  107. article = Ticket::Article.create(
  108. ticket_id: ticket.id,
  109. body: text,
  110. type: Ticket::Article::Type.find_by(name: 'twitter status'),
  111. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  112. internal: false,
  113. updated_by_id: 1,
  114. created_by_id: 1,
  115. )
  116. assert(article, "outbound article created, text: #{text}")
  117. assert_equal(system_login, article.from, 'ticket article from')
  118. assert_equal('', article.to, 'ticket article to')
  119. # reply by me_bauer
  120. client = Twitter::REST::Client.new do |config|
  121. config.consumer_key = consumer_key
  122. config.consumer_secret = consumer_secret
  123. config.access_token = customer_token
  124. config.access_token_secret = customer_token_secret
  125. end
  126. tweet_found = false
  127. client.user_timeline(system_login_without_at).each { |tweet|
  128. next if tweet.id.to_s != article.message_id.to_s
  129. tweet_found = true
  130. break
  131. }
  132. assert(tweet_found, "found outbound '#{text}' tweet '#{article.message_id}'")
  133. reply_text = "#{system_login} on my side the weather is nice, too! 😍😍😍 #weather#{rand(999_999)}"
  134. tweet = client.update(
  135. reply_text,
  136. {
  137. in_reply_to_status_id: article.message_id
  138. }
  139. )
  140. # fetch check system account
  141. sleep 10
  142. article = nil
  143. (1..2).each {
  144. Channel.fetch
  145. # check if follow up article has been created
  146. article = Ticket::Article.find_by(message_id: tweet.id)
  147. break if article
  148. sleep 10
  149. }
  150. assert(article, "article tweet '#{tweet.id}' imported")
  151. assert_equal(customer_login, article.from, 'ticket article from')
  152. assert_equal(system_login, article.to, 'ticket article to')
  153. assert_equal(tweet.id.to_s, article.message_id, 'ticket article inbound message_id')
  154. assert_equal(2, article.ticket.articles.count, 'ticket article inbound count')
  155. assert_equal(reply_text.utf8_to_3bytesutf8, ticket.articles.last.body, 'ticket article inbound body')
  156. channel = Channel.find(channel.id)
  157. assert_equal('', channel.last_log_out)
  158. assert_equal('ok', channel.status_out)
  159. assert_equal('', channel.last_log_in)
  160. assert_equal('ok', channel.status_in)
  161. end
  162. test 'b new inbound and reply' do
  163. # new tweet by me_bauer
  164. client = Twitter::REST::Client.new do |config|
  165. config.consumer_key = consumer_key
  166. config.consumer_secret = consumer_secret
  167. config.access_token = customer_token
  168. config.access_token_secret = customer_token_secret
  169. end
  170. hash = "#zarepl24 ##{hash_gen}"
  171. text = "Today #{rand_word}... #{hash}"
  172. tweet = client.update(
  173. text,
  174. )
  175. # fetch check system account
  176. sleep 20
  177. article = nil
  178. (1..3).each {
  179. Channel.fetch
  180. # check if ticket and article has been created
  181. article = Ticket::Article.find_by(message_id: tweet.id)
  182. break if article
  183. sleep 20
  184. }
  185. assert(article, "Can't find tweet id #{tweet.id}/#{text}")
  186. assert_equal(customer_login, article.from, 'ticket article from')
  187. assert_equal(nil, article.to, 'ticket article to')
  188. ticket = article.ticket
  189. # send reply
  190. reply_text = "#{customer_login} on my side #weather#{hash_gen}"
  191. article = Ticket::Article.create(
  192. ticket_id: ticket.id,
  193. body: reply_text,
  194. type: Ticket::Article::Type.find_by(name: 'twitter status'),
  195. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  196. internal: false,
  197. updated_by_id: 1,
  198. created_by_id: 1,
  199. )
  200. assert(article, "outbound article created, text: #{reply_text}")
  201. assert_equal(system_login, article.from, 'ticket article from')
  202. assert_equal(customer_login, article.to, 'ticket article to')
  203. sleep 5
  204. tweet_found = false
  205. client.user_timeline(system_login_without_at).each { |local_tweet|
  206. sleep 10
  207. next if local_tweet.id.to_s != article.message_id.to_s
  208. tweet_found = true
  209. break
  210. }
  211. assert(tweet_found, "found outbound '#{reply_text}' tweet '#{article.message_id}'")
  212. channel = Channel.find(channel.id)
  213. assert_equal('', channel.last_log_out)
  214. assert_equal('ok', channel.status_out)
  215. assert_equal('', channel.last_log_in)
  216. assert_equal('ok', channel.status_in)
  217. end
  218. test 'c new by direct message inbound' do
  219. # cleanup direct messages of system
  220. client = Twitter::REST::Client.new do |config|
  221. config.consumer_key = consumer_key
  222. config.consumer_secret = consumer_secret
  223. config.access_token = system_token
  224. config.access_token_secret = system_token_secret
  225. end
  226. dms = client.direct_messages(count: 100)
  227. dms.each {|dm|
  228. client.destroy_direct_message(dm.id)
  229. }
  230. client = Twitter::REST::Client.new(
  231. consumer_key: consumer_key,
  232. consumer_secret: consumer_secret,
  233. access_token: customer_token,
  234. access_token_secret: customer_token_secret
  235. )
  236. dms = client.direct_messages(count: 100)
  237. dms.each {|dm|
  238. client.destroy_direct_message(dm.id)
  239. }
  240. hash = "#citheo44 #{hash_gen}"
  241. text = "How about #{rand_word} the details? #{hash}"
  242. dm = client.create_direct_message(
  243. system_login_without_at,
  244. text,
  245. )
  246. assert(dm, "dm with ##{hash} created")
  247. # fetch check system account
  248. sleep 15
  249. article = nil
  250. (1..2).each {
  251. Channel.fetch
  252. # check if ticket and article has been created
  253. article = Ticket::Article.find_by(message_id: dm.id)
  254. break if article
  255. sleep 10
  256. }
  257. assert(article, "inbound article '#{text}' created")
  258. assert_equal(customer_login, article.from, 'ticket article from')
  259. assert_equal(system_login, article.to, 'ticket article to')
  260. ticket = article.ticket
  261. assert(ticket, 'ticket of inbound article exists')
  262. assert(ticket.articles, 'ticket.articles exists')
  263. assert_equal(1, ticket.articles.count, 'ticket article inbound count')
  264. assert_equal(ticket.state.name, 'new')
  265. # reply via ticket
  266. outbound_article = Ticket::Article.create(
  267. ticket_id: ticket.id,
  268. to: customer_login,
  269. body: "Will call you later #{rand_word}!",
  270. type: Ticket::Article::Type.find_by(name: 'twitter direct-message'),
  271. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  272. internal: false,
  273. updated_by_id: 1,
  274. created_by_id: 1,
  275. )
  276. assert(outbound_article, 'outbound article created')
  277. assert_equal(2, outbound_article.ticket.articles.count, 'ticket article outbound count')
  278. assert_equal(system_login, outbound_article.from, 'ticket article from')
  279. assert_equal(customer_login, outbound_article.to, 'ticket article to')
  280. ticket.state = Ticket::State.find_by(name: 'pending reminder')
  281. ticket.save
  282. text = "#{rand_word}. #{hash}"
  283. dm = client.create_direct_message(
  284. system_login_without_at,
  285. text,
  286. )
  287. assert(dm, "second dm with ##{hash} created")
  288. # fetch check system account
  289. sleep 15
  290. article = nil
  291. (1..2).each {
  292. Channel.fetch
  293. # check if ticket and article has been created
  294. article = Ticket::Article.find_by(message_id: dm.id)
  295. break if article
  296. sleep 10
  297. }
  298. assert(article, "inbound article '#{text}' created")
  299. assert_equal(customer_login, article.from, 'ticket article inbound from')
  300. assert_equal(system_login, article.to, 'ticket article inbound to')
  301. assert_equal(article.ticket.id, ticket.id, 'still the same ticket')
  302. ticket = article.ticket
  303. assert(ticket, 'ticket of inbound article exists')
  304. assert(ticket.articles, 'ticket.articles exists')
  305. assert_equal(3, ticket.articles.count, 'ticket article inbound count')
  306. assert_equal(ticket.state.name, 'open')
  307. # close dm ticket, next dm should open a new
  308. ticket.state = Ticket::State.find_by(name: 'closed')
  309. ticket.save
  310. text = "Thanks #{rand_word} for your call. I just have one question. #{hash}"
  311. dm = client.create_direct_message(
  312. system_login_without_at,
  313. text,
  314. )
  315. assert(dm, "third dm with ##{hash} created")
  316. # fetch check system account
  317. sleep 15
  318. article = nil
  319. (1..2).each {
  320. Channel.fetch
  321. # check if ticket and article has been created
  322. article = Ticket::Article.find_by(message_id: dm.id)
  323. break if article
  324. sleep 15
  325. }
  326. assert(article, "inbound article '#{text}' created with dm id #{dm.id}")
  327. assert_equal(customer_login, article.from, 'ticket article inbound from')
  328. assert_equal(system_login, article.to, 'ticket article inbound to')
  329. ticket = article.ticket
  330. assert(ticket, 'ticket of inbound article exists')
  331. assert(ticket.articles, 'ticket.articles exists')
  332. assert_equal(1, ticket.articles.count, 'ticket article inbound count')
  333. assert_equal(ticket.state.name, 'new')
  334. channel = Channel.find(channel.id)
  335. assert_equal('', channel.last_log_out)
  336. assert_equal('ok', channel.status_out)
  337. assert_equal('', channel.last_log_in)
  338. assert_equal('ok', channel.status_in)
  339. end
  340. test 'd streaming test' do
  341. Thread.new {
  342. Channel.stream
  343. }
  344. sleep 10
  345. # new tweet I - by me_bauer
  346. client = Twitter::REST::Client.new do |config|
  347. config.consumer_key = consumer_key
  348. config.consumer_secret = consumer_secret
  349. config.access_token = customer_token
  350. config.access_token_secret = customer_token_secret
  351. end
  352. hash = '#zarepl24 #' + hash_gen
  353. text = "Today... #{rand_word} #{hash}"
  354. tweet = client.update(
  355. text,
  356. )
  357. sleep 10
  358. article = nil
  359. (1..2).each {
  360. article = Ticket::Article.find_by(message_id: tweet.id)
  361. break if article
  362. sleep 15
  363. }
  364. assert(article)
  365. assert_equal(customer_login, article.from, 'ticket article from')
  366. assert_equal(nil, article.to, 'ticket article to')
  367. # new tweet II - by me_bauer
  368. client = Twitter::REST::Client.new do |config|
  369. config.consumer_key = consumer_key
  370. config.consumer_secret = consumer_secret
  371. config.access_token = customer_token
  372. config.access_token_secret = customer_token_secret
  373. end
  374. hash = '#zarepl24 #' + rand(999_999).to_s
  375. text = "Today... #{rand_word} #{hash}"
  376. tweet = client.update(
  377. text,
  378. )
  379. ActiveRecord::Base.connection.reconnect!
  380. sleep 10
  381. article = nil
  382. (1..2).each {
  383. article = Ticket::Article.find_by(message_id: tweet.id)
  384. break if article
  385. sleep 15
  386. }
  387. assert(article)
  388. assert_equal(customer_login, article.from, 'ticket article from')
  389. assert_equal(nil, article.to, 'ticket article to')
  390. # get dm via stream
  391. client = Twitter::REST::Client.new(
  392. consumer_key: consumer_key,
  393. consumer_secret: consumer_secret,
  394. access_token: customer_token,
  395. access_token_secret: customer_token_secret
  396. )
  397. hash = '#citheo44' + rand(999_999).to_s
  398. text = "How about the #{rand_word}? " + hash
  399. dm = client.create_direct_message(
  400. system_login_without_at,
  401. text,
  402. )
  403. assert(dm, "dm with ##{hash} created")
  404. #ActiveRecord::Base.connection.reconnect!
  405. sleep 10
  406. article = nil
  407. (1..2).each {
  408. article = Ticket::Article.find_by(message_id: dm.id)
  409. break if article
  410. sleep 10
  411. }
  412. assert(article, "inbound article '#{text}' created")
  413. assert_equal(customer_login, article.from, 'ticket article from')
  414. assert_equal(system_login, article.to, 'ticket article to')
  415. end
  416. def hash_gen
  417. rand(999).to_s + (0...10).map { ('a'..'z').to_a[rand(26)] }.join
  418. end
  419. def rand_word
  420. words = [
  421. 'dog',
  422. 'cat',
  423. 'house',
  424. 'home',
  425. 'yesterday',
  426. 'tomorrow',
  427. 'new york',
  428. 'berlin',
  429. 'coffee script',
  430. 'java script',
  431. 'bob smith',
  432. 'be open',
  433. 'really nice',
  434. 'stay tuned',
  435. 'be a good boy',
  436. 'invent new things',
  437. ]
  438. words[rand(words.length)]
  439. end
  440. end