attachments_spec.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Queries::Ticket::Attachments, authenticated_as: :user, type: :graphql do
  4. let(:query) do
  5. <<~QUERY
  6. query ticketAttachments($ticketId: ID!) {
  7. ticketAttachments(ticketId: $ticketId) {
  8. id
  9. internalId
  10. name
  11. size
  12. type
  13. preferences
  14. }
  15. }
  16. QUERY
  17. end
  18. let(:ticket) { create(:ticket) }
  19. let(:cid) { "#{SecureRandom.uuid}@zammad.example.com" }
  20. let(:articles) do
  21. create_list(:ticket_article, 2, ticket: ticket, content_type: 'text/html', body: "<img src=\"cid:#{cid}\"> some text") do |article, _i|
  22. create(
  23. :store,
  24. object: 'Ticket::Article',
  25. o_id: article.id,
  26. data: 'fake',
  27. filename: 'inline_image.jpg',
  28. preferences: {
  29. 'Content-Type' => 'image/jpeg',
  30. 'Mime-Type' => 'image/jpeg',
  31. 'Content-ID' => "<#{cid}>",
  32. 'Content-Disposition' => 'inline',
  33. }
  34. )
  35. create(
  36. :store,
  37. object: 'Ticket::Article',
  38. o_id: article.id,
  39. data: 'fake',
  40. filename: 'attached_image.jpg',
  41. preferences: {
  42. 'Content-Type' => 'image/jpeg',
  43. 'Mime-Type' => 'image/jpeg',
  44. 'Content-ID' => "<#{cid}.not.referenced>",
  45. }
  46. )
  47. end
  48. end
  49. let(:variables) { { ticketId: gql.id(ticket) } }
  50. context 'when an agent is fetching ticket attachments' do
  51. let(:user) { create(:agent, groups: [ticket.group]) }
  52. before do
  53. articles
  54. gql.execute(query, variables: variables)
  55. end
  56. it 'returns the ticket attachments' do
  57. expect(gql.result.data).to include(hash_including(
  58. 'id' => gql.id(articles.first.attachments.last),
  59. 'internalId' => articles.first.attachments.last.id,
  60. 'name' => 'attached_image.jpg',
  61. ))
  62. end
  63. context 'when the ticket is in a group the agent is not a member of' do
  64. let(:user) { create(:agent, groups: []) }
  65. it 'returns an error' do
  66. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  67. end
  68. end
  69. end
  70. context 'when a customer is fetching ticket attachments' do
  71. let(:user) { create(:customer) }
  72. context 'when no access to the ticket' do
  73. before do
  74. articles
  75. gql.execute(query, variables: variables)
  76. end
  77. it 'returns an error' do
  78. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  79. end
  80. end
  81. context 'when access to the ticket' do
  82. context 'when all articles are public' do
  83. before do
  84. ticket.update!(customer: user)
  85. articles.each { |article| article.update!(internal: false) }
  86. gql.execute(query, variables: variables)
  87. end
  88. it 'returns the ticket attachments' do
  89. expect(gql.result.data).to include(hash_including(
  90. 'id' => gql.id(articles.first.attachments.last),
  91. 'internalId' => articles.first.attachments.last.id,
  92. 'name' => 'attached_image.jpg',
  93. ))
  94. end
  95. end
  96. context 'when some articles are internal' do
  97. before do
  98. ticket.update!(customer: user)
  99. articles.each { |article| article.update!(internal: true) }
  100. gql.execute(query, variables: variables)
  101. end
  102. it 'returns the ticket attachments (empty)' do
  103. expect(gql.result.data).to eq([])
  104. end
  105. end
  106. end
  107. end
  108. context 'when not authenticated' do
  109. let(:user) { nil }
  110. before do
  111. gql.execute(query, variables: variables)
  112. end
  113. it_behaves_like 'graphql responds with error if unauthenticated'
  114. end
  115. end