twitter_test.rb 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. assert(article, "outbound article created, text: #{text}")
  120. assert_equal(system_login, article.from, 'ticket article from')
  121. assert_equal('', article.to, 'ticket article to')
  122. # reply by me_bauer
  123. client = Twitter::REST::Client.new do |config|
  124. config.consumer_key = consumer_key
  125. config.consumer_secret = consumer_secret
  126. config.access_token = customer_token
  127. config.access_token_secret = customer_token_secret
  128. end
  129. tweet_found = false
  130. client.user_timeline(system_login_without_at).each { |tweet|
  131. next if tweet.id.to_s != article.message_id.to_s
  132. tweet_found = true
  133. break
  134. }
  135. assert(tweet_found, "found outbound '#{text}' tweet '#{article.message_id}'")
  136. reply_text = "#{system_login} on my side the weather is nice, too! 😍😍😍 #weather#{rand(999_999)}"
  137. tweet = client.update(
  138. reply_text,
  139. {
  140. in_reply_to_status_id: article.message_id
  141. }
  142. )
  143. # fetch check system account
  144. sleep 10
  145. article = nil
  146. (1..2).each {
  147. Channel.fetch
  148. # check if follow up article has been created
  149. article = Ticket::Article.find_by(message_id: tweet.id)
  150. break if article
  151. sleep 10
  152. }
  153. assert(article, "article tweet '#{tweet.id}' imported")
  154. assert_equal(customer_login, article.from, 'ticket article from')
  155. assert_equal(system_login, article.to, 'ticket article to')
  156. assert_equal(tweet.id.to_s, article.message_id, 'ticket article inbound message_id')
  157. assert_equal(2, article.ticket.articles.count, 'ticket article inbound count')
  158. assert_equal(reply_text.utf8_to_3bytesutf8, ticket.articles.last.body, 'ticket article inbound body')
  159. channel = Channel.find(channel.id)
  160. assert_equal('', channel.last_log_out)
  161. assert_equal('ok', channel.status_out)
  162. assert_equal('', channel.last_log_in)
  163. assert_equal('ok', channel.status_in)
  164. end
  165. test 'b new inbound and reply' do
  166. # new tweet by me_bauer
  167. client = Twitter::REST::Client.new do |config|
  168. config.consumer_key = consumer_key
  169. config.consumer_secret = consumer_secret
  170. config.access_token = customer_token
  171. config.access_token_secret = customer_token_secret
  172. end
  173. hash = "#zarepl24 ##{hash_gen}"
  174. text = "Today #{rand_word}... #{hash}"
  175. tweet = client.update(
  176. text,
  177. )
  178. # fetch check system account
  179. sleep 20
  180. article = nil
  181. (1..3).each {
  182. Channel.fetch
  183. # check if ticket and article has been created
  184. article = Ticket::Article.find_by(message_id: tweet.id)
  185. break if article
  186. sleep 20
  187. }
  188. assert(article, "Can't find tweet id #{tweet.id}/#{text}")
  189. assert_equal(customer_login, article.from, 'ticket article from')
  190. assert_equal(nil, article.to, 'ticket article to')
  191. ticket = article.ticket
  192. # send reply
  193. reply_text = "#{customer_login} on my side #weather#{hash_gen}"
  194. article = Ticket::Article.create(
  195. ticket_id: ticket.id,
  196. body: reply_text,
  197. type: Ticket::Article::Type.find_by(name: 'twitter status'),
  198. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  199. internal: false,
  200. updated_by_id: 1,
  201. created_by_id: 1,
  202. )
  203. assert(article, "outbound article created, text: #{reply_text}")
  204. assert_equal(system_login, article.from, 'ticket article from')
  205. assert_equal(customer_login, article.to, 'ticket article to')
  206. sleep 5
  207. tweet_found = false
  208. client.user_timeline(system_login_without_at).each { |local_tweet|
  209. sleep 10
  210. next if local_tweet.id.to_s != article.message_id.to_s
  211. tweet_found = true
  212. break
  213. }
  214. assert(tweet_found, "found outbound '#{reply_text}' tweet '#{article.message_id}'")
  215. channel = Channel.find(channel.id)
  216. assert_equal('', channel.last_log_out)
  217. assert_equal('ok', channel.status_out)
  218. assert_equal('', channel.last_log_in)
  219. assert_equal('ok', channel.status_in)
  220. end
  221. test 'c new by direct message inbound' do
  222. # cleanup direct messages of system
  223. client = Twitter::REST::Client.new do |config|
  224. config.consumer_key = consumer_key
  225. config.consumer_secret = consumer_secret
  226. config.access_token = system_token
  227. config.access_token_secret = system_token_secret
  228. end
  229. dms = client.direct_messages(count: 100)
  230. dms.each {|dm|
  231. client.destroy_direct_message(dm.id)
  232. }
  233. client = Twitter::REST::Client.new(
  234. consumer_key: consumer_key,
  235. consumer_secret: consumer_secret,
  236. access_token: customer_token,
  237. access_token_secret: customer_token_secret
  238. )
  239. dms = client.direct_messages(count: 100)
  240. dms.each {|dm|
  241. client.destroy_direct_message(dm.id)
  242. }
  243. hash = "#citheo44 #{hash_gen}"
  244. text = "How about #{rand_word} the details? #{hash}"
  245. dm = client.create_direct_message(
  246. system_login_without_at,
  247. text,
  248. )
  249. assert(dm, "dm with ##{hash} created")
  250. # fetch check system account
  251. sleep 15
  252. article = nil
  253. (1..2).each {
  254. Channel.fetch
  255. # check if ticket and article has been created
  256. article = Ticket::Article.find_by(message_id: dm.id)
  257. break if article
  258. sleep 10
  259. }
  260. assert(article, "inbound article '#{text}' created")
  261. assert_equal(customer_login, article.from, 'ticket article from')
  262. assert_equal(system_login, article.to, 'ticket article to')
  263. ticket = article.ticket
  264. assert(ticket, 'ticket of inbound article exists')
  265. assert(ticket.articles, 'ticket.articles exists')
  266. assert_equal(1, ticket.articles.count, 'ticket article inbound count')
  267. assert_equal(ticket.state.name, 'new')
  268. # reply via ticket
  269. outbound_article = Ticket::Article.create(
  270. ticket_id: ticket.id,
  271. to: customer_login,
  272. body: "Will call you later #{rand_word}!",
  273. type: Ticket::Article::Type.find_by(name: 'twitter direct-message'),
  274. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  275. internal: false,
  276. updated_by_id: 1,
  277. created_by_id: 1,
  278. )
  279. assert(outbound_article, 'outbound article created')
  280. assert_equal(2, outbound_article.ticket.articles.count, 'ticket article outbound count')
  281. assert_equal(system_login, outbound_article.from, 'ticket article from')
  282. assert_equal(customer_login, outbound_article.to, 'ticket article to')
  283. ticket.state = Ticket::State.find_by(name: 'pending reminder')
  284. ticket.save
  285. text = "#{rand_word}. #{hash}"
  286. dm = client.create_direct_message(
  287. system_login_without_at,
  288. text,
  289. )
  290. assert(dm, "second dm with ##{hash} created")
  291. # fetch check system account
  292. sleep 15
  293. article = nil
  294. (1..2).each {
  295. Channel.fetch
  296. # check if ticket and article has been created
  297. article = Ticket::Article.find_by(message_id: dm.id)
  298. break if article
  299. sleep 10
  300. }
  301. assert(article, "inbound article '#{text}' created")
  302. assert_equal(customer_login, article.from, 'ticket article inbound from')
  303. assert_equal(system_login, article.to, 'ticket article inbound to')
  304. assert_equal(article.ticket.id, ticket.id, 'still the same ticket')
  305. ticket = article.ticket
  306. assert(ticket, 'ticket of inbound article exists')
  307. assert(ticket.articles, 'ticket.articles exists')
  308. assert_equal(3, ticket.articles.count, 'ticket article inbound count')
  309. assert_equal(ticket.state.name, 'open')
  310. # close dm ticket, next dm should open a new
  311. ticket.state = Ticket::State.find_by(name: 'closed')
  312. ticket.save
  313. text = "Thanks #{rand_word} for your call. I just have one question. #{hash}"
  314. dm = client.create_direct_message(
  315. system_login_without_at,
  316. text,
  317. )
  318. assert(dm, "third dm with ##{hash} created")
  319. # fetch check system account
  320. sleep 15
  321. article = nil
  322. (1..2).each {
  323. Channel.fetch
  324. # check if ticket and article has been created
  325. article = Ticket::Article.find_by(message_id: dm.id)
  326. break if article
  327. sleep 15
  328. }
  329. assert(article, "inbound article '#{text}' created with dm id #{dm.id}")
  330. assert_equal(customer_login, article.from, 'ticket article inbound from')
  331. assert_equal(system_login, article.to, 'ticket article inbound to')
  332. ticket = article.ticket
  333. assert(ticket, 'ticket of inbound article exists')
  334. assert(ticket.articles, 'ticket.articles exists')
  335. assert_equal(1, ticket.articles.count, 'ticket article inbound count')
  336. assert_equal(ticket.state.name, 'new')
  337. channel = Channel.find(channel.id)
  338. assert_equal('', channel.last_log_out)
  339. assert_equal('ok', channel.status_out)
  340. assert_equal('', channel.last_log_in)
  341. assert_equal('ok', channel.status_in)
  342. end
  343. test 'd streaming test' do
  344. Thread.new {
  345. Channel.stream
  346. }
  347. sleep 10
  348. # new tweet I - by me_bauer
  349. client = Twitter::REST::Client.new do |config|
  350. config.consumer_key = consumer_key
  351. config.consumer_secret = consumer_secret
  352. config.access_token = customer_token
  353. config.access_token_secret = customer_token_secret
  354. end
  355. hash = '#zarepl24 #' + hash_gen
  356. text = "Today... #{rand_word} #{hash}"
  357. tweet = client.update(
  358. text,
  359. )
  360. sleep 10
  361. article = nil
  362. (1..2).each {
  363. article = Ticket::Article.find_by(message_id: tweet.id)
  364. break if article
  365. sleep 15
  366. }
  367. assert(article)
  368. assert_equal(customer_login, article.from, 'ticket article from')
  369. assert_equal(nil, article.to, 'ticket article to')
  370. # new tweet II - by me_bauer
  371. client = Twitter::REST::Client.new do |config|
  372. config.consumer_key = consumer_key
  373. config.consumer_secret = consumer_secret
  374. config.access_token = customer_token
  375. config.access_token_secret = customer_token_secret
  376. end
  377. hash = '#zarepl24 #' + rand(999_999).to_s
  378. text = "Today... #{rand_word} #{hash}"
  379. tweet = client.update(
  380. text,
  381. )
  382. ActiveRecord::Base.connection.reconnect!
  383. sleep 10
  384. article = nil
  385. (1..2).each {
  386. article = Ticket::Article.find_by(message_id: tweet.id)
  387. break if article
  388. sleep 15
  389. }
  390. assert(article)
  391. assert_equal(customer_login, article.from, 'ticket article from')
  392. assert_equal(nil, article.to, 'ticket article to')
  393. # send reply
  394. reply_text = "RE #{text}"
  395. article = Ticket::Article.create(
  396. ticket_id: article.ticket_id,
  397. body: reply_text,
  398. type: Ticket::Article::Type.find_by(name: 'twitter status'),
  399. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  400. internal: false,
  401. updated_by_id: 1,
  402. created_by_id: 1,
  403. )
  404. assert(article, "outbound article created, text: #{reply_text}")
  405. assert_equal(system_login, article.from, 'ticket article from')
  406. assert_equal('', article.to, 'ticket article to')
  407. sleep 5
  408. tweet_found = false
  409. client.user_timeline(system_login_without_at).each { |local_tweet|
  410. sleep 10
  411. next if local_tweet.id.to_s != article.message_id.to_s
  412. tweet_found = true
  413. break
  414. }
  415. assert(tweet_found, "found outbound '#{reply_text}' tweet '#{article.message_id}'")
  416. count = Ticket::Article.where(message_id: article.message_id).count
  417. assert_equal(1, count)
  418. channel_id = article.ticket.preferences[:channel_id]
  419. assert(channel_id)
  420. channel = Channel.find(channel_id)
  421. assert_equal('', channel.last_log_out)
  422. assert_equal('ok', channel.status_out)
  423. #assert_equal('', channel.last_log_in)
  424. #assert_equal('ok', channel.status_in)
  425. # get dm via stream
  426. client = Twitter::REST::Client.new(
  427. consumer_key: consumer_key,
  428. consumer_secret: consumer_secret,
  429. access_token: customer_token,
  430. access_token_secret: customer_token_secret
  431. )
  432. hash = '#citheo44' + rand(999_999).to_s
  433. text = "How about the #{rand_word}? " + hash
  434. dm = client.create_direct_message(
  435. system_login_without_at,
  436. text,
  437. )
  438. assert(dm, "dm with ##{hash} created")
  439. #ActiveRecord::Base.connection.reconnect!
  440. sleep 10
  441. article = nil
  442. (1..2).each {
  443. article = Ticket::Article.find_by(message_id: dm.id)
  444. break if article
  445. sleep 10
  446. }
  447. assert(article, "inbound article '#{text}' created")
  448. assert_equal(customer_login, article.from, 'ticket article from')
  449. assert_equal(system_login, article.to, 'ticket article to')
  450. end
  451. def hash_gen
  452. rand(999).to_s + (0...10).map { ('a'..'z').to_a[rand(26)] }.join
  453. end
  454. def rand_word
  455. words = [
  456. 'dog',
  457. 'cat',
  458. 'house',
  459. 'home',
  460. 'yesterday',
  461. 'tomorrow',
  462. 'new york',
  463. 'berlin',
  464. 'coffee script',
  465. 'java script',
  466. 'bob smith',
  467. 'be open',
  468. 'really nice',
  469. 'stay tuned',
  470. 'be a good boy',
  471. 'invent new things',
  472. ]
  473. words[rand(words.length)]
  474. end
  475. end