facebook_test.rb 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. require 'integration_test_helper'
  2. class FacebookTest < 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: 'Facebook',
  10. note: 'All Facebook feed posts.',
  11. updated_by_id: 1,
  12. created_by_id: 1
  13. )
  14. # account config
  15. if !ENV['FACEBOOK_USER']
  16. raise "ERROR: Need FACEBOOK_USER - hint FACEBOOK_USER='name:1234:access_token'"
  17. end
  18. user_name = ENV['FACEBOOK_USER'].split(':')[0]
  19. user_id = ENV['FACEBOOK_USER'].split(':')[1]
  20. user_access_token = ENV['FACEBOOK_USER'].split(':')[2]
  21. if !ENV['FACEBOOK_PAGE']
  22. raise "ERROR: Need FACEBOOK_PAGE - hint FACEBOOK_PAGE='name:1234:access_token'"
  23. end
  24. page_name = ENV['FACEBOOK_PAGE'].split(':')[0]
  25. page_id = ENV['FACEBOOK_PAGE'].split(':')[1]
  26. page_access_token = ENV['FACEBOOK_PAGE'].split(':')[2]
  27. if !ENV['FACEBOOK_CUSTOMER']
  28. raise "ERROR: Need FACEBOOK_CUSTOMER - hint FACEBOOK_CUSTOMER='name:1234:access_token'"
  29. end
  30. customer_name = ENV['FACEBOOK_CUSTOMER'].split(':')[0]
  31. customer_id = ENV['FACEBOOK_CUSTOMER'].split(':')[1]
  32. customer_access_token = ENV['FACEBOOK_CUSTOMER'].split(':')[2]
  33. provider_options = {
  34. adapter: 'facebook',
  35. auth: {
  36. access_token: user_access_token
  37. },
  38. user: {
  39. name: user_name,
  40. id: user_id,
  41. },
  42. pages: [
  43. {
  44. 'id' => page_id,
  45. 'name' => page_name,
  46. 'access_token' => page_access_token,
  47. }
  48. ],
  49. sync: {
  50. pages: {
  51. page_id => { 'group_id' => group.id.to_s },
  52. }
  53. }
  54. }
  55. # add channel
  56. current = Channel.where(area: 'Facebook::Account')
  57. current.each(&:destroy)
  58. Channel.create(
  59. area: 'Facebook::Account',
  60. options: provider_options,
  61. active: true,
  62. created_by_id: 1,
  63. updated_by_id: 1,
  64. )
  65. # check users account
  66. test 'a - user account' do
  67. client = Facebook.new(user_access_token)
  68. current_user = client.current_user
  69. assert_equal(user_id, current_user['id'])
  70. assert_equal(user_name, current_user['name'])
  71. end
  72. # check available pages
  73. test 'b - available pages' do
  74. client = Facebook.new(user_access_token)
  75. page_found = false
  76. client.pages.each do |page|
  77. next if page[:name] != page_name
  78. page_found = true
  79. assert_equal(page_id, page[:id])
  80. assert_equal(page_name, page[:name])
  81. end
  82. assert(page_found, "Page lookup for '#{page_name}'")
  83. end
  84. # check access to pages
  85. test 'c - page access' do
  86. page_found = false
  87. provider_options[:pages].each do |page|
  88. client = Facebook.new(page['access_token'])
  89. current_user = client.current_user
  90. next if page['name'] != page_name
  91. page_found = true
  92. assert_equal(page_id, current_user['id'])
  93. assert_equal(page_name, current_user['name'])
  94. end
  95. assert(page_found, "Page lookup for '#{page_name}'")
  96. end
  97. # check page account
  98. test 'd - page account' do
  99. client = Facebook.new(page_access_token)
  100. current_user = client.current_user
  101. assert_equal(page_id, current_user['id'])
  102. assert_equal(page_name, current_user['name'])
  103. end
  104. test 'e - feed post to ticket' do
  105. customer_client = Koala::Facebook::API.new(customer_access_token)
  106. message = "I've got an issue with my hat, serial number ##{rand(99_999)}"
  107. post = customer_client.put_wall_post(message, {}, page_id)
  108. sleep 8
  109. # fetch check system account
  110. Channel.fetch
  111. # check if first article has been created
  112. article = Ticket::Article.find_by(message_id: post['id'])
  113. assert_equal('new', article.ticket.state.name)
  114. assert(article, "article post '#{post['id']}' imported")
  115. assert_equal(article.from, customer_name, 'ticket article inbound body')
  116. assert_equal(article.to, page_name, 'ticket article inbound body')
  117. assert_equal(article.body, message, 'ticket article inbound body')
  118. assert_equal(1, article.ticket.articles.count, 'ticket article inbound count')
  119. assert_equal(message, article.ticket.articles.last.body, 'ticket article inbound body')
  120. # check customer
  121. customer = article.ticket.customer
  122. assert_equal('Bernd', customer.firstname)
  123. assert_equal('Hofbecker', customer.lastname)
  124. post_comment = "Any updates yet? It's urgent. I love my hat."
  125. comment = customer_client.put_comment(post['id'], post_comment)
  126. sleep 8
  127. # fetch check system account
  128. Channel.fetch
  129. # check if second article has been created
  130. article = Ticket::Article.find_by(message_id: comment['id'])
  131. assert_equal('new', article.ticket.reload.state.name)
  132. assert(article, "article comment '#{comment['id']}' imported")
  133. assert_equal(article.from, customer_name, 'ticket article inbound body')
  134. assert_equal(article.body, post_comment, 'ticket article inbound body')
  135. assert_equal(2, article.ticket.articles.count, 'ticket article inbound count')
  136. assert_equal(post_comment, article.ticket.articles.last.body, 'ticket article inbound body')
  137. end
  138. test 'f - feed post and comment reply' do
  139. customer_client = Koala::Facebook::API.new(customer_access_token)
  140. feed_post = "I've got an issue with my hat, serial number ##{rand(99_999)}"
  141. post = customer_client.put_wall_post(feed_post, {}, page_id)
  142. sleep 8
  143. # fetch check system account
  144. Channel.fetch
  145. article = Ticket::Article.find_by(message_id: post['id'])
  146. ticket = article.ticket
  147. assert_equal('new', ticket.state.name)
  148. assert(article, "article post '#{post['id']}' imported")
  149. # check customer
  150. customer = ticket.customer
  151. assert_equal(customer_name, "#{customer.firstname} #{customer.lastname}")
  152. # reply via ticket
  153. reply_text = "What's your issue Bernd?"
  154. outbound_article = Ticket::Article.create(
  155. ticket_id: ticket.id,
  156. body: reply_text,
  157. in_reply_to: post['id'],
  158. type: Ticket::Article::Type.find_by(name: 'facebook feed comment'),
  159. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  160. internal: false,
  161. updated_by_id: 1,
  162. created_by_id: 1,
  163. )
  164. Scheduler.worker(true)
  165. assert_equal('open', ticket.reload.state.name)
  166. outbound_article = Ticket::Article.find(outbound_article.id)
  167. assert(outbound_article, 'outbound article created')
  168. assert_equal(outbound_article.from, page_name, 'ticket article outbound count')
  169. assert_equal(outbound_article.ticket.articles.count, 2, 'ticket article outbound count')
  170. ticket = Ticket.find(outbound_article.ticket_id)
  171. ticket.state = Ticket::State.find_by(name: 'closed')
  172. ticket.save!
  173. post_comment = 'The peacock feather is fallen off.'
  174. comment = customer_client.put_comment(post['id'], post_comment)
  175. sleep 8
  176. # fetch check system account
  177. Channel.fetch
  178. article = Ticket::Article.find_by(message_id: comment['id'])
  179. assert(article, "article comment '#{comment['id']}' imported")
  180. assert_equal('open', ticket.reload.state.name)
  181. # reply via ticket
  182. reply_text = "Please send it to our address and add the ticket number #{article.ticket.number}."
  183. outbound_article = Ticket::Article.create(
  184. ticket_id: ticket.id,
  185. body: reply_text,
  186. in_reply_to: comment['id'],
  187. type: Ticket::Article::Type.find_by(name: 'facebook feed comment'),
  188. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  189. internal: false,
  190. updated_by_id: 1,
  191. created_by_id: 1,
  192. )
  193. Scheduler.worker(true)
  194. outbound_article = Ticket::Article.find(outbound_article.id)
  195. assert(outbound_article, 'outbound article created')
  196. assert_equal(outbound_article.from, page_name, 'ticket article outbound count')
  197. assert_equal(outbound_article.ticket.articles.count, 4, 'ticket article outbound count')
  198. end
  199. end