forked from docusealco/docuseal
-
Notifications
You must be signed in to change notification settings - Fork 0
CP-14339 - Guard against a missing area page in result attachment generation #77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe Submissions::GenerateResultAttachments do | ||
| let(:account) { create(:account) } | ||
| let(:user) { create(:user, account:) } | ||
| let(:template) { create(:template, account:, author: user) } | ||
| let(:submission) { create(:submission, template:, created_by_user: user) } | ||
| let(:submitter) { create(:submitter, submission:, uuid: SecureRandom.uuid) } | ||
| let(:attachment_uuid) { SecureRandom.uuid } | ||
|
|
||
| # A single-page in-memory PDF keyed by the area's attachment_uuid, so the | ||
| # `pdf.nil?` guard passes and we reach the `pdf.pages[area['page']]` index. | ||
| let(:pdfs_index) do | ||
| doc = HexaPDF::Document.new | ||
| doc.pages.add | ||
| { attachment_uuid => doc } | ||
| end | ||
|
|
||
| # Point the submitter's submission at a single text field whose one area is | ||
| # `area`, so `fill_submitter_fields` reaches the page lookup for that area. | ||
| def assign_field_area(area) | ||
| submitter.submission.update!( | ||
| template_fields: [ | ||
| { 'uuid' => SecureRandom.uuid, 'submitter_uuid' => submitter.uuid, | ||
| 'type' => 'text', 'areas' => [area] } | ||
| ] | ||
| ) | ||
| end | ||
|
|
||
| def fill | ||
| described_class.send( | ||
| :fill_submitter_fields, submitter, account, pdfs_index, | ||
| with_signature_id: false, is_flatten: false, with_headings: false | ||
| ) | ||
| end | ||
|
|
||
| before { allow(Rails.logger).to receive(:warn) } | ||
|
|
||
| describe '.fill_submitter_fields with a missing area page' do | ||
| context 'when the area omits the page key' do | ||
| before { assign_field_area('attachment_uuid' => attachment_uuid) } | ||
|
|
||
| it 'skips the area instead of raising (regression: HexaPDF pages[nil])' do | ||
| expect { fill }.not_to raise_error | ||
| end | ||
|
|
||
| it 'logs that the area was skipped' do | ||
| fill | ||
| expect(Rails.logger).to have_received(:warn).with(/Skipping field area with no page/) | ||
| end | ||
| end | ||
|
|
||
| context 'when the area page is explicitly nil' do | ||
| before { assign_field_area('attachment_uuid' => attachment_uuid, 'page' => nil) } | ||
|
|
||
| it 'skips the area instead of raising' do | ||
| expect { fill }.not_to raise_error | ||
| end | ||
|
|
||
| it 'logs that the area was skipped' do | ||
| fill | ||
| expect(Rails.logger).to have_received(:warn).with(/Skipping field area with no page/) | ||
| end | ||
| end | ||
|
|
||
| context 'when the area page is out of range' do | ||
| before { assign_field_area('attachment_uuid' => attachment_uuid, 'page' => 5) } | ||
|
|
||
| it 'does not raise (existing next-if-page-nil handling still applies)' do | ||
| expect { fill }.not_to raise_error | ||
| end | ||
|
|
||
| it 'does not log a skipped-no-page warning (the guard only catches nil)' do | ||
| fill | ||
| expect(Rails.logger).not_to have_received(:warn).with(/Skipping field area with no page/) | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is reasonable for now, if anyone complains about missing pages we can check logs and whatnot, but crashing completely isn't really acceptable lol