passport icon indicating copy to clipboard operation
passport copied to clipboard

How to test req.isAuthenticated() in express3 sample

Open evaletolab opened this issue 12 years ago • 6 comments

Hello,

I'm wondering how to test authentication with passport. I'm using mocha ( and supertest ) and I didn't find a way to test controllers that depend on req.isAuthenticated()?

It would be really useful to see the test on the example here, https://github.com/jaredhanson/passport-local/tree/master/examples/express3

Bests regards, olivier

evaletolab avatar Dec 15 '12 09:12 evaletolab

+1

Volox avatar Oct 28 '13 09:10 Volox

+1

TookTheRedBean avatar Nov 13 '13 04:11 TookTheRedBean

Hello, I wrote a simple sample that demonstrate authentication during the test.

var app = require("../app");
describe("session", function(){
  var request= require('supertest');
  var cookie;

  it('POST authorized  /login should return 200',function(done){  
    request(app)
      .post('/login')
      .send({ email:"[email protected]", provider:'local', password:'password' })
      .end(function(err,res){
        res.should.have.status(200);
        cookie = res.headers['set-cookie'];
        done();        
      });
  });

  it('GET /v1/users/me should return 200',function(done){
    request(app)
      .get('/v1/users/me')
      .set('cookie', cookie)
      .expect(200,done);
  });

  it('GET /v1/users/me should return 401',function(done){
    request(app)
      .get('/v1/users/me')
      .expect(401,done);
  });

evaletolab avatar Nov 13 '13 09:11 evaletolab

Is there a way to "mock an authenticated user" without having to post to /login?

cyrusdavid avatar Jun 02 '14 00:06 cyrusdavid

Any ideas how to workaround this

TheBlackDude avatar Jan 31 '18 17:01 TheBlackDude

+1

AsebWebDev avatar Sep 11 '20 14:09 AsebWebDev