Back to Repositories

Testing Document Submission Completion Process in docusealco/docuseal

This test suite validates the ProcessSubmitterCompletionJob functionality in a Ruby on Rails application, focusing on document submission completion and verification processes. The tests ensure proper handling of submitter completion, document processing, and error scenarios.

Test Coverage Overview

The test suite provides comprehensive coverage of the ProcessSubmitterCompletionJob functionality.

Key areas tested include:
  • Submitter completion record creation
  • Document completion verification
  • Error handling for invalid submitter IDs
  • Association validation between submitters, submissions, and accounts

Implementation Analysis

The implementation uses RSpec’s describe/it blocks with factory-based test data setup. The tests leverage let statements for efficient test object creation and employ expect blocks to verify state changes and record creation.

Notable patterns include:
  • Factory-based test data generation
  • Change expectation syntax for tracking record creation
  • Proper setup of encrypted certificates

Technical Details

Testing tools and configuration:
  • RSpec testing framework
  • FactoryBot for test data generation
  • SecureRandom for UUID generation
  • Rails test helpers and configurations
  • Encrypted configuration setup for E-sign certificates

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Isolated test scenarios with proper setup and teardown
  • Comprehensive validation of object attributes and relationships
  • Error case handling and validation
  • Clear and focused test descriptions
  • Efficient use of shared test data setup

docusealco/docuseal

spec/jobs/process_submitter_completion_job_spec.rb

            
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe ProcessSubmitterCompletionJob 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, completed_at: Time.current) }

  before do
    create(:encrypted_config, key: EncryptedConfig::ESIGN_CERTS_KEY,
                              value: GenerateCertificate.call.transform_values(&:to_pem))
  end

  describe '#perform' do
    it 'creates a completed submitter' do
      expect do
        described_class.new.perform('submitter_id' => submitter.id)
      end.to change(CompletedSubmitter, :count).by(1)

      completed_submitter = CompletedSubmitter.last
      submitter.reload

      expect(completed_submitter.submitter_id).to eq(submitter.id)
      expect(completed_submitter.submission_id).to eq(submitter.submission_id)
      expect(completed_submitter.account_id).to eq(submitter.submission.account_id)
      expect(completed_submitter.template_id).to eq(submitter.submission.template_id)
      expect(completed_submitter.source).to eq(submitter.submission.source)
    end

    it 'creates a completed document' do
      expect do
        described_class.new.perform('submitter_id' => submitter.id)
      end.to change(CompletedDocument, :count).by(1)

      completed_document = CompletedDocument.last

      expect(completed_document.submitter_id).to eq(submitter.id)
      expect(completed_document.sha256).to be_present
      expect(completed_document.sha256).to eq(submitter.documents.first.metadata['sha256'])
    end

    it 'raises an error if the submitter is not found' do
      expect do
        described_class.new.perform('submitter_id' => 'invalid_id')
      end.to raise_error(ActiveRecord::RecordNotFound)
    end
  end
end