forked from ZyntariHQ/Invoisio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth-testing.util.ts
More file actions
52 lines (46 loc) · 1.36 KB
/
Copy pathauth-testing.util.ts
File metadata and controls
52 lines (46 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { JwtModule, JwtService } from "@nestjs/jwt";
import { PassportModule } from "@nestjs/passport";
import { PassportStrategy } from "@nestjs/passport";
import { ExtractJwt, Strategy } from "passport-jwt";
import { TestingModule } from "@nestjs/testing";
import { MerchantRole } from "../../common/enums/merchant-role.enum";
export const TEST_JWT_SECRET = "test-secret-at-least-32-chars-0123456789";
class TestJwtStrategy extends PassportStrategy(Strategy, "jwt") {
constructor() {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
ignoreExpiration: false,
secretOrKey: TEST_JWT_SECRET,
});
}
validate(payload: any) {
return {
id: payload.sub,
merchantId: payload.merchantId,
role: payload.role,
isAdmin: payload.isAdmin ?? false,
};
}
}
export const jwtAuthImports = [
PassportModule.register({ defaultStrategy: "jwt" }),
JwtModule.register({ secret: TEST_JWT_SECRET }),
];
export const jwtAuthProviders = [TestJwtStrategy];
export function signUserToken(
module: TestingModule,
user: {
id: string;
merchantId: string;
role: MerchantRole;
isAdmin?: boolean;
},
): string {
const jwtService = module.get<JwtService>(JwtService);
return jwtService.sign({
sub: user.id,
merchantId: user.merchantId,
role: user.role,
isAdmin: user.isAdmin ?? false,
});
}