Arreglo de config de tests para jwt

This commit is contained in:
2026-01-27 10:00:51 +01:00
parent 1d8af66564
commit 453cb05228
11 changed files with 150 additions and 44 deletions

View File

@@ -21,7 +21,8 @@
"cors": "^2.8.5", "cors": "^2.8.5",
"dotenv": "^17.2.3", "dotenv": "^17.2.3",
"express": "^5.2.1", "express": "^5.2.1",
"typescript": "^5.9.3" "typescript": "^5.9.3",
"vite-tsconfig-paths": "^6.0.5"
}, },
"devDependencies": { "devDependencies": {
"@types/cors": "^2.8.19", "@types/cors": "^2.8.19",

View File

@@ -1,26 +1,3 @@
PORT=3000
RABBITMQ_USER=guest
RABBITMQ_PASSWORD=guest
ENVIORMENT=development
RABBITMQ_HOST=rabbitmq-sim-broker
#RABBITMQ_HOST=localhost
RABBITMQ_PORT=5672
RABBITMQ_USER=guest
RABBITMQ_PASSWORD=guest
RABBITMQ_SECURE=false
RABBITMQ_VHOST=sim-vhost
# Hay cosas que unificar de varios servicios
POSTGRES_DB=postgres
POSTGRES_DATABASE=postres
POSTGRES_HOST=postgresql-sim-1
POSTGRES_PORT=5432
DEV_POSTGRES_PORT=5432
POSTGRES_USER=postgres
POSTGRES_PASSWORD=1234
# claves de Objenious # claves de Objenious
OBJ_PEM_PATH=./obj.pem OBJ_PEM_PATH=./obj.pem
OBJ_AUTHORIZATION=XOc7FtwXD8hUX2SFVX94XSty8wkOmChkwDNF09O_aIxPubMDdFUdCDCB4zpzSIxi8nOcTg7r_LM_nmd5qm7uLbksf_XArjI8iAyhjKz_2BAXPhmvKs4Fc9f3vv5LDfCVrPB9lP8P7rJ66_qnWs4jvhLQxSfn29m96hgXeCf8oySdIDUjN2q9Js3KAS5LL52Ri6ryvUeO1PvMhaPQMWRqoHIqTV1wPfPtiqQwcjUPmu5GeW164Kq1JLgV3KaGzfCZ9Qv9lbv30EJrukXxWuLCAhBS0kzrBXZoWvf2pb9uh3Am_93_dDxiIGQfIap9ZU_m8ZD1HPgvZOMCY6ZkxQconQ OBJ_AUTHORIZATION=XOc7FtwXD8hUX2SFVX94XSty8wkOmChkwDNF09O_aIxPubMDdFUdCDCB4zpzSIxi8nOcTg7r_LM_nmd5qm7uLbksf_XArjI8iAyhjKz_2BAXPhmvKs4Fc9f3vv5LDfCVrPB9lP8P7rJ66_qnWs4jvhLQxSfn29m96hgXeCf8oySdIDUjN2q9Js3KAS5LL52Ri6ryvUeO1PvMhaPQMWRqoHIqTV1wPfPtiqQwcjUPmu5GeW164Kq1JLgV3KaGzfCZ9Qv9lbv30EJrukXxWuLCAhBS0kzrBXZoWvf2pb9uh3Am_93_dDxiIGQfIap9ZU_m8ZD1HPgvZOMCY6ZkxQconQ

View File

@@ -0,0 +1,15 @@
import { test, describe } from "vitest"
import { JWTService } from "./JWT.service"
import { loadEnvFile } from "node:process"
describe("Tokens Objenious", () => {
const jwtService = new JWTService()
loadEnvFile("packages/sim-consumidor-activaciones/.env")
console.log("test env", process.env.OBJ_CLIENT_ID)
test("Solicicitud normal de auth", async () => {
//const token = await jwtService.getAccessToken()
})
})

View File

@@ -6,7 +6,6 @@ import {
JWTToken JWTToken
} from "#shared/domain/JWT" } from "#shared/domain/JWT"
import axios from "axios"; import axios from "axios";
import { throwDeprecation } from "node:process";
type GrantAccessRequestBody = { type GrantAccessRequestBody = {
grant_type: string, grant_type: string,
@@ -15,7 +14,7 @@ type GrantAccessRequestBody = {
client_assertion: string client_assertion: string
} }
type GrantAccessRequestResponse = { type TokensRequestResponse = {
"access_token": string, "access_token": string,
"expires_in": number, "expires_in": number,
"refresh_token": string "refresh_token": string
@@ -79,8 +78,10 @@ export class JWTService {
let res; let res;
try { try {
res = (await req).data as GrantAccessRequestResponse; res = (await req).data as TokensRequestResponse;
this.authToken = new JWTToken(res.access_token)
this.refreshToken = new JWTToken(res.refresh_token)
return this.authToken
} catch (e) { } catch (e) {
const errorString = "No se ha podido conseguir el token de acceso de OBJENIOUS" const errorString = "No se ha podido conseguir el token de acceso de OBJENIOUS"
console.error(errorString, e) console.error(errorString, e)
@@ -107,7 +108,10 @@ export class JWTService {
let res; let res;
try { try {
res = (await req).data as GrantAccessRequestResponse; res = (await req).data as TokensRequestResponse;
this.authToken = new JWTToken(res.access_token)
this.refreshToken = new JWTToken(res.refresh_token)
return this.authToken
} catch (e) { } catch (e) {
const errorString = "No se ha podido conseguir el token de acceso de OBJENIOUS" const errorString = "No se ha podido conseguir el token de acceso de OBJENIOUS"
console.error(errorString, e) console.error(errorString, e)

View File

@@ -1,8 +1,9 @@
import { loadEnvFile } from "node:process"; import { loadEnvFile } from "node:process";
import path from "node:path";
loadEnvFile("../../../../.env") // Global
loadEnvFile("../../.env") // Especifica del servicio
loadEnvFile(path.join(__dirname, "../../../../.env")) // Global
loadEnvFile(path.join(__dirname, "../../.env")) // base
export const env = { export const env = {
ENVIRONMENT: process.env.ENVIORMENT, ENVIRONMENT: process.env.ENVIORMENT,
@@ -30,3 +31,4 @@ export const env = {
}; };
console.log("env", env)

View File

@@ -1,7 +1,7 @@
{ {
"extends": "../../tsconfig.json", "extends": "../../tsconfig.json",
"compilerOptions": { "compilerOptions": {
"outDir": "../../dist/sim-consumidor", "outDir": "../../dist/sim-consumidor-activaciones",
"baseUrl": ".", "baseUrl": ".",
"paths": { "paths": {
"#config/*": [ "#config/*": [
@@ -22,6 +22,9 @@
"#shared/*": [ "#shared/*": [
"../shared/*" "../shared/*"
], ],
"#root/*": [
"../../*"
],
} }
}, },
"exclude": [ "exclude": [

View File

@@ -0,0 +1,15 @@
import { defineConfig } from 'vitest/config'
import tsconfigPaths from 'vite-tsconfig-paths'
import { loadEnv } from 'vite'
export default defineConfig(({ mode }) => ({
test: {
env: loadEnv(mode, process.cwd(), '')
},
plugins: [
tsconfigPaths({
root: "./"
})
]
}))

View File

@@ -1,13 +1,15 @@
{ {
"extends": "@tsconfig/node22/tsconfig.json", "extends": "@tsconfig/node22/tsconfig.json",
"compilerOptions": { "compilerOptions": {
"outDir": "./dist", "outDir": "./dist",
"rootDir": "./" "rootDir": "./",
}, "paths": {}
"include": [ },
"**/*.ts" "include": [
], "**/*.ts",
"exclude": [ "tsconfig.vitest.json"
"dist" ],
] "exclude": [
"dist"
]
} }

15
tsconfig.vitest.json Normal file
View File

@@ -0,0 +1,15 @@
{
"extends": "./tsconfig.app.json",
"include": [
"vitest/**/*",
],
"exclude": [],
"compilerOptions": {
"composite": true,
"lib": [],
"types": [
"node",
"jsdom"
],
}
}

37
vitest.config.ts Normal file
View File

@@ -0,0 +1,37 @@
import { defineConfig } from 'vitest/config'
import tsconfigPaths from 'vite-tsconfig-paths'
import fs from 'node:fs'
import path from 'node:path'
const packagesDir = path.resolve(__dirname, 'packages')
const projects = [path.resolve(__dirname, 'tsconfig.json')]
if (fs.existsSync(packagesDir)) {
const packages = fs.readdirSync(packagesDir)
for (const pkg of packages) {
const tsconfigPath = path.join(packagesDir, pkg, 'tsconfig.json')
if (fs.existsSync(tsconfigPath)) {
projects.push(tsconfigPath)
}
}
}
export default defineConfig({
test: {
projects: [
'./packages/*',
{
extends: true,
envDir: "./"
}
]
},
plugins: [
tsconfigPaths({
root: "./",
projects
})
],
resolve: {
}
})

View File

@@ -990,7 +990,7 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"debug@npm:4, debug@npm:^4.3.4, debug@npm:^4.3.7, debug@npm:^4.4.0, debug@npm:^4.4.3": "debug@npm:4, debug@npm:^4.1.1, debug@npm:^4.3.4, debug@npm:^4.3.7, debug@npm:^4.4.0, debug@npm:^4.4.3":
version: 4.4.3 version: 4.4.3
resolution: "debug@npm:4.4.3" resolution: "debug@npm:4.4.3"
dependencies: dependencies:
@@ -1480,6 +1480,13 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"globrex@npm:^0.1.2":
version: 0.1.2
resolution: "globrex@npm:0.1.2"
checksum: 10/81ce62ee6f800d823d6b7da7687f841676d60ee8f51f934ddd862e4057316d26665c4edc0358d4340a923ac00a514f8b67c787e28fe693aae16350f4e60d55e9
languageName: node
linkType: hard
"gopd@npm:^1.2.0": "gopd@npm:^1.2.0":
version: 1.2.0 version: 1.2.0
resolution: "gopd@npm:1.2.0" resolution: "gopd@npm:1.2.0"
@@ -2396,6 +2403,7 @@ __metadata:
supertest: "npm:^7.1.4" supertest: "npm:^7.1.4"
tsx: "npm:^4.21.0" tsx: "npm:^4.21.0"
typescript: "npm:^5.9.3" typescript: "npm:^5.9.3"
vite-tsconfig-paths: "npm:^6.0.5"
vitest: "npm:^4.0.16" vitest: "npm:^4.0.16"
languageName: unknown languageName: unknown
linkType: soft linkType: soft
@@ -2635,6 +2643,20 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"tsconfck@npm:^3.0.3":
version: 3.1.6
resolution: "tsconfck@npm:3.1.6"
peerDependencies:
typescript: ^5.0.0
peerDependenciesMeta:
typescript:
optional: true
bin:
tsconfck: bin/tsconfck.js
checksum: 10/8574595286850273bf83319b4e67ca760088df3c36f7ca1425aaf797416672e854271bd31e75c9b3e1836ed5b66410c6bc38cbbda9c638a5416c6a682ed94132
languageName: node
linkType: hard
"tslib@npm:^2.1.0": "tslib@npm:^2.1.0":
version: 2.8.1 version: 2.8.1
resolution: "tslib@npm:2.8.1" resolution: "tslib@npm:2.8.1"
@@ -2738,6 +2760,19 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"vite-tsconfig-paths@npm:^6.0.5":
version: 6.0.5
resolution: "vite-tsconfig-paths@npm:6.0.5"
dependencies:
debug: "npm:^4.1.1"
globrex: "npm:^0.1.2"
tsconfck: "npm:^3.0.3"
peerDependencies:
vite: "*"
checksum: 10/1c3d38102ed34d057fc602c332bfd059bfedd0b378ee87b1a73eac89e20f6d81ee4bd9639557287e275cae2230f1d9225d2d7d83a2fa355a380cf77568f2cd31
languageName: node
linkType: hard
"vite@npm:^6.0.0 || ^7.0.0": "vite@npm:^6.0.0 || ^7.0.0":
version: 7.3.1 version: 7.3.1
resolution: "vite@npm:7.3.1" resolution: "vite@npm:7.3.1"