UNPKG

guardflux

Version:

A light callable lib to keep your API alive

98 lines (97 loc) 4.78 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 index_1 = require("./index"); const postgresql_1 = require("@mikro-orm/postgresql"); const mysql_1 = require("@mikro-orm/mysql"); const mongodb_1 = require("@mikro-orm/mongodb"); const core_1 = require("@mikro-orm/core"); const entity_1 = require("./lib/entity"); const Joi = require("joi"); describe('getDriver', () => { it('should return PostgreSqlDriver for postgresql', () => { expect((0, index_1.getDriver)('postgresql')).toBe(postgresql_1.PostgreSqlDriver); }); it('should return MySqlDriver for mysql', () => { expect((0, index_1.getDriver)('mysql')).toBe(mysql_1.MySqlDriver); }); it('should return MongoDriver for mongodb', () => { expect((0, index_1.getDriver)('mongodb')).toBe(mongodb_1.MongoDriver); }); it('should throw an error for unsupported database type', () => { expect(() => (0, index_1.getDriver)('unsupported')).toThrow('Unsupported database type'); }); }); describe('checkObject', () => { const testSchema = Joi.object({ name: Joi.string().required(), age: Joi.number().required() }); it('should return invalid result for empty object', () => __awaiter(void 0, void 0, void 0, function* () { const result = yield (0, index_1.checkObject)({}, testSchema); expect(result.isValid).toBe(false); expect(result.log).toBeDefined(); })); it('should return valid result for valid object', () => __awaiter(void 0, void 0, void 0, function* () { const result = yield (0, index_1.checkObject)({ name: 'John', age: 30 }, testSchema); expect(result.isValid).toBe(true); })); it('should return invalid result for invalid object', () => __awaiter(void 0, void 0, void 0, function* () { const result = yield (0, index_1.checkObject)({ name: 'John' }, testSchema); expect(result.isValid).toBe(false); expect(result.log).toBeDefined(); })); }); describe('rateLimit', () => { const mockDbConfig = { dbName: 'testDb', dbURI: 'mongodb://localhost:27017', dbType: 'mongodb', dbDebug: false }; const mockOptions = { route: '/test', maxRequests: 5, cycleTime: 60 }; beforeAll(() => __awaiter(void 0, void 0, void 0, function* () { const orm = yield core_1.MikroORM.init({ dbName: mockDbConfig.dbName, clientUrl: mockDbConfig.dbURI, entities: [entity_1.RateLimit], driver: (0, index_1.getDriver)(mockDbConfig.dbType), allowGlobalContext: true }); yield orm.getSchemaGenerator().dropSchema(); yield orm.getSchemaGenerator().createSchema(); })); it('should create a new rate limit record if none exists', () => __awaiter(void 0, void 0, void 0, function* () { const result = yield (0, index_1.rateLimit)('user1', mockOptions, mockDbConfig); expect(result.isValid).toBe(true); })); it('should reset request count if last request is older than cycle start', () => __awaiter(void 0, void 0, void 0, function* () { const result = yield (0, index_1.rateLimit)('user2', mockOptions, mockDbConfig); expect(result.isValid).toBe(true); })); it('should increment request count if below max requests', () => __awaiter(void 0, void 0, void 0, function* () { yield (0, index_1.rateLimit)('user3', mockOptions, mockDbConfig); const result = yield (0, index_1.rateLimit)('user3', mockOptions, mockDbConfig); expect(result.isValid).toBe(true); })); it('should return invalid result if max requests are exceeded', () => __awaiter(void 0, void 0, void 0, function* () { for (let i = 0; i < mockOptions.maxRequests; i++) { yield (0, index_1.rateLimit)('user4', mockOptions, mockDbConfig); } const result = yield (0, index_1.rateLimit)('user4', mockOptions, mockDbConfig); expect(result.isValid).toBe(false); expect(result.log).toBeDefined(); })); });