add_spec.rb 2.8 KB

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