Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coverage calculated wrong. #505

Closed
TomerAvni opened this issue Dec 27, 2015 · 2 comments
Closed

Coverage calculated wrong. #505

TomerAvni opened this issue Dec 27, 2015 · 2 comments
Assignees
Labels
bug
Milestone

Comments

@TomerAvni
Copy link

@TomerAvni TomerAvni commented Dec 27, 2015

When running the coverage on "onPostHandler" method it show completely wrong results.

Here is the handler:

var Config = require('../config');
var Util = require('util');
var SessionAdapters = require('./common/session-adapters');
var SessionExtension = require('./session-ext');

exports.register = function (server, options, next) {
    var SessionStat = server.plugins['hapi-mongo-models'].SessionStat;

    server.ext('onPostHandler', function (request, reply) {
        var sessionId = SessionExtension.getSessionState(request);
        if (sessionId) {
            SessionStat.findById(sessionId, function (err, session) {
                if (err) {
                    return reply.continue();
                }

                if (request.auth.credentials) {
                    SessionAdapters.adapt(request.response.source, session, request.auth.credentials.roles);
                } else {
                    SessionAdapters.adapt(request.response.source, session, null);
                }

                return reply.continue();
            });
        } else {
            reply.continue();
        }
    });

    next();
};

exports.register.attributes = {
    name: 'session-adapters'
};

Now the the following tests are simple toggling weather the "sessionId" result exits or null:

var Lab = require('lab');
var Code = require('code');
var Path = require('path');
var Config = require('../../config');
var Manifest = require('../../manifest');
var Hapi = require('hapi');
var HapiAuth = require('hapi-auth-cookie');
var Proxyquire = require('proxyquire');
var AuthPlugin = require('../../server/auth');
var CookieAdmin = require('./fixtures/cookie-admin');



var lab = exports.lab = Lab.script();
var ModelsPlugin, server, stub;


lab.beforeEach(function (done) {

    stub = {
        SessionExtension: {},
        SessionAdapters: {},
        SessionStat: {}
    };


    var Extensions = Proxyquire('../../server/session-adt', {
        './session-ext': stub.SessionExtension,
        './common/session-adapters': stub.SessionAdapters
    });


    var proxy = {};
    proxy[Path.join(process.cwd(), './server/models/session-stat')] = stub.SessionStat;

    ModelsPlugin = {
        register: Proxyquire('hapi-mongo-models', proxy),
        options: Manifest.get('/plugins')['hapi-mongo-models']
    };

    var plugins = [ModelsPlugin, Extensions, HapiAuth, AuthPlugin];
    server = new Hapi.Server();
    server.connection({port: Config.get('/port/web')});
    server.register(plugins, function (err) {

        if (err) {
            return done(err);
        }

        done();
    });
});


lab.afterEach(function (done) {

    server.plugins['hapi-mongo-models'].BaseModel.disconnect();

    done();
});


lab.experiment('Session Adapter', function () {

    lab.test('it returns without adaptation when no session found', function (done) {
        var checkpoints = [];
        stub.SessionExtension.getSessionState = function () {
            checkpoints.push('getSessionState');
            return null;
        };

        server.route({
            method: 'GET',
            path: '/',
            handler: function (request, reply) {
                reply('ok');
            }
        });

        var request = {
            method: 'GET',
            url: '/'
        };

        server.inject(request, function () {
            Code.expect(checkpoints).to.include('getSessionState');

            done();
        });
    });


    lab.test('it does nothing when session-id is not on the DB (some error)', function (done) {
        var checkpoints = [];

        stub.SessionExtension.getSessionState = function () {
            checkpoints.push('getSessionState');
            return "A1";
        };

        stub.SessionStat.findById = function (id, callback) {
            checkpoints.push('findById');
            callback({});
        };

        server.route({
            method: 'GET',
            path: '/',
            handler: function (request, reply) {
                reply('ok');
            }
        });

        var request = {
            method: 'GET',
            url: '/'
        };

        server.inject(request, function () {
            Code.expect(checkpoints).to.include('getSessionState');
            Code.expect(checkpoints).to.include('findById');
            done();
        });
    });
});

Now, the coverage result, which shows both the "sessionId" and the "err" as always true while it is simply not true (the first test returns null as the result of the "getSessionState" where the second returns a string.
Is is because of the 'onPostHandler' method.
Should I simply test the use case without the onPostHandler?
Regardless is it a bug?

image

@geek

This comment has been minimized.

Copy link
Member

@geek geek commented Dec 28, 2015

The green areas do seem to indicate that you can simplify the conditionals:

server.ext('onPostHandler', function (request, reply) {
    var sessionId = SessionExtension.getSessionState(request);
    if (!sessionId) {
        return reply.continue();
    }

    SessionStat.findById(sessionId, function (err, session) {
        if (!err) {
            SessionAdapters.adapt(request.response.source, session, (request.auth.credentials && request.auth.credentials.roles) || null);
        }
        return reply.continue();
    });
});
@geek geek added the question label Dec 28, 2015
@geek geek self-assigned this Dec 28, 2015
@tjmehta

This comment has been minimized.

Copy link
Contributor

@tjmehta tjmehta commented Jan 11, 2016

I noticed that your example is using proxyquire. I also noticed strange issue w/ coverage recently w/ lab and had only recently started using proxyquire in those projects. Removing proxyquire resolved my issues w/ misreported coverage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.