与JWT Guard一起测试E2E
我正在尝试将E2E测试用于此用户更新功能。当用户登录其ID和电子邮件中时,将其保存到JWT令牌中并存储在Httponly Cookie中。我的问题是如何获取这个令牌,以使其通过JWT Guard,从JWT令牌获得ID并更新正确的用户。因为目前,我显然会得到未经授权的
消息。
user.2e2-spec.ts
const mockUser: UpdateUserDto = {
email: '[email protected]',
first_name: 'first_name1',
last_name: 'last_name1',
password: 'password1',
};
const mockLoginUser: LoginUserDto = {
email: '[email protected]',
password: 'password',
};
describe('Auth controller (e2e)', () => {
let app: INestApplication;
beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleFixture.createNestApplication();
await app.init();
});
it('should update user info', async () => {
const loginRes = await request(app.getHttpServer())
.post('/login')
.send(mockLoginUser);
const data = await request(app.getHttpServer())
.put('/me/update')
.send(mockUser)
.expect('success');
});
afterAll(async () => {
await app.close();
});
});
user.controller.ts
@UseGuards(jwtAuthGuard)
@Put('me/update')
async updateUser(@Req() request, @Body() updateUser: UpdateUserDto) {
const data = await this.userService.updateUser(request.user.id, updateUser);
return data;
}
jwt.strategy.ts
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
constructor() {
super({
jwtFromRequest: (req) => {
var token = null;
if (req && req.cookies) {
token = req.cookies['jwt'];
}
return token;
},
ignoreExpiration: false,
secretOrKey: process.env.ACCESS_SECRET,
});
}
async validate(payload: any) {
return {
id: payload.id,
email: payload.email,
};
}
}
I’m trying to get e2e testing to work for this user update function. When user logs in their id and email are saved into JWT token and stored in HttpOnly Cookie. My problem is how to get this token so that it passes through JWT guard, gets id from JWT token and updates correct user. Because currently i'm obviously getting Unauthorized
message.
user.2e2-spec.ts
const mockUser: UpdateUserDto = {
email: '[email protected]',
first_name: 'first_name1',
last_name: 'last_name1',
password: 'password1',
};
const mockLoginUser: LoginUserDto = {
email: '[email protected]',
password: 'password',
};
describe('Auth controller (e2e)', () => {
let app: INestApplication;
beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleFixture.createNestApplication();
await app.init();
});
it('should update user info', async () => {
const loginRes = await request(app.getHttpServer())
.post('/login')
.send(mockLoginUser);
const data = await request(app.getHttpServer())
.put('/me/update')
.send(mockUser)
.expect('success');
});
afterAll(async () => {
await app.close();
});
});
user.controller.ts
@UseGuards(jwtAuthGuard)
@Put('me/update')
async updateUser(@Req() request, @Body() updateUser: UpdateUserDto) {
const data = await this.userService.updateUser(request.user.id, updateUser);
return data;
}
jwt.strategy.ts
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
constructor() {
super({
jwtFromRequest: (req) => {
var token = null;
if (req && req.cookies) {
token = req.cookies['jwt'];
}
return token;
},
ignoreExpiration: false,
secretOrKey: process.env.ACCESS_SECRET,
});
}
async validate(payload: any) {
return {
id: payload.id,
email: payload.email,
};
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须在呼叫之间自己处理这一点:
如果使用typeScript,
superagent
supertest
使用的,将为您提供set set set
函数的提示,它接受只能是cookie
的字段参数You have to handle that yourself between calls:
If using typescript,
superagent
which is used bysupertest
will give you a hint of theset
function, it accepts a field parameter which only can beCookie