upload_cache_spec.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'UploadCache', type: :request do
  4. let(:user) { create(:customer) }
  5. let(:auth) { user }
  6. let(:form_id) { SecureRandom.uuid }
  7. let(:upload_cache) { UploadCache.new(form_id) }
  8. # required for adding items to the Store
  9. before do
  10. UserInfo.current_user_id = user.id
  11. authenticated_as(auth)
  12. end
  13. describe '/upload_caches/:id' do
  14. context 'for POST requests' do
  15. it 'adds items to UploadCache' do
  16. params = {
  17. File: fixture_file_upload('upload/hello_world.txt', 'text/plain')
  18. }
  19. post "/api/v1/upload_caches/#{form_id}", params: params
  20. expect(response).to have_http_status(:ok)
  21. end
  22. it 'detects Content-Type for binary uploads' do
  23. params = {
  24. File: fixture_file_upload('upload/hello_world.txt', 'application/octet-stream')
  25. }
  26. post "/api/v1/upload_caches/#{form_id}", params: params
  27. expect(Store.last.preferences['Content-Type']).to eq('text/plain')
  28. end
  29. end
  30. context 'for DELETE requests' do
  31. before do
  32. 2.times do |iteration|
  33. upload_cache.add(
  34. data: "Can't touch this #{iteration}",
  35. filename: 'keep.txt',
  36. preferences: {
  37. 'Content-Type' => 'text/plain',
  38. },
  39. )
  40. end
  41. end
  42. it 'removes all form_id UploadCache items' do
  43. expect do
  44. delete "/api/v1/upload_caches/#{form_id}", as: :json
  45. end.to change(upload_cache, :attachments).to([])
  46. end
  47. context 'with invalid user' do
  48. let(:auth) { create(:customer) }
  49. it 'returns forbidden' do
  50. delete "/api/v1/upload_caches/#{form_id}", as: :json
  51. expect(response).to have_http_status(:forbidden)
  52. end
  53. end
  54. end
  55. end
  56. describe '/upload_caches/:id/items/:store_id' do
  57. context 'for DELETE requests' do
  58. before do
  59. upload_cache.add(
  60. data: "Can't touch this",
  61. filename: 'keep.txt',
  62. preferences: {
  63. 'Content-Type' => 'text/plain',
  64. },
  65. )
  66. end
  67. it 'removes a UploadCache item by given store id' do
  68. store_id = upload_cache.attachments.first.id
  69. delete "/api/v1/upload_caches/#{form_id}/items/#{store_id}", as: :json
  70. expect(Store.exists?(store_id)).to be(false)
  71. end
  72. context 'with invalid user' do
  73. let(:auth) { create(:customer) }
  74. it 'returns forbidden' do
  75. store_id = upload_cache.attachments.first.id
  76. delete "/api/v1/upload_caches/#{form_id}/items/#{store_id}", as: :json
  77. expect(response).to have_http_status(:forbidden)
  78. end
  79. end
  80. end
  81. end
  82. end