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

@@ -18,7 +18,8 @@
"pg": "^8.7.3",
"redis": "^4.2.0",
"reflect-metadata": "^0.1.13",
"typeorm": "^0.3.7"
"typeorm": "^0.3.7",
"uuid": "^8.3.2"
},
"devDependencies": {
"@types/bcrypt": "^5.0.0",
@@ -26,6 +27,7 @@
"@types/jest": "^28.1.4",
"@types/jsonwebtoken": "^8.5.8",
"@types/node": "^18.0.3",
"@types/uuid": "^8.3.4",
"concurrently": "^7.2.2",
"jest": "^28.1.2",
"nodemon": "^2.0.19",

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

View File

@@ -794,6 +794,11 @@
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.1.tgz#20f18294f797f2209b5f65c8e3b5c8e8261d127c"
integrity sha512-Hl219/BT5fLAaz6NDkSuhzasy49dwQS/DSdu4MdggFB8zcXv7vflBI3xp7FEmkmdDkBUI2bPUNeMttp2knYdxw==
"@types/uuid@^8.3.4":
version "8.3.4"
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-8.3.4.tgz#bd86a43617df0594787d38b735f55c805becf1bc"
integrity sha512-c/I8ZRb51j+pYGAu5CrFMRxqZ2ke4y2grEBO5AUjgSkSk+qT2Ea+OdWElz/OiMf5MNpn2b17kuVBwZLQJXzihw==
"@types/yargs-parser@*":
version "21.0.0"
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-21.0.0.tgz#0c60e537fa790f5f9472ed2776c2b71ec117351b"