facebook_test.rb 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.create_if_not_exists(
  8. id: 2,
  9. name: 'Facebook',
  10. note: 'All Facebook feed posts.',
  11. updated_by_id: 1,
  12. created_by_id: 1
  13. )
  14. provider_key = 'CAACEdEose0cBAC56WJvrGb5avKbTlH0c7P4xZCZBfT8zG4nkgEWeFKGnnpNZC8xeedXzmqZCxEUrAumX245T4MborvAmRW52PSpuDiXwXXSMjYaZCJOih5v6CsP3xrZAfGxhPWBbI8dSoquBv8eRbUAMSir9SDSoDeKJSdSfhuytqx5wfveE8YibzT2ZAwYz0d7d2QZAN4b10d9j9UpBhXCCCahj4hyk9JQZD'
  15. consumer_key = 'CAACEdEose0cBAHZCXAQ68snZBf2C7jT6G7pVXaWajbZCZAZAFWRZAVUb9FAMXHZBQECZBX0iL5qOeTsZA0mnR0586XTq9vYiWP8Y3qCzftrd9hnsP7J9VB6APnR67NEdY8SozxIFtctQA9Xp4Lb8lbxBmig2v5oXRIH513kImPYXJoCFUlQs0aJeZBCtRG6BekfPs5GPZB8tieQE3yGgtZBTZA3HI2TtQLZBNXyLAZD'
  16. provider_page_name = 'Hansi Merkurs Hutfabrik'
  17. provider_options = {
  18. adapter: 'facebook',
  19. auth: {
  20. access_token: provider_key
  21. },
  22. sync: {
  23. page: provider_page_name,
  24. group_id: 2,
  25. limit: 1,
  26. }
  27. }
  28. # add channel
  29. current = Channel.where(area: 'Facebook::Account')
  30. current.each(&:destroy)
  31. Channel.create(
  32. area: 'Facebook::Account',
  33. options: provider_options,
  34. active: true,
  35. created_by_id: 1,
  36. updated_by_id: 1,
  37. )
  38. test 'pages' do
  39. provider_options_clone = provider_options
  40. provider_options_clone[:sync].delete(:page)
  41. facebook = Facebook.new( provider_options_clone )
  42. pages = facebook.pages
  43. page_found = false
  44. pages.each { |page|
  45. next if page[:name] != provider_page_name
  46. page_found = true
  47. }
  48. assert( page_found, "Page lookup for '#{provider_page_name}'" )
  49. end
  50. test 'feed post to ticket' do
  51. consumer_client = Koala::Facebook::API.new( consumer_key )
  52. feed_post = "I've got an issue with my hat, serial number ##{rand(9999)}"
  53. facebook = Facebook.new( provider_options )
  54. post = consumer_client.put_wall_post(feed_post, {}, facebook.account['id'])
  55. # fetch check system account
  56. Channel.fetch
  57. # check if first article has been created
  58. article = Ticket::Article.find_by( message_id: post['id'] )
  59. assert( article, "article post '#{post['id']}' imported" )
  60. assert_equal( article.body, feed_post, 'ticket article inbound body' )
  61. assert_equal( 1, article.ticket.articles.count, 'ticket article inbound count' )
  62. assert_equal( feed_post, article.ticket.articles.last.body, 'ticket article inbound body' )
  63. post_comment = "Any updates yet? It's urgent. I love my hat."
  64. comment = consumer_client.put_comment(post['id'], post_comment)
  65. # fetch check system account
  66. Channel.fetch
  67. # check if second article has been created
  68. article = Ticket::Article.find_by( message_id: comment['id'] )
  69. assert( article, "article comment '#{comment['id']}' imported" )
  70. assert_equal( article.body, post_comment, 'ticket article inbound body' )
  71. assert_equal( 2, article.ticket.articles.count, 'ticket article inbound count' )
  72. assert_equal( post_comment, article.ticket.articles.last.body, 'ticket article inbound body' )
  73. end
  74. test 'feed post and comment reply' do
  75. consumer_client = Koala::Facebook::API.new( consumer_key )
  76. feed_post = "I've got an issue with my hat, serial number ##{rand(9999)}"
  77. facebook = Facebook.new( provider_options )
  78. post = consumer_client.put_wall_post(feed_post, {}, facebook.account['id'])
  79. # fetch check system account
  80. Channel.fetch
  81. # check if first article has been created
  82. article = Ticket::Article.find_by( message_id: post['id'] )
  83. reply_text = "What's your issue Bernd?"
  84. # reply via ticket
  85. outbound_article = Ticket::Article.create(
  86. ticket_id: article.ticket.id,
  87. body: reply_text,
  88. in_reply_to: post['id'],
  89. type: Ticket::Article::Type.find_by( name: 'facebook feed comment' ),
  90. sender: Ticket::Article::Sender.find_by( name: 'Agent' ),
  91. internal: false,
  92. updated_by_id: 1,
  93. created_by_id: 1,
  94. )
  95. assert( outbound_article, 'outbound article created' )
  96. assert_equal( outbound_article.ticket.articles.count, 2, 'ticket article outbound count' )
  97. post_comment = 'The peacock feather is fallen off.'
  98. comment = consumer_client.put_comment(post['id'], post_comment)
  99. # fetch check system account
  100. Channel.fetch
  101. reply_text = "Please send it to our address and add the ticket number #{article.ticket.number}."
  102. # reply via ticket
  103. outbound_article = Ticket::Article.create(
  104. ticket_id: article.ticket.id,
  105. body: reply_text,
  106. in_reply_to: comment['id'],
  107. type: Ticket::Article::Type.find_by( name: 'facebook feed comment' ),
  108. sender: Ticket::Article::Sender.find_by( name: 'Agent' ),
  109. internal: false,
  110. updated_by_id: 1,
  111. created_by_id: 1,
  112. )
  113. assert( outbound_article, 'outbound article created' )
  114. assert_equal( outbound_article.ticket.articles.count, 4, 'ticket article outbound count' )
  115. end
  116. end