2023-09-01 19:59:33 +03:00
|
|
|
/// <reference types="../global.d.ts" />
|
|
|
|
|
|
|
|
import { Test, TestingModule } from '@nestjs/testing';
|
|
|
|
import { PrismaClient } from '@prisma/client';
|
2023-09-01 22:41:29 +03:00
|
|
|
import test from 'ava';
|
2023-09-01 19:59:33 +03:00
|
|
|
|
|
|
|
import { ConfigModule } from '../config';
|
|
|
|
import { SessionModule, SessionService } from '../session';
|
|
|
|
|
|
|
|
let session: SessionService;
|
|
|
|
let module: TestingModule;
|
|
|
|
|
|
|
|
// cleanup database before each test
|
2023-09-01 22:41:29 +03:00
|
|
|
test.beforeEach(async () => {
|
2023-09-01 19:59:33 +03:00
|
|
|
const client = new PrismaClient();
|
|
|
|
await client.$connect();
|
|
|
|
await client.user.deleteMany({});
|
2023-09-07 23:55:04 +03:00
|
|
|
await client.$disconnect();
|
2023-09-01 19:59:33 +03:00
|
|
|
});
|
|
|
|
|
2023-09-01 22:41:29 +03:00
|
|
|
test.beforeEach(async () => {
|
2023-09-01 19:59:33 +03:00
|
|
|
module = await Test.createTestingModule({
|
|
|
|
imports: [ConfigModule.forRoot(), SessionModule],
|
|
|
|
}).compile();
|
|
|
|
session = module.get(SessionService);
|
|
|
|
});
|
|
|
|
|
2023-09-01 22:41:29 +03:00
|
|
|
test.afterEach(async () => {
|
2023-09-01 19:59:33 +03:00
|
|
|
await module.close();
|
|
|
|
});
|
|
|
|
|
2023-09-01 22:41:29 +03:00
|
|
|
test('should be able to set session', async t => {
|
2023-09-01 19:59:33 +03:00
|
|
|
await session.set('test', 'value');
|
2023-09-05 11:01:45 +03:00
|
|
|
t.is(await session.get('test'), 'value');
|
2023-09-01 19:59:33 +03:00
|
|
|
});
|
|
|
|
|
2023-09-01 22:41:29 +03:00
|
|
|
test('should be expired by ttl', async t => {
|
2023-09-01 19:59:33 +03:00
|
|
|
await session.set('test', 'value', 100);
|
2023-09-05 11:01:45 +03:00
|
|
|
t.is(await session.get('test'), 'value');
|
2023-09-01 19:59:33 +03:00
|
|
|
await new Promise(resolve => setTimeout(resolve, 500));
|
2023-09-05 11:01:45 +03:00
|
|
|
t.is(await session.get('test'), undefined);
|
2023-09-01 19:59:33 +03:00
|
|
|
});
|