Unit testing 模拟Firebase管理员身份验证,用于单元测试已验证的路由
我正在使用firebase admin在我的express后端上进行身份验证。我有一个检查请求是否经过身份验证的中间件Unit testing 模拟Firebase管理员身份验证,用于单元测试已验证的路由,unit-testing,express,firebase,supertest,firebase-admin,Unit Testing,Express,Firebase,Supertest,Firebase Admin,我正在使用firebase admin在我的express后端上进行身份验证。我有一个检查请求是否经过身份验证的中间件 public resolve(): (req, res, next) => void { return async (req, res, next) => { const header = req.header('Authorization'); if (!header || !header.split(' ')) { throw n
public resolve(): (req, res, next) => void {
return async (req, res, next) => {
const header = req.header('Authorization');
if (!header || !header.split(' ')) {
throw new HttpException('Unauthorized', UNAUTHORIZED);
}
const token = header.split(' ')[1];
await admin.auth().verifyIdToken(token).then((decodedToken: any) => {
req.user = decodedToken;
next();
}).catch((error: any) => {
throw new HttpException(error, UNAUTHORIZED);
});
};
}
到目前为止,我只能对我的路由进行单元测试,以确保它们响应未经授权
,而不是未找到
it('GET /api/menu should return 401 ', done => {
const NOT_FOUND = 404;
const UNAUTHORIZED = 401;
supertest(instance)
.get('/api/menu')
.end((error, response: superagent.Response) => {
expect(response.status).not.toEqual(NOT_FOUND);
expect(response.status).toEqual(UNAUTHORIZED);
done();
});
});
但是,我想写更多的单元测试!我想模仿用户,这样我就可以发出授权的请求!我想使用我对用户拥有的type
属性来验证某个类型的用户可以/不能使用某些路由。有人知道我如何使用firebase管理节点做到这一点吗
它看起来像是为单元测试生成标记,但我不确定如何将其应用于我的特定问题。请参阅