Correccion de los test
This commit is contained in:
@@ -54,24 +54,6 @@ services:
|
|||||||
- "traefik.http.routers.sf-nfc-server-secure.entrypoints=websecure"
|
- "traefik.http.routers.sf-nfc-server-secure.entrypoints=websecure"
|
||||||
- "traefik.http.routers.sf-nfc-server-secure.rule=Host(`sf-nfc-server.savefamilygps.net`)"
|
- "traefik.http.routers.sf-nfc-server-secure.rule=Host(`sf-nfc-server.savefamilygps.net`)"
|
||||||
- "traefik.http.routers.sf-nfc-server-secure.tls=true"
|
- "traefik.http.routers.sf-nfc-server-secure.tls=true"
|
||||||
- "traefik.http.routers.sf-nfc-server-secure.service=sf-sims"
|
- "traefik.http.routers.sf-nfc-server-secure.service=sf-nfc-server"
|
||||||
- "traefik.http.routers.sf-nfc-server-secure.tls.certresolver=myresolver"
|
|
||||||
- "traefik.http.services.sf-nfc-server.loadbalancer.server.port=${PORT}"
|
- "traefik.http.services.sf-nfc-server.loadbalancer.server.port=${PORT}"
|
||||||
- "traefik.docker.network=proxy"
|
- "traefik.docker.network=proxy"
|
||||||
|
|
||||||
postgresql-nfc:
|
|
||||||
container_name: postgresql-nfc
|
|
||||||
image: postgres:16.1
|
|
||||||
env_file:
|
|
||||||
- .env
|
|
||||||
ports:
|
|
||||||
- "${POSTGRES_PORT}:5432"
|
|
||||||
volumes:
|
|
||||||
- ./sql-data/:/var/lib/postgres/data
|
|
||||||
healthcheck:
|
|
||||||
test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
|
|
||||||
interval: 5s
|
|
||||||
retries: 5
|
|
||||||
start_period: 5s
|
|
||||||
timeout: 5s
|
|
||||||
command: -p 5432
|
|
||||||
|
|||||||
@@ -5,7 +5,8 @@
|
|||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "tsx --test --watch",
|
"test": "tsx --test",
|
||||||
|
"test:node": "node --import tsx --test --test-reporter spec --test-force-exit",
|
||||||
"dev": "tsx src/main.ts",
|
"dev": "tsx src/main.ts",
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"build:esbuild": "esbuild --bundle src/main.ts --outdir=dist --platform=node --format=esm --packages=external",
|
"build:esbuild": "esbuild --bundle src/main.ts --outdir=dist --platform=node --format=esm --packages=external",
|
||||||
|
|||||||
@@ -12,7 +12,10 @@ describe("NFC activation code (Private methods)", () => {
|
|||||||
HttpClient: httpclient
|
HttpClient: httpclient
|
||||||
}
|
}
|
||||||
|
|
||||||
const nfcUsecases = new NfcUsecases(serverContext, {} as NfcRepository)
|
const nfcUsecases = new NfcUsecases({
|
||||||
|
serverContext: serverContext,
|
||||||
|
nfcRepository: {} as NfcRepository
|
||||||
|
})
|
||||||
|
|
||||||
test("Should generate 8 digit codes", () => {
|
test("Should generate 8 digit codes", () => {
|
||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
@@ -29,7 +32,6 @@ describe("NFC activation code (Private methods)", () => {
|
|||||||
const validation = nfcUsecases.validateActivationCode({
|
const validation = nfcUsecases.validateActivationCode({
|
||||||
code
|
code
|
||||||
})
|
})
|
||||||
console.log("Codigo, validation", code, validation)
|
|
||||||
assert(validation == true)
|
assert(validation == true)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -60,7 +62,10 @@ describe("NfcUsecases Unit Tests (Public methods with Mocks)", () => {
|
|||||||
HttpClient: {} as any,
|
HttpClient: {} as any,
|
||||||
};
|
};
|
||||||
|
|
||||||
nfcUsecases = new NfcUsecases(mockServerContext, mockNfcRepository as unknown as NfcRepository);
|
nfcUsecases = new NfcUsecases({
|
||||||
|
serverContext: mockServerContext,
|
||||||
|
nfcRepository: mockNfcRepository as unknown as NfcRepository
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("generateActivationLabel", () => {
|
describe("generateActivationLabel", () => {
|
||||||
@@ -100,10 +105,16 @@ describe("NfcUsecases Unit Tests (Public methods with Mocks)", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("should generate new code if no existing codes found", async () => {
|
test("should generate new code if no existing codes found", async () => {
|
||||||
|
|
||||||
|
const newCode = "v1234567";
|
||||||
mockNfcRepository.findActivationCodes.mock.mockImplementation(async () => ({
|
mockNfcRepository.findActivationCodes.mock.mockImplementation(async () => ({
|
||||||
data: []
|
data: []
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
mockNfcRepository.createActivationCode.mock.mockImplementation(async () => ({
|
||||||
|
data: { card_id, code_plain: newCode }
|
||||||
|
}));
|
||||||
|
|
||||||
const usecase = nfcUsecases.generateActivationLabel();
|
const usecase = nfcUsecases.generateActivationLabel();
|
||||||
const result = await usecase({ card_id });
|
const result = await usecase({ card_id });
|
||||||
|
|
||||||
@@ -115,10 +126,16 @@ describe("NfcUsecases Unit Tests (Public methods with Mocks)", () => {
|
|||||||
|
|
||||||
test("should generate new code if override is true even if existing codes exist", async () => {
|
test("should generate new code if override is true even if existing codes exist", async () => {
|
||||||
const existingCode = "v1234567";
|
const existingCode = "v1234567";
|
||||||
|
const newCode = "v89101112";
|
||||||
|
|
||||||
mockNfcRepository.findActivationCodes.mock.mockImplementation(async () => ({
|
mockNfcRepository.findActivationCodes.mock.mockImplementation(async () => ({
|
||||||
data: [{ code_plain: existingCode }]
|
data: [{ code_plain: existingCode }]
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
mockNfcRepository.createActivationCode.mock.mockImplementation(async () => ({
|
||||||
|
data: { card_id, code_plain: newCode }
|
||||||
|
}));
|
||||||
|
|
||||||
const usecase = nfcUsecases.generateActivationLabel();
|
const usecase = nfcUsecases.generateActivationLabel();
|
||||||
const result = await usecase({ card_id, override: true });
|
const result = await usecase({ card_id, override: true });
|
||||||
|
|
||||||
|
|||||||
@@ -89,7 +89,6 @@ export class NfcUsecases {
|
|||||||
override?: boolean,
|
override?: boolean,
|
||||||
}): Promise<Result<string, CodeGenerationResultDTO>> => {
|
}): Promise<Result<string, CodeGenerationResultDTO>> => {
|
||||||
const generatedCodes = await this.nfcRepository.findActivationCodes(args.card_id)
|
const generatedCodes = await this.nfcRepository.findActivationCodes(args.card_id)
|
||||||
|
|
||||||
if (generatedCodes.error != undefined) {
|
if (generatedCodes.error != undefined) {
|
||||||
console.error(generatedCodes.error)
|
console.error(generatedCodes.error)
|
||||||
return generatedCodes // Caso de error
|
return generatedCodes // Caso de error
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
import test, { describe, before, after } from "node:test";
|
import test, { describe, before, after } from "node:test";
|
||||||
import assert from "node:assert";
|
import assert from "node:assert/strict";
|
||||||
import { httpclient } from "config/httpclient.config.js";
|
import { httpclient } from "config/httpclient.config.js";
|
||||||
import { pgClient } from "config/pgclient.config.js";
|
import { pgClient } from "config/pgclient.config.js";
|
||||||
import { NfcRepository } from "./Nfc.repository.js";
|
import { NfcRepository } from "./Nfc.repository.js";
|
||||||
import type { ServerContext } from "domain/ServerContext.js";
|
import type { ServerContext } from "domain/ServerContext.js";
|
||||||
import { uuidv7 } from "uuidv7";
|
import { uuidv7 } from "uuidv7";
|
||||||
|
|
||||||
describe("NfcRepository Integration Tests", () => {
|
await describe("NfcRepository Integration Tests", async () => {
|
||||||
const serverContext: ServerContext = {
|
const serverContext: ServerContext = {
|
||||||
PostgresClient: pgClient,
|
PostgresClient: pgClient,
|
||||||
HttpClient: httpclient
|
HttpClient: httpclient
|
||||||
@@ -16,6 +16,8 @@ describe("NfcRepository Integration Tests", () => {
|
|||||||
const testCardId = uuidv7()
|
const testCardId = uuidv7()
|
||||||
const testCode = "12345678";
|
const testCode = "12345678";
|
||||||
|
|
||||||
|
let lastCodeGenerated: string | undefined = undefined;
|
||||||
|
|
||||||
// Clean up before and after tests to ensure isolation
|
// Clean up before and after tests to ensure isolation
|
||||||
const cleanup = async () => {
|
const cleanup = async () => {
|
||||||
await pgClient.query("DELETE FROM activation_codes WHERE card_id = $1", [testCardId]);
|
await pgClient.query("DELETE FROM activation_codes WHERE card_id = $1", [testCardId]);
|
||||||
@@ -29,7 +31,7 @@ describe("NfcRepository Integration Tests", () => {
|
|||||||
await cleanup();
|
await cleanup();
|
||||||
});
|
});
|
||||||
|
|
||||||
test("createActivationCode should insert a record into the database", async () => {
|
await test("createActivationCode should insert a record into the database", async () => {
|
||||||
const result = await repo.createActivationCode({
|
const result = await repo.createActivationCode({
|
||||||
cardId: testCardId,
|
cardId: testCardId,
|
||||||
code: testCode
|
code: testCode
|
||||||
@@ -43,9 +45,11 @@ describe("NfcRepository Integration Tests", () => {
|
|||||||
assert.strictEqual(result.data.card_id, testCardId);
|
assert.strictEqual(result.data.card_id, testCardId);
|
||||||
assert.strictEqual(result.data.code_plain, testCode);
|
assert.strictEqual(result.data.code_plain, testCode);
|
||||||
assert.ok(result.data.code_hash, "Hash should be generated");
|
assert.ok(result.data.code_hash, "Hash should be generated");
|
||||||
|
|
||||||
|
lastCodeGenerated = result.data.code_plain;
|
||||||
});
|
});
|
||||||
|
|
||||||
test("findActivationCodes should retrieve the inserted record", async () => {
|
await test("findActivationCodes should retrieve the inserted record", async () => {
|
||||||
// We assume the previous test inserted the record, but lets be safe or just rely on sequence
|
// We assume the previous test inserted the record, but lets be safe or just rely on sequence
|
||||||
const result = await repo.findActivationCodes(testCardId);
|
const result = await repo.findActivationCodes(testCardId);
|
||||||
|
|
||||||
@@ -61,7 +65,7 @@ describe("NfcRepository Integration Tests", () => {
|
|||||||
assert.strictEqual(found.card_id, testCardId);
|
assert.strictEqual(found.card_id, testCardId);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("findActivationCodes should return empty array if card has no codes", async () => {
|
await test("findActivationCodes should return empty array if card has no codes", async () => {
|
||||||
const nonExistentCard = uuidv7()
|
const nonExistentCard = uuidv7()
|
||||||
const result = await repo.findActivationCodes(nonExistentCard);
|
const result = await repo.findActivationCodes(nonExistentCard);
|
||||||
|
|
||||||
@@ -73,4 +77,50 @@ describe("NfcRepository Integration Tests", () => {
|
|||||||
assert.ok(result.data, "Data should be returned");
|
assert.ok(result.data, "Data should be returned");
|
||||||
assert.strictEqual(result.data.length, 0);
|
assert.strictEqual(result.data.length, 0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await test("createActivationCode should add a new activation even if other exists", async () => {
|
||||||
|
const newCode = "secondcode"
|
||||||
|
const creation = await repo.createActivationCode({
|
||||||
|
cardId: testCardId,
|
||||||
|
code: newCode,
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await repo.findActivationCodes(testCardId);
|
||||||
|
|
||||||
|
if (result.error) {
|
||||||
|
assert.fail(`createActivationCode failed: ${result.error}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.ok(result.error == undefined, "Creation must be sucessfull")
|
||||||
|
assert.ok(result.data, "Data must be non-empty")
|
||||||
|
|
||||||
|
assert.ok(result.data, "Data should be returned");
|
||||||
|
assert.ok(result.data.length > 1, "Code list should be at least 2");
|
||||||
|
|
||||||
|
const lastCode = result.data[0]
|
||||||
|
assert.ok(lastCode, "First value of the list shouldnt be undefined")
|
||||||
|
assert.strictEqual(lastCode.card_id, testCardId, `${lastCode.card_id} != ${testCardId}`);
|
||||||
|
assert.strictEqual(lastCode.code_plain, newCode, `${lastCode.code_plain} != ${newCode}`);
|
||||||
|
|
||||||
|
lastCodeGenerated = newCode;
|
||||||
|
});
|
||||||
|
|
||||||
|
await test("findActivationCodes should return the last valid code first", async () => {
|
||||||
|
// We assume the previous test inserted the record, but lets be safe or just rely on sequence
|
||||||
|
const result = await repo.findActivationCodes(testCardId);
|
||||||
|
|
||||||
|
if (result.error) {
|
||||||
|
assert.fail(`findActivationCodes failed: ${result.error}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.ok(result.data, "Data should be returned");
|
||||||
|
|
||||||
|
assert.ok(Array.isArray(result.data));
|
||||||
|
|
||||||
|
const first = result.data[0];
|
||||||
|
assert.ok(first, "At least 1 code should exist");
|
||||||
|
assert.strictEqual(first.card_id, testCardId);
|
||||||
|
assert.strictEqual(first.code_plain, lastCodeGenerated);
|
||||||
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ export class NfcRepository {
|
|||||||
const query = `
|
const query = `
|
||||||
SELECT * FROM activation_codes
|
SELECT * FROM activation_codes
|
||||||
WHERE card_id = $1
|
WHERE card_id = $1
|
||||||
|
ORDER by code_id DESC
|
||||||
`
|
`
|
||||||
const values = [cardId]
|
const values = [cardId]
|
||||||
try {
|
try {
|
||||||
@@ -64,6 +65,8 @@ export class NfcRepository {
|
|||||||
return {
|
return {
|
||||||
error: e as string
|
error: e as string
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
conn.release()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user