From fd9855c3be395614d8de0b1129826ec46e5104ab Mon Sep 17 00:00:00 2001 From: Stephen Date: Sat, 30 Sep 2023 07:49:40 +1000 Subject: [PATCH] increased reliability of auth rate limiting tests --- spec/beef/api/auth_rate_spec.rb | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/spec/beef/api/auth_rate_spec.rb b/spec/beef/api/auth_rate_spec.rb index 2bb4b9e38..c4485c19b 100644 --- a/spec/beef/api/auth_rate_spec.rb +++ b/spec/beef/api/auth_rate_spec.rb @@ -74,20 +74,36 @@ RSpec.describe 'BeEF API Rate Limit' do end # Give the server time to start-up - sleep 1 + sleep 3 - # Authenticate to REST API & pull the token from the response - @response = RestClient.post "#{RESTAPI_ADMIN}/login", { 'username': "#{@username}", 'password': "#{@password}" }.to_json, :content_type => :json + # Try to connect 3 times + (0..2).each do |again| + # Authenticate to REST API & pull the token from the response + if @response.nil? + print_info "Try to connect: " + again.to_s + begin + creds = { 'username': "#{@username}", 'password': "#{@password}" }.to_json + @response = RestClient.post "#{RESTAPI_ADMIN}/login", creds, :content_type => :json + rescue RestClient::ServerBrokeConnection, Errno::ECONNREFUSED # likely to be starting up still + rescue => error + print_error error.message + end + print_info "Rescue: sleep for 10 and try to connect again" + sleep 10 + end + end + expect(@response) .to be_truthy # confirm the test has connected to the server + print_info "Connection with server was successful" @token = JSON.parse(@response)['token'] end after(:all) do print_info "Shutting down server" - Process.kill("KILL",@pid) - Process.kill("KILL",@pids) + Process.kill("KILL",@pid) unless @pid.nil? + Process.kill("KILL",@pids) unless @pid.nil? end - xit 'adheres to auth rate limits' do + it 'adheres to auth rate limits' do passwds = (1..9).map { |i| "broken_pass"} passwds.push BEEF_PASSWD apis = passwds.map { |pswd| BeefRestClient.new('http', ATTACK_DOMAIN, '3000', BEEF_USER, pswd) }