facebook_spec.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Facebook, current_user_id: 1, integration: true, required_envs: %w[FACEBOOK_ADMIN_USER_ID FACEBOOK_ADMIN_FIRSTNAME FACEBOOK_ADMIN_LASTNAME FACEBOOK_ADMIN_ACCESS_TOKEN FACEBOOK_PAGE_1_ACCCESS_TOKEN FACEBOOK_PAGE_1_ID FACEBOOK_PAGE_1_NAME FACEBOOK_PAGE_1_POST_ID FACEBOOK_PAGE_1_POST_MESSAGE 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(:page_access_token) { ENV['FACEBOOK_PAGE_1_ACCCESS_TOKEN'] }
  5. let(:page_client) { described_class.new page_access_token }
  6. let(:admin_access_token) { ENV['FACEBOOK_ADMIN_ACCESS_TOKEN'] }
  7. let(:admin_client) { described_class.new admin_access_token }
  8. let(:post) do
  9. page_client
  10. .client
  11. .get_connection('me', 'feed', fields: 'id,from,to,message,created_time,permalink_url,comments{id,from,to,message,created_time}')
  12. .first
  13. end
  14. let(:page) { admin_client.pages.first }
  15. describe '#connect' do
  16. it 'works as expected' do
  17. expect(page_client.client.get_object('me')['name']).to eq ENV['FACEBOOK_PAGE_1_NAME']
  18. end
  19. end
  20. describe '#pages' do
  21. it 'works as expected' do
  22. expect(admin_client.pages.pluck(:name)).to eq [ENV['FACEBOOK_PAGE_2_NAME'], ENV['FACEBOOK_PAGE_1_NAME']]
  23. end
  24. end
  25. describe '#current_user' do
  26. it 'works for user' do
  27. expect(admin_client.current_user['name']).to eq "#{ENV['FACEBOOK_ADMIN_FIRSTNAME']} #{ENV['FACEBOOK_ADMIN_LASTNAME']}"
  28. end
  29. it 'works for page' do
  30. expect(page_client.current_user['name']).to eq ENV['FACEBOOK_PAGE_1_NAME']
  31. end
  32. end
  33. describe '#to_user' do
  34. let(:posts) { page_client.client.get_connection('me', 'feed', fields: 'id,from,to,message,created_time,permalink_url,comments{id,from,to,message,created_time}') }
  35. let(:user) { page_client.to_user(posts.first) }
  36. it 'works as expected' do
  37. expect(user).to have_attributes(
  38. firstname: ENV['FACEBOOK_CUSTOMER_FIRSTNAME'],
  39. )
  40. end
  41. end
  42. describe '#to_ticket' do
  43. it 'works as expected' do
  44. ticket = page_client.to_ticket(post, Group.first.id, Channel.first, page)
  45. expect(ticket.title).to eq ENV['FACEBOOK_PAGE_1_POST_MESSAGE']
  46. end
  47. end
  48. describe '#to_article' do
  49. it 'works as expected' do
  50. ticket = page_client.to_ticket(post, Group.first.id, Channel.first, page)
  51. articles = page_client.to_article(post, ticket, page)
  52. expect(articles.first[:body]).to eq ENV['FACEBOOK_PAGE_1_POST_MESSAGE']
  53. end
  54. end
  55. describe '#to_group' do
  56. let(:ticket) { page_client.to_group(post, Group.first.id, Channel.first, page) }
  57. it 'parses title correctly' do
  58. expect(ticket.title).to eq ENV['FACEBOOK_PAGE_1_POST_MESSAGE']
  59. end
  60. it 'parses body correctly', current_user_id: 1 do
  61. expect(ticket.articles.first.body).to eq ENV['FACEBOOK_PAGE_1_POST_MESSAGE']
  62. end
  63. end
  64. describe '#from_article' do
  65. let(:ticket) { page_client.to_group(post, Group.first.id, Channel.first, page) }
  66. let(:article) { create(:ticket_article, ticket: ticket, type_name: 'facebook feed comment', in_reply_to: ticket.articles.last.message_id) }
  67. let(:response) do
  68. page_client.from_article(
  69. type: article.type.name,
  70. to: article.to,
  71. body: article.body,
  72. in_reply_to: article.in_reply_to,
  73. )
  74. end
  75. # Cleanup of the test comment.
  76. after do
  77. page_client
  78. .client
  79. .delete_object(response['id'])
  80. end
  81. it 'works as expected' do
  82. expect(response['id']).to be_present
  83. end
  84. end
  85. end