Skip to content

Commit

Permalink
fix: remove the default JwtAuthGuard from /me (#628)
Browse files Browse the repository at this point in the history
* fix: remove the default JwtAuthGuard from /me

* fix: update-method spec in nestjs-authorized-service

* test: update the tests

---------

Co-authored-by: Mickael Martos <[email protected]>
  • Loading branch information
floross and Mickael-Martos authored Jun 15, 2023
1 parent 2e6d984 commit fe5fb60
Show file tree
Hide file tree
Showing 21 changed files with 86 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,16 @@ describe('generateCountMethod', () => {
expect(prismaParameters?.name).toEqual('prisma');
expect(prismaParameters?.kind).toEqual(30);
expect(prismaParameters?.kind).toEqual(StructureKind.Parameter);
expect(prismaParameters?.type).toEqual(`Prisma.UserDelegate<undefined>`);
expect(prismaParameters?.type).toEqual(
`Prisma.UserDelegate<GlobalRejectSettings>`,
);
});

it('generates a method declaration with the correct statements', () => {
expect(
compressWhitespace((methodDeclaration.statements as string[]).join('\n')),
).toEqual(
`const where = { AND: [abilities ? accessibleBy(abilities).User : {}, args?.where ?? {}], }; return this.userService.count<T>({ ...args, where });`,
`const where = { AND: [abilities ? accessibleBy(abilities).User : {}, args?.where ?? {}], }; return this.userService.count<T, GlobalRejectSettings>({ ...args, where });`,
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ describe('generateCreateMethod', () => {
expect(prismaParameters?.name).toEqual('prisma');
expect(prismaParameters?.kind).toEqual(30);
expect(prismaParameters?.kind).toEqual(StructureKind.Parameter);
expect(prismaParameters?.type).toEqual(`Prisma.UserDelegate<undefined>`);
expect(prismaParameters?.type).toEqual(
`Prisma.UserDelegate<GlobalRejectSettings>`,
);

expect(abilitiesParameters?.name).toEqual('abilities');
expect(abilitiesParameters?.kind).toEqual(30);
Expand All @@ -69,7 +71,7 @@ describe('generateCreateMethod', () => {
});

it('generates a method declaration with the correct statements', () => {
const expectedStatements = `const create = async(client: Prisma.UserDelegate<undefined>) => { const user = await this.userService.create<T>(args, client); if (abilities?.cannot(Action.Create, subject('User', user))) throw new ForbiddenException('cannot create User'); return user; } if (prisma) return create(prisma); return this.prisma.$transaction((client) => create(client.user));`;
const expectedStatements = `const create = async(client: Prisma.UserDelegate<undefined>) => { const user = await this.userService.create<T, GlobalRejectSettings>(args, client); if (abilities?.cannot(Action.Create, subject('User', user))) throw new ForbiddenException('cannot create User'); return user; } if (prisma) return create(prisma); return this.prisma.$transaction((client) => create(client.user));`;

expect(
compressWhitespace((methodDeclaration.statements as string[]).join('\n')),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,16 @@ describe('generateDeleteMethod', () => {
expect(prismaParameters?.name).toEqual('prisma');
expect(prismaParameters?.kind).toEqual(30);
expect(prismaParameters?.kind).toEqual(StructureKind.Parameter);
expect(prismaParameters?.type).toEqual(`Prisma.UserDelegate<undefined>`);
expect(prismaParameters?.type).toEqual(
`Prisma.UserDelegate<GlobalRejectSettings>`,
);
});

it('generates a method declaration with the correct statements', () => {
expect(
compressWhitespace((method.statements as string[]).join('\n')),
).toEqual(
`const deleteCb = async(client: Prisma.UserDelegate<undefined>) => { const user = await this.userService.delete<T>(args, client); if (abilities?.cannot(Action.Delete, subject('User', user))) throw new ForbiddenException('cannot delete User'); return user; } if (prisma) return deleteCb(prisma); return this.prisma.$transaction((client) => deleteCb(client.user));`,
`const deleteCb = async(client: Prisma.UserDelegate<undefined>) => { const user = await this.userService.delete<T, GlobalRejectSettings>(args, client); if (abilities?.cannot(Action.Delete, subject('User', user))) throw new ForbiddenException('cannot delete User'); return user; } if (prisma) return deleteCb(prisma); return this.prisma.$transaction((client) => deleteCb(client.user));`,
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ describe('generateFindManyMethod', () => {
expect(parameters?.[1].kind).toEqual(30); // corresponds to `StructureKind.Parameter`

expect(parameters?.[2].name).toEqual('prisma');
expect(parameters?.[2].type).toEqual(`Prisma.UserDelegate<undefined>`);
expect(parameters?.[2].type).toEqual(
`Prisma.UserDelegate<GlobalRejectSettings>`,
);
expect(parameters?.[2].kind).toEqual(30); // corresponds to `StructureKind.Parameter`
});

it('generates a method declaration with the correct statements', () => {
const expectedStatements = `const where = { AND: [abilities ? accessibleBy(abilities).User : {}, args?.where ?? {}], }; return this.userService.findMany<T>({ ...args, where }, prisma);`;
const expectedStatements = `const where = { AND: [abilities ? accessibleBy(abilities).User : {}, args?.where ?? {}], }; return this.userService.findMany<T, GlobalRejectSettings>({ ...args, where }, prisma);`;
expect(
compressWhitespace((generatedMethod.statements as string[]).join('\n')),
).toEqual(expectedStatements);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ describe('generateFindUniqueMethod', () => {
kind: StructureKind.TypeParameter,
constraint: `Prisma.UserFindUniqueArgs`,
},
{
name: 'GlobalRejectSettings',
kind: StructureKind.TypeParameter,
constraint: `Prisma.RejectOnNotFound | Prisma.RejectPerOperation | false | undefined`,
},
];

expect(methodDeclaration.typeParameters).toEqual(expectedTypeParameters);
Expand All @@ -64,7 +69,9 @@ describe('generateFindUniqueMethod', () => {

expect(prismaParameters?.name).toEqual('prisma');
expect(prismaParameters?.kind).toEqual(30); // StructureKind.Parameter is equal to 30
expect(prismaParameters?.type).toEqual(`Prisma.UserDelegate<undefined>`);
expect(prismaParameters?.type).toEqual(
`Prisma.UserDelegate<GlobalRejectSettings>`,
);

expect(abilitiesParameters?.name).toEqual('abilities');
expect(abilitiesParameters?.kind).toEqual(30); // StructureKind.Parameter is equal to 30
Expand All @@ -77,7 +84,7 @@ describe('generateFindUniqueMethod', () => {
expect(
compressWhitespace((methodDeclaration.statements as string[]).join('\n')),
).toBe(
`const user = await this.userService.findUnique<T>(args, prisma); if (user && abilities?.cannot(Action.Read, subject('User', user))) throw new ForbiddenException('cannot read this user'); return user`,
`const user = await this.userService.findUnique<T, GlobalRejectSettings>(args, prisma); if (user && abilities?.cannot(Action.Read, subject('User', user))) throw new ForbiddenException('cannot read this user'); return user`,
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,19 @@ describe('generateUpdateMethod', () => {
kind: StructureKind.TypeParameter,
constraint: `Prisma.UserUpdateArgs`,
},
{
name: 'GlobalRejectSettings',
kind: StructureKind.TypeParameter,
constraint:
'Prisma.RejectOnNotFound | Prisma.RejectPerOperation | false | undefined',
},
]);

// Check return type
expect(
compressWhitespace((methodDeclaration.statements as string[]).join('\n')),
).toBe(
`const update = async(client: Prisma.UserDelegate<undefined>) => { const user = await this.userService.update<T>(args, client); if (abilities?.cannot(Action.Update, subject('User', user))) throw new ForbiddenException('cannot update User'); return user; } if (prisma) return update(prisma); return this.prisma.$transaction((client) => update(client.user));`,
`const update = async(client: Prisma.UserDelegate<undefined>) => { const user = await this.userService.update<T, GlobalRejectSettings>(args, client); if (abilities?.cannot(Action.Update, subject('User', user))) throw new ForbiddenException('cannot update User'); return user; } if (prisma) return update(prisma); return this.prisma.$transaction((client) => update(client.user));`,
);

// TODO : a check for description ? see with Max
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ describe('generateAggregateMethod', () => {
expect(prismaParameters?.name).toEqual('prisma');
expect(prismaParameters?.kind).toEqual(30);
expect(prismaParameters?.kind).toEqual(StructureKind.Parameter);
expect(prismaParameters?.type).toEqual(`Prisma.UserDelegate<undefined>`);
expect(prismaParameters?.type).toEqual(
`Prisma.UserDelegate<GlobalRejectSettings>`,
);
});

it('generates a method declaration with the correct statements', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ describe('generateCountMethod', () => {
expect(prismaParameters?.name).toEqual('prisma');
expect(prismaParameters?.kind).toEqual(30);
expect(prismaParameters?.kind).toEqual(StructureKind.Parameter);
expect(prismaParameters?.type).toEqual(`Prisma.UserDelegate<undefined>`);
expect(prismaParameters?.type).toEqual(
`Prisma.UserDelegate<GlobalRejectSettings>`,
);
});

it('generates a method declaration with the correct statements', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ describe('generateCreateMethod', () => {
expect(prismaParameters?.name).toEqual('prisma');
expect(prismaParameters?.kind).toEqual(30);
expect(prismaParameters?.kind).toEqual(StructureKind.Parameter);
expect(prismaParameters?.type).toEqual(`Prisma.UserDelegate<undefined>`);
expect(prismaParameters?.type).toEqual(
`Prisma.UserDelegate<GlobalRejectSettings>`,
);
});

it('generates a method declaration with the correct statements', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ describe('generateCreateMethod', () => {
expect(prismaParameters?.name).toEqual('prisma');
expect(prismaParameters?.kind).toEqual(30);
expect(prismaParameters?.kind).toEqual(StructureKind.Parameter);
expect(prismaParameters?.type).toEqual(`Prisma.UserDelegate<undefined>`);
expect(prismaParameters?.type).toEqual(
`Prisma.UserDelegate<GlobalRejectSettings>`,
);
});

it('generates a method declaration with the correct statements', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ describe('generateDeleteMethod', () => {
expect(prismaParameters?.name).toEqual('prisma');
expect(prismaParameters?.kind).toEqual(30);
expect(prismaParameters?.kind).toEqual(StructureKind.Parameter);
expect(prismaParameters?.type).toEqual(`Prisma.UserDelegate<undefined>`);
expect(prismaParameters?.type).toEqual(
`Prisma.UserDelegate<GlobalRejectSettings>`,
);
});

it('generates a method declaration with the correct statements', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ describe('generateDeleteMethod', () => {
expect(prismaParameters?.name).toEqual('prisma');
expect(prismaParameters?.kind).toEqual(30);
expect(prismaParameters?.kind).toEqual(StructureKind.Parameter);
expect(prismaParameters?.type).toEqual(`Prisma.UserDelegate<undefined>`);
expect(prismaParameters?.type).toEqual(
`Prisma.UserDelegate<GlobalRejectSettings>`,
);
});

it('generates a method declaration with the correct statements', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('generateFindFirstMethod', () => {
const typeParameters: TypeParameterDeclarationStructure[] =
method.typeParameters as TypeParameterDeclarationStructure[];

expect(typeParameters.length).toEqual(1);
expect(typeParameters.length).toEqual(2);
expect(typeParameters[0].name).toEqual('T');
expect(typeParameters[0].constraint).toEqual(
`Prisma.${pascal(model.name)}FindFirstArgs`,
Expand All @@ -58,13 +58,15 @@ describe('generateFindFirstMethod', () => {
);

expect(parameters[1].name).toEqual('prisma');
expect(parameters[1].type).toEqual(`Prisma.UserDelegate<undefined>`);
expect(parameters[1].type).toEqual(
`Prisma.UserDelegate<GlobalRejectSettings>`,
);
expect(parameters[1].initializer).toEqual(`this.prismaClient.user`);
});

it('generates a method declaration with the correct statements', () => {
expect(compressWhitespace(method.statements as string)).toEqual(
`const user = await prisma.findFirst<T>(args); return user;`,
`const user = await prisma.findFirst<T, false>(args); return user;`,
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ describe('generateFindManyMethod', () => {
expect(parameters?.[0].kind).toEqual(30); // corresponds to `StructureKind.Parameter`

expect(parameters?.[1].name).toEqual('prisma');
expect(parameters?.[1].type).toEqual(`Prisma.UserDelegate<undefined>`);
expect(parameters?.[1].type).toEqual(
`Prisma.UserDelegate<GlobalRejectSettings>`,
);
expect(parameters?.[1].kind).toEqual(30); // corresponds to `StructureKind.Parameter`
expect(parameters?.[1].initializer).toEqual(`this.prismaClient.user`);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ describe('generateFindUniqueMethod', () => {
kind: StructureKind.TypeParameter,
constraint: `Prisma.UserFindUniqueArgs`,
},
{
name: 'GlobalRejectSettings',
kind: StructureKind.TypeParameter,
constraint: `Prisma.RejectOnNotFound | Prisma.RejectPerOperation | false | undefined`,
},
];

expect(methodDeclaration.typeParameters).toEqual(expectedTypeParameters);
Expand All @@ -64,12 +69,14 @@ describe('generateFindUniqueMethod', () => {

expect(prismaParameters?.name).toEqual('prisma');
expect(prismaParameters?.kind).toEqual(30); // StructureKind.Parameter is equal to 30
expect(prismaParameters?.type).toEqual(`Prisma.UserDelegate<undefined>`);
expect(prismaParameters?.type).toEqual(
`Prisma.UserDelegate<GlobalRejectSettings>`,
);
});

it('generates a method declaration with the correct statements', () => {
expect(compressWhitespace(methodDeclaration.statements as string)).toBe(
'const user = await prisma.findUnique<T>(args); return user;',
'const user = await prisma.findUnique<T, false>(args); return user;',
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('generateUpdateManyMethod', () => {
);
expect(methodDeclaration.parameters?.[1].name).toBe('prisma');
expect(methodDeclaration.parameters?.[1].type).toBe(
`Prisma.UserDelegate<undefined>`,
`Prisma.UserDelegate<GlobalRejectSettings>`,
);
expect(methodDeclaration.parameters?.[1].initializer).toBe(
`this.prismaClient.user`,
Expand All @@ -51,13 +51,16 @@ describe('generateUpdateManyMethod', () => {
kind: StructureKind.TypeParameter,
constraint: `Prisma.UserUpdateManyArgs`,
},
{
name: 'GlobalRejectSettings',
kind: StructureKind.TypeParameter,
constraint: `Prisma.RejectOnNotFound | Prisma.RejectPerOperation | false | undefined`,
},
]);

// Check return type
expect(methodDeclaration.statements).toBe(
`return prisma.updateMany<T>(args);`,
);

// TODO : a check for description ? see with Max
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ describe('generateUpdateMethod', () => {
kind: StructureKind.TypeParameter,
constraint: `Prisma.UserUpdateArgs`,
},
{
name: 'GlobalRejectSettings',
kind: StructureKind.TypeParameter,
constraint: `Prisma.RejectOnNotFound | Prisma.RejectPerOperation | false | undefined`,
},
]);

// Check return type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ describe('generateUpsertMethod', () => {

const prismaParameter = methodDeclaration.parameters?.[1];
expect(prismaParameter?.name).toBe('prisma');
expect(prismaParameter?.type).toBe(`Prisma.UserDelegate<undefined>`);
expect(prismaParameter?.type).toBe(
`Prisma.UserDelegate<GlobalRejectSettings>`,
);
expect(prismaParameter?.initializer).toBe(`this.prismaClient.user`);

expect(compressWhitespace(methodDeclaration.statements as string)).toBe(
Expand Down
13 changes: 4 additions & 9 deletions libs/nestjs/authentication/src/authentication.module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,6 @@ describe('Authentication Module', () => {

app = moduleFixture.createNestApplication();
await app.init();

app = moduleFixture.createNestApplication();

await app.init();
});

afterEach(async () => {
Expand All @@ -163,15 +159,14 @@ describe('Authentication Module', () => {
await request(app.getHttpServer()).post('/logout').expect(200);
});

it('/me should fail with 401 and reset cookie', async () => {
it('/me should fail with 400 and not reset cookie', async () => {
const response = await request(app.getHttpServer())
.get('/me')
.expect(401);
.expect(400);

const cookie = response.headers['set-cookie'][0];
const cookie = response.headers['set-cookie'];

expect(cookie).toMatch(/authCookie=/);
expect(cookie).toMatch(/Path=\//);
expect(cookie).toBeUndefined();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ describe('Login Controller', () => {

expect(response.body).toEqual(mockUser);
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(mockUserService.findUserById).toHaveBeenCalledTimes(2);
expect(mockUserService.findUserById).toHaveBeenCalledTimes(1);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export class LoginController {
}

@Get('me')
@UseGuards(JwtAuthGuard)
async me<U extends User = MinimalUser>(
@CurrentUser() currentUser: U,
): Promise<U> {
Expand Down

0 comments on commit fe5fb60

Please sign in to comment.