remove_spec.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Mutations::Form::UploadCache::Remove, type: :graphql do
  4. context 'when uploading files for a form', authenticated_as: :agent do
  5. let(:agent) { create(:agent) }
  6. let(:query) do
  7. <<~QUERY
  8. mutation formUploadCacheRemove($formId: FormId!, $fileIds: [ID!]!) {
  9. formUploadCacheRemove(formId: $formId, fileIds: $fileIds) {
  10. success
  11. }
  12. }
  13. QUERY
  14. end
  15. let(:form_id) { SecureRandom.uuid }
  16. let(:upload_cache_file) { UploadCache.new(form_id).add(filename: file_name, data: file_content, created_by_id: agent.id) }
  17. let(:file_name) { 'my_testfile.pdf' }
  18. let(:file_type) { 'application/pdf' }
  19. let(:file_content) { 'some test content' }
  20. let(:variables) { { formId: form_id, fileIds: [ gql.id(upload_cache_file) ] } }
  21. before do
  22. gql.execute(query, variables: variables)
  23. end
  24. it 'responds with success' do
  25. expect(gql.result.data).to eq({ 'success' => true })
  26. end
  27. it 'deletes upload cache entry' do
  28. expect(Store.find_by(id: upload_cache_file.id)).to be_nil
  29. end
  30. context 'when trying to delete a missing file' do
  31. before do
  32. # Execute query a second time
  33. gql.execute(query, variables: variables)
  34. end
  35. it 'fails' do
  36. expect(gql.result.error_type).to eq(ActiveRecord::RecordNotFound)
  37. end
  38. end
  39. context 'when trying to delete a not owned upload cache' do
  40. let(:upload_cache_file) { UploadCache.new(form_id).add(filename: file_name, data: file_content, created_by_id: 2) }
  41. it 'fails' do
  42. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  43. end
  44. end
  45. it_behaves_like 'graphql responds with error if unauthenticated'
  46. end
  47. end