From 318bf33d2c873c990a138faa8c408c46b3fc6493 Mon Sep 17 00:00:00 2001 From: Clement Michaud Date: Sun, 18 Dec 2016 00:07:56 +0100 Subject: [PATCH] Move unit tests to unitary directory and add integration tests --- package.json | 3 +- test/integration/test_server.js | 170 ++++++++++++++++++ test/{ => unitary}/res_mock.js | 0 .../test_authentication.js} | 2 +- test/{jwt_test.js => unitary/test_jwt.js} | 2 +- .../test_ldap_checker.js} | 2 +- .../test_replies.js} | 2 +- test/{ => unitary}/test_server.js | 23 +-- .../test_totp_checker.js} | 2 +- 9 files changed, 190 insertions(+), 16 deletions(-) create mode 100644 test/integration/test_server.js rename test/{ => unitary}/res_mock.js (100%) rename test/{authentication_test.js => unitary/test_authentication.js} (98%) rename test/{jwt_test.js => unitary/test_jwt.js} (95%) rename test/{ldap_checker_test.js => unitary/test_ldap_checker.js} (93%) rename test/{replies_test.js => unitary/test_replies.js} (96%) rename test/{ => unitary}/test_server.js (87%) rename test/{totp_checker_test.js => unitary/test_totp_checker.js} (93%) diff --git a/package.json b/package.json index 1bd63959a..0f245ffd0 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "", "main": "src/index.js", "scripts": { - "test": "./node_modules/.bin/mocha", + "test": "./node_modules/.bin/mocha --recursive test/unitary", + "integration-test": "./node_modules/.bin/mocha --recursive test/integration", "coverage": "./node_modules/.bin/istanbul cover _mocha -- -R spec" }, "repository": { diff --git a/test/integration/test_server.js b/test/integration/test_server.js new file mode 100644 index 000000000..7fd86a34a --- /dev/null +++ b/test/integration/test_server.js @@ -0,0 +1,170 @@ + +var request_ = require('request'); +var assert = require('assert'); +var speakeasy = require('speakeasy'); +var j = request_.jar(); +var request = request_.defaults({jar: j}); +var Q = require('q'); + +var BASE_URL = 'http://localhost:8080'; + +describe('test the server', function() { + var home_page; + var login_page; + var config = { + port: 8090, + totp_secret: 'totp_secret', + ldap_url: 'ldap://127.0.0.1:389', + ldap_users_dn: 'ou=users,dc=example,dc=com', + jwt_secret: 'jwt_secret', + jwt_expiration_time: '1h' + }; + + before(function() { + var home_page_promise = getHomePage() + .then(function(data) { + home_page = data.body; + }); + var login_page_promise = getLoginPage() + .then(function(data) { + login_page = data.body; + }); + return Q.all([home_page_promise, + login_page_promise]); + }); + + it('should serve the login page', function(done) { + getPromised(BASE_URL + '/auth/login?redirect=/') + .then(function(data) { + assert.equal(data.response.statusCode, 200); + done(); + }); + }); + + it('should serve the homepage', function(done) { + getPromised(BASE_URL + '/') + .then(function(data) { + assert.equal(data.response.statusCode, 200); + done(); + }); + }); + + it('should redirect when logout', function(done) { + getPromised(BASE_URL + '/auth/logout?redirect=/') + .then(function(data) { + assert.equal(data.response.statusCode, 200); + assert.equal(data.body, home_page); + done(); + }); + }); + + it('should be redirected to the login page when accessing secret while not authenticated', function(done) { + getPromised(BASE_URL + '/secret.html') + .then(function(data) { + assert.equal(data.response.statusCode, 200); + assert.equal(data.body, login_page); + done(); + }); + }); + + it('should fail the login', function(done) { + postPromised(BASE_URL + '/_auth', { + form: { + username: 'admin', + password: 'password', + token: 'abc' + } + }) + .then(function(data) { + assert.equal(data.body, 'Authentication failed'); + done(); + }); + }); + + it('should login and access the secret', function(done) { + var token = speakeasy.totp({ + secret: 'GRWGIJS6IRHVEODVNRCXCOBMJ5AGC6ZE', + encoding: 'base32' + }); + + postPromised(BASE_URL + '/_auth', { + form: { + username: 'admin', + password: 'password', + token: token + } + }) + .then(function(data) { + assert.equal(data.response.statusCode, 200); + assert.equal(data.body.length, 148); + var cookie = request.cookie('access_token=' + data.body); + j.setCookie(cookie, BASE_URL + '/_auth'); + return getPromised(BASE_URL + '/secret.html'); + }) + .then(function(data) { + var content = data.body; + var is_secret_page_content = + (content.indexOf('This is a very important secret!') > -1); + assert(is_secret_page_content); + done(); + }); + }); + + it('should logoff and should not be able to access secret anymore', function(done) { + getPromised(BASE_URL + '/secret.html') + .then(function(data) { + var content = data.body; + var is_secret_page_content = + (content.indexOf('This is a very important secret!') > -1); + assert(is_secret_page_content); + return getPromised(BASE_URL + '/auth/logout') + }) + .then(function(data) { + assert.equal(data.response.statusCode, 200); + assert.equal(data.body, home_page); + return getPromised(BASE_URL + '/secret.html'); + }) + .then(function(data) { + var content = data.body; + assert.equal(data.body, login_page); + done(); + }) + .fail(function(err) { + console.error(err); + }); + }); +}); + +function responsePromised(defer) { + return function(error, response, body) { + if(error) { + console.error(error); + defer.reject(error); + return; + } + defer.resolve({ + response: response, + body: body + }); + } +} + +function getPromised(url) { + var defer = Q.defer(); + request.get(url, responsePromised(defer)); + return defer.promise; +} + +function postPromised(url, body) { + var defer = Q.defer(); + request.post(url, body, responsePromised(defer)); + return defer.promise; +} + +function getHomePage() { + return getPromised(BASE_URL + '/'); +} + +function getLoginPage() { + return getPromised(BASE_URL + '/auth/login'); +} diff --git a/test/res_mock.js b/test/unitary/res_mock.js similarity index 100% rename from test/res_mock.js rename to test/unitary/res_mock.js diff --git a/test/authentication_test.js b/test/unitary/test_authentication.js similarity index 98% rename from test/authentication_test.js rename to test/unitary/test_authentication.js index 00eb6e958..03bea70e4 100644 --- a/test/authentication_test.js +++ b/test/unitary/test_authentication.js @@ -1,6 +1,6 @@ var assert = require('assert'); -var authentication = require('../src/lib/authentication'); +var authentication = require('../../src/lib/authentication'); var create_res_mock = require('./res_mock'); var sinon = require('sinon'); var sinonPromise = require('sinon-promise'); diff --git a/test/jwt_test.js b/test/unitary/test_jwt.js similarity index 95% rename from test/jwt_test.js rename to test/unitary/test_jwt.js index 30cef7d90..2f37d5009 100644 --- a/test/jwt_test.js +++ b/test/unitary/test_jwt.js @@ -1,5 +1,5 @@ -var Jwt = require('../src/lib/jwt'); +var Jwt = require('../../src/lib/jwt'); var sinon = require('sinon'); var sinonPromise = require('sinon-promise'); sinonPromise(sinon); diff --git a/test/ldap_checker_test.js b/test/unitary/test_ldap_checker.js similarity index 93% rename from test/ldap_checker_test.js rename to test/unitary/test_ldap_checker.js index db3b82fe4..8dfd65228 100644 --- a/test/ldap_checker_test.js +++ b/test/unitary/test_ldap_checker.js @@ -1,5 +1,5 @@ -var ldap_checker = require('../src/lib/ldap_checker'); +var ldap_checker = require('../../src/lib/ldap_checker'); var sinon = require('sinon'); var sinonPromise = require('sinon-promise'); diff --git a/test/replies_test.js b/test/unitary/test_replies.js similarity index 96% rename from test/replies_test.js rename to test/unitary/test_replies.js index c7ea98d1b..03ceadc58 100644 --- a/test/replies_test.js +++ b/test/unitary/test_replies.js @@ -1,5 +1,5 @@ -var replies = require('../src/lib/replies'); +var replies = require('../../src/lib/replies'); var assert = require('assert'); var sinon = require('sinon'); var sinonPromise = require('sinon-promise'); diff --git a/test/test_server.js b/test/unitary/test_server.js similarity index 87% rename from test/test_server.js rename to test/unitary/test_server.js index 739cd4f33..18e030e9d 100644 --- a/test/test_server.js +++ b/test/unitary/test_server.js @@ -1,11 +1,14 @@ +var server = require('../../src/lib/server'); +var Jwt = require('../../src/lib/jwt'); + var request = require('request'); var assert = require('assert'); -var server = require('../src/lib/server'); -var Jwt = require('../src/lib/jwt'); var speakeasy = require('speakeasy'); var sinon = require('sinon'); +var BASE_URL = 'http://localhost:8090'; + describe('test the server', function() { var jwt = new Jwt('jwt_secret'); var ldap_client = { @@ -14,7 +17,7 @@ describe('test the server', function() { before(function() { var config = { - port: 8080, + port: 8090, totp_secret: 'totp_secret', ldap_url: 'ldap://127.0.0.1:389', ldap_users_dn: 'ou=users,dc=example,dc=com', @@ -50,7 +53,7 @@ describe('test the server', function() { function test_login() { it('should serve the login page', function(done) { - request.get('http://localhost:8080/login') + request.get(BASE_URL + '/login') .on('response', function(response) { assert.equal(response.statusCode, 200); done(); @@ -60,7 +63,7 @@ function test_login() { function test_logout() { it('should logout and redirect to /', function(done) { - request.get('http://localhost:8080/logout') + request.get(BASE_URL + '/logout') .on('response', function(response) { assert.equal(response.req.path, '/'); done(); @@ -70,7 +73,7 @@ function test_logout() { function test_get_auth(jwt) { it('should return status code 401 when user is not authenticated', function(done) { - request.get('http://localhost:8080/_auth') + request.get(BASE_URL + '/_auth') .on('response', function(response) { assert.equal(response.statusCode, 401); done(); @@ -82,9 +85,9 @@ function test_get_auth(jwt) { var r = request.defaults({jar: j}); var token = jwt.sign({ user: 'test' }, '1h'); var cookie = r.cookie('access_token=' + token); - j.setCookie(cookie, 'http://localhost:8080/_auth'); + j.setCookie(cookie, BASE_URL + '/_auth'); - r.get('http://localhost:8080/_auth') + r.get(BASE_URL + '/_auth') .on('response', function(response) { assert.equal(response.statusCode, 204); done(); @@ -101,7 +104,7 @@ function test_post_auth() { }); var expectedJwt = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoidGVzdF9vayIsImlhdCI6MCwiZXhwIjozNjAwfQ.ihvaljGjO5h3iSO_h3PkNNSCYeePyB8Hr5lfVZZYyrQ'; - request.post('http://localhost:8080/_auth', { + request.post(BASE_URL + '/_auth', { form: { username: 'test_ok', password: 'password', @@ -131,7 +134,7 @@ function test_post_auth() { } } - request.post('http://localhost:8080/_auth', data, function (error, response, body) { + request.post(BASE_URL + '/_auth', data, function (error, response, body) { if(response.statusCode == 401) { clock.restore(); done(); diff --git a/test/totp_checker_test.js b/test/unitary/test_totp_checker.js similarity index 93% rename from test/totp_checker_test.js rename to test/unitary/test_totp_checker.js index c89b5aaa2..c1582f513 100644 --- a/test/totp_checker_test.js +++ b/test/unitary/test_totp_checker.js @@ -1,5 +1,5 @@ -var totp_checker = require('../src/lib/totp_checker'); +var totp_checker = require('../../src/lib/totp_checker'); var sinon = require('sinon'); var sinonPromise = require('sinon-promise'); sinonPromise(sinon);