test(server): utils (#10028)

This commit is contained in:
forehalo
2025-02-10 06:35:11 +00:00
parent 8f6b512cfd
commit c3f5b4abb4
40 changed files with 1577 additions and 2222 deletions
@@ -106,7 +106,7 @@ function gql(app: INestApplication, query: string) {
}
test.before(async ({ context }) => {
const { app } = await createTestingApp({
const app = await createTestingApp({
providers: [TestResolver, TestGateway],
controllers: [TestController],
});
@@ -3,7 +3,7 @@ import '../../plugins/config';
import { Controller, Get, HttpStatus, UseGuards } from '@nestjs/common';
import ava, { TestFn } from 'ava';
import Sinon from 'sinon';
import request, { type Response } from 'supertest';
import { type Response } from 'supertest';
import { AppModule } from '../../app.module';
import { ConfigModule } from '../../base/config';
@@ -13,12 +13,11 @@ import {
Throttle,
ThrottlerStorage,
} from '../../base/throttler';
import { AuthService, Public } from '../../core/auth';
import { createTestingApp, internalSignIn, TestingApp } from '../utils';
import { Public } from '../../core/auth';
import { createTestingApp, TestingApp } from '../utils';
const test = ava as TestFn<{
storage: ThrottlerStorage;
cookie: string;
app: TestingApp;
}>;
@@ -88,7 +87,7 @@ class NonThrottledController {
}
test.before(async t => {
const { app } = await createTestingApp({
const app = await createTestingApp({
imports: [
ConfigModule.forRoot({
throttler: {
@@ -108,11 +107,8 @@ test.before(async t => {
});
test.beforeEach(async t => {
await t.context.app.initTestingDB();
const { app } = t.context;
const auth = app.get(AuthService);
const u1 = await auth.signUp('u1@affine.pro', 'test');
t.context.cookie = await internalSignIn(app, u1.id);
await app.initTestingDB();
});
test.after.always(async t => {
@@ -137,8 +133,8 @@ test('should be able to prevent requests if limit is reached', async t => {
isBlocked: true,
timeToBlockExpire: 10,
});
const res = await request(app.getHttpServer())
.get('/nonthrottled/strict')
const res = await app
.GET('/nonthrottled/strict')
.expect(HttpStatus.TOO_MANY_REQUESTS);
const headers = rateLimitHeaders(res);
@@ -152,9 +148,7 @@ test('should be able to prevent requests if limit is reached', async t => {
test('should use default throttler for unauthenticated user when not specified', async t => {
const { app } = t.context;
const res = await request(app.getHttpServer())
.get('/nonthrottled/default')
.expect(200);
const res = await app.GET('/nonthrottled/default').expect(200);
const headers = rateLimitHeaders(res);
@@ -165,9 +159,7 @@ test('should use default throttler for unauthenticated user when not specified',
test('should skip throttler for unauthenticated user when specified', async t => {
const { app } = t.context;
let res = await request(app.getHttpServer())
.get('/nonthrottled/skip')
.expect(200);
let res = await app.GET('/nonthrottled/skip').expect(200);
let headers = rateLimitHeaders(res);
@@ -175,7 +167,7 @@ test('should skip throttler for unauthenticated user when specified', async t =>
t.is(headers.remaining, undefined!);
t.is(headers.reset, undefined!);
res = await request(app.getHttpServer()).get('/throttled/skip').expect(200);
res = await app.GET('/throttled/skip').expect(200);
headers = rateLimitHeaders(res);
@@ -187,9 +179,7 @@ test('should skip throttler for unauthenticated user when specified', async t =>
test('should use specified throttler for unauthenticated user', async t => {
const { app } = t.context;
const res = await request(app.getHttpServer())
.get('/nonthrottled/strict')
.expect(200);
const res = await app.GET('/nonthrottled/strict').expect(200);
const headers = rateLimitHeaders(res);
@@ -199,12 +189,10 @@ test('should use specified throttler for unauthenticated user', async t => {
// ==== authenticated user visits ====
test('should not protect unspecified routes', async t => {
const { app, cookie } = t.context;
const { app } = t.context;
const res = await request(app.getHttpServer())
.get('/nonthrottled/default')
.set('Cookie', cookie)
.expect(200);
await app.signup('u1@affine.pro');
const res = await app.GET('/nonthrottled/default').expect(200);
const headers = rateLimitHeaders(res);
@@ -214,12 +202,10 @@ test('should not protect unspecified routes', async t => {
});
test('should use default throttler for authenticated user when not specified', async t => {
const { app, cookie } = t.context;
const { app } = t.context;
const res = await request(app.getHttpServer())
.get('/throttled/default')
.set('Cookie', cookie)
.expect(200);
await app.signup('u1@affine.pro');
const res = await app.GET('/throttled/default').expect(200);
const headers = rateLimitHeaders(res);
@@ -228,22 +214,17 @@ test('should use default throttler for authenticated user when not specified', a
});
test('should use same throttler for multiple routes', async t => {
const { app, cookie } = t.context;
const { app } = t.context;
let res = await request(app.getHttpServer())
.get('/throttled/default')
.set('Cookie', cookie)
.expect(200);
await app.signup('u1@affine.pro');
let res = await app.GET('/throttled/default').expect(200);
let headers = rateLimitHeaders(res);
t.is(headers.limit, '120');
t.is(headers.remaining, '119');
res = await request(app.getHttpServer())
.get('/throttled/default2')
.set('Cookie', cookie)
.expect(200);
res = await app.GET('/throttled/default2').expect(200);
headers = rateLimitHeaders(res);
@@ -252,22 +233,17 @@ test('should use same throttler for multiple routes', async t => {
});
test('should use different throttler if specified', async t => {
const { app, cookie } = t.context;
const { app } = t.context;
let res = await request(app.getHttpServer())
.get('/throttled/default')
.set('Cookie', cookie)
.expect(200);
await app.signup('u1@affine.pro');
let res = await app.GET('/throttled/default').expect(200);
let headers = rateLimitHeaders(res);
t.is(headers.limit, '120');
t.is(headers.remaining, '119');
res = await request(app.getHttpServer())
.get('/throttled/default3')
.set('Cookie', cookie)
.expect(200);
res = await app.GET('/throttled/default3').expect(200);
headers = rateLimitHeaders(res);
@@ -276,12 +252,10 @@ test('should use different throttler if specified', async t => {
});
test('should skip throttler for authenticated if `authenticated` throttler used', async t => {
const { app, cookie } = t.context;
const { app } = t.context;
const res = await request(app.getHttpServer())
.get('/throttled/authenticated')
.set('Cookie', cookie)
.expect(200);
await app.signup('u1@affine.pro');
const res = await app.GET('/throttled/authenticated').expect(200);
const headers = rateLimitHeaders(res);
@@ -290,12 +264,10 @@ test('should skip throttler for authenticated if `authenticated` throttler used'
t.is(headers.reset, undefined!);
});
test('should apply `default` throttler for authenticated user if `authenticated` throttler used', async t => {
test('should apply `default` throttler for unauthenticated user if `authenticated` throttler used', async t => {
const { app } = t.context;
const res = await request(app.getHttpServer())
.get('/throttled/authenticated')
.expect(200);
const res = await app.GET('/throttled/authenticated').expect(200);
const headers = rateLimitHeaders(res);
@@ -304,12 +276,10 @@ test('should apply `default` throttler for authenticated user if `authenticated`
});
test('should skip throttler for authenticated user when specified', async t => {
const { app, cookie } = t.context;
const { app } = t.context;
const res = await request(app.getHttpServer())
.get('/throttled/skip')
.set('Cookie', cookie)
.expect(200);
await app.signup('u1@affine.pro');
const res = await app.GET('/throttled/skip').expect(200);
const headers = rateLimitHeaders(res);
@@ -319,12 +289,10 @@ test('should skip throttler for authenticated user when specified', async t => {
});
test('should use specified throttler for authenticated user', async t => {
const { app, cookie } = t.context;
const { app } = t.context;
const res = await request(app.getHttpServer())
.get('/throttled/strict')
.set('Cookie', cookie)
.expect(200);
await app.signup('u1@affine.pro');
const res = await app.GET('/throttled/strict').expect(200);
const headers = rateLimitHeaders(res);
@@ -333,15 +301,13 @@ test('should use specified throttler for authenticated user', async t => {
});
test('should separate anonymous and authenticated user throttlers', async t => {
const { app, cookie } = t.context;
const { app } = t.context;
const authenticatedUserRes = await request(app.getHttpServer())
.get('/throttled/default')
.set('Cookie', cookie)
.expect(200);
const unauthenticatedUserRes = await request(app.getHttpServer())
.get('/nonthrottled/default')
const unauthenticatedUserRes = await app
.GET('/nonthrottled/default')
.expect(200);
await app.signup('u1@affine.pro');
const authenticatedUserRes = await app.GET('/throttled/default').expect(200);
const authenticatedResHeaders = rateLimitHeaders(authenticatedUserRes);
const unauthenticatedResHeaders = rateLimitHeaders(unauthenticatedUserRes);