Shun Miyazawa 1 년 전
부모
커밋
3874e1fd58
2개의 변경된 파일82개의 추가작업 그리고 2개의 파일을 삭제
  1. 80 0
      apps/app/src/features/rate-limiter/middleware/factory.spec.ts
  2. 2 2
      apps/app/src/features/rate-limiter/middleware/factory.ts

+ 80 - 0
apps/app/src/features/rate-limiter/middleware/factory.spec.ts

@@ -0,0 +1,80 @@
+import { _consumePoints, POINTS_THRESHOLD } from './factory';
+
+const mocks = vi.hoisted(() => {
+  return {
+    comsumeMock: vi.fn(),
+  };
+});
+
+vi.mock('rate-limiter-flexible', () => ({
+  RateLimiterMongo: vi.fn().mockImplementation(() => {
+    return {
+      consume: mocks.comsumeMock,
+    };
+  }),
+}));
+
+describe('factory.ts', () => {
+  describe('_consumePoints()', () => {
+    it('Should consume points as 1 * THRESHOLD if maxRequest: 1 is specified', async() => {
+      // setup
+      const method = 'GET';
+      const key = 'test-key';
+      const maxRequests = 1;
+
+      // when
+      const pointsToConsume = POINTS_THRESHOLD / maxRequests;
+      await _consumePoints(method, key, { method, maxRequests });
+
+      // then
+      expect(mocks.comsumeMock).toHaveBeenCalledWith(key, pointsToConsume);
+      expect(maxRequests * pointsToConsume).toBe(POINTS_THRESHOLD);
+    });
+
+    it('Should consume points as 2 * THRESHOLD if maxRequest: 2 is specified', async() => {
+      // setup
+      const method = 'GET';
+      const key = 'test-key';
+      const maxRequests = 2;
+
+      // when
+      const pointsToConsume = POINTS_THRESHOLD / maxRequests;
+      await _consumePoints(method, key, { method, maxRequests });
+
+      // then
+      expect(mocks.comsumeMock).toHaveBeenCalledWith(key, pointsToConsume);
+      expect(maxRequests * pointsToConsume).toBe(POINTS_THRESHOLD);
+    });
+
+    it('Should consume points as 3 * THRESHOLD if maxRequest: 3 is specified', async() => {
+      // setup
+      const method = 'GET';
+      const key = 'test-key';
+      const maxRequests = 3;
+
+      // when
+      const pointsToConsume = POINTS_THRESHOLD / maxRequests;
+      await _consumePoints(method, key, { method, maxRequests });
+
+      // then
+      expect(mocks.comsumeMock).toHaveBeenCalledWith(key, pointsToConsume);
+      expect(maxRequests * pointsToConsume).toBe(POINTS_THRESHOLD);
+    });
+
+    it('Should consume points as 500 * THRESHOLD if maxRequest: 500 is specified', async() => {
+      // setup
+      const method = 'GET';
+      const key = 'test-key';
+      const maxRequests = 500;
+
+      // when
+      const pointsToConsume = POINTS_THRESHOLD / maxRequests;
+      await _consumePoints(method, key, { method, maxRequests });
+
+      // then
+      expect(mocks.comsumeMock).toHaveBeenCalledWith(key, pointsToConsume);
+      expect(maxRequests * pointsToConsume).toBe(POINTS_THRESHOLD);
+    });
+
+  });
+});

+ 2 - 2
apps/app/src/features/rate-limiter/middleware/factory.ts

@@ -19,7 +19,7 @@ const logger = loggerFactory('growi:middleware:api-rate-limit');
 // API_RATE_LIMIT_010_FOO_METHODS=GET,POST
 // API_RATE_LIMIT_010_FOO_MAX_REQUESTS=10
 
-const POINTS_THRESHOLD = 100;
+export const POINTS_THRESHOLD = 100;
 
 const opts: IRateLimiterMongoOptions = {
   storeClient: connection,
@@ -37,7 +37,7 @@ const keysWithRegExp = Object.keys(configWithRegExp).map(key => new RegExp(`^${k
 const valuesWithRegExp = Object.values(configWithRegExp);
 
 
-const _consumePoints = async(
+export const _consumePoints = async(
     method: string, key: string | null, customizedConfig?: IApiRateLimitConfig, maxRequestsMultiplier?: number,
 ) => {
   if (key == null) {