Back to Repositories

Testing Custom Directory Management in WPScan

This test suite validates the custom directories functionality in WPScan, focusing on content directory detection and CLI argument handling. The tests ensure proper initialization and error handling for WordPress content directory configurations.

Test Coverage Overview

The test suite provides comprehensive coverage of the CustomDirectories controller functionality.

Key areas tested include:
  • CLI options validation and parsing
  • Content directory detection and verification
  • Error handling for missing content directories
  • Integration with WPScan’s core configuration system

Implementation Analysis

The testing approach utilizes RSpec’s behavior-driven development patterns with extensive use of context blocks and expectation matchers. The implementation leverages RSpec’s subject/let syntax for clean test setup and employs mock objects to isolate the controller behavior.

Technical implementation features:
  • Shared context setup using before blocks
  • Mock expectations for directory detection
  • CLI argument parsing validation
  • Exception handling verification

Technical Details

Testing tools and configuration:
  • RSpec testing framework
  • Custom CLI argument parser
  • Mock object framework for isolation testing
  • Frozen string literal pragma
  • Controller-specific test helpers
  • Subject/let syntax for test setup

Best Practices Demonstrated

The test suite exemplifies several testing best practices including isolation of test cases, proper setup and teardown patterns, and comprehensive error condition coverage. Notable practices include:
  • Clear test case organization
  • Isolated test contexts
  • Descriptive test naming
  • Proper mock object usage
  • Comprehensive error scenario coverage

wpscanteam/wpscan

spec/app/controllers/custom_directories_spec.rb

            
# frozen_string_literal: true

describe WPScan::Controller::CustomDirectories do
  subject(:controller) { described_class.new }
  let(:target_url)     { 'http://ex.lo/' }
  let(:cli_args)       { "--url #{target_url}" }

  before do
    WPScan::ParsedCli.options = rspec_parsed_options(cli_args)
  end

  describe '#cli_options' do
    its(:cli_options) { should_not be_empty }
    its(:cli_options) { should be_a Array }

    it 'contains to correct options' do
      expect(controller.cli_options.map(&:to_sym)).to eq %i[wp_content_dir wp_plugins_dir]
    end
  end

  describe '#before_scan' do
    context 'when the content_dir is not found and not supplied' do
      before { expect(controller.target).to receive(:content_dir).and_return(nil) }

      it 'raises an exception' do
        expect { controller.before_scan }.to raise_error(WPScan::Error::WpContentDirNotDetected)
      end
    end

    context 'when content_dir found/supplied' do
      let(:cli_args) { "#{super()} --wp-content-dir wp-content" }

      it 'does not raise any error' do
        expect { controller.before_scan }.to_not raise_error
        expect(controller.target.content_dir).to eq WPScan::ParsedCli.wp_content_dir
      end
    end
  end
end