facebook_test.rb 7.2 KB

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