twitter_test.rb 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. require 'integration_test_helper'
  2. class TwitterTest < ActiveSupport::TestCase
  3. self.test_order = :sorted
  4. self.use_transactional_tests = false
  5. # set system mode to done / to activate
  6. Setting.set('system_init_done', true)
  7. # needed to check correct behavior
  8. group = Group.create_if_not_exists(
  9. name: 'Twitter',
  10. note: 'All Tweets.',
  11. updated_by_id: 1,
  12. created_by_id: 1
  13. )
  14. {
  15. 'TWITTER_CONSUMER_KEY' => '1234',
  16. 'TWITTER_CONSUMER_SECRET' => '1234',
  17. 'TWITTER_SYSTEM_LOGIN' => '@system',
  18. 'TWITTER_SYSTEM_ID' => '1405469528',
  19. 'TWITTER_SYSTEM_TOKEN' => '1234',
  20. 'TWITTER_SYSTEM_TOKEN_SECRET' => '1234',
  21. 'TWITTER_CUSTOMER_LOGIN' => '@customer',
  22. 'TWITTER_CUSTOMER_TOKEN' => '1234',
  23. 'TWITTER_CUSTOMER_TOKEN_SECRET' => '1234',
  24. }.each do |key, example_value|
  25. next if ENV[key]
  26. raise "ERROR: Need ENV #{key} - hint: export #{key}='#{example_value}'"
  27. end
  28. # app config
  29. consumer_key = ENV['TWITTER_CONSUMER_KEY']
  30. consumer_secret = ENV['TWITTER_CONSUMER_SECRET']
  31. # armin_theo (is system and is following marion_bauer)
  32. system_login = ENV['TWITTER_SYSTEM_LOGIN']
  33. system_id = ENV['TWITTER_SYSTEM_ID']
  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. hash_tag1 = "#zarepl#{rand(999)}"
  38. hash_tag2 = "#citheo#{rand(999)}"
  39. # me_bauer (is customer and is following armin_theo)
  40. customer_login = ENV['TWITTER_CUSTOMER_LOGIN']
  41. customer_login_without_at = customer_login[1, customer_login.length]
  42. customer_token = ENV['TWITTER_CUSTOMER_TOKEN']
  43. customer_token_secret = ENV['TWITTER_CUSTOMER_TOKEN_SECRET']
  44. # ensure channel configuration
  45. Channel.where(area: 'Twitter::Account').each(&:destroy)
  46. channel = Channel.create!(
  47. area: 'Twitter::Account',
  48. options: {
  49. adapter: 'twitter',
  50. auth: {
  51. consumer_key: consumer_key,
  52. consumer_secret: consumer_secret,
  53. oauth_token: system_token,
  54. oauth_token_secret: system_token_secret,
  55. },
  56. user: {
  57. screen_name: system_login,
  58. id: system_id,
  59. },
  60. sync: {
  61. track_retweets: true,
  62. search: [
  63. {
  64. term: hash_tag2,
  65. group_id: group.id,
  66. },
  67. {
  68. term: hash_tag1,
  69. group_id: 1,
  70. },
  71. ],
  72. mentions: {
  73. group_id: group.id,
  74. },
  75. direct_messages: {
  76. group_id: group.id,
  77. }
  78. }
  79. },
  80. active: true,
  81. created_by_id: 1,
  82. updated_by_id: 1,
  83. )
  84. test 'a new outbound and reply' do
  85. hash = "#{hash_tag2}#{rand(999_999)}"
  86. user = User.find(2)
  87. text = "Today the weather is really #{rand_word}... #{hash}"
  88. ticket = Ticket.create!(
  89. title: text[0, 40],
  90. customer_id: user.id,
  91. group_id: group.id,
  92. state: Ticket::State.find_by(name: 'new'),
  93. priority: Ticket::Priority.find_by(name: '2 normal'),
  94. preferences: {
  95. channel_id: channel.id,
  96. },
  97. updated_by_id: 1,
  98. created_by_id: 1,
  99. )
  100. assert(ticket, "outbound ticket created, text: #{text}")
  101. article = Ticket::Article.create!(
  102. ticket_id: ticket.id,
  103. body: text,
  104. type: Ticket::Article::Type.find_by(name: 'twitter status'),
  105. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  106. internal: false,
  107. updated_by_id: 1,
  108. created_by_id: 1,
  109. )
  110. Scheduler.worker(true)
  111. article = Ticket::Article.find(article.id)
  112. assert(article, "outbound article created, text: #{text}")
  113. assert_equal(system_login, article.from, 'ticket article from')
  114. assert_equal('', article.to, 'ticket article to')
  115. ticket = Ticket.find(article.ticket_id)
  116. ticket.state = Ticket::State.find_by(name: 'closed')
  117. ticket.save!
  118. # reply by me_bauer
  119. client = Twitter::REST::Client.new do |config|
  120. config.consumer_key = consumer_key
  121. config.consumer_secret = consumer_secret
  122. config.access_token = customer_token
  123. config.access_token_secret = customer_token_secret
  124. end
  125. tweet_found = false
  126. client.user_timeline(system_login_without_at).each do |tweet|
  127. next if tweet.id.to_s != article.message_id.to_s
  128. tweet_found = true
  129. break
  130. end
  131. assert(tweet_found, "found outbound '#{text}' tweet '#{article.message_id}'")
  132. reply_text = "#{system_login} on my side the weather is nice, too! 😍😍😍 #weather#{rand(999_999)}"
  133. tweet = client.update(
  134. reply_text,
  135. {
  136. in_reply_to_status_id: article.message_id
  137. }
  138. )
  139. # fetch check system account
  140. sleep 10
  141. article = nil
  142. 2.times do
  143. Channel.fetch
  144. # check if follow up article has been created
  145. article = Ticket::Article.find_by(message_id: tweet.id)
  146. break if article
  147. sleep 10
  148. end
  149. assert(article, "article tweet '#{tweet.id}' imported")
  150. assert_equal(customer_login, article.from, 'ticket article from')
  151. assert_equal(system_login, article.to, 'ticket article to')
  152. assert_equal(tweet.id.to_s, article.message_id, 'ticket article inbound message_id')
  153. assert_equal(2, article.ticket.articles.count, 'ticket article inbound count')
  154. assert_equal(reply_text.utf8_to_3bytesutf8, ticket.articles.last.body, 'ticket article inbound body')
  155. assert_equal('open', ticket.reload.state.name)
  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 = "#{hash_tag1} ##{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. 2.times do
  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. end
  185. assert(article, "Can't find tweet id #{tweet.id}/#{text}")
  186. assert_equal(customer_login, article.from, 'ticket article from')
  187. assert_nil(article.to, 'ticket article to')
  188. ticket = article.ticket
  189. assert_equal('new', ticket.reload.state.name)
  190. # send reply
  191. reply_text = "#{customer_login} on my side #weather#{hash_gen}"
  192. article = Ticket::Article.create!(
  193. ticket_id: ticket.id,
  194. body: reply_text,
  195. type: Ticket::Article::Type.find_by(name: 'twitter status'),
  196. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  197. internal: false,
  198. updated_by_id: 1,
  199. created_by_id: 1,
  200. )
  201. Scheduler.worker(true)
  202. assert_equal('open', ticket.reload.state.name)
  203. article = Ticket::Article.find(article.id)
  204. assert(article, "outbound article created, text: #{reply_text}")
  205. assert_equal(system_login, article.from, 'ticket article from')
  206. assert_equal(customer_login, article.to, 'ticket article to')
  207. sleep 5
  208. tweet_found = false
  209. client.user_timeline(system_login_without_at).each do |local_tweet|
  210. sleep 10
  211. next if local_tweet.id.to_s != article.message_id.to_s
  212. tweet_found = true
  213. break
  214. end
  215. assert(tweet_found, "found outbound '#{reply_text}' tweet '#{article.message_id}'")
  216. channel = Channel.find(channel.id)
  217. assert_equal('', channel.last_log_out)
  218. assert_equal('ok', channel.status_out)
  219. assert_equal('', channel.last_log_in)
  220. assert_equal('ok', channel.status_in)
  221. ticket = Ticket.find(article.ticket_id)
  222. ticket.state = Ticket::State.find_by(name: 'closed')
  223. ticket.save!
  224. # reply with zammad user directly
  225. client = Twitter::REST::Client.new do |config|
  226. config.consumer_key = consumer_key
  227. config.consumer_secret = consumer_secret
  228. config.access_token = system_token
  229. config.access_token_secret = system_token_secret
  230. end
  231. hash = "#{hash_tag1} ##{hash_gen}"
  232. text = "Today #{system_login} #{rand_word}... #{hash}"
  233. tweet = client.update(
  234. text,
  235. )
  236. # fetch check system account
  237. sleep 20
  238. article = nil
  239. 2.times do
  240. Channel.fetch
  241. # check if ticket and article has been created
  242. article = Ticket::Article.find_by(message_id: tweet.id)
  243. break if article
  244. sleep 20
  245. end
  246. assert(article, "Can't find tweet id #{tweet.id}/#{text}")
  247. assert_equal('closed', ticket.reload.state.name)
  248. end
  249. test 'c new by direct message inbound' do
  250. # cleanup direct messages of system
  251. client = Twitter::REST::Client.new do |config|
  252. config.consumer_key = consumer_key
  253. config.consumer_secret = consumer_secret
  254. config.access_token = system_token
  255. config.access_token_secret = system_token_secret
  256. end
  257. dms = client.direct_messages(count: 100)
  258. dms.each do |dm|
  259. client.destroy_direct_message(dm.id)
  260. end
  261. client = Twitter::REST::Client.new(
  262. consumer_key: consumer_key,
  263. consumer_secret: consumer_secret,
  264. access_token: customer_token,
  265. access_token_secret: customer_token_secret
  266. )
  267. dms = client.direct_messages(count: 100)
  268. dms.each do |dm|
  269. client.destroy_direct_message(dm.id)
  270. end
  271. hash = "#citheo44 #{hash_gen}"
  272. text = "How about #{rand_word} the details? #{hash} - #{'Long' * 50}"
  273. dm = client.create_direct_message(
  274. system_login_without_at,
  275. text,
  276. )
  277. assert(dm, "dm with ##{hash} created")
  278. # fetch check system account
  279. sleep 15
  280. article = nil
  281. 1.times do
  282. Channel.fetch
  283. # check if ticket and article has been created
  284. article = Ticket::Article.find_by(message_id: dm.id)
  285. break if article
  286. sleep 10
  287. end
  288. assert(article, "inbound article '#{text}' created")
  289. assert_equal(customer_login, article.from, 'ticket article from')
  290. assert_equal(text, article.body, 'ticket article body')
  291. ticket = article.ticket
  292. assert(ticket, 'ticket of inbound article exists')
  293. assert(ticket.articles, 'ticket.articles exists')
  294. assert_equal(1, ticket.articles.count, 'ticket article inbound count')
  295. assert_equal(ticket.state.name, 'new')
  296. # reply via ticket
  297. outbound_article = Ticket::Article.create!(
  298. ticket_id: ticket.id,
  299. to: customer_login,
  300. body: "Will call you later #{rand_word}!",
  301. type: Ticket::Article::Type.find_by(name: 'twitter direct-message'),
  302. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  303. internal: false,
  304. updated_by_id: 1,
  305. created_by_id: 1,
  306. )
  307. Scheduler.worker(true)
  308. outbound_article = Ticket::Article.find(outbound_article.id)
  309. assert(outbound_article, 'outbound article created')
  310. assert_equal(2, outbound_article.ticket.articles.count, 'ticket article outbound count')
  311. assert_equal(system_login, outbound_article.from, 'ticket article from')
  312. assert_equal(customer_login, outbound_article.to, 'ticket article to')
  313. ticket.state = Ticket::State.find_by(name: 'pending reminder')
  314. ticket.save
  315. text = "#{rand_word}. #{hash}"
  316. dm = client.create_direct_message(
  317. system_login_without_at,
  318. text,
  319. )
  320. assert(dm, "second dm with ##{hash} created")
  321. # fetch check system account
  322. sleep 15
  323. article = nil
  324. 1.times do
  325. Channel.fetch
  326. # check if ticket and article has been created
  327. article = Ticket::Article.find_by(message_id: dm.id)
  328. break if article
  329. sleep 10
  330. end
  331. assert(article, "inbound article '#{text}' created")
  332. assert_equal(customer_login, article.from, 'ticket article inbound from')
  333. assert_equal(system_login, article.to, 'ticket article inbound to')
  334. assert_equal(article.ticket.id, ticket.id, 'still the same ticket')
  335. ticket = article.ticket
  336. assert(ticket, 'ticket of inbound article exists')
  337. assert(ticket.articles, 'ticket.articles exists')
  338. assert_equal(3, ticket.articles.count, 'ticket article inbound count')
  339. assert_equal(ticket.state.name, 'open')
  340. # close dm ticket, next dm should open a new
  341. ticket.state = Ticket::State.find_by(name: 'closed')
  342. ticket.save
  343. text = "Thanks #{rand_word} for your call. I just have one question. #{hash}"
  344. dm = client.create_direct_message(
  345. system_login_without_at,
  346. text,
  347. )
  348. assert(dm, "third dm with ##{hash} created")
  349. # fetch check system account
  350. sleep 15
  351. article = nil
  352. 1.times do
  353. Channel.fetch
  354. # check if ticket and article has been created
  355. article = Ticket::Article.find_by(message_id: dm.id)
  356. break if article
  357. sleep 15
  358. end
  359. assert(article, "inbound article '#{text}' created with dm id #{dm.id}")
  360. assert_equal(customer_login, article.from, 'ticket article inbound from')
  361. assert_equal(system_login, article.to, 'ticket article inbound to')
  362. ticket = article.ticket
  363. assert(ticket, 'ticket of inbound article exists')
  364. assert(ticket.articles, 'ticket.articles exists')
  365. assert_equal(1, ticket.articles.count, 'ticket article inbound count')
  366. assert_equal(ticket.state.name, 'new')
  367. channel = Channel.find(channel.id)
  368. assert_equal('', channel.last_log_out)
  369. assert_equal('ok', channel.status_out)
  370. assert_equal('', channel.last_log_in)
  371. assert_equal('ok', channel.status_in)
  372. end
  373. test 'c new by direct message outbound without required parameters' do
  374. # cleanup direct messages of system
  375. client = Twitter::REST::Client.new do |config|
  376. config.consumer_key = consumer_key
  377. config.consumer_secret = consumer_secret
  378. config.access_token = system_token
  379. config.access_token_secret = system_token_secret
  380. end
  381. dms = client.direct_messages(count: 100)
  382. dms.each do |dm|
  383. client.destroy_direct_message(dm.id)
  384. end
  385. client = Twitter::REST::Client.new(
  386. consumer_key: consumer_key,
  387. consumer_secret: consumer_secret,
  388. access_token: customer_token,
  389. access_token_secret: customer_token_secret
  390. )
  391. dms = client.direct_messages(count: 100)
  392. dms.each do |dm|
  393. client.destroy_direct_message(dm.id)
  394. end
  395. hash = "#citheo44 #{hash_gen}"
  396. text = "How about #{rand_word} the details? #{hash} - #{'Long' * 50}"
  397. dm = client.create_direct_message(
  398. system_login_without_at,
  399. text,
  400. )
  401. assert(dm, "dm with ##{hash} created")
  402. # fetch check system account
  403. sleep 15
  404. article = nil
  405. 1.times do
  406. Channel.fetch
  407. # check if ticket and article has been created
  408. article = Ticket::Article.find_by(message_id: dm.id)
  409. break if article
  410. sleep 10
  411. end
  412. assert(article, "inbound article '#{text}' created")
  413. assert_equal(customer_login, article.from, 'ticket article from')
  414. assert_equal(text, article.body, 'ticket article body')
  415. ticket = article.ticket
  416. assert(ticket, 'ticket of inbound article exists')
  417. assert(ticket.articles, 'ticket.articles exists')
  418. assert_equal(1, ticket.articles.count, 'ticket article inbound count')
  419. assert_equal(ticket.state.name, 'closed')
  420. # reply via ticket
  421. reply = assert_raises(Exceptions::UnprocessableEntity) do
  422. Ticket::Article.create!(
  423. ticket_id: ticket.id,
  424. in_reply_to: '123456789',
  425. body: "Will call you later #{rand_word}!",
  426. type: Ticket::Article::Type.find_by(name: 'twitter direct-message'),
  427. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  428. internal: false,
  429. updated_by_id: 1,
  430. created_by_id: 1,
  431. )
  432. end
  433. assert_equal('twitter to: parameter is missing', reply.message)
  434. end
  435. test 'd track_retweets enabled' do
  436. # enable track_retweets
  437. channel[:options]['sync']['track_retweets'] = true
  438. channel.save!
  439. client = Twitter::REST::Client.new do |config|
  440. config.consumer_key = consumer_key
  441. config.consumer_secret = consumer_secret
  442. config.access_token = system_token
  443. config.access_token_secret = system_token_secret
  444. end
  445. hash = "#{hash_tag1} ##{hash_gen}"
  446. text = "Retweet me - I'm #{system_login} - #{rand_word}... #{hash}"
  447. tweet = client.update(text)
  448. client = Twitter::REST::Client.new(
  449. consumer_key: consumer_key,
  450. consumer_secret: consumer_secret,
  451. access_token: customer_token,
  452. access_token_secret: customer_token_secret
  453. )
  454. retweet = client.retweet(tweet).first
  455. # fetch check system account
  456. sleep 15
  457. article = nil
  458. 2.times do
  459. Channel.fetch
  460. # check if ticket and article has been created
  461. article = Ticket::Article.find_by(message_id: retweet.id)
  462. break if article
  463. sleep 10
  464. end
  465. assert(article, "retweet article '#{text}' created")
  466. end
  467. test 'e track_retweets disabled' do
  468. # disable track_retweets
  469. channel[:options]['sync']['track_retweets'] = false
  470. channel.save!
  471. client = Twitter::REST::Client.new do |config|
  472. config.consumer_key = consumer_key
  473. config.consumer_secret = consumer_secret
  474. config.access_token = system_token
  475. config.access_token_secret = system_token_secret
  476. end
  477. hash = "#{hash_tag1} ##{hash_gen}"
  478. text = "Retweet me - I'm #{system_login} - #{rand_word}... #{hash}"
  479. tweet = client.update(text)
  480. client = Twitter::REST::Client.new(
  481. consumer_key: consumer_key,
  482. consumer_secret: consumer_secret,
  483. access_token: customer_token,
  484. access_token_secret: customer_token_secret
  485. )
  486. retweet = client.retweet(tweet).first
  487. # fetch check system account
  488. sleep 15
  489. article = nil
  490. 2.times do
  491. Channel.fetch
  492. # check if ticket and article has been created
  493. article = Ticket::Article.find_by(message_id: retweet.id)
  494. break if article
  495. sleep 10
  496. end
  497. assert_nil(article, "retweet article '#{text}' not created")
  498. end
  499. test 'f streaming test' do
  500. thread = Thread.new do
  501. Channel.stream
  502. end
  503. sleep 10
  504. # new tweet I - by me_bauer
  505. client = Twitter::REST::Client.new do |config|
  506. config.consumer_key = consumer_key
  507. config.consumer_secret = consumer_secret
  508. config.access_token = customer_token
  509. config.access_token_secret = customer_token_secret
  510. end
  511. hash = "#{hash_tag1} ##{hash_gen}"
  512. text = "Today... #{rand_word} #{hash}"
  513. tweet = client.update(
  514. text,
  515. )
  516. article = nil
  517. 5.times do
  518. Scheduler.worker(true)
  519. article = Ticket::Article.find_by(message_id: tweet.id)
  520. break if article
  521. ActiveRecord::Base.clear_all_connections!
  522. ActiveRecord::Base.connection.query_cache.clear
  523. sleep 10
  524. end
  525. assert(article, "article from customer with text '#{text}' message_id '#{tweet.id}' created")
  526. assert_equal(customer_login, article.from, 'ticket article from')
  527. assert_nil(article.to, 'ticket article to')
  528. # new tweet II - by me_bauer
  529. client = Twitter::REST::Client.new do |config|
  530. config.consumer_key = consumer_key
  531. config.consumer_secret = consumer_secret
  532. config.access_token = customer_token
  533. config.access_token_secret = customer_token_secret
  534. end
  535. hash = "#{hash_tag1} ##{rand(999_999)}"
  536. text = "Today... #{rand_word} #{hash}"
  537. tweet = client.update(
  538. text,
  539. )
  540. article = nil
  541. 5.times do
  542. Scheduler.worker(true)
  543. article = Ticket::Article.find_by(message_id: tweet.id)
  544. break if article
  545. ActiveRecord::Base.clear_all_connections!
  546. ActiveRecord::Base.connection.query_cache.clear
  547. sleep 10
  548. end
  549. assert(article, "article from customer with text '#{text}' message_id '#{tweet.id}' created")
  550. assert_equal(customer_login, article.from, 'ticket article from')
  551. assert_nil(article.to, 'ticket article to')
  552. # send reply
  553. reply_text = "RE #{text}"
  554. article = Ticket::Article.create!(
  555. ticket_id: article.ticket_id,
  556. body: reply_text,
  557. type: Ticket::Article::Type.find_by(name: 'twitter status'),
  558. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  559. internal: false,
  560. updated_by_id: 1,
  561. created_by_id: 1,
  562. )
  563. Scheduler.worker(true)
  564. article = Ticket::Article.find(article.id)
  565. assert(article, "outbound article created, text: #{reply_text}")
  566. assert_equal(system_login, article.from, 'ticket article from')
  567. assert_equal('', article.to, 'ticket article to')
  568. sleep 5
  569. tweet_found = false
  570. client.user_timeline(system_login_without_at).each do |local_tweet|
  571. sleep 10
  572. next if local_tweet.id.to_s != article.message_id.to_s
  573. tweet_found = true
  574. break
  575. end
  576. assert(tweet_found, "found outbound '#{reply_text}' tweet '#{article.message_id}'")
  577. count = Ticket::Article.where(message_id: article.message_id).count
  578. assert_equal(1, count, "tweet #{article.message_id}")
  579. channel_id = article.ticket.preferences[:channel_id]
  580. assert(channel_id)
  581. channel = Channel.find(channel_id)
  582. assert_equal('', channel.last_log_out)
  583. assert_equal('ok', channel.status_out)
  584. # get dm via stream
  585. client = Twitter::REST::Client.new(
  586. consumer_key: consumer_key,
  587. consumer_secret: consumer_secret,
  588. access_token: customer_token,
  589. access_token_secret: customer_token_secret
  590. )
  591. hash = "#citheo44#{rand(999_999)}"
  592. text = "How about the #{rand_word}? #{hash}"
  593. dm = client.create_direct_message(
  594. system_login_without_at,
  595. text,
  596. )
  597. assert(dm, "dm with ##{hash} created")
  598. article = nil
  599. 5.times do
  600. Scheduler.worker(true)
  601. article = Ticket::Article.find_by(message_id: dm.id)
  602. break if article
  603. sleep 10
  604. end
  605. assert(article, "inbound article '#{text}' message_id '#{dm.id}' created")
  606. assert_equal(customer_login, article.from, 'ticket article from')
  607. assert_equal(system_login, article.to, 'ticket article to')
  608. thread.exit
  609. thread.join
  610. end
  611. test 'g streaming test retweet enabled' do
  612. thread = Thread.new do
  613. # enable track_retweets in current thread
  614. # since Threads are not spawned in the same scope
  615. # as the current test is running in .....
  616. channel_thread = Channel.find(channel.id)
  617. channel_thread[:options]['sync']['track_retweets'] = true
  618. channel_thread.save!
  619. Channel.stream
  620. end
  621. sleep 10
  622. client = Twitter::REST::Client.new do |config|
  623. config.consumer_key = consumer_key
  624. config.consumer_secret = consumer_secret
  625. config.access_token = system_token
  626. config.access_token_secret = system_token_secret
  627. end
  628. hash = "#{hash_tag1} ##{hash_gen}"
  629. text = "Retweet me - I'm #{system_login} - #{rand_word}... #{hash}"
  630. tweet = client.update(text)
  631. client = Twitter::REST::Client.new(
  632. consumer_key: consumer_key,
  633. consumer_secret: consumer_secret,
  634. access_token: customer_token,
  635. access_token_secret: customer_token_secret
  636. )
  637. retweet = client.retweet(tweet).first
  638. # fetch check system account
  639. sleep 15
  640. article = nil
  641. 2.times do
  642. Channel.fetch
  643. # check if ticket and article has been created
  644. article = Ticket::Article.find_by(message_id: retweet.id)
  645. break if article
  646. ActiveRecord::Base.clear_all_connections!
  647. ActiveRecord::Base.connection.query_cache.clear
  648. sleep 10
  649. end
  650. assert(article, "retweet article '#{text}' created")
  651. thread.exit
  652. thread.join
  653. end
  654. test 'h streaming test retweet disabled' do
  655. thread = Thread.new do
  656. # disable track_retweets in current thread
  657. # since Threads are not spawned in the same scope
  658. # as the current test is running in .....
  659. channel_thread = Channel.find(channel.id)
  660. channel_thread[:options]['sync']['track_retweets'] = false
  661. channel_thread.save!
  662. Channel.stream
  663. end
  664. sleep 10
  665. client = Twitter::REST::Client.new do |config|
  666. config.consumer_key = consumer_key
  667. config.consumer_secret = consumer_secret
  668. config.access_token = system_token
  669. config.access_token_secret = system_token_secret
  670. end
  671. hash = "#{hash_tag1} ##{hash_gen}"
  672. text = "Retweet me - I'm #{system_login} - #{rand_word}... #{hash}"
  673. tweet = client.update(text)
  674. client = Twitter::REST::Client.new(
  675. consumer_key: consumer_key,
  676. consumer_secret: consumer_secret,
  677. access_token: customer_token,
  678. access_token_secret: customer_token_secret
  679. )
  680. retweet = client.retweet(tweet).first
  681. # fetch check system account
  682. article = nil
  683. 4.times do
  684. # check if ticket and article has been created
  685. article = Ticket::Article.find_by(message_id: retweet.id)
  686. break if article
  687. sleep 10
  688. end
  689. assert_nil(article, "retweet article '#{text}' not created")
  690. thread.exit
  691. thread.join
  692. end
  693. test 'i restart stream after config of channel has changed' do
  694. hash = "#citheo#{rand(999)}"
  695. thread = Thread.new do
  696. Channel.stream
  697. sleep 10
  698. item = {
  699. term: hash,
  700. group_id: group.id,
  701. }
  702. channel_thread = Channel.find(channel.id)
  703. channel_thread[:options]['sync']['search'].push item
  704. channel_thread.save!
  705. end
  706. sleep 60
  707. # new tweet - by me_bauer
  708. client = Twitter::REST::Client.new do |config|
  709. config.consumer_key = consumer_key
  710. config.consumer_secret = consumer_secret
  711. config.access_token = customer_token
  712. config.access_token_secret = customer_token_secret
  713. end
  714. hash = "#{hash_tag1} ##{hash_gen}"
  715. text = "Today... #{rand_word} #{hash}"
  716. tweet = client.update(
  717. text,
  718. )
  719. article = nil
  720. 5.times do
  721. Channel.fetch
  722. Scheduler.worker(true)
  723. article = Ticket::Article.find_by(message_id: tweet.id)
  724. break if article
  725. ActiveRecord::Base.clear_all_connections!
  726. ActiveRecord::Base.connection.query_cache.clear
  727. sleep 10
  728. end
  729. assert(article, "article from customer with text '#{text}' message_id '#{tweet.id}' created")
  730. assert_equal(customer_login, article.from, 'ticket article from')
  731. assert_nil(article.to, 'ticket article to')
  732. thread.exit
  733. thread.join
  734. channel_thread = Channel.find(channel.id)
  735. channel_thread[:options]['sync']['search'].pop
  736. channel_thread.save!
  737. end
  738. def hash_gen
  739. rand(999).to_s + (0...10).map { ('a'..'z').to_a[rand(26)] }.join
  740. end
  741. def rand_word
  742. [
  743. 'dog',
  744. 'cat',
  745. 'house',
  746. 'home',
  747. 'yesterday',
  748. 'tomorrow',
  749. 'new york',
  750. 'berlin',
  751. 'coffee script',
  752. 'java script',
  753. 'bob smith',
  754. 'be open',
  755. 'really nice',
  756. 'stay tuned',
  757. 'be a good boy',
  758. 'invent new things',
  759. ].sample
  760. end
  761. end