node.js - 在单元测试中模拟 Passport.js(本地策略)

标签 node.js authentication testing passport.js

我正在使用 Passport.js 的本地策略来验证我的 API 的用户。我正在为此 API 编写测试,我需要一种在测试中模拟 Passport.js 的方法来模拟经过身份验证/未经身份验证的用户访问 API。

这样做最有效的方法是什么?

最佳答案

一个可能的选择:在您的测试设置中,您可以passport.use本地名称下的模拟策略:

passport.use('local', new MockStrategy());

实现 MockStrategy 以通过请求,或任何您的测试需要。

关于node.js - 在单元测试中模拟 Passport.js(本地策略),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18452147/

相关文章:

node.js - 使用 Google Cloud Functions 确定点是否位于多边形中的简单方法

javascript - 重构: Anonymous functions to named functions,参数引用

php - MySQL 正确查询结果无效

security - 是否可以使用 Nancy.Authentication.Token 撤销 token ?

testing - 使用 dbunit 和 jetty 进行集成测试 - dbunit 不填充表

javascript - 开 Jest - TypeError : Cannot read property 'fn' of undefined

javascript - Nightwatch.js 无法将字符串变量设置为输入字段?

node.js - ZMQ 失去订阅连接

authentication - AuthorizationHandlerContext 中的声明在 HandleRequirementAsync 中为空

javascript - 为什么我无法通过 id 获取一件商品?