add_spec.rb 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Mutations::Form::UploadCache::Add, type: :graphql do
  4. context 'when uploading files for a form', authenticated_as: :agent do
  5. let(:agent) { create(:agent) }
  6. let(:query) do
  7. <<~QUERY
  8. mutation formUploadCacheAdd($formId: FormId!, $files: [UploadFileInput!]!) {
  9. formUploadCacheAdd(formId: $formId, files: $files) {
  10. uploadedFiles {
  11. id
  12. name
  13. type
  14. }
  15. }
  16. }
  17. QUERY
  18. end
  19. let(:form_id) { SecureRandom.uuid }
  20. let(:file_name) { 'my_testfile.pdf' }
  21. let(:file_type) { 'application/pdf' }
  22. let(:file_content) { 'some test content' }
  23. let(:inline) { nil }
  24. let(:variables) do
  25. {
  26. formId: form_id,
  27. files: [
  28. {
  29. name: file_name,
  30. type: file_type,
  31. content: Base64.strict_encode64(file_content),
  32. inline: inline
  33. }.compact
  34. ]
  35. }
  36. end
  37. let(:expected_response) do
  38. [{
  39. 'id' => gql.id(UploadCache.new(form_id).attachments.first),
  40. 'name' => file_name,
  41. 'type' => file_type,
  42. }]
  43. end
  44. before do
  45. gql.execute(query, variables: variables)
  46. end
  47. it 'creates Store entry' do
  48. expect(gql.result.data[:uploadedFiles]).to eq(expected_response)
  49. end
  50. it 'does not mark uploaded file as inline' do
  51. attachment = UploadCache.new(form_id).attachments.first
  52. expect(attachment).not_to be_inline
  53. end
  54. context 'when inline flag is given' do
  55. context 'when flag is true' do
  56. let(:inline) { true }
  57. it 'creates Store entry' do
  58. expect(gql.result.data[:uploadedFiles]).to eq(expected_response)
  59. end
  60. it 'marks uploaded file as inline' do
  61. attachment = UploadCache.new(form_id).attachments.first
  62. expect(attachment).to be_inline
  63. end
  64. end
  65. context 'when flag is false' do
  66. let(:inline) { false }
  67. it 'creates Store entry' do
  68. expect(gql.result.data[:uploadedFiles]).to eq(expected_response)
  69. end
  70. it 'does not mark uploaded file as inline' do
  71. attachment = UploadCache.new(form_id).attachments.first
  72. expect(attachment).not_to be_inline
  73. end
  74. end
  75. end
  76. it_behaves_like 'graphql responds with error if unauthenticated'
  77. end
  78. end