mercredi 3 février 2016

Testing Node/Express app using Supertest with Facebook login: How do you use authorization

I'm writing tests for a very early stage Node/Express app. I've made the decision to use only FB logins for most users, so many of my controllers depend on the authenticated state from FB. But I'm not clear how to either test this, or how to preserve the session with the logged in user in a way that passport, which I'm using, will understand.

var expect    = require("chai").expect;
var mongoose = require('mongoose');
var User = require('../models/users');
var nock = require('nock');
var parser = require('cookie-parser')

var session = require('supertest-session');
var agent = require('superagent');

var _ = require('lodash');

var app = require('../app');

var testSession = null;

var fbRedirectURL;

beforeEach(function () {
  testSession = session(app);
});

it('should authorize with FB', function (done) {

    testSession.get('/auth/facebook')
        .expect(302)
        .end(function(err, res){
            fbRedirectURL=decodeURIComponent(res.headers.location);
            done()
        });
});

it('should get FB data', function (done) {

    testSession.get(fbRedirectURL)
        .expect(200)
        .end(function(err,res) {
            console.log()
            done()
        })
});

These tests work, but the second throws an error: and I am not clear how I use this to maintain a session through other tests, or if I have to somehow mock that, or otherwise try to fool passport.

I've looked high and low, and haven't found much of anything that seems to address the issue of testing with third-party authentication services, and it seems problematic. Are there resources I haven't found? Even passport-facebook's documentation seems to glide over the topic of testing code that depends on FB authenication, and provides a test suite for only their own code.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire