upload_cache_spec.rb 2.1 KB

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