upload_cache_spec.rb 2.6 KB

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