upload_cache_spec.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe UploadCache do
  4. subject(:upload_cache) { described_class.new(form_id) }
  5. let(:form_id) { SecureRandom.uuid }
  6. # required for adding items to the Store
  7. before { UserInfo.current_user_id = 1 }
  8. describe '#add' do
  9. it 'adds a Store item' do
  10. expect do
  11. upload_cache.add(
  12. data: 'content_file3_normally_should_be_an_image',
  13. filename: 'some_file3.jpg',
  14. preferences: {
  15. 'Content-Type' => 'image/jpeg',
  16. 'Mime-Type' => 'image/jpeg',
  17. 'Content-Disposition' => 'attached',
  18. },
  19. )
  20. end.to change(Store, :count).by(1)
  21. end
  22. end
  23. describe '#attachments' do
  24. before do
  25. upload_cache.add(
  26. data: 'hello world',
  27. filename: 'some.txt',
  28. preferences: {
  29. 'Content-Type' => 'text/plain',
  30. },
  31. )
  32. end
  33. it 'returns all Store items' do
  34. attachments = upload_cache.attachments
  35. expect(attachments.count).to be(1)
  36. expect(attachments).to include(Store.last)
  37. end
  38. end
  39. describe '#destroy' do
  40. before do
  41. upload_cache.add(
  42. data: 'hello world',
  43. filename: 'some.txt',
  44. preferences: {
  45. 'Content-Type' => 'text/plain',
  46. },
  47. )
  48. upload_cache.add(
  49. data: 'hello other world',
  50. filename: 'another_some.txt',
  51. preferences: {
  52. 'Content-Type' => 'text/plain',
  53. },
  54. )
  55. end
  56. it 'removes all added Store items' do
  57. expect { upload_cache.destroy }.to change(Store, :count).by(-2)
  58. end
  59. end
  60. describe '#remove_item' do
  61. before do
  62. upload_cache.add(
  63. data: 'hello world',
  64. filename: 'some.txt',
  65. preferences: {
  66. 'Content-Type' => 'text/plain',
  67. },
  68. )
  69. end
  70. it 'removes the Store item matching the given ID' do
  71. expect { upload_cache.remove_item(Store.last.id) }.to change(Store, :count).by(-1)
  72. end
  73. it 'prevents removage of non UploadCache Store items' do
  74. item = create(:store,
  75. object: 'Ticket',
  76. o_id: 1,
  77. data: "Can't touch this",
  78. filename: 'keep.txt',
  79. preferences: {
  80. 'Content-Type' => 'text/plain',
  81. },)
  82. expect { upload_cache.remove_item(item.id) }.to raise_error(Exceptions::UnprocessableEntity)
  83. end
  84. it 'fails for non existing UploadCache Store items' do
  85. expect { upload_cache.remove_item(form_id) }.to raise_error(ActiveRecord::RecordNotFound)
  86. end
  87. end
  88. describe '.files_include_attachment?' do
  89. let(:files) do
  90. [
  91. { name: 'name.jpg', type: 'image/jpg' },
  92. { name: 'name.png', type: 'wrong' },
  93. { name: 'name2.exe' },
  94. { name: 'other.jpg' }
  95. ]
  96. end
  97. context 'when one of files match by name' do
  98. let(:attachment) { create(:store, :image, filename: 'other.jpg') }
  99. it 'returns true' do
  100. expect(described_class).to be_files_include_attachment files, attachment
  101. end
  102. end
  103. context 'when one of files match by name but not type' do
  104. let(:attachment) { create(:store, :image, filename: 'name.png') }
  105. it 'returns false' do
  106. expect(described_class).not_to be_files_include_attachment files, attachment
  107. end
  108. end
  109. context 'when one of files match by name and type' do
  110. let(:attachment) { create(:store, :image, filename: 'name.jpg') }
  111. it 'returns true' do
  112. expect(described_class).to be_files_include_attachment files, attachment
  113. end
  114. end
  115. context 'when no files match' do
  116. let(:attachment) { create(:store, :txt) }
  117. it 'returns false' do
  118. expect(described_class).not_to be_files_include_attachment files, attachment
  119. end
  120. end
  121. end
  122. end