Skip to content

Commit

Permalink
Merge pull request #306 from amplication/fix/7549
Browse files Browse the repository at this point in the history
  • Loading branch information
mulygottlieb authored Dec 6, 2023
2 parents 6442473 + 4ff3ce8 commit a769b7d
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 20 deletions.
4 changes: 2 additions & 2 deletions plugins/auth-core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugins/auth-core/package.json
Original file line number Diff line number Diff line change
@@ -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": {},
Expand Down
14 changes: 8 additions & 6 deletions plugins/auth-core/src/core/create-auth-service-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Module> {
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);
Expand All @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions plugins/auth-core/src/templates/auth.service.spec.template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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,
Expand All @@ -99,8 +99,8 @@ describe("AuthService", () => {
await expect(
service.validateUser(
INVALID_CREDENTIALS.username,
INVALID_CREDENTIALS.password
)
INVALID_CREDENTIALS.password,
),
).resolves.toBe(null);
});
});
Expand Down
4 changes: 2 additions & 2 deletions plugins/auth-jwt/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugins/auth-jwt/package.json
Original file line number Diff line number Diff line change
@@ -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": {},
Expand Down
2 changes: 2 additions & 0 deletions plugins/auth-jwt/src/core/create-jwt-strategy-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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}`;
Expand Down
2 changes: 2 additions & 0 deletions plugins/auth-jwt/src/core/create-jwt-strategy-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class JwtStrategyBase

async validate(payload: ENTITY_NAME_INFO): Promise<ENTITY_NAME_INFO> {
const { username } = payload;
const user = await this.ENTITY_SERVICE.findOne({
const user = await this.ENTITY_SERVICE.FIND_ONE_FUNCTION({
where: { username },
});
if (!user) {
Expand Down
4 changes: 2 additions & 2 deletions plugins/auth-jwt/src/templates/jwt.strategy.template.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ describe("Testing the jwtStrategyBase.validate()", () => {
const userService = mock<ENTITY_SERVICE>();
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
Expand Down

0 comments on commit a769b7d

Please sign in to comment.