使用 mocha、chai 和 supertest 进行身份验证时出现问题
Problem authenticating with mocha, chai and supertest
我在之前的函数中将 userRecord 添加到数据库中,但是当我尝试使用 supertest 进行身份验证时,它给了我错误的组合。
我后台使用的是sails,代码如下
var request = require('supertest');
let should = chai.should();
require('../../boostrap.test.js');
describe('The bookedservices Controller', function() {
let userRecord,
studioRecord,
serviceRecord,
timingRecord,
bookedServiceRecord,
authenticatedUser,
authenticatedStudio,
session = null;
before(async function() {
// beforeEach(async function(done) {
userRecord = await User.create({
emailAddress: 'xx@gmail.com',
password: 'xxisnotgood123',
fullName: 'siddhant',
location: 'vellore',
image: 'not a pipi pic',
mobile: '9681901311',
description: 'words cant describe me'
}).fetch();
creditRecord = await Credits.create({
expirydate: '2019-05-25T03:23:55.016Z',
creditsPresent: 30,
passType: '1 month',
userId: userRecord.id
}).fetch();
studioRecord = await Studios.create({
emailAddress: 'yy@gmail.com',
password: 'yyisnotgood123',
fullName: 'siddhant',
location: 'vellore',
image: 'not a lili pic',
mobile: '9681901311',
description: 'words cant describe me'
}).fetch();
serviceRecord = await Services.create({
serviceName: 'zumba',
price: 1500,
creditCost: 3,
studioId: studioRecord.id
}).fetch();
timingRecord = await Timings.create({
eventInTime: '2019-05-11T03:23:55.016Z',
eventOutTime: '2019-05-13T00:00:02.001Z',
numberOfSlotsAvailable: 3,
serviceId: serviceRecord.id
}).fetch();
bookedServiceRecord = await BookedServices.create({
isComplete: 'false',
bookingDate: '2019-05-13T03:23:55.016Z',
timingId: timingRecord.id,
userId: userRecord.id,
studioId: studioRecord.id,
serviceId: serviceRecord.id
}).fetch();
authenticatedUser = await request.agent(sails.hooks.http.app);
authenticatedUser
.post('/api/v1/users/entrance/login')
.set('Accept', 'application/json')
.send({ emailAddress: 'xx@gmail.com', password: 'xxisnotgood123' })
.end((err, res) => {
if (err) {
throw err;
} else {
console.log(res);
session = res.header['Sails.sid'];
}
});
// authenticatedStudio = await request.agent(sails.hooks.http.app);
// authenticatedStudio
// .post('/api/v1/studios/login')
// .set('Accept', 'application/json')
// .send({ emailAddress: 'yy@gmail.com', password: 'yyisnotgood123' });
// done();
});
it('all the records have been added', function(done) {
userRecord.should.have.property('id');
console.log('OUTPUT: userRecord', userRecord);
studioRecord.should.have.property('id');
// console.log('OUTPUT: studioRecord', studioRecord);
serviceRecord.should.have.property('id');
// console.log('OUTPUT: serviceRecord', serviceRecord);
timingRecord.should.have.property('id');
// console.log('OUTPUT: timingRecord', timingRecord);
bookedServiceRecord.should.have.property('id');
// console.log('OUTPUT: bookedServiceRecord', bookedServiceRecord);
done();
});
it('should post and return a bookedService model document', function(done) {
timingRecordId = timingRecord.id;
// console.log(`/api/v1/timings/${timingRecordId}/bookedservices`);
authenticatedUser
.post(`/api/v1/timings/${timingRecordId}/bookedservices`)
.set('Accept', 'application/json')
.set('Cookie', session)
.send({ isComplete: false, bookedDate: '2019-05-13T00:10:02.001Z' })
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, result) {
if (err) {
done(err);
} else {
result.body.should.be.an('object');
result.body.should.have.property('bookedServiceDoc');
result.body.should.have.property('message');
createdPostId = result.body.bookedServiceDoc.should.have.property(
'id'
);
console.log(result.body);
done();
}
});
});
错误是这样的
<- POST /api/v1/users/entrance/login(36 毫秒 401)
|糟糕的组合
|提供的电子邮件和密码组合与数据库中的任何用户都不匹配。
Edit-1:- 我将其缩小到不是用户不存在而是由于未处理密码而抛出此错误,我正在使用用户身份验证附带的模板并且它使用helpers.password 用于确认密码。它在那里抛出错误。
但是这个助手在项目中的什么位置呢?
感谢您的帮助
对于以后的任何人,我就是这样改的。
当我直接添加它时,问题不是散列,而是当我验证它时,它会散列传入的密码,从而导致密码不匹配
let hashedPasswordUser = await sails.helpers.passwords.hashPassword(
'xxisnotgood123',
12
);
userRecord = await User.create({
emailAddress: 'xx@gmail.com',
password: hashedPasswordUser,
fullName: 'siddhant',
location: 'vellore',
image: 'not a cipdsai pic',
mobile: '9681901311',
description: 'words cant describe me'
}).fetch();
我在之前的函数中将 userRecord 添加到数据库中,但是当我尝试使用 supertest 进行身份验证时,它给了我错误的组合。 我后台使用的是sails,代码如下
var request = require('supertest');
let should = chai.should();
require('../../boostrap.test.js');
describe('The bookedservices Controller', function() {
let userRecord,
studioRecord,
serviceRecord,
timingRecord,
bookedServiceRecord,
authenticatedUser,
authenticatedStudio,
session = null;
before(async function() {
// beforeEach(async function(done) {
userRecord = await User.create({
emailAddress: 'xx@gmail.com',
password: 'xxisnotgood123',
fullName: 'siddhant',
location: 'vellore',
image: 'not a pipi pic',
mobile: '9681901311',
description: 'words cant describe me'
}).fetch();
creditRecord = await Credits.create({
expirydate: '2019-05-25T03:23:55.016Z',
creditsPresent: 30,
passType: '1 month',
userId: userRecord.id
}).fetch();
studioRecord = await Studios.create({
emailAddress: 'yy@gmail.com',
password: 'yyisnotgood123',
fullName: 'siddhant',
location: 'vellore',
image: 'not a lili pic',
mobile: '9681901311',
description: 'words cant describe me'
}).fetch();
serviceRecord = await Services.create({
serviceName: 'zumba',
price: 1500,
creditCost: 3,
studioId: studioRecord.id
}).fetch();
timingRecord = await Timings.create({
eventInTime: '2019-05-11T03:23:55.016Z',
eventOutTime: '2019-05-13T00:00:02.001Z',
numberOfSlotsAvailable: 3,
serviceId: serviceRecord.id
}).fetch();
bookedServiceRecord = await BookedServices.create({
isComplete: 'false',
bookingDate: '2019-05-13T03:23:55.016Z',
timingId: timingRecord.id,
userId: userRecord.id,
studioId: studioRecord.id,
serviceId: serviceRecord.id
}).fetch();
authenticatedUser = await request.agent(sails.hooks.http.app);
authenticatedUser
.post('/api/v1/users/entrance/login')
.set('Accept', 'application/json')
.send({ emailAddress: 'xx@gmail.com', password: 'xxisnotgood123' })
.end((err, res) => {
if (err) {
throw err;
} else {
console.log(res);
session = res.header['Sails.sid'];
}
});
// authenticatedStudio = await request.agent(sails.hooks.http.app);
// authenticatedStudio
// .post('/api/v1/studios/login')
// .set('Accept', 'application/json')
// .send({ emailAddress: 'yy@gmail.com', password: 'yyisnotgood123' });
// done();
});
it('all the records have been added', function(done) {
userRecord.should.have.property('id');
console.log('OUTPUT: userRecord', userRecord);
studioRecord.should.have.property('id');
// console.log('OUTPUT: studioRecord', studioRecord);
serviceRecord.should.have.property('id');
// console.log('OUTPUT: serviceRecord', serviceRecord);
timingRecord.should.have.property('id');
// console.log('OUTPUT: timingRecord', timingRecord);
bookedServiceRecord.should.have.property('id');
// console.log('OUTPUT: bookedServiceRecord', bookedServiceRecord);
done();
});
it('should post and return a bookedService model document', function(done) {
timingRecordId = timingRecord.id;
// console.log(`/api/v1/timings/${timingRecordId}/bookedservices`);
authenticatedUser
.post(`/api/v1/timings/${timingRecordId}/bookedservices`)
.set('Accept', 'application/json')
.set('Cookie', session)
.send({ isComplete: false, bookedDate: '2019-05-13T00:10:02.001Z' })
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, result) {
if (err) {
done(err);
} else {
result.body.should.be.an('object');
result.body.should.have.property('bookedServiceDoc');
result.body.should.have.property('message');
createdPostId = result.body.bookedServiceDoc.should.have.property(
'id'
);
console.log(result.body);
done();
}
});
});
错误是这样的
<- POST /api/v1/users/entrance/login(36 毫秒 401) |糟糕的组合 |提供的电子邮件和密码组合与数据库中的任何用户都不匹配。
Edit-1:- 我将其缩小到不是用户不存在而是由于未处理密码而抛出此错误,我正在使用用户身份验证附带的模板并且它使用helpers.password 用于确认密码。它在那里抛出错误。
但是这个助手在项目中的什么位置呢?
感谢您的帮助
对于以后的任何人,我就是这样改的。
当我直接添加它时,问题不是散列,而是当我验证它时,它会散列传入的密码,从而导致密码不匹配
let hashedPasswordUser = await sails.helpers.passwords.hashPassword(
'xxisnotgood123',
12
);
userRecord = await User.create({
emailAddress: 'xx@gmail.com',
password: hashedPasswordUser,
fullName: 'siddhant',
location: 'vellore',
image: 'not a cipdsai pic',
mobile: '9681901311',
description: 'words cant describe me'
}).fetch();