mirror of
https://github.com/toeverything/AFFiNE.git
synced 2024-12-25 03:53:19 +03:00
test(server): make testing more isolated (#4290)
This commit is contained in:
parent
a97fd486c3
commit
58a935b31d
@ -1,6 +1,6 @@
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
|
||||
import { SessionService } from '../../session';
|
||||
import { SessionModule } from '../../session';
|
||||
import { MAILER, MailService } from './mailer';
|
||||
import { NextAuthController } from './next-auth.controller';
|
||||
import { NextAuthOptionsProvider } from './next-auth-options';
|
||||
@ -9,9 +9,9 @@ import { AuthService } from './service';
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
imports: [SessionModule],
|
||||
providers: [
|
||||
AuthService,
|
||||
SessionService,
|
||||
AuthResolver,
|
||||
NextAuthOptionsProvider,
|
||||
MAILER,
|
||||
|
@ -1,19 +1,23 @@
|
||||
import KeyvRedis from '@keyv/redis';
|
||||
import { Global, Injectable, Module } from '@nestjs/common';
|
||||
import {
|
||||
FactoryProvider,
|
||||
Global,
|
||||
Inject,
|
||||
Injectable,
|
||||
Module,
|
||||
} from '@nestjs/common';
|
||||
import Redis from 'ioredis';
|
||||
import Keyv from 'keyv';
|
||||
|
||||
import { Config } from './config';
|
||||
|
||||
@Injectable()
|
||||
export class SessionService {
|
||||
private readonly cache: Keyv;
|
||||
private readonly prefix = 'session:';
|
||||
private readonly sessionTtl = 30 * 60 * 1000; // 30 min
|
||||
export const KeyvProvide = Symbol('KeyvProvide');
|
||||
|
||||
constructor(protected readonly config: Config) {
|
||||
export const KeyvProvider: FactoryProvider<Keyv> = {
|
||||
provide: KeyvProvide,
|
||||
useFactory(config: Config) {
|
||||
if (config.redis.enabled) {
|
||||
this.cache = new Keyv({
|
||||
return new Keyv({
|
||||
store: new KeyvRedis(
|
||||
new Redis(config.redis.port, config.redis.host, {
|
||||
username: config.redis.username,
|
||||
@ -23,9 +27,18 @@ export class SessionService {
|
||||
),
|
||||
});
|
||||
} else {
|
||||
this.cache = new Keyv();
|
||||
return new Keyv();
|
||||
}
|
||||
}
|
||||
},
|
||||
inject: [Config],
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class SessionService {
|
||||
private readonly prefix = 'session:';
|
||||
private readonly sessionTtl = 30 * 60 * 1000; // 30 min
|
||||
|
||||
constructor(@Inject(KeyvProvide) private readonly cache: Keyv) {}
|
||||
|
||||
/**
|
||||
* get session
|
||||
@ -54,7 +67,7 @@ export class SessionService {
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
providers: [SessionService],
|
||||
exports: [SessionService],
|
||||
providers: [KeyvProvider, SessionService],
|
||||
exports: [KeyvProvider, SessionService],
|
||||
})
|
||||
export class SessionModule {}
|
||||
|
@ -1,40 +1,44 @@
|
||||
/// <reference types="../global.d.ts" />
|
||||
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import test from 'ava';
|
||||
import ava, { TestFn } from 'ava';
|
||||
|
||||
import { ConfigModule } from '../config';
|
||||
import { SessionModule, SessionService } from '../session';
|
||||
|
||||
let session: SessionService;
|
||||
let module: TestingModule;
|
||||
const test = ava as TestFn<{
|
||||
session: SessionService;
|
||||
app: TestingModule;
|
||||
}>;
|
||||
|
||||
// cleanup database before each test
|
||||
test.beforeEach(async () => {
|
||||
const client = new PrismaClient();
|
||||
await client.$connect();
|
||||
await client.user.deleteMany({});
|
||||
await client.$disconnect();
|
||||
});
|
||||
|
||||
test.beforeEach(async () => {
|
||||
module = await Test.createTestingModule({
|
||||
imports: [ConfigModule.forRoot(), SessionModule],
|
||||
test.beforeEach(async t => {
|
||||
const module = await Test.createTestingModule({
|
||||
imports: [
|
||||
ConfigModule.forRoot({
|
||||
redis: {
|
||||
enabled: false,
|
||||
},
|
||||
}),
|
||||
SessionModule,
|
||||
],
|
||||
}).compile();
|
||||
session = module.get(SessionService);
|
||||
const session = module.get(SessionService);
|
||||
t.context.app = module;
|
||||
t.context.session = session;
|
||||
});
|
||||
|
||||
test.afterEach(async () => {
|
||||
await module.close();
|
||||
test.afterEach(async t => {
|
||||
await t.context.app.close();
|
||||
});
|
||||
|
||||
test('should be able to set session', async t => {
|
||||
const { session } = t.context;
|
||||
await session.set('test', 'value');
|
||||
t.is(await session.get('test'), 'value');
|
||||
});
|
||||
|
||||
test('should be expired by ttl', async t => {
|
||||
const { session } = t.context;
|
||||
await session.set('test', 'value', 100);
|
||||
t.is(await session.get('test'), 'value');
|
||||
await new Promise(resolve => setTimeout(resolve, 500));
|
||||
|
Loading…
Reference in New Issue
Block a user