Node.js 摩卡柴测试保护路线
我有一个Express服务器,我正在使用passport local进行身份验证。我有以下受保护的路线:Node.js 摩卡柴测试保护路线,node.js,express,mocha.js,chai,passport-local,Node.js,Express,Mocha.js,Chai,Passport Local,我有一个Express服务器,我正在使用passport local进行身份验证。我有以下受保护的路线: app.post("/api/test", connect.ensureLoggedIn("/"), (req, res) => { let test = new Test(req.body); test .save() .then(data => { return res.json(data); }) .catch(e => { retu
app.post("/api/test", connect.ensureLoggedIn("/"), (req, res) => {
let test = new Test(req.body);
test
.save()
.then(data => {
return res.json(data);
})
.catch(e => {
return res.status(HTTP_RESPONDE_BAD_REQUEST).send(e);
});
});
我想知道如何测试上面提到的路由以确保用户已登录
这是我当前的测试(它没有通过,因为我无法发送身份验证:
it("Testing protected route", done => {
chai
.request(server)
.post("/api/test")
.send(test)
.end((err, res) => {
expect(res.status).to.equal(200);
done();
});
});
我尝试了以下方法,但当我运行测试时,它们会将我重定向到登录页面
it("Testing protected route", done => {
chai
.request(server)
.post("/api/test")
.set('Authorization', 'Bearer ' + token) // user token id
.send(test)
.end((err, res) => {
expect(res.status).to.equal(200);
done();
});
});
it("Testing protected route", done => {
chai
.request(server)
.post("/api/test")
.set('token', token) // user token id
.send(test)
.end((err, res) => {
expect(res.status).to.equal(200);
done();
});
});
有没有更简单的方法来测试这一点?我正在使用
supertest
和mocha
进行单元测试,并为令牌使用自定义头,但我在测试中使用了类似的模式。在运行任何测试之前,数据都会加载到数据库中,用户会登录,然后该令牌将用于每个测试eed身份验证
TestUtils类
this.authenticateUser = (user, app) =>
new Promise((resolve, reject) => {
request(app)
.post('/authenticate')
.send({
email: user.email,
password: user.test_password,
})
.end((err, res) => {
if (err) {
return reject(err);
}
return resolve(res.body.token);
});
});
describe('Authed Routes', () => {
let app = null;
let authUser = null;
before((done) => {
// mocking should happen before the app is created.
app = require('../server');
// Populate redis data
TestUtils.populateRedis(() => {
// Populate db data
TestUtils.syncAndPopulateDatabase('public-tests', () => {
// Get the test user
authUser = TestUtils.getUser();
// Authenticate the user to get a token
TestUtils.authenticateUser(authUser, app)
.then((accessToken) => {
// Keep the token on the user so we can use it in the tests
authUser.access_token = accessToken;
return done();
})
.catch((err) => done(err));
});
});
});
describe('/secure/route', () => {
it('should allow /secure/route with correct token provided', (done) => {
request(app)
.get('/secure/route')
// add the access token from the user as a header value
.set('x-access-token', authUser.access_token)
.expect(200)
.end((err, res) => {
done();
});
});
});
});
测试类
this.authenticateUser = (user, app) =>
new Promise((resolve, reject) => {
request(app)
.post('/authenticate')
.send({
email: user.email,
password: user.test_password,
})
.end((err, res) => {
if (err) {
return reject(err);
}
return resolve(res.body.token);
});
});
describe('Authed Routes', () => {
let app = null;
let authUser = null;
before((done) => {
// mocking should happen before the app is created.
app = require('../server');
// Populate redis data
TestUtils.populateRedis(() => {
// Populate db data
TestUtils.syncAndPopulateDatabase('public-tests', () => {
// Get the test user
authUser = TestUtils.getUser();
// Authenticate the user to get a token
TestUtils.authenticateUser(authUser, app)
.then((accessToken) => {
// Keep the token on the user so we can use it in the tests
authUser.access_token = accessToken;
return done();
})
.catch((err) => done(err));
});
});
});
describe('/secure/route', () => {
it('should allow /secure/route with correct token provided', (done) => {
request(app)
.get('/secure/route')
// add the access token from the user as a header value
.set('x-access-token', authUser.access_token)
.expect(200)
.end((err, res) => {
done();
});
});
});
});
我使用
supertest
和mocha
进行单元测试,并使用令牌的自定义头,但我的测试使用类似的模式。在运行任何测试之前,数据加载到数据库中,用户登录,然后该令牌用于需要验证的每个测试
TestUtils类
this.authenticateUser = (user, app) =>
new Promise((resolve, reject) => {
request(app)
.post('/authenticate')
.send({
email: user.email,
password: user.test_password,
})
.end((err, res) => {
if (err) {
return reject(err);
}
return resolve(res.body.token);
});
});
describe('Authed Routes', () => {
let app = null;
let authUser = null;
before((done) => {
// mocking should happen before the app is created.
app = require('../server');
// Populate redis data
TestUtils.populateRedis(() => {
// Populate db data
TestUtils.syncAndPopulateDatabase('public-tests', () => {
// Get the test user
authUser = TestUtils.getUser();
// Authenticate the user to get a token
TestUtils.authenticateUser(authUser, app)
.then((accessToken) => {
// Keep the token on the user so we can use it in the tests
authUser.access_token = accessToken;
return done();
})
.catch((err) => done(err));
});
});
});
describe('/secure/route', () => {
it('should allow /secure/route with correct token provided', (done) => {
request(app)
.get('/secure/route')
// add the access token from the user as a header value
.set('x-access-token', authUser.access_token)
.expect(200)
.end((err, res) => {
done();
});
});
});
});
测试类
this.authenticateUser = (user, app) =>
new Promise((resolve, reject) => {
request(app)
.post('/authenticate')
.send({
email: user.email,
password: user.test_password,
})
.end((err, res) => {
if (err) {
return reject(err);
}
return resolve(res.body.token);
});
});
describe('Authed Routes', () => {
let app = null;
let authUser = null;
before((done) => {
// mocking should happen before the app is created.
app = require('../server');
// Populate redis data
TestUtils.populateRedis(() => {
// Populate db data
TestUtils.syncAndPopulateDatabase('public-tests', () => {
// Get the test user
authUser = TestUtils.getUser();
// Authenticate the user to get a token
TestUtils.authenticateUser(authUser, app)
.then((accessToken) => {
// Keep the token on the user so we can use it in the tests
authUser.access_token = accessToken;
return done();
})
.catch((err) => done(err));
});
});
});
describe('/secure/route', () => {
it('should allow /secure/route with correct token provided', (done) => {
request(app)
.get('/secure/route')
// add the access token from the user as a header value
.set('x-access-token', authUser.access_token)
.expect(200)
.end((err, res) => {
done();
});
});
});
});
你的第二个例子是正确的方法,除了你需要登录或创建一个有效的
令牌
来使用之外。我使用的是一个有效的令牌。它在PostmanI上运行良好。我在我的应用程序中做了几乎完全相同的事情,并且它在使用有效令牌时运行良好(假设你也有正确的头)。我不知道你所说的正确标头是什么意思。你能给我看看你的代码吗?你是如何找到你的令牌值的?我正在使用supertest
和一个名为x-access-token
的自定义HTTP令牌标头,因此你需要根据自己的需要进行调整。你的第二个示例是正确的方法,只是你需要登录或其他明智地创建一个有效的令牌
来使用。我正在使用一个有效的令牌。它在PostmanI上运行良好。我在我的应用程序中做了几乎完全相同的事情,并且它在使用有效令牌时运行良好(假设您也有正确的标头)。我不知道您所说的正确标头是什么意思。您能给我看一下您的代码吗?您是如何找到您的令牌值的?我正在使用supertest
和一个名为x-access-token
的自定义HTTP令牌标头,因此您需要根据需要进行调整。