add test to session repository
This commit is contained in:
42
server/src/repository/__tests__/session.repository.spec.ts
Normal file
42
server/src/repository/__tests__/session.repository.spec.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { RedisConnection } from "../../infra/redis";
|
||||
import { sessionRepository } from "../session.repository";
|
||||
import { v4 as uuid } from "uuid";
|
||||
describe("sessionRepository", () => {
|
||||
beforeAll(async () => {
|
||||
await RedisConnection.connect();
|
||||
});
|
||||
afterAll(async () => {
|
||||
await RedisConnection.disconnect();
|
||||
});
|
||||
|
||||
describe("saveSession", () => {
|
||||
it("should save a new session on redis", async () => {
|
||||
const sessionToken = uuid();
|
||||
|
||||
expect(await sessionRepository.saveSession(sessionToken)).toBeTruthy();
|
||||
expect(await sessionRepository.sessionExists(sessionToken)).toBeTruthy();
|
||||
});
|
||||
});
|
||||
describe("sessionExists", () => {
|
||||
it("should return true if session exists", async () => {
|
||||
const sessionToken = uuid();
|
||||
|
||||
expect(await sessionRepository.saveSession(sessionToken)).toBeTruthy();
|
||||
expect(await sessionRepository.sessionExists(sessionToken)).toBeTruthy();
|
||||
});
|
||||
|
||||
it("should return false if session does not exists", async () => {
|
||||
const sessionToken = uuid();
|
||||
expect(await sessionRepository.sessionExists(sessionToken)).toBeFalsy();
|
||||
});
|
||||
});
|
||||
describe("deleteByToken", () => {
|
||||
it("should remove session from redis", async () => {
|
||||
const sessionToken = uuid();
|
||||
|
||||
await sessionRepository.saveSession(sessionToken);
|
||||
expect(await sessionRepository.deleteSession(sessionToken)).toBeTruthy();
|
||||
expect(await sessionRepository.sessionExists(sessionToken)).toBeFalsy();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -3,13 +3,13 @@ import { RedisConnection } from "../infra/redis"
|
||||
const sessionExists = async (jwt: string): Promise<boolean> => {
|
||||
const result = await RedisConnection.get(jwt)
|
||||
|
||||
return result == "valid"
|
||||
return result === "EXIST"
|
||||
}
|
||||
|
||||
const saveSession = async (jwt: string): Promise<boolean> => {
|
||||
const result = await RedisConnection.set(jwt, 'valid')
|
||||
const result = await RedisConnection.set(jwt, 'EXIST')
|
||||
|
||||
return result == "valid"
|
||||
return result === "OK"
|
||||
}
|
||||
|
||||
const deleteSession = async (jwt: string): Promise<boolean> => {
|
||||
|
||||
Reference in New Issue
Block a user