twitter_test.rb 17 KB

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