Skip to content

Commit

Permalink
test for filter with immediate
Browse files Browse the repository at this point in the history
  • Loading branch information
jrochkind committed Feb 25, 2025
1 parent fe28a97 commit 7f13204
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
40 changes: 40 additions & 0 deletions spec/controllers/enforce_immediate_filter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'rails_helper'

# We spec that the BotDetect filter is actually applying protection, as well as exempting what
# we want
describe DummyImmediateController, type: :controller do

# enable functionality, and reset config to fresh after any further changes
around(:each) do |example|
orig_config = BotChallengePage::BotChallengePageController.bot_challenge_config.dup
BotChallengePage::BotChallengePageController.bot_challenge_config.enabled = true

example.run

# reset config and rack-attack back to orig config
BotChallengePage::BotChallengePageController.bot_challenge_config = orig_config
BotChallengePage::BotChallengePageController.rack_attack_init
end

describe "when rack key requests bot challenge on protected controller" do
it "redirects even with no ENV request" do
get :index

expect(response).to have_http_status(307)
expect(response).to redirect_to(bot_detect_challenge_path(dest: dummy_immediate_path))
end

# we configured this to try to exempt fetch/ajax to #facet
it "does not redirect if we have stored a pass in session" do
request.session[BotChallengePage::BotChallengePageController.bot_challenge_config.session_passed_key] = {
BotChallengePage::BotChallengePageController::SESSION_DATETIME_KEY => Time.now.utc.iso8601,
BotChallengePage::BotChallengePageController::SESSION_IP_KEY => request.remote_ip
}

get :index

expect(response).to have_http_status(:success) # not a redirect
expect(response.body).to include "rendered action"
end
end
end
8 changes: 8 additions & 0 deletions spec/dummy/app/controllers/dummy_immediate_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class DummyImmediateController < ApplicationController
# with immediate:true
before_action { |controller| BotChallengePage::BotChallengePageController.bot_challenge_enforce_filter(controller, immediate: true) }

def index
render plain: "rendered action dummy"
end
end
1 change: 1 addition & 0 deletions spec/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
post "/challenge", to: "bot_challenge_page/bot_challenge_page#verify_challenge"

get "/dummy", to: "dummy#index", as: :dummy
get "/dummy_immediate", to: "dummy_immediate#index", as: :dummy_immediate
end

0 comments on commit 7f13204

Please sign in to comment.