upload_cache_spec.rb 2.1 KB

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