SailsJs :: Store sessions with mocha

I need my sessions to be live between mocha requests.

After logging in, I store the user id in the express session object:

req.session.user = user.id ;

      

In the browser, the session is saved without any questions (verified with Postman).

But I need my REST API to be available to an external application and I would not want it to authenticate for every request in my API.

Is there a way to keep the session between two requests in mocha or through an API client application?

Thanks in advance.

English is not my mom, I may not have been as clear as I would like. Therefore, I can provide any information you may need to help me.

UPDATE

Thanks to Alberto, I figured out how to save my Mocca sessions with Supertest. The agent retains its sessions until it is destroyed or a request to exit. What donne should be using the same agent for login and API request.

What I've done:

var request = require('supertest'),
    should = require('chai').should();

describe('ImageController', function() {
  var agent = request.agent('http://localhost:1337') ;

  before(function(done){
      agent
        .post('/auth/local')
        .send({identifier: 'email', password: 'password'})
        .end(function(err, res) {
          if (err) return done(err);

          done();
        });
  })

  after(function(done){
      agent
        .get('/logout')
        .end(function(err, res) {
          if (err) return done(err);

          done();
        });
  })
  describe('POST /image', function(){
    it('should return 201 for image creation after login', function (done) {
      agent
        .post('/image')
        .send({name: 'test.png'})
        .end(function (err, res) {
          if (err) return done(err);

          res.status.should.be.equal(201);
          done();
        });
    });
  });
});

      

+3


source to share


1 answer


Use the superuser function how to store cookies.

Has one example in supertest files: https://github.com/tj/supertest#example

Sails.js example with super test example: https://github.com/albertosouza/sails-test-example



Sample snipplet test file:

var request = require('supertest');
var assert = require('assert');
var authenticated;

describe('Example test', function() {
  // use efore all to create custom stub data
  before(function(done) {
    // use supertest.agent for store cookies ...
    // logged in agent

    // after authenticated requests 
    //login and save one agent with your session cookies. Ex:
    authenticated = request.agent(sails.hooks.http.app);
    authenticated.post('/auth/login')
    .send({
      email: user.email,
      password: user.password
    })
    .end(function(err) {
      done(err);
    });
  });

  describe('authenticated requests', function(){
    it ('should access one protected route', function(done){
      // use the authenticated agent to do authenticated requests
      authenticated.get('/protected')
      .expect(200)
      .end(function(err, res) {
        if (err) return done(err);

        console.log('response:', res.text);

        done();
      });
    });
  });
});

      

+3


source







All Articles