From 2f2df954642346d8362ade23f70413bc10da496b Mon Sep 17 00:00:00 2001 From: Mor Hagbi Date: Wed, 6 Dec 2023 22:50:24 +0200 Subject: [PATCH 1/2] fix(auth-core): add dynamic authEntity find function name --- plugins/auth-core/package-lock.json | 4 ++-- plugins/auth-core/package.json | 2 +- .../auth-core/src/core/create-auth-service-spec.ts | 14 ++++++++------ .../src/templates/auth.service.spec.template.ts | 10 +++++----- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/plugins/auth-core/package-lock.json b/plugins/auth-core/package-lock.json index 2540ae3f..566b04a9 100644 --- a/plugins/auth-core/package-lock.json +++ b/plugins/auth-core/package-lock.json @@ -1,12 +1,12 @@ { "name": "@amplication/plugin-auth-core", - "version": "2.0.5", + "version": "2.0.6", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@amplication/plugin-auth-core", - "version": "2.0.5", + "version": "2.0.6", "license": "Apache-2.0", "dependencies": { "@amplication/code-gen-types": "^2.0.17", diff --git a/plugins/auth-core/package.json b/plugins/auth-core/package.json index f053d070..d5c4451c 100644 --- a/plugins/auth-core/package.json +++ b/plugins/auth-core/package.json @@ -1,6 +1,6 @@ { "name": "@amplication/plugin-auth-core", - "version": "2.0.5", + "version": "2.0.6", "description": "set auth for Amplication build", "main": "dist/index.js", "nx": {}, diff --git a/plugins/auth-core/src/core/create-auth-service-spec.ts b/plugins/auth-core/src/core/create-auth-service-spec.ts index ed00f76d..5bdbbfe8 100644 --- a/plugins/auth-core/src/core/create-auth-service-spec.ts +++ b/plugins/auth-core/src/core/create-auth-service-spec.ts @@ -13,18 +13,19 @@ import { removeTSClassDeclares, } from "../util/ast"; import { builders, namedTypes } from "ast-types"; +import { camelCase } from "lodash"; const authServiceSpecPath = join( templatesPath, - "auth.service.spec.template.ts" + "auth.service.spec.template.ts", ); export async function createAuthServiceSpec( - dsgContext: DsgContext + dsgContext: DsgContext, ): Promise { const { entities, resourceInfo, serverDirectories } = dsgContext; const authEntity = entities?.find( - (x) => x.name === resourceInfo?.settings.authEntityName + (x) => x.name === resourceInfo?.settings.authEntityName, ); if (!authEntity) { dsgContext.logger.error(AUTH_ENTITY_LOG_ERROR); @@ -40,18 +41,19 @@ export async function createAuthServiceSpec( const entityServiceImport = importNames( [authServiceNameId], - `../${entityNameToLower}/${entityNameToLower}.service` + `../${entityNameToLower}/${entityNameToLower}.service`, ); addImports( template, [entityServiceImport].filter( - (x) => x //remove nulls and undefined - ) as namedTypes.ImportDeclaration[] + (x) => x, //remove nulls and undefined + ) as namedTypes.ImportDeclaration[], ); const templateMapping = { ENTITY_SERVICE: builders.identifier(entityServiceName), + FIND_ONE_FUNCTION: builders.identifier(`${camelCase(authEntity?.name)}`), }; interpolate(template, templateMapping); diff --git a/plugins/auth-core/src/templates/auth.service.spec.template.ts b/plugins/auth-core/src/templates/auth.service.spec.template.ts index 83ccd362..b5d37909 100644 --- a/plugins/auth-core/src/templates/auth.service.spec.template.ts +++ b/plugins/auth-core/src/templates/auth.service.spec.template.ts @@ -32,7 +32,7 @@ const USER: any = { const SIGN_TOKEN = "SIGN_TOKEN"; const authEntityService = { - findOne(args: { where: { username: string } }): any | null { + FIND_ONE_FUNCTION(args: { where: { username: string } }): any | null { if (args.where.username === VALID_CREDENTIALS.username) { return USER; } @@ -86,8 +86,8 @@ describe("AuthService", () => { await expect( service.validateUser( VALID_CREDENTIALS.username, - VALID_CREDENTIALS.password - ) + VALID_CREDENTIALS.password, + ), ).resolves.toEqual({ username: USER.username, roles: USER.roles, @@ -99,8 +99,8 @@ describe("AuthService", () => { await expect( service.validateUser( INVALID_CREDENTIALS.username, - INVALID_CREDENTIALS.password - ) + INVALID_CREDENTIALS.password, + ), ).resolves.toBe(null); }); }); From 4ff3ce8b7cbe7a2f426c5220bb67a39e51609aa3 Mon Sep 17 00:00:00 2001 From: Mor Hagbi Date: Wed, 6 Dec 2023 22:51:36 +0200 Subject: [PATCH 2/2] fix(auth-jwt):add dynamic authEntity find function name --- plugins/auth-jwt/package-lock.json | 4 ++-- plugins/auth-jwt/package.json | 2 +- plugins/auth-jwt/src/core/create-jwt-strategy-base.ts | 2 ++ plugins/auth-jwt/src/core/create-jwt-strategy-spec.ts | 2 ++ plugins/auth-jwt/src/templates/jwt.strategy.template.base.ts | 2 +- plugins/auth-jwt/src/templates/jwt.strategy.template.spec.ts | 4 ++-- 6 files changed, 10 insertions(+), 6 deletions(-) diff --git a/plugins/auth-jwt/package-lock.json b/plugins/auth-jwt/package-lock.json index 34552fef..dadc78b8 100644 --- a/plugins/auth-jwt/package-lock.json +++ b/plugins/auth-jwt/package-lock.json @@ -1,12 +1,12 @@ { "name": "@amplication/plugin-auth-jwt", - "version": "1.4.5", + "version": "1.4.6", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@amplication/plugin-auth-jwt", - "version": "1.4.5", + "version": "1.4.6", "license": "ISC", "dependencies": { "@amplication/code-gen-types": "^2.0.19", diff --git a/plugins/auth-jwt/package.json b/plugins/auth-jwt/package.json index 5a1b2a5c..6f9cd542 100644 --- a/plugins/auth-jwt/package.json +++ b/plugins/auth-jwt/package.json @@ -1,6 +1,6 @@ { "name": "@amplication/plugin-auth-jwt", - "version": "1.4.5", + "version": "1.4.6", "description": "set jwt as provider for Amplication build", "main": "dist/index.js", "nx": {}, diff --git a/plugins/auth-jwt/src/core/create-jwt-strategy-base.ts b/plugins/auth-jwt/src/core/create-jwt-strategy-base.ts index c3b3afb3..bb4dee14 100644 --- a/plugins/auth-jwt/src/core/create-jwt-strategy-base.ts +++ b/plugins/auth-jwt/src/core/create-jwt-strategy-base.ts @@ -16,6 +16,7 @@ import { } from "../util/ast"; import { builders, namedTypes } from "ast-types"; import { print } from "@amplication/code-gen-utils"; +import { camelCase } from "lodash"; const jwtStrategyBasePath = join( templatesPath, @@ -76,6 +77,7 @@ async function mapJwtStrategyTemplate( const templateMapping = { ENTITY_NAME_INFO: builders.identifier(`${authEntity.name}Info`), ENTITY_SERVICE: builders.identifier(`${entityNameToLower}Service`), + FIND_ONE_FUNCTION: builders.identifier(`${camelCase(authEntity?.name) }`) }; const filePath = `${serverDirectories.authDirectory}/jwt/base/${fileName}`; diff --git a/plugins/auth-jwt/src/core/create-jwt-strategy-spec.ts b/plugins/auth-jwt/src/core/create-jwt-strategy-spec.ts index c330f3fa..8a4cc7d0 100644 --- a/plugins/auth-jwt/src/core/create-jwt-strategy-spec.ts +++ b/plugins/auth-jwt/src/core/create-jwt-strategy-spec.ts @@ -14,6 +14,7 @@ import { } from "../util/ast"; import { builders, namedTypes } from "ast-types"; import { print } from "@amplication/code-gen-utils"; +import { camelCase } from "lodash"; const jwtStrategySpecPath = join( templatesPath, @@ -65,6 +66,7 @@ async function mapJwtStrategySpecTemplate( const templateMapping = { ENTITY_SERVICE: builders.identifier(`${entityServiceName}`), + FIND_ONE_FUNCTION: builders.identifier(`${camelCase(authEntity?.name) }`) }; const filePath = `${serverDirectories.srcDirectory}/tests/auth/jwt/${fileName}`; diff --git a/plugins/auth-jwt/src/templates/jwt.strategy.template.base.ts b/plugins/auth-jwt/src/templates/jwt.strategy.template.base.ts index 30704cd9..ce1c55d3 100644 --- a/plugins/auth-jwt/src/templates/jwt.strategy.template.base.ts +++ b/plugins/auth-jwt/src/templates/jwt.strategy.template.base.ts @@ -20,7 +20,7 @@ export class JwtStrategyBase async validate(payload: ENTITY_NAME_INFO): Promise { const { username } = payload; - const user = await this.ENTITY_SERVICE.findOne({ + const user = await this.ENTITY_SERVICE.FIND_ONE_FUNCTION({ where: { username }, }); if (!user) { diff --git a/plugins/auth-jwt/src/templates/jwt.strategy.template.spec.ts b/plugins/auth-jwt/src/templates/jwt.strategy.template.spec.ts index 5865be96..0e098854 100644 --- a/plugins/auth-jwt/src/templates/jwt.strategy.template.spec.ts +++ b/plugins/auth-jwt/src/templates/jwt.strategy.template.spec.ts @@ -8,11 +8,11 @@ describe("Testing the jwtStrategyBase.validate()", () => { const userService = mock(); const jwtStrategy = new JwtStrategyBase("Secrete", userService); beforeEach(() => { - userService.findOne.mockClear(); + userService.FIND_ONE_FUNCTION.mockClear(); }); it("should throw UnauthorizedException where there is no user", async () => { //ARRANGE - userService.findOne + userService.FIND_ONE_FUNCTION .calledWith({ where: { username: TEST_USER.username } }) .mockReturnValue(Promise.resolve(null)); //ACT