upload_cache_spec.rb 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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(:form_id) { SecureRandom.uuid }
  6. let(:upload_cache) { UploadCache.new(form_id) }
  7. # required for adding items to the Store
  8. before do
  9. UserInfo.current_user_id = 1
  10. authenticated_as(user)
  11. end
  12. describe '/upload_caches/:id' do
  13. context 'for POST requests' do
  14. it 'adds items to UploadCache' do
  15. params = {
  16. File: fixture_file_upload('upload/hello_world.txt', 'text/plain')
  17. }
  18. post "/api/v1/upload_caches/#{form_id}", params: params
  19. expect(response).to have_http_status(:ok)
  20. end
  21. it 'detects Content-Type for binary uploads' do
  22. params = {
  23. File: fixture_file_upload('upload/hello_world.txt', 'application/octet-stream')
  24. }
  25. post "/api/v1/upload_caches/#{form_id}", params: params
  26. expect(Store.last.preferences['Content-Type']).to eq('text/plain')
  27. end
  28. end
  29. context 'for DELETE requests' do
  30. before do
  31. 2.times do |iteration|
  32. upload_cache.add(
  33. data: "Can't touch this #{iteration}",
  34. filename: 'keep.txt',
  35. preferences: {
  36. 'Content-Type' => 'text/plain',
  37. },
  38. )
  39. end
  40. end
  41. it 'removes all form_id UploadCache items' do
  42. expect do
  43. delete "/api/v1/upload_caches/#{form_id}", as: :json
  44. end.to change(upload_cache, :attachments).to([])
  45. end
  46. end
  47. end
  48. describe '/upload_caches/:id/items/:store_id' do
  49. context 'for DELETE requests' do
  50. before do
  51. upload_cache.add(
  52. data: "Can't touch this",
  53. filename: 'keep.txt',
  54. preferences: {
  55. 'Content-Type' => 'text/plain',
  56. },
  57. )
  58. end
  59. it 'removes a UploadCache item by given store id' do
  60. store_id = upload_cache.attachments.first.id
  61. delete "/api/v1/upload_caches/#{form_id}/items/#{store_id}", as: :json
  62. expect(Store.exists?(store_id)).to be(false)
  63. end
  64. end
  65. end
  66. end