【问题标题】:Problem authenticating with mocha, chai and supertest使用 mocha、chai 和 supertest 进行身份验证时出现问题
【发布时间】:2019-07-08 20:36:54
【问题描述】:

我在 before 函数中将 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();
        }
      });
  });

错误是这样的

Edit-1:- 我将其范围缩小到不是用户不存在,而是由于未处理密码而引发此错误,我使用的是用户身份验证附带的模板,它使用了帮助程序.password 用于确认密码。它在那里抛出错误。

但是这个助手在项目中在哪里呢?

感谢您的帮助

【问题讨论】:

    标签: mocha.js sails.js chai supertest sails-mongo


    【解决方案1】:

    对于未来的任何人,这就是我改变它的方式。

    当我直接添加它时问题不是散列但是当我验证它时会散列传入的密码,从而导致密码不匹配

    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();
    

    【讨论】:

      猜你喜欢
      • 2020-05-08
      • 1970-01-01
      • 2014-03-29
      • 2016-03-20
      • 1970-01-01
      • 1970-01-01
      • 2015-04-29
      • 2019-06-01
      • 1970-01-01
      相关资源
      最近更新 更多