UNPKG

flamesshield-sdk

Version:

Flames Shield Smart rate limiting for Firebase Cloud Functions to protect your services from excessive requests

58 lines (57 loc) 3.07 kB
"use strict"; 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_limiter_1 = require("./rate_limiter"); describe('RateLimiter', () => { const failure = () => __awaiter(void 0, void 0, void 0, function* () { return false; }); const success = () => __awaiter(void 0, void 0, void 0, function* () { return true; }); const error = () => __awaiter(void 0, void 0, void 0, function* () { throw new Error('Failure'); }); beforeEach(() => { // Spy on console.log to verify verbose logging jest.spyOn(console, 'log').mockImplementation(() => { }); jest.spyOn(console, 'error').mockImplementation(() => { }); }); afterEach(() => { jest.restoreAllMocks(); }); it('should allow execution if tokens are available', () => __awaiter(void 0, void 0, void 0, function* () { expect(yield new rate_limiter_1.RateLimiter(success).checkAndUpdateQuota()).toBe(true); })); it('should not allow execution if tokens are not available', () => __awaiter(void 0, void 0, void 0, function* () { expect(yield new rate_limiter_1.RateLimiter(failure).checkAndUpdateQuota()).toBe(false); })); it('should handle an error effectively when thrown', () => __awaiter(void 0, void 0, void 0, function* () { yield expect(new rate_limiter_1.RateLimiter(error).checkAndUpdateQuota()).rejects.toThrow(rate_limiter_1.QuotaCheckError); })); it('should log verbose messages when verbose option is true', () => __awaiter(void 0, void 0, void 0, function* () { yield new rate_limiter_1.RateLimiter(success, { verbose: true }).checkAndUpdateQuota(); expect(console.log).toHaveBeenCalled(); })); it('should not log verbose messages when verbose option is false', () => __awaiter(void 0, void 0, void 0, function* () { yield new rate_limiter_1.RateLimiter(success, { verbose: false }).checkAndUpdateQuota(); expect(console.log).not.toHaveBeenCalled(); })); it('should log error details in verbose mode when an error occurs', () => __awaiter(void 0, void 0, void 0, function* () { try { yield new rate_limiter_1.RateLimiter(error, { verbose: true }).checkAndUpdateQuota(); } catch (e) { // Expected error } expect(console.error).toHaveBeenCalled(); })); });