Hernan
Hernan

Reputation: 87

Can't made authenticated requests with Mocha and Supertest

I'm trying to test an API with Mocha and Supertest without lucky to make it work. I have the following code:

var supertest = require('supertest');  
describe('Routing', function() {
    var url = 'http://example.com';
    var server = supertest.agent(url);

    var credentials = {
        user: 'username',
        pass: 'password'
    };


    describe('Login', function() {

        it('should login ok given valid credentials', function(done) {
            server
                .post('/login.php')
                .send(credentials)
                .end(function(err, res) {
                    if (err) {
                        throw err;
                    }                   
                    server.saveCookies(res);
                    done();
                });
        });


        it('should correctly make an authenticated request', function(done){
            server
                .get('/api/me/accounts?_=1449865354112')
                .end(function(err,res) {
                    if (err) {
                        throw err;
                    }
                    res.status.should.be.equal(200);
                    done();
                });
        }); 
    });
});

The login request works fine, I get authenticated. The second call throws a 401 status. I read the documentation but I can't make it work.

What is wrong? thanks!

UPDATE: I finally get authenticated by sending the params using .field('user', 'myUsername') and .field('pass', 'myPassword').

Also I have to persist the cookie between calls: cookie = res.headers['set-cookie']; when I get authenticated, and .set('cookie', cookie) in the next requests.

Upvotes: 1

Views: 1462

Answers (2)

Sachacr
Sachacr

Reputation: 732

That is because the session (cookie) is not persisted between your two tests.

  • First you should do the two calls inside the same test.
  • Second i remember that i have used superagent to persist the session between two calls to the same server. But it seems that supertest now expose the agent to persist the session.

    var supertest = require('supertest');
    var app = express();
    var agent = supertest.agent(app);
    // then you can persist cookie
    agent
      .post('/login.php')
      .auth(credentials)
      ...
    

edit : here is an example of how i have used superagent for tests :

    var request = require('superagent');
    var postData= {
      email: '[email protected]',
      password: 'test'
    };

    var user1 = request.agent();
      user1.post('http://localhost:3000/user/login')
        .send(postData)
        .end(function (err, res) {
          expect(err).to.not.exist;
          expect(res.status).to.equal(200);
          var result = res.body;
          expect(result.data.message).to.equal('Login successful');

          user1.get('http://localhost:3000/user')
            .end(function (err, res) {
              expect(err).to.not.exist;
              expect(res.status).to.equal(200);
              var result = res.body;
              expect(result.data.email).to.equal('[email protected]');
              done();
            });
        });

Upvotes: 0

Brandon Taylor
Brandon Taylor

Reputation: 34593

.send() is for your data. .auth() is for your credentials. Try:

it('should login ok given valid credentials', function(done) {
    server
        .post('/login.php')
        .auth(credentials)
        .send({"some": "value"})
        .expect(200)
        .end(function(err, res) {
            if (err) {
                done(error);
            }                   
            server.saveCookies(res);
            done();
        });
});

See http://visionmedia.github.io/superagent/docs/test.html for a bit more information on supertest.

Upvotes: 0

Related Questions