flamesshield-sdk
Version:
Flames Shield Smart rate limiting for Firebase Cloud Functions to protect your services from excessive requests
111 lines (110 loc) • 5.42 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const rate_limiting_enabled_1 = require("./rate_limiting_enabled");
class MockConfigurationApi {
constructor(booleanValues) {
this.booleanValues = new Map();
this.numberValues = new Map();
this.stringValues = new Map();
if (booleanValues) {
Object.entries(booleanValues).forEach(([key, value]) => {
this.booleanValues.set(key, value);
});
}
}
load() {
return __awaiter(this, void 0, void 0, function* () {
// Mock implementation does nothing
});
}
getBoolean(key) {
return this.booleanValues.get(key) || false;
}
getNumber(key) {
return this.numberValues.get(key) || 0;
}
getString(key) {
return this.stringValues.get(key) || '';
}
// Helper method for testing
setBoolean(key, value) {
this.booleanValues.set(key, value);
}
}
describe('RateLimitingEnabled', () => {
const originalEnv = process.env;
let consoleSpy;
beforeEach(() => {
jest.resetModules();
process.env = Object.assign({}, originalEnv);
consoleSpy = jest.spyOn(console, 'log').mockImplementation();
});
afterEach(() => {
consoleSpy.mockRestore();
});
afterAll(() => {
process.env = originalEnv;
});
it('constructor: should set enabled to false when config value is not set', () => {
const config = new MockConfigurationApi();
const rateLimiting = new rate_limiting_enabled_1.RateLimitingEnabled('myFunction', config);
expect(rateLimiting.enabled).toBe(false);
});
it('constructor: should set enabled to true when config value is true', () => {
// Generate the correct environment variable key using the same hashing function
const functionName = 'myFunction';
const envName = rate_limiting_enabled_1.RateLimitingEnabled.generateEnvName(functionName) + "_ENABLED";
const config = new MockConfigurationApi({
[envName]: true
});
const rateLimiting = new rate_limiting_enabled_1.RateLimitingEnabled(functionName, config);
expect(rateLimiting.enabled).toBe(true);
});
it('constructor: should set enabled to false when config value is false', () => {
// Generate the correct environment variable key using the same hashing function
const functionName = 'myFunction';
const envName = rate_limiting_enabled_1.RateLimitingEnabled.generateEnvName(functionName) + "_ENABLED";
const config = new MockConfigurationApi({
[envName]: false
});
const rateLimiting = new rate_limiting_enabled_1.RateLimitingEnabled(functionName, config);
expect(rateLimiting.enabled).toBe(false);
});
it('constructor: should not log when verbose is not set', () => {
const config = new MockConfigurationApi();
new rate_limiting_enabled_1.RateLimitingEnabled('myFunction', config);
expect(consoleSpy).not.toHaveBeenCalled();
});
it('constructor: should not log when verbose is false', () => {
const config = new MockConfigurationApi();
new rate_limiting_enabled_1.RateLimitingEnabled('myFunction', config, { verbose: false });
expect(consoleSpy).not.toHaveBeenCalled();
});
it('constructor: should log when verbose is true', () => {
const config = new MockConfigurationApi();
const functionName = 'myFunction';
new rate_limiting_enabled_1.RateLimitingEnabled(functionName, config, { verbose: true });
// Check that console.log was called with the expected messages
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining(`[FlameShield] Checking rate limiting configuration for function: ${functionName}`));
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining(`[FlameShield] Rate limiting for function ${functionName} is`));
});
it('generateEnvName: should generate correct environment variable name with hash', () => {
const envName = rate_limiting_enabled_1.RateLimitingEnabled.generateEnvName('myFunction');
// The actual value should match the pattern with a hash, not just the function name
expect(envName).toMatch(/^FLAMESHIELD_ENABLED_[0-9A-F]{12}$/);
});
it('generateEnvName: should return consistent values for the same function name', () => {
const envName1 = rate_limiting_enabled_1.RateLimitingEnabled.generateEnvName('myFunctionTwo');
const envName2 = rate_limiting_enabled_1.RateLimitingEnabled.generateEnvName('myFunctionTwo');
expect(envName1).toBe(envName2);
});
});