"ddos challenge" style script #4

Open
luna wants to merge 20 commits from claude/ddos-protection-challenge-01CMAtrK6Dt24x9Q3v6Gz9fS into mistress
Showing only changes of commit b31357b037 - Show all commits

View file

@ -27,10 +27,10 @@ local function createMockSharedDict()
end
function TestDDoSProtectionChallenge:setup()
-- First reset ngx to get the base ngx object
-- Reset ngx
resetNgx()
-- Create mock shared dictionaries
-- Create mock shared dictionaries AFTER resetNgx
ngx.shared = {
aproxy_bans = createMockSharedDict(),
aproxy_tokens = createMockSharedDict()
@ -39,19 +39,10 @@ function TestDDoSProtectionChallenge:setup()
-- Mock ngx.time for consistent testing
ngx.time = function() return 1000000 end
-- Add request method support
ngx.var.request_method = 'GET'
-- Add header setting support
ngx.header = {}
-- Add POST body reading support
ngx.req.read_body = function() end
ngx.req.get_post_args = function()
return ngx._post_args or {}
end
self.mod = setupTest('scripts.ddos_protection_challenge', {
-- Setup module manually (can't use setupTest because it calls resetNgx)
local ctx = require('ctx')
local config = require('config')
local test_config = {
ban_duration = 3600,
token_duration = 86400,
cookie_name = 'aproxy_token',
@ -60,7 +51,17 @@ function TestDDoSProtectionChallenge:setup()
protected_paths = {},
challenge_type = 'button',
pow_difficulty = 4
})
}
self.mod = require('scripts.ddos_protection_challenge')
local schema_errors = config.validateSchema(self.mod.config, test_config)
local count = table.pprint(schema_errors)
lu.assertIs(count, 0)
local state = self.mod.init(test_config)
ctx.compiled_chain = {
{self.mod, test_config, state}
}
end
function TestDDoSProtectionChallenge:testNoTokenShowsChallenge()
@ -212,25 +213,21 @@ end
TestDDoSProtectionChallengePaths = {}
function TestDDoSProtectionChallengePaths:setup()
-- First reset ngx to get the base ngx object
-- Reset ngx
resetNgx()
-- Create mock shared dictionaries
-- Create mock shared dictionaries AFTER resetNgx
ngx.shared = {
aproxy_bans = createMockSharedDict(),
aproxy_tokens = createMockSharedDict()
}
ngx.time = function() return 1000000 end
ngx.var.request_method = 'GET'
ngx.header = {}
ngx.req.read_body = function() end
ngx.req.get_post_args = function()
return ngx._post_args or {}
end
-- Configure with specific protected paths
self.mod = setupTest('scripts.ddos_protection_challenge', {
-- Setup module manually
local ctx = require('ctx')
local config = require('config')
local test_config = {
ban_duration = 3600,
token_duration = 86400,
cookie_name = 'aproxy_token',
@ -239,7 +236,17 @@ function TestDDoSProtectionChallengePaths:setup()
protected_paths = {'/api/.*', '/search'},
challenge_type = 'button',
pow_difficulty = 4
})
}
self.mod = require('scripts.ddos_protection_challenge')
local schema_errors = config.validateSchema(self.mod.config, test_config)
local count = table.pprint(schema_errors)
lu.assertIs(count, 0)
local state = self.mod.init(test_config)
ctx.compiled_chain = {
{self.mod, test_config, state}
}
end
function TestDDoSProtectionChallengePaths:testProtectedPathShowsChallenge()
@ -363,23 +370,21 @@ end
TestDDoSProtectionChallengeQuestion = {}
function TestDDoSProtectionChallengeQuestion:setup()
-- First reset ngx to get the base ngx object
-- Reset ngx
resetNgx()
-- Create mock shared dictionaries AFTER resetNgx
ngx.shared = {
aproxy_bans = createMockSharedDict(),
aproxy_tokens = createMockSharedDict()
}
ngx.time = function() return 1000000 end
ngx.var.request_method = 'GET'
ngx.header = {}
ngx.req.read_body = function() end
ngx.req.get_post_args = function()
return ngx._post_args or {}
end
self.mod = setupTest('scripts.ddos_protection_challenge', {
-- Setup module manually
local ctx = require('ctx')
local config = require('config')
local test_config = {
ban_duration = 3600,
token_duration = 86400,
cookie_name = 'aproxy_token',
@ -388,7 +393,17 @@ function TestDDoSProtectionChallengeQuestion:setup()
protected_paths = {},
challenge_type = 'question',
pow_difficulty = 4
})
}
self.mod = require('scripts.ddos_protection_challenge')
local schema_errors = config.validateSchema(self.mod.config, test_config)
local count = table.pprint(schema_errors)
lu.assertIs(count, 0)
local state = self.mod.init(test_config)
ctx.compiled_chain = {
{self.mod, test_config, state}
}
end
function TestDDoSProtectionChallengeQuestion:testQuestionChallengeShown()
@ -480,23 +495,21 @@ end
TestDDoSProtectionChallengePow = {}
function TestDDoSProtectionChallengePow:setup()
-- First reset ngx to get the base ngx object
-- Reset ngx
resetNgx()
-- Create mock shared dictionaries AFTER resetNgx
ngx.shared = {
aproxy_bans = createMockSharedDict(),
aproxy_tokens = createMockSharedDict()
}
ngx.time = function() return 1000000 end
ngx.var.request_method = 'GET'
ngx.header = {}
ngx.req.read_body = function() end
ngx.req.get_post_args = function()
return ngx._post_args or {}
end
self.mod = setupTest('scripts.ddos_protection_challenge', {
-- Setup module manually
local ctx = require('ctx')
local config = require('config')
local test_config = {
ban_duration = 3600,
token_duration = 86400,
cookie_name = 'aproxy_token',
@ -505,7 +518,17 @@ function TestDDoSProtectionChallengePow:setup()
protected_paths = {},
challenge_type = 'pow',
pow_difficulty = 4
})
}
self.mod = require('scripts.ddos_protection_challenge')
local schema_errors = config.validateSchema(self.mod.config, test_config)
local count = table.pprint(schema_errors)
lu.assertIs(count, 0)
local state = self.mod.init(test_config)
ctx.compiled_chain = {
{self.mod, test_config, state}
}
end
function TestDDoSProtectionChallengePow:testPowChallengeShown()