facebook_spec.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Channel::Driver::Facebook, integration: true, performs_jobs: true, required_envs: %w[FACEBOOK_ADMIN_USER_ID FACEBOOK_ADMIN_FIRSTNAME FACEBOOK_ADMIN_LASTNAME FACEBOOK_PAGE_1_ACCCESS_TOKEN FACEBOOK_PAGE_1_ID FACEBOOK_PAGE_1_NAME FACEBOOK_PAGE_1_POST_ID FACEBOOK_PAGE_1_POST_COMMENT_ID FACEBOOK_PAGE_2_ACCCESS_TOKEN FACEBOOK_PAGE_2_ID FACEBOOK_PAGE_2_NAME FACEBOOK_CUSTOMER_ID FACEBOOK_CUSTOMER_FIRSTNAME], use_vcr: true do
  4. let(:channel) { create(:facebook_channel) }
  5. let(:page_access_token) { ENV['FACEBOOK_PAGE_1_ACCCESS_TOKEN'] }
  6. let(:page_client) { Facebook.new page_access_token }
  7. before do
  8. # Make sure to use the correct time for the test, otherwise posts are getting too old.
  9. travel_to(DateTime.parse('2023-05-04 18:00:00 UTC'))
  10. channel
  11. end
  12. # Cleanup of the test comment.
  13. after do
  14. page_client
  15. .client
  16. .delete_object(Ticket.last.articles.last.message_id)
  17. end
  18. # This test requires ENV variables to run
  19. # Yes, it runs off VCR cassette
  20. # But it requires following ENV variables to be present:
  21. #
  22. # export FACEBOOK_ADMIN_USER_ID=placeholder
  23. # export FACEBOOK_ADMIN_FIRSTNAME=placeholder
  24. # export FACEBOOK_ADMIN_LASTNAME=placeholder
  25. # export FACEBOOK_PAGE_1_ACCCESS_TOKEN=placeholder
  26. # export FACEBOOK_PAGE_1_ID=placeholder
  27. # export FACEBOOK_PAGE_1_NAME=placeholder
  28. # export FACEBOOK_PAGE_1_POST_ID=placeholder
  29. # export FACEBOOK_PAGE_1_POST_COMMENT_ID=placeholder
  30. # export FACEBOOK_PAGE_2_ACCCESS_TOKEN=placeholder
  31. # export FACEBOOK_PAGE_2_ID=placeholder
  32. # export FACEBOOK_PAGE_2_NAME=placeholder
  33. # export FACEBOOK_CUSTOMER_ID=placeholder
  34. # export FACEBOOK_CUSTOMER_FIRSTNAME=placeholder
  35. #
  36. it 'tests full run', :aggregate_failures do
  37. allow(ApplicationHandleInfo).to receive('context=')
  38. ExternalCredential.create name: :facebook, credentials: { application_id: ENV['FACEBOOK_APPLICATION_ID'], application_secret: ENV['FACEBOOK_APPLICATION_SECRET'] }
  39. channel.fetch
  40. ticket = Ticket.last
  41. ticket_initial_count = ticket.articles.count
  42. expect(ticket.preferences['channel_fb_object_id']).to be_present
  43. message_id = "#{ENV['FACEBOOK_PAGE_1_POST_ID']}_#{ENV['FACEBOOK_PAGE_1_POST_COMMENT_ID']}"
  44. post_article = ticket.articles.find_by(message_id: message_id)
  45. article = Ticket::Article.find_by(message_id: post_article.message_id)
  46. ticket = article.ticket
  47. expect(article).to be_present
  48. customer = ticket.customer
  49. expect(customer.fullname).to eq ENV['FACEBOOK_CUSTOMER_FIRSTNAME']
  50. outbound_article = Ticket::Article.create(
  51. ticket_id: ticket.id,
  52. body: "What's your issue Nicole?",
  53. in_reply_to: post_article.message_id,
  54. type: Ticket::Article::Type.find_by(name: 'facebook feed comment'),
  55. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  56. internal: false,
  57. updated_by_id: 1,
  58. created_by_id: 1,
  59. )
  60. perform_enqueued_jobs
  61. outbound_article = Ticket::Article.find(outbound_article.id)
  62. expect(outbound_article).to be_present
  63. expect(outbound_article.from).to eq ENV['FACEBOOK_PAGE_1_NAME']
  64. expect(outbound_article.ticket.articles.count).to be ticket_initial_count + 1
  65. expect(ApplicationHandleInfo).to have_received('context=').with('facebook').at_least(1)
  66. end
  67. end