Merge pull request #6566 from opf/fix/ignore_container_errors

allow adding attachments to invalid containers

[ci skip]
pull/6569/head
Oliver Günther 6 years ago committed by GitHub
commit 96f73ac5af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      app/services/add_attachment_service.rb
  2. 44
      spec/services/add_attachment_service_spec.rb

@ -59,7 +59,11 @@ class AddAttachmentService
# reload to get the newly added attachment # reload to get the newly added attachment
container.attachments.reload container.attachments.reload
container.add_journal author container.add_journal author
container.save! # We allow invalid containers to be saved as
# adding the attachments does not change the validity of the container
# but without that leeway, the user needs to fix the container before
# the attachment can be added.
container.save!(validate: false)
end end
end end
end end

@ -42,45 +42,57 @@ describe AddAttachmentService do
description: description description: description
end end
context 'happy path' do shared_examples 'successful creation' do
before do it 'saves the attachment' do
call_tested_method
end
it 'should save the attachment' do
attachment = Attachment.first attachment = Attachment.first
expect(attachment.filename).to eq 'foobar.txt' expect(attachment.filename).to eq 'foobar.txt'
expect(attachment.description).to eq description expect(attachment.description).to eq description
end end
it 'should add the attachment to the WP' do it 'adds the attachment to the WP' do
work_package.reload work_package.reload
expect(work_package.attachments).to include Attachment.first expect(work_package.attachments).to include Attachment.first
end end
it 'should add a journal entry on the WP' do it 'adds a journal entry on the WP' do
expect(work_package.journals.count).to eq 2 # 1 for WP creation + 1 for the attachment expect(work_package.journals.count).to eq 2 # 1 for WP creation + 1 for the attachment
end end
end end
context "can't save work package" do context 'happy path' do
before do
call_tested_method
end
it_behaves_like 'successful creation'
end
context "invalid container" do
before do before do
allow(work_package).to receive(:save!) # have an invalid work package
.and_raise(ActiveRecord::RecordInvalid.new(work_package)) work_package.subject = ''
call_tested_method
end
it_behaves_like 'successful creation'
end end
it 'should raise the exception' do context 'invalid attachment', with_settings: { attachment_max_size: 0 } do
expect { call_tested_method }.to raise_error ActiveRecord::RecordInvalid it 'raises the exception' do
expect { call_tested_method }
.to raise_exception ActiveRecord::RecordInvalid
end end
it 'should not save the attachment' do it 'does not create the attachment' do
begin begin
call_tested_method call_tested_method
rescue ActiveRecord::RecordInvalid rescue ActiveRecord::RecordInvalid
# we expect that to happen # expected
end end
expect(Attachment.count).to eq 0 expect(Attachment.count)
.to eq 0
end end
end end
end end

Loading…
Cancel
Save