add tests
This commit is contained in:
parent
5c90b2475d
commit
1f12d1b7a3
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,4 +1,5 @@
|
||||
.vscode
|
||||
.idea
|
||||
dist
|
||||
coverage
|
||||
node_modules
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
.vscode
|
||||
.idea
|
||||
node_modules
|
||||
coverage
|
||||
src
|
||||
.gitignore
|
||||
tsconfig.json
|
||||
4708
package-lock.json
generated
4708
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
38
package.json
38
package.json
@ -1,18 +1,27 @@
|
||||
{
|
||||
"name": "@apihub24/in-memory-sessions",
|
||||
"version": "1.0.0",
|
||||
"version": "1.0.1",
|
||||
"description": "",
|
||||
"main": "dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"scripts": {
|
||||
"build": "tsc"
|
||||
"build": "rimraf ./dist && tsc",
|
||||
"test": "jest",
|
||||
"test:coverage": "jest --coverage",
|
||||
"pub": "npm publish",
|
||||
"rel": "npm i && npm run build && npm run test:coverage"
|
||||
},
|
||||
"dependencies": {
|
||||
"@apihub24/token-authentication": "^1.0.0",
|
||||
"@apihub24/token-authentication": "^1.0.4",
|
||||
"@nestjs/common": "^11.1.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^5.9.2"
|
||||
"rimraf": "^6.0.1",
|
||||
"@nestjs/testing": "^11.1.6",
|
||||
"typescript": "^5.9.2",
|
||||
"@types/jest": "^30.0.0",
|
||||
"jest": "^30.0.0",
|
||||
"ts-jest": "^29.4.1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": {
|
||||
@ -23,5 +32,26 @@
|
||||
"license": "MIT",
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"jest": {
|
||||
"moduleFileExtensions": [
|
||||
"js",
|
||||
"json",
|
||||
"ts"
|
||||
],
|
||||
"rootDir": ".",
|
||||
"testRegex": ".*\\.spec\\.ts$",
|
||||
"transform": {
|
||||
"^.+\\.(t|j)s$": "ts-jest"
|
||||
},
|
||||
"collectCoverageFrom": [
|
||||
"**/*.(t|j)s"
|
||||
],
|
||||
"coverageDirectory": "./coverage",
|
||||
"testEnvironment": "node",
|
||||
"roots": [
|
||||
"<rootDir>/src/"
|
||||
],
|
||||
"moduleNameMapper": {}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,27 +1,29 @@
|
||||
import {
|
||||
Account,
|
||||
Session,
|
||||
SessionService,
|
||||
IAccount,
|
||||
ISession,
|
||||
ISessionService,
|
||||
} from "@apihub24/token-authentication";
|
||||
import { Injectable } from "@nestjs/common";
|
||||
|
||||
@Injectable()
|
||||
export class InMemorySessionService implements SessionService {
|
||||
private source: Session[] = [];
|
||||
export class InMemorySessionService implements ISessionService {
|
||||
private source: ISession[] = [];
|
||||
|
||||
create(account: Account): Promise<Session> {
|
||||
const s = new Session();
|
||||
s.id = crypto.randomUUID().toString();
|
||||
s.account = account;
|
||||
create(account: IAccount): Promise<ISession> {
|
||||
const s: ISession = {
|
||||
id: crypto.randomUUID().toString(),
|
||||
account,
|
||||
metaData: {},
|
||||
};
|
||||
this.source.push(s);
|
||||
return Promise.resolve(s);
|
||||
}
|
||||
|
||||
getBy(filter: (account: Account) => boolean): Promise<Session[]> {
|
||||
getBy(filter: (account: IAccount) => boolean): Promise<ISession[]> {
|
||||
return Promise.resolve(this.source.filter((x) => filter(x.account)));
|
||||
}
|
||||
|
||||
getById(sessionId: string): Promise<Session | null> {
|
||||
getById(sessionId: string): Promise<ISession | null> {
|
||||
return Promise.resolve(
|
||||
this.source.filter((x) => x.id === sessionId)[0] ?? null
|
||||
);
|
||||
|
||||
25
src/in.memory.sessions.module.spec.ts
Normal file
25
src/in.memory.sessions.module.spec.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { TestingModule, Test } from "@nestjs/testing";
|
||||
import {
|
||||
InMemorySessionsModule,
|
||||
APIHUB24_SESSION_SERVICE_INJECTION_KEY,
|
||||
} from "./";
|
||||
|
||||
describe("InMemorySessionsModule Tests", () => {
|
||||
let module: TestingModule | null = null;
|
||||
|
||||
beforeAll(async () => {
|
||||
module = await Test.createTestingModule({
|
||||
imports: [InMemorySessionsModule.forRoot()],
|
||||
}).compile();
|
||||
});
|
||||
|
||||
it("should export injection Key @apihub24/session_service", () => {
|
||||
expect(APIHUB24_SESSION_SERVICE_INJECTION_KEY).toBeDefined();
|
||||
});
|
||||
|
||||
it("should get InMemorySessionService by injection Key", () => {
|
||||
expect(module).toBeDefined();
|
||||
const service = module?.get("@apihub24/session_service");
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
||||
@ -1,12 +1,15 @@
|
||||
import { DynamicModule, Module } from '@nestjs/common';
|
||||
import { InMemorySessionService } from './in.memory.session.service';
|
||||
import { DynamicModule, Module } from "@nestjs/common";
|
||||
import { InMemorySessionService } from "./in.memory.session.service";
|
||||
|
||||
export const APIHUB24_SESSION_SERVICE_INJECTION_KEY =
|
||||
"@apihub24/session_service";
|
||||
|
||||
@Module({})
|
||||
export class InMemorySessionsModule {
|
||||
static forRoot(): DynamicModule {
|
||||
const providers = [
|
||||
{
|
||||
provide: '@apihub24/session_service',
|
||||
provide: APIHUB24_SESSION_SERVICE_INJECTION_KEY,
|
||||
useClass: InMemorySessionService,
|
||||
},
|
||||
];
|
||||
|
||||
103
src/in.memory.sessions.service.spec.ts
Normal file
103
src/in.memory.sessions.service.spec.ts
Normal file
@ -0,0 +1,103 @@
|
||||
import { Test, TestingModule } from "@nestjs/testing";
|
||||
import {
|
||||
APIHUB24_SESSION_SERVICE_INJECTION_KEY,
|
||||
InMemorySessionService,
|
||||
InMemorySessionsModule,
|
||||
} from "./";
|
||||
|
||||
describe("InMemorySessionService Tests", () => {
|
||||
let module: TestingModule | null = null;
|
||||
|
||||
beforeAll(async () => {
|
||||
module = await Test.createTestingModule({
|
||||
imports: [InMemorySessionsModule.forRoot()],
|
||||
}).compile();
|
||||
});
|
||||
|
||||
it("should create a new Session", async () => {
|
||||
expect(module).toBeDefined();
|
||||
const service = module?.get<InMemorySessionService>(
|
||||
APIHUB24_SESSION_SERVICE_INJECTION_KEY
|
||||
);
|
||||
expect(service).toBeDefined();
|
||||
|
||||
const createdSession = await service?.create({
|
||||
id: "1",
|
||||
accountName: "Test",
|
||||
email: "test@example.de",
|
||||
passwordHash: "123",
|
||||
active: true,
|
||||
emailVerified: true,
|
||||
groups: [],
|
||||
});
|
||||
expect(createdSession).toBeDefined();
|
||||
expect(createdSession?.id).toBeDefined();
|
||||
expect(createdSession?.account).toBeDefined();
|
||||
expect(createdSession?.metaData).toBeDefined();
|
||||
});
|
||||
|
||||
it("should get Session by Id", async () => {
|
||||
expect(module).toBeDefined();
|
||||
const service = module?.get<InMemorySessionService>(
|
||||
APIHUB24_SESSION_SERVICE_INJECTION_KEY
|
||||
);
|
||||
expect(service).toBeDefined();
|
||||
|
||||
const createdSession = await service?.create({
|
||||
id: "1",
|
||||
accountName: "Test",
|
||||
email: "test@example.de",
|
||||
passwordHash: "123",
|
||||
active: true,
|
||||
emailVerified: true,
|
||||
groups: [],
|
||||
});
|
||||
const selected = await service?.getById(createdSession?.id ?? "");
|
||||
expect(selected).toBeDefined();
|
||||
expect(selected?.id).toBe(createdSession?.id);
|
||||
});
|
||||
|
||||
it("should get Session by Filter", async () => {
|
||||
expect(module).toBeDefined();
|
||||
const service = module?.get<InMemorySessionService>(
|
||||
APIHUB24_SESSION_SERVICE_INJECTION_KEY
|
||||
);
|
||||
expect(service).toBeDefined();
|
||||
|
||||
const createdSession = await service?.create({
|
||||
id: "1",
|
||||
accountName: "Test",
|
||||
email: "test@example.de",
|
||||
passwordHash: "123",
|
||||
active: true,
|
||||
emailVerified: true,
|
||||
groups: [],
|
||||
});
|
||||
const selected = await service?.getBy(
|
||||
(x) => x.id === (createdSession?.account?.id ?? "")
|
||||
);
|
||||
expect(selected).toBeDefined();
|
||||
expect(selected?.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("should remove Session", async () => {
|
||||
expect(module).toBeDefined();
|
||||
const service = module?.get<InMemorySessionService>(
|
||||
APIHUB24_SESSION_SERVICE_INJECTION_KEY
|
||||
);
|
||||
expect(service).toBeDefined();
|
||||
|
||||
const createdSession = await service?.create({
|
||||
id: "1",
|
||||
accountName: "Test",
|
||||
email: "test@example.de",
|
||||
passwordHash: "123",
|
||||
active: true,
|
||||
emailVerified: true,
|
||||
groups: [],
|
||||
});
|
||||
await service?.remove(createdSession?.id ?? "");
|
||||
const selected = await service?.getById(createdSession?.id ?? "");
|
||||
expect(selected).toBeNull();
|
||||
});
|
||||
});
|
||||
@ -22,5 +22,6 @@
|
||||
"strictBindCallApply": false,
|
||||
"noFallthroughCasesInSwitch": false
|
||||
},
|
||||
"include": ["./src"]
|
||||
"include": ["./src"],
|
||||
"exclude": ["**/*.spec.ts", "**/*.mock.ts"]
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user