facebook_test.rb 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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(article, "article post '#{post['id']}' imported")
  113. assert_equal(article.from, customer_name, 'ticket article inbound body')
  114. assert_equal(article.to, page_name, 'ticket article inbound body')
  115. assert_equal(article.body, message, 'ticket article inbound body')
  116. assert_equal(1, article.ticket.articles.count, 'ticket article inbound count')
  117. assert_equal(message, article.ticket.articles.last.body, 'ticket article inbound body')
  118. # check customer
  119. customer = article.ticket.customer
  120. assert_equal('Bernd', customer.firstname)
  121. assert_equal('Hofbecker', customer.lastname)
  122. post_comment = "Any updates yet? It's urgent. I love my hat."
  123. comment = customer_client.put_comment(post['id'], post_comment)
  124. sleep 8
  125. # fetch check system account
  126. Channel.fetch
  127. # check if second article has been created
  128. article = Ticket::Article.find_by(message_id: comment['id'])
  129. assert(article, "article comment '#{comment['id']}' imported")
  130. assert_equal(article.from, customer_name, 'ticket article inbound body')
  131. assert_equal(article.body, post_comment, 'ticket article inbound body')
  132. assert_equal(2, article.ticket.articles.count, 'ticket article inbound count')
  133. assert_equal(post_comment, article.ticket.articles.last.body, 'ticket article inbound body')
  134. end
  135. test 'f - feed post and comment reply' do
  136. customer_client = Koala::Facebook::API.new(customer_access_token)
  137. feed_post = "I've got an issue with my hat, serial number ##{rand(99_999)}"
  138. post = customer_client.put_wall_post(feed_post, {}, page_id)
  139. sleep 8
  140. # fetch check system account
  141. Channel.fetch
  142. article = Ticket::Article.find_by(message_id: post['id'])
  143. ticket = article.ticket
  144. assert(article, "article post '#{post['id']}' imported")
  145. # check customer
  146. customer = ticket.customer
  147. assert_equal(customer_name, "#{customer.firstname} #{customer.lastname}")
  148. # reply via ticket
  149. reply_text = "What's your issue Bernd?"
  150. outbound_article = Ticket::Article.create(
  151. ticket_id: ticket.id,
  152. body: reply_text,
  153. in_reply_to: post['id'],
  154. type: Ticket::Article::Type.find_by(name: 'facebook feed comment'),
  155. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  156. internal: false,
  157. updated_by_id: 1,
  158. created_by_id: 1,
  159. )
  160. Scheduler.worker(true)
  161. outbound_article = Ticket::Article.find(outbound_article.id)
  162. assert(outbound_article, 'outbound article created')
  163. assert_equal(outbound_article.from, page_name, 'ticket article outbound count')
  164. assert_equal(outbound_article.ticket.articles.count, 2, 'ticket article outbound count')
  165. post_comment = 'The peacock feather is fallen off.'
  166. comment = customer_client.put_comment(post['id'], post_comment)
  167. sleep 8
  168. # fetch check system account
  169. Channel.fetch
  170. article = Ticket::Article.find_by(message_id: comment['id'])
  171. assert(article, "article comment '#{comment['id']}' imported")
  172. # reply via ticket
  173. reply_text = "Please send it to our address and add the ticket number #{article.ticket.number}."
  174. outbound_article = Ticket::Article.create(
  175. ticket_id: ticket.id,
  176. body: reply_text,
  177. in_reply_to: comment['id'],
  178. type: Ticket::Article::Type.find_by(name: 'facebook feed comment'),
  179. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  180. internal: false,
  181. updated_by_id: 1,
  182. created_by_id: 1,
  183. )
  184. Scheduler.worker(true)
  185. outbound_article = Ticket::Article.find(outbound_article.id)
  186. assert(outbound_article, 'outbound article created')
  187. assert_equal(outbound_article.from, page_name, 'ticket article outbound count')
  188. assert_equal(outbound_article.ticket.articles.count, 4, 'ticket article outbound count')
  189. end
  190. end