upload_cache_spec.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. end