upload_cache_spec.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe UploadCache do
  4. subject(:upload_cache) { described_class.new(1337) }
  5. # required for adding items to the Store
  6. before { UserInfo.current_user_id = 1 }
  7. describe '#initialize' do
  8. it 'converts given (form_)id to an Integer' do
  9. expect(described_class.new('1337').id).to eq(1337)
  10. end
  11. end
  12. describe '#add' do
  13. it 'adds a Store item' do
  14. expect do
  15. upload_cache.add(
  16. data: 'content_file3_normally_should_be_an_image',
  17. filename: 'some_file3.jpg',
  18. preferences: {
  19. 'Content-Type' => 'image/jpeg',
  20. 'Mime-Type' => 'image/jpeg',
  21. 'Content-Disposition' => 'attached',
  22. },
  23. )
  24. end.to change(Store, :count).by(1)
  25. end
  26. end
  27. describe '#attachments' do
  28. before do
  29. upload_cache.add(
  30. data: 'hello world',
  31. filename: 'some.txt',
  32. preferences: {
  33. 'Content-Type' => 'text/plain',
  34. },
  35. )
  36. end
  37. it 'returns all Store items' do
  38. attachments = upload_cache.attachments
  39. expect(attachments.count).to be(1)
  40. expect(attachments).to include(Store.last)
  41. end
  42. end
  43. describe '#destroy' do
  44. before do
  45. upload_cache.add(
  46. data: 'hello world',
  47. filename: 'some.txt',
  48. preferences: {
  49. 'Content-Type' => 'text/plain',
  50. },
  51. )
  52. upload_cache.add(
  53. data: 'hello other world',
  54. filename: 'another_some.txt',
  55. preferences: {
  56. 'Content-Type' => 'text/plain',
  57. },
  58. )
  59. end
  60. it 'removes all added Store items' do
  61. expect { upload_cache.destroy }.to change(Store, :count).by(-2)
  62. end
  63. end
  64. describe '#remove_item' do
  65. before do
  66. upload_cache.add(
  67. data: 'hello world',
  68. filename: 'some.txt',
  69. preferences: {
  70. 'Content-Type' => 'text/plain',
  71. },
  72. )
  73. end
  74. it 'removes the Store item matching the given ID' do
  75. expect { upload_cache.remove_item(Store.last.id) }.to change(Store, :count).by(-1)
  76. end
  77. it 'prevents removage of non UploadCache Store items' do
  78. item = create(:store,
  79. object: 'Ticket',
  80. o_id: 1,
  81. data: "Can't touch this",
  82. filename: 'keep.txt',
  83. preferences: {
  84. 'Content-Type' => 'text/plain',
  85. },)
  86. expect { upload_cache.remove_item(item.id) }.to raise_error(Exceptions::UnprocessableEntity)
  87. end
  88. it 'fails for non existing UploadCache Store items' do
  89. expect { upload_cache.remove_item(1337) }.to raise_error(ActiveRecord::RecordNotFound)
  90. end
  91. end
  92. end