Back to Repositories

Validating Application Setup Process in DocuSeal

This RSpec test suite validates the initial setup functionality of the DocuSeal application, covering user registration, account creation, and configuration settings. The tests ensure proper handling of form submissions and data persistence across multiple system components.

Test Coverage Overview

The test suite provides comprehensive coverage of the application setup process with multiple scenarios:

  • Initial setup page rendering and form field validation
  • Successful setup flow with valid user information
  • Account creation with encrypted configuration storage
  • Error handling for invalid password scenarios
  • Redirection logic for already configured applications

Implementation Analysis

The testing approach utilizes RSpec’s behavior-driven development patterns with system specs. It leverages factory-based test data generation and context-specific test organization.

Key implementation features include:
  • Page object interactions using Capybara
  • Network idle state monitoring
  • Database state verification
  • Encrypted configuration handling

Technical Details

Testing tools and configuration:
  • RSpec Rails integration
  • Capybara for browser simulation
  • Factory-based test data generation
  • Custom form filling helper methods
  • Database cleaner configuration
  • Network state monitoring capabilities

Best Practices Demonstrated

The test suite exemplifies several testing best practices including isolation of test scenarios, proper setup and teardown management, and comprehensive state verification.

Notable practices include:
  • Contextual organization of test cases
  • Proper use of before hooks and let blocks
  • Helper method extraction for common operations
  • Explicit state change expectations
  • Clear test case naming and organization

docusealco/docuseal

spec/system/setup_spec.rb

            
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'App Setup' do
  let(:form_data) do
    {
      first_name: 'John',
      last_name: 'Doe',
      email: '[email protected]',
      company_name: 'Example Company',
      password: 'password',
      app_url: 'https://example.com'
    }
  end

  before do
    visit setup_index_path
  end

  it 'shows the setup page' do
    expect(page).to have_content('Initial Setup')

    ['First name', 'Last name', 'Email', 'Company name', 'Password', 'App URL'].each do |field|
      expect(page).to have_field(field)
    end
  end

  context 'when valid information' do
    it 'setups the app' do
      fill_setup_form(form_data)

      expect do
        click_button 'Submit'
        page.driver.wait_for_network_idle
      end.to change(Account, :count).by(1).and change(User, :count).by(1).and change(EncryptedConfig, :count).by(2)

      user = User.last
      encrypted_config_app_url = EncryptedConfig.find_by(account: user.account,
                                                         key: EncryptedConfig::APP_URL_KEY)
      encrypted_config_esign_certs = EncryptedConfig.find_by(account: user.account,
                                                             key: EncryptedConfig::ESIGN_CERTS_KEY)

      expect(user.first_name).to eq(form_data[:first_name])
      expect(user.last_name).to eq(form_data[:last_name])
      expect(user.email).to eq(form_data[:email])
      expect(user.account.timezone).to eq('UTC')
      expect(user.account.locale).to eq('en-US')
      expect(user.account.name).to eq(form_data[:company_name])
      expect(encrypted_config_app_url.value).to eq(form_data[:app_url])
      expect(encrypted_config_esign_certs.value).to be_present
    end
  end

  context 'when invalid information' do
    it 'does not setup the app if the password is too short' do
      fill_setup_form(form_data.merge(password: 'pass'))

      expect do
        click_button 'Submit'
      end.not_to(change(User, :count))

      expect(page).to have_content('Password is too short (minimum is 6 characters)')
    end
  end

  context 'when the app is already setup' do
    let!(:user) { create(:user, account: create(:account)) }

    it 'redirects to the dashboard page' do
      sign_in(user)
      visit setup_index_path

      expect(page).to have_link('Create', href: new_template_path)
    end
  end

  private

  def fill_setup_form(form_data)
    fill_in 'First name', with: form_data[:first_name]
    fill_in 'Last name', with: form_data[:last_name]
    fill_in 'Email', with: form_data[:email]
    fill_in 'Company name', with: form_data[:company_name]
    fill_in 'Password', with: form_data[:password]
    fill_in 'App URL', with: form_data[:app_url]
  end
end