list_spec.rb 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Queries::Link::List, type: :graphql do
  4. context 'when fetching link list' do
  5. let(:from_group) { create(:group) }
  6. let(:from) { create(:ticket, group: from_group) }
  7. let(:to_group) { create(:group) }
  8. let(:to) { create(:ticket, group: to_group) }
  9. let(:type) { ENV.fetch('LINK_TYPE') { %w[child parent normal].sample } }
  10. let(:link) { create(:link, from:, to:, link_type: type) }
  11. let(:variables) { { objectId: gql.id(from), targetType: 'Ticket' } }
  12. let(:query) do
  13. <<~QUERY
  14. query linkList($objectId: ID!, $targetType: String!) {
  15. linkList(objectId: $objectId, targetType: $targetType) {
  16. type
  17. item {
  18. ... on Ticket {
  19. id
  20. title
  21. }
  22. }
  23. }
  24. }
  25. QUERY
  26. end
  27. before do
  28. link
  29. gql.execute(query, variables: variables)
  30. next if RSpec.configuration.formatters.first
  31. .class.name.exclude?('DocumentationFormatter')
  32. puts "with link type: #{type}" # rubocop:disable Rails/Output
  33. end
  34. context 'with authenticated session', authenticated_as: :authenticated do
  35. let(:authenticated) { create(:agent, groups: [from_group, to_group]) }
  36. it 'returns link list' do
  37. link_type = if type == 'normal'
  38. type
  39. else
  40. type == 'parent' ? 'child' : 'parent'
  41. end
  42. expect(gql.result.data.first).to eq(
  43. {
  44. 'item' => { 'id' => gql.id(to), 'title' => to.title },
  45. 'type' => link_type
  46. }
  47. )
  48. end
  49. context 'when source is not accessible' do
  50. let(:authenticated) { create(:agent, groups: [to_group]) }
  51. it 'raises an error' do
  52. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  53. end
  54. end
  55. context 'when target is not accessible' do
  56. before do
  57. create(:link, from: from, to: create(:ticket), link_type: type)
  58. end
  59. it 'returns link list without the related link', :aggregate_failures do
  60. expect(Link.count).to eq(2)
  61. expect(gql.result.data.size).to eq(1)
  62. end
  63. end
  64. end
  65. end
  66. end