hapi-auth-github
Version:
Quick & Simple GitHub Authentication for Hapi.js apps
97 lines (85 loc) • 3.56 kB
JavaScript
var test = require('tape');
var nock = require('nock');
var fs = require('fs');
var dir = __dirname.split('/')[__dirname.split('/').length-1];
var file = dir + __filename.replace(__dirname, '') + " > ";
var server = require('../example/github_server.js');
test(file+'Visit / root url expect to see a link', function(t) {
var options = {
method: "GET",
url: "/"
};
server.inject(options, function(response) {
t.equal(response.statusCode, 200, "Server is working.");
setTimeout(function(){ server.stop(t.end); }, 100);
});
});
// test a bad code does not crash the server!
test(file+'GET /githubauth?code=oauth2codehere', function(t) {
var options = {
method: "GET",
url: "/githubauth?code=badcode"
};
server.inject(options, function(response) {
t.equal(response.statusCode, 401, "Bad Code is Rejected (as expected)");
t.ok(response.payload.indexOf('something went wrong') > -1,
'Got: '+response.payload + ' (as expected)');
server.stop(t.end);
});
});
test(file+'MOCK GitHub OAuth2 Flow /githubauth?code=mockcode', function(t) {
// google oauth2 token request url:
var token_fixture = fs.readFileSync('./test/fixtures/sample_access_token.json');
nock('https://github.com')
.persist() // https://github.com/pgte/nock#persist
.post('/login/oauth/access_token')
.reply(200, token_fixture);
// see: http://git.io/v4nTR for google plus api url
// https://www.googleapis.com/plus/v1/people/{userId}
var sample_profile = fs.readFileSync('./test/fixtures/sample_profile.json');
nock('https://api.github.com')
.get('/user')
.reply(200, sample_profile);
var options = {
method: "GET",
url: "/githubauth?code=mockcode"
};
server.inject(options, function(response) {
t.equal(response.statusCode, 200, "Profile retrieved (Mock)");
var expected = 'Hello Alex, You Logged in Using GitHub!';
t.equal(response.payload, expected, "Got: " + expected + " (as expected)");
// console.log(' - - - - - - - - - - - - - - - - - -');
// console.log(response.payload);
// console.log(' - - - - - - - - - - - - - - - - - -');
server.stop(t.end);
});
});
test(file+'MOCK GitHub OAuth2 Flow /githubauth?code=mockcode with custom GitHub URLs', function(t) {
process.env.GITHUB_HOSTNAME = 'github.at-my-custom.host';
process.env.GITHUB_API_HOSTNAME = 'api.github.at-my-custom.host';
// google oauth2 token request url:
var token_fixture = fs.readFileSync('./test/fixtures/sample_access_token.json');
nock('https://' + process.env.GITHUB_HOSTNAME)
.persist() // https://github.com/pgte/nock#persist
.post('/login/oauth/access_token')
.reply(200, token_fixture);
// see: http://git.io/v4nTR for google plus api url
// https://www.googleapis.com/plus/v1/people/{userId}
var sample_profile = fs.readFileSync('./test/fixtures/sample_profile.json');
nock('https://' + process.env.GITHUB_API_HOSTNAME)
.get('/user')
.reply(200, sample_profile);
var options = {
method: "GET",
url: "/githubauth?code=mockcode"
};
server.inject(options, function(response) {
t.equal(response.statusCode, 200, "Profile retrieved (Mock)");
var expected = 'Hello Alex, You Logged in Using GitHub!';
t.equal(response.payload, expected, "Got: " + expected + " (as expected)");
// console.log(' - - - - - - - - - - - - - - - - - -');
// console.log(response.payload);
// console.log(' - - - - - - - - - - - - - - - - - -');
server.stop(t.end);
});
});