1

I'm coding an app which uses only Facebook-based authentication with PassportJS. Part of the provided API is available only when the user is authenticated. I'm trying to find a way to test this part of the API. I found many examples of testing authenticated API where login+password are used but this is not the case here. I also found this thread but unfortunately, I don't understand how the answer could help.

Do you guys navy any example which could explain how this could be achieved?

Let's say I have an API:

app.get('/users/me, users.me);

and this call requires the user to be authenticated with Facebook.

How could I test is with super test and simulate user authentication with FB?

Community
  • 1
  • 1
Jakub
  • 3,129
  • 8
  • 44
  • 63

1 Answers1

1

You can use zombie + mocha to test this ....Zombie opens up a virtual browser fron tthe backend (which you dont see ) and clicks on any link on the page and then test this result by asserting using mocha . login to facebook before you start the application and then the link which fb authentication and assert for the new route(because you would have success redirection url) using zombie . Lets say the redirection url after successful login is /auth/facebook/success and /auth/facebook be the url where the login is tested . HERe is the code that could achieve it

var assert = require('assert'),
    virtualBrowser = require('zombie');
describe('facebbok Login test', function () {
    describe('Checking whether login is successfull based o nthe redirection URL',         function () {
        it('Checking for the sucess URL', function (done) {
            virtualBrowser.visit('http://127.0.0.1:3000/auth/facebook', function (err, browser) {
                if (err)
                    throw err;
               console.log('Zombie visited the page,page loaded    ');
               assert.equal(browser.location.pathname, '/auth/facebook/success');

                //all your tests goes here

                /*you'll be using this 'browser object in the callback for further tests */

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

Run the test by running it against the mocha (will be present inside the node_module/mocha of the folder of the installed module ) ~/node_modules/mocha/bin/mocha ./zombie-test/test_sign_facebook.js If mocha reports timeout since the login might be slow , just add more threshhold test time by using -t parameter ~/node_modules/mocha/bin/mocha -t 12000 ./zombie-test/test_sign

Karthic Rao
  • 3,624
  • 8
  • 30
  • 44