我的API可以在登录后使用。例如,/api/sales
需要像下面的代码一样的身份验证。 (我使用Node.js和Express.js)
app.all('/api/*', userController.requiresLogin);
app.route('/api/sales')
.get(common.readAll(Sale, 'name'))
.post(common.create(Sale));
userController.requiresLogin
功能是:
exports.requiresLogin = function(req, res, next) {
console.log('Check login...');
if(!req.user) return res.status(401).send();
else next();
};
要登录,我使用passport.js
。
exports.login = passport.authenticate('local', {
successRedirect: '/',
failureRedirect: '/login',
failureFlash: false
});
当我运行我的服务器并在浏览器上测试此API时,它可以正常工作。
为了自动测试这个API,我使用Supertest构建了我的Mocha测试代码。
var request = require('supertest'),
should = require('should'),
utils = require('./../utils'),
mongoose = require('mongoose');
var Sale = mongoose.model('Sale'),
User = mongoose.model('User');
var user;
describe('Controller Testing:', function() {
describe('Sale Controller Testing:', function() {
var agent = request.agent();
beforeEach(function(done) {
user = new User({
username:'defaultUser@test.com',
password: 1234,
userId: 'default user'
});
user.save();
request(app).post('/login')
.send( {username: 'defaultUser@test.com', password: '1234'} )
.end(function(err, res) {
should.not.exist(err);
agent.saveCookies(res);
done();
});
});
afterEach(function() {
User.remove({}, function(){});
Sale.remove({}, function(){});
}
it('create a sale', function(done) {
var sale = {
name: 'New Sale',
orders: [order1]
};
var req = request(app).post('/api/sales')
.send(sale)
.expect('Content-Type', /json/)
.expect(200);
agent.attachCookies(req);
req.end(function(err, res) {
console.log(res.statusCode);
res.body.name.should.equal('New Sale');
done();
});
});
}
此测试失败。测试'创建销售'无法识别用户已登录,即使我保存并附加了相应的cookie。
答案 0 :(得分:2)
检查supertest npm page,然后将测试脚本修改为
var request = require('supertest'),
should = require('should'),
utils = require('./../utils'),
mongoose = require('mongoose');
var Sale = mongoose.model('Sale'),
User = mongoose.model('User');
var user;
describe('Controller Testing:', function() {
describe('Sale Controller Testing:', function() {
var agent = request.agent(app); // revised
beforeEach(function(done) {
user = new User({
username:'defaultUser@test.com',
password: 1234,
userId: 'default user'
});
user.save();
agent.post('/login') // revised
.send( {username: 'defaultUser@test.com', password: '1234'} )
.expect("set-cookie", "check correct cookie here") // appended
.end(function(err, res) {
should.not.exist(err);
//agent.saveCookies(res);
// or write some code to check your cookie here
done();
});
});
afterEach(function() {
User.remove({}, function(){});
Sale.remove({}, function(){});
}
it('create a sale', function(done) {
var sale = {
name: 'New Sale',
orders: [order1]
};
var req = agent.post('/api/sales') // revised
.send(sale)
.expect('Content-Type', /json/)
.expect(200);
//agent.attachCookies(req);
req.end(function(err, res) {
console.log(res.statusCode);
res.body.name.should.equal('New Sale');
done();
});
});
}
我没有看到您的测试脚本中出现app
的位置,假设您已正确使用并应用了cookieParser
中间件。
我认为您的测试脚本中的问题是在添加Cookie之前发送的请求。也许你也可以试试
var req = aggent.attachCookies(request(app));
req.post('/api/sales')
.send(sale)
.expect('Content-Type', /json/)
.expect(200);
//agent.attachCookies(req);