如果需要登录,如何在Node.js服务器端使用Mocha Chai测试RESTful CRUD api
How to test RESTful CRUD api with Mocha Chai on Node.js server side if login needed?
我想测试一个web应用程序服务器端的CRUD逻辑。但是只有登录用户才能访问web应用。
if (res.locals.user){
//CRUD functions here
}
我知道如何使用Mocha和Chai来测试这些CRUD功能,而不需要登录检查,但是我如何模拟登录用户来测试它们?用饼干吗?
我一般是这样做的:
var request = require('supertest');
describe('My tests', function(){
var agent = request.agent(app);
//before all the tests run, log in
before(function(done){
request(app)
.post('/login')
.send({
username: 'a@b.com',
password: 'password123'
})
.end(function (err, res) {
if (err) { return done(err); }
agent.saveCookies(res);
done();
});
});
it('Does something when logged in', function (done){
var req = request(app).get('/account/something')
.expect(200);
//attach the logged in cookies to the agent
agent.attachCookies(req);
req.end(done);
//assertions here
});
})
首先触发一个登录请求,将cookie保存到代理
然后,在请求中,我想使用经过身份验证的请求。将cookie附加到它。
app
是Express应用程序的实例
@Russj,假设:
- 您正在使用
passport-local
作为passport
认证策略 - 你使用
supertest
来模拟你的api调用 - 你已经有一个导出Express应用程序的文件
那么我将如何测试经过验证的端点:
var request = require('supertest'),
agent = request.agent();
mongoose = require('mongoose'),
// this examples assumes /path/to/your/app exports your Express app
app = require('/path/to/your/app'),
// replace this with the model you use for authentication
UserModel = mongoose.model('UserModel');
// this example assumes your user model looks something like the following:
//
// UserModel = new mongoose.Schema({
// username: String,
// password: String
// });
describe('testing authenticated end-point', function () {
var UserModel, testuser;
before(function (done) {
// this is just to ensure we have a user to log in for your tests
UserModel.findOneAndUpdate({
{ username: 'testuser' },
{ username: 'testuser', password: 'testpassword' },
{ upsert: true }, // this will create the user if it doesn't already exist
function(err, doc) {
testuser = doc
}
});
// I assume /simulate-login is not an existing route in your app
app.get('/simulate-login', function(req, res) {
req.login(testuser); // .login is exposed in req by passport
});
// now we simulate login of our testuser and save the cookies
request(app)
.get('/simulate-login')
.end(function (err, res) {
if (err) { return done(err); }
// save cookies
agent.saveCookies(res);
done();
});
});
// clean up after ourselves
after(function () {
UserModel.remove({ username: 'testuser' }).exec();
});
// now we can test an authenticated end-point as long as we attach the saved cookies
it('should do whatever...', function (done) {
var req;
req = request(app)
.get('/path/to/route/to/test')
.expect(200);
// attach cookies
agent.attachCookies(req);
// do your reqeust
req.end(done);
});
});
相关文章:
- 如何在Mocha/Chai中测试JS原型(非模块)
- Mocha/Chai测试链接到函数返回断言错误
- 如何按照承诺使用mocha/chai/chai测试ES7异步函数
- 当“new constructor()”与mocha和chai一起使用时,我如何断言throw
- 给Chai/Mocha一份应该包括在内的部分钥匙清单
- Mocha和Chai如约超时
- 测试承诺链以 .catch 结尾(按照承诺使用 Mocha / Chai)
- 如何测试名为“示例”的 Angular 服务.Svc' 在我的模块中使用 mocha+chai
- 节点.js + Chai/Mocha/Should:针对同一响应运行多个测试
- Mocha和Chai发现了超出范围的错误
- 浏览器中的Mocha:如何使用chai.assert获取报告
- 为什么我的mocha/chai错误投掷测试失败
- 当使用Mocha/Chai测试异步函数时,与期望值不匹配总是会导致超时
- 如何使用Mocha Chai组织用于单元测试BDD的代码
- 如果需要登录,如何在Node.js服务器端使用Mocha Chai测试RESTful CRUD api
- 测试新手,我该如何用Mocha, Chai, Enzyme和Sinon测试这种方法呢?
- 在浏览器和Node.JS中导入用Mocha/Chai/TypeScript编写的测试文件
- 如何在使用Mocha/Chai的函数中使用某些参数时正确抛出TypeError
- 如何在使用Mocha/Chai调用函数时强制抛出异常
- 为什么Mocha/Chai不将babel自定义错误与常规自定义错误一样对待?