add test to session repository

This commit is contained in:
João Geonizeli
2022-07-09 11:31:02 -03:00
parent b94dd8ee9b
commit 97dac843fe
4 changed files with 53 additions and 4 deletions

View 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();
});
});
});

View File

@@ -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> => {