facebook_test.rb 7.8 KB

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