link_updates_spec.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Subscriptions::LinkUpdates, type: :graphql do
  4. let(:subscription) do
  5. <<~SUBSCRIPTION
  6. subscription linkUpdates($objectId: ID!, $targetType: String!) {
  7. linkUpdates(objectId: $objectId, targetType: $targetType) {
  8. links {
  9. item {
  10. ... on Ticket {
  11. id
  12. }
  13. }
  14. type
  15. }
  16. }
  17. }
  18. SUBSCRIPTION
  19. end
  20. let(:mock_channel) { build_mock_channel }
  21. let(:from_group) { create(:group) }
  22. let(:from) { create(:ticket, group: from_group) }
  23. let(:to_group) { create(:group) }
  24. let(:to) { create(:ticket, group: to_group) }
  25. let(:type) { ENV.fetch('LINK_TYPE') { %w[child parent normal].sample } }
  26. let(:link) { create(:link, from:, to:) }
  27. let(:variables) { { objectId: gql.id(from), targetType: 'Ticket' } }
  28. before do
  29. link
  30. gql.execute(subscription, variables: variables, context: { channel: mock_channel })
  31. next if RSpec.configuration.formatters.first
  32. .class.name.exclude?('DocumentationFormatter')
  33. puts "with link type: #{type}" # rubocop:disable Rails/Output
  34. end
  35. context 'with authenticated user', authenticated_as: :agent do
  36. context 'when object is not accessible' do
  37. let(:agent) { create(:agent) }
  38. it 'raises an error' do
  39. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  40. end
  41. end
  42. context 'when object is accessible' do
  43. let(:agent) { create(:agent, groups: [ from_group, to_group ]) }
  44. it 'subscribes to the channel' do
  45. expect(gql.result.data).to eq({ 'links' => nil })
  46. end
  47. context 'when link is updated' do
  48. it 'receives updates' do
  49. link.save!
  50. expect(mock_channel.mock_broadcasted_messages.first[:result]['data']['linkUpdates']['links']).to be_present
  51. end
  52. end
  53. context 'when link is destroyed' do
  54. it 'receives updates' do
  55. link.destroy
  56. expect(mock_channel.mock_broadcasted_messages.first[:result]['data']['linkUpdates']['links']).to be_empty
  57. end
  58. end
  59. context 'when link target is destroyed' do
  60. it 'receives updates' do
  61. to.destroy
  62. expect(mock_channel.mock_broadcasted_messages.first[:result]['data']['linkUpdates']['links']).to be_empty
  63. end
  64. end
  65. context 'when link source is destroyed' do
  66. it 'receives no updates' do
  67. from.destroy
  68. expect(mock_channel.mock_broadcasted_messages).to be_empty
  69. end
  70. end
  71. context 'when link target is updated' do
  72. it 'receives updates' do
  73. to.update!(title: 'New title')
  74. expect(mock_channel.mock_broadcasted_messages.first[:result]['data']['linkUpdates']['links']).to be_present
  75. end
  76. end
  77. context 'when reverse link is created' do
  78. it 'receives updates' do
  79. create(:link, from: to, to: from)
  80. expect(mock_channel.mock_broadcasted_messages.first[:result]['data']['linkUpdates']['links']).to be_present
  81. end
  82. end
  83. end
  84. end
  85. end