Path: blob/master/spec/app/controllers/vuln_api_spec.rb
486 views
# frozen_string_literal: true12describe WPScan::Controller::VulnApi do3subject(:controller) { described_class.new }4let(:target_url) { 'http://ex.lo/' }5let(:cli_args) { "--url #{target_url}" }67before do8WPScan::ParsedCli.options = rspec_parsed_options(cli_args)9WPScan::DB::VulnApi.instance_variable_set(:@default_request_params, nil)10end1112describe '#cli_options' do13its(:cli_options) { should_not be_empty }14its(:cli_options) { should be_a Array }1516it 'contains to correct options' do17expect(controller.cli_options.map(&:to_sym)).to eq %i[api_token]18end19end2021describe '#before_scan' do22context 'when no --api-token provided' do23its(:before_scan) { should be nil }24end2526context 'when --api-token given' do27let(:cli_args) { "#{super()} --api-token token" }2829context 'when the token is invalid' do30before { expect(WPScan::DB::VulnApi).to receive(:status).and_return('status' => 'forbidden') }3132it 'raise an InvalidApiToken error' do33expect { controller.before_scan }.to raise_error(WPScan::Error::InvalidApiToken)34end35end3637context 'when the token is valid' do38context 'when the limit has been reached' do39before do40expect(WPScan::DB::VulnApi)41.to receive(:status)42.and_return('success' => true, 'plan' => 'free', 'requests_remaining' => 0)43end4445it 'raises an ApiLimitReached error' do46expect { controller.before_scan }.to raise_error(WPScan::Error::ApiLimitReached)47end48end4950context 'when a HTTP error, like a timeout' do51before do52expect(WPScan::DB::VulnApi)53.to receive(:status)54.and_return(55'http_error' => WPScan::Error::HTTP.new(56Typhoeus::Response.new(effective_url: 'mock-url', return_code: 28)57)58)59end6061it 'raises an HTTP error' do62expect { controller.before_scan }63.to raise_error(WPScan::Error::HTTP, 'HTTP Error: mock-url (Timeout was reached)')64end65end6667context 'when the token is valid and no HTTP error' do68before do69expect(WPScan::DB::VulnApi)70.to receive(:status)71.and_return('success' => true, 'plan' => 'free', 'requests_remaining' => requests)72end7374context 'when limited requests' do75let(:requests) { 100 }7677it 'sets the token and does not raise an error' do78expect { controller.before_scan }.to_not raise_error7980expect(WPScan::DB::VulnApi.token).to eql 'token'81end8283context 'when unlimited requests' do84let(:requests) { 'Unlimited' }8586it 'sets the token and does not raise an error' do87expect { controller.before_scan }.to_not raise_error8889expect(WPScan::DB::VulnApi.token).to eql 'token'90end91end92end93end94end95end9697context 'when token in ENV' do98before do99ENV[described_class::ENV_KEY] = 'token-from-env'100101expect(WPScan::DB::VulnApi)102.to receive(:status)103.and_return('success' => true, 'plan' => 'free', 'requests_remaining' => 'Unlimited')104end105106it 'sets the token and does not raise an error' do107expect { controller.before_scan }.to_not raise_error108109expect(WPScan::DB::VulnApi.token).to eql 'token-from-env'110end111end112end113end114115116