remove_spec.rb 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Mutations::Link::Remove, :aggregate_failures, type: :graphql do
  4. let(:mutation) do
  5. <<~MUTATION
  6. mutation linkRemove($input: LinkInput!) {
  7. linkRemove(input: $input) {
  8. success
  9. errors {
  10. message
  11. field
  12. }
  13. }
  14. }
  15. MUTATION
  16. end
  17. let(:from_group) { create(:group) }
  18. let(:from) { create(:ticket, group: from_group) }
  19. let(:to_group) { create(:group) }
  20. let(:to) { create(:ticket, group: to_group) }
  21. let(:type) { ENV.fetch('LINK_TYPE') { %w[child parent normal].sample } }
  22. let(:input) do
  23. {
  24. sourceId: gql.id(from),
  25. targetId: gql.id(to),
  26. type: type
  27. }
  28. end
  29. let(:variables) { { input: input } }
  30. before do
  31. create(:link, from: from, to: to, link_type: type)
  32. next if RSpec.configuration.formatters.first
  33. .class.name.exclude?('DocumentationFormatter')
  34. puts "with link type: #{type}" # rubocop:disable Rails/Output
  35. end
  36. context 'with unauthenticated session' do
  37. it 'raises an error' do
  38. gql.execute(mutation, variables: variables)
  39. expect(gql.result.error_type).to eq(Exceptions::NotAuthorized)
  40. end
  41. end
  42. context 'with authenticated session', authenticated_as: :authenticated do
  43. let(:authenticated) { create(:agent, groups: [from_group, to_group]) }
  44. it 'remove link' do
  45. expect { gql.execute(mutation, variables: variables) }
  46. .to change(Link, :count).by(-1)
  47. end
  48. context 'when reverse link exists' do
  49. before do
  50. create(:link, from: to, to: from, link_type: type)
  51. end
  52. it 'removes both links if existing' do
  53. decremet = type == 'normal' ? -2 : -1
  54. expect { gql.execute(mutation, variables: variables) }
  55. .to change(Link, :count).by(decremet)
  56. end
  57. end
  58. context 'when source is not accessible' do
  59. let(:authenticated) { create(:agent, groups: [to_group]) }
  60. it 'raises an error' do
  61. gql.execute(mutation, variables: variables)
  62. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  63. end
  64. end
  65. context 'when target is not accessible' do
  66. let(:authenticated) { create(:agent, groups: [from_group]) }
  67. it 'raises an error' do
  68. gql.execute(mutation, variables: variables)
  69. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  70. end
  71. end
  72. end
  73. end