mocha.jssails.jschaisupertestsails-mongo

Problem authenticating with mocha, chai and supertest


I add the userRecord to the database in the before function but when I try to authenticate using supertest it gives me bad combo. I am using sails for the backend, the code is as follows

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

The error is like this

<- POST /api/v1/users/entrance/login (36ms 401) | bad combo | The provided email and password combination does not match any user in the database.

Edit-1:- I narrowed it down to that it is not that the user does not exist but it throws this error due to not maching the password, I am using the template that comes with user authentication and it uses the helpers.password for confirming the password. It is throwing error there.

But where is this helper in the project?

Thank you for your help


Solution

  • For anyone in the future, this is how I changed it.

    The problem is not hashing when I add it directly but when I verify it sails hashes the incoming password, thus resulting in mismatch of passwords

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