Closed vytautas-pranskunas- closed 2 years ago
Where exactly are you binding the guard ? Is it global or supposed to be for a controller ? I don't see it in the code, but that should be an issue on my end
Could you put this in a git so it's easier to work with in a local environment?
@vytautas-pranskunas- I think I know what's going on, assuming you have that app.useGlobalGuards
line in your src/main.ts
file only
You're expecting that the bootstrap
function of your src/main.ts
to being called. But it won't unless you call it explicity in your test suite setup (due to how nodejs works)
You can circumvent that by removing the app.useGlobalGuards
in favor of this another approach:
import { Module } from '@nestjs/common';
import { APP_GUARD } from '@nestjs/core';
@Module({
providers: [
{
provide: APP_GUARD,
useClass: YourGuardClassHere,
},
],
})
export class AppModule {}
now you just need to add AppModule
in your imports
array if you want to enable the guard YourGuardClassHere
globally.
Sry I forgot to add @useguard(LocalAuth) decorator on e dpoint in example but I ha e it in my code.
Glilobal Jwt decorator gets called but local is not
make sure you have registered the LocalStrategy
as a provider.
Your AppModule
that you shared should be providers: [AppService, LocalStrategy]
I was doing this example code very quick. As I have mentioned it is working when I am starting server locally but it does not when in e2e tests. I have it registered as provider
as well as decorator:
I did: https://github.com/vytautas-pranskunas-/temp
if you run loclly and hit signin endpoint with any username you will see console output: console.log('validating local strategy');
meaning that local auth guard was visited.
If you run e2e tests you will see 'Unauthorized' message without console output
Or you can simply debug
Works fine
❯ pnpm test:e2e
> temp@0.0.1 test:e2e /home/jay/Documents/code/help/guard-e2e
> jest --config ./tests/jest-e2e.json --runInBand --verbose --silent --detectOpenHandles --coverage
FAIL src/features/auth/auth.e2e.spec.ts
Auth E2E
on sign in
✕ should return error if email was not found (98 ms)
● Auth E2E › on sign in › should return error if email was not found
expect(received).toContain(expected) // indexOf
Expected substring: "some message"
Received string: "{\"statusCode\":401,\"message\":\"Unauthorized\"}"
40 | expect(statusCode).toBe(401);
41 |
> 42 | expect(body).toContain('some message');
| ^
43 | });
44 | });
45 | });
at Object.<anonymous> (features/auth/auth.e2e.spec.ts:42:26)
You get the 401, you get the unauthorized response, this works as I would expect it to.
For further support please use our Discord channel (Support). We are using GitHub to track Bug Reports, Feature Requests, and Regressions.
nooo! because it never goes to AuthGuard - dont you see? I could get 401 but it should visit AuthGuard! same as when you run server
It does go to the AuthGuard
, and AuthGuard('local')
calls to passport, passport returns an error because there's a missing password
, Nest catches the error in the AuthGuard()
and returns a 401. this is how the AuthGuard()
works
Ah, OK I will try to add password when I am home. Thanks for now.
Is there an existing issue for this?
Current behavior
I have local auth strategy giard and it is beying called when running application but it is not called when running e2e tests. I have tried litterally everything. I see similar issue here ithout answer: https://github.com/nestjs/nest/issues/628
I have added all required elements to repro just could not run e2e tests. In case it works - I am using not express but fastify adapter so code can be changed a bit to:
Minimum reproduction code
https://codesandbox.io/s/proud-architecture-rrubq6?file=/src/guards/jwt-auth.guard.ts
Steps to reproduce
No response
Expected behavior
Guards should be called same like in normal call
Package
Other package
No response
NestJS version
No response
Packages versions
Node.js version
No response
In which operating systems have you tested?
Other
No response