<commit_before>var ConfigManager = require("../lib/ConfigManager")
 , Helper = require('./Helper')
 , orm = require('../lib/orm')
, fs = require('fs')
, sinon = require('sinon')
, mysql = require('mysql')
, magnetId = require('node-uuid')
, _ = require('underscore');

jasmine.getEnv().defaultTimeoutInterval = 5000;

var configStatic = Helper.deepCopy(require('../lib/config/config.json'));

describe('ConfigManager', function(){

    beforeAll(function(done){
        ENV_CONFIG.DatabaseLog.enabled = false;
        var connection =  mysql.createConnection({
            host     : ENV_CONFIG.Database.host,
            port     : ENV_CONFIG.Database.port,
            user     : ENV_CONFIG.Database.username,
            password : <PASSWORD>
        });
        connection.query('CREATE DATABASE IF NOT EXISTS '+ENV_CONFIG.Database.dbName+';', function(e){
            if(e){
                expect(e).toEqual('failed-test');
            }
            orm.setup('./lib/models', function(){
                done();
            });
        });
    });

    var mmxManagerStub = {
        getServerStatus : function(obj, cb){
            cb(null, {"setupComplete":true,"mmxAdminPort":"6060","mmxAdminPortSecure":"6061","mmxPublicPort":"5220","mmxPublicPortSecure":"5221"}, 200);
        },
        getConfigs : function(section, cb){
            cb(null, {});
        },
        provisionServer: function(connect, config, cb){
            cb(null, 'test-status', 201);
        }
    };

    var fsStub  = {
        readFile: function(configPath, encoding, cb){
            cb(null, {});
        },
        readFileSync: function(configPath, encoding){
            if(configPath == './lib/config/auth.json')
                return {
                    email    : '<EMAIL>',
                    password : '<PASSWORD>'
                };
            if(configPath == './lib/config/config.json')
                return JSON.stringify(Helper.deepCopy(configStatic));
        },
        writeFile: function(configPath, str, cb){
            (cb || function(){})();
        }
    };

    var redisClientStub = function(){
        this.evts = {};
        this.auth = function(password){
        };
        this.on = function(evt, cb){
            this.evts[evt] = cb;
        };
        this.get = function(tag, cb){
            if(tag == 'redis-init')
                this.evts['connect'](null, this);
            else
                cb(null, JSON.stringify(Helper.deepCopy(configStatic)));
        }
        this.set = function(tag, cfg, cb){
            cb();
        }
    };
    var redisStub = {
        createClient: function(port, host){
            return new redisClientStub();
        }
    };

    describe('init', function(){

        beforeAll(function(done){
            ConfigManager.define(fsStub, null, null, null, mmxManagerStub);
            done();
        });

        beforeEach(function(done){
            ConfigManager.configs = Helper.deepCopy(configStatic);
            done();
        });

        it("should callback if server is not configured", function(done) {
            ConfigManager.configs.App.configured = false;
            ConfigManager.init(function(e){
                expect(e).toBeUndefined();
                done();
            });
        });

        it("should callback if server is configured", function(done) {
            ConfigManager.configs.App.configured = true;
            ConfigManager.init(function(e){
                expect(e).toBeUndefined();
                done();
            });
        });

        describe('redis', function(){

            it("should fail to connect if redis not available", function(done) {
                ConfigManager.configs.App.configured = true;
                ConfigManager.configs.Redis.enabled = true;
                ConfigManager.init(function(e){
                    expect(e).toEqual('connect-error');
                    done();
                });
            });

            it("should connect if redis available", function(done) {
                ConfigManager.configs.App.configured = true;
                ConfigManager.configs.Redis.enabled = true;
                ConfigManager.define(fsStub, null, redisStub, null, mmxManagerStub);
                ConfigManager.init(function(e){
                    expect(e).toBeUndefined();
                    done();
                });
            });

            it("should connect and storeConfig", function(done) {
                ConfigManager.configs.App.configured = true;
                ConfigManager.configs.Redis.enabled = true;
                ConfigManager.configs.Redis.storeConfig = true;
                ConfigManager.define(fsStub, null, redisStub, null, mmxManagerStub);
                ConfigManager.init(function(e){
                    expect(e).toBeUndefined();
                    done();
                });
            });

        });

        afterAll(function(done){
            ConfigManager.configs = Helper.deepCopy(configStatic);
            ConfigManager.define(fsStub, null, null, null, mmxManagerStub);
            done();
        });

    });

    describe('initComplete', function(){

        it("should connect to messaging server", function(done) {
            ConfigManager.initComplete(function(e){
                expect(e).toBeUndefined();
                done();
            });
        });

    });

    describe('get', function(){

        it("should obtain cached config", function(done) {
            ConfigManager.configs.Redis.enabled = false;
            ConfigManager.define(fsStub, null, null, null, mmxManagerStub);
            ConfigManager.get('App', function(e, config){
                expect(e).toBeNull();
                expect(config.port).toEqual(3000);
                done();
            }, true);
        });

        it("should obtain redis config", function(done) {
            ConfigManager.configs.App.configured = true;
            ConfigManager.configs.Redis.enabled = true;
            ConfigManager.configs.Redis.storeConfig = true;
            ConfigManager.define(fsStub, null, redisStub, null, mmxManagerStub);
            ConfigManager.get('App', function(e, config){
                expect(e).toBeNull();
                expect(config.port).toEqual(3000);
                done();
            });
        });

    });

    describe('retrieveLocal', function(){

        it("should return config", function(done) {
            var config = ConfigManager.retrieveLocal();
            expect(config.App.port).toEqual(3000);
            done();
        });

        it("should obtain redis config", function(done) {
            var fsStubInst = Helper.deepCopy(fsStub);
            fsStubInst.readFileSync = function(){
                throw 'test-error';
            };
            ConfigManager.define(fsStubInst, null, null, null, mmxManagerStub);
            expect(function(){ConfigManager.retrieveLocal()}).toThrow();
            done();
        });

        afterAll(function(done){
            ConfigManager.define(fsStub, null, null, null, mmxManagerStub);
            done();
        });

    });

    describe('set', function(){

        beforeEach(function(done){
            ConfigManager.configs = Helper.deepCopy(configStatic);
            done();
        });

        it("should fail given invalid feature", function(done) {
            ConfigManager.set('invalid', {
                'foo' : 'bar'
            }, function(e){
                expect(e).toEqual('invalid-feature');
                done();
            });
        });

        it("should fail given invalid data types", function(done) {
            ConfigManager.set('App', {
                'port' : 'should-be-int'
            }, function(e){
                expect(e).toEqual([ 'port' ]);
                done();
            });
        });

    });

    describe('storeLocal', function(){

        beforeEach(function(done){
            ConfigManager.configs = Helper.deepCopy(configStatic);
            done();
        });

        it("should fail given invalid feature", function(done) {
            var fsStubInst = {
                writeFile: function(configPath, str, cb){
                    cb('test-error');
                }
            };
            ConfigManager.define(fsStubInst, null, null, null, mmxManagerStub);
            ConfigManager.storeLocal(function(e){
                expect(e).toEqual('save-error');
                done();
            });
        });

        it("should set config after storing", function(done) {
            expect(ENV_CONFIG.App.port).toEqual(3000);
            ConfigManager.configs.App.port = 3001;
            ConfigManager.storeLocal(function(e){
                expect(ConfigManager.configs.App.port).toEqual(3001);
                done();
            });
        });

        afterAll(function(done){
            ConfigManager.configs = Helper.deepCopy(configStatic);
            ConfigManager.define(fsStub, null, null, null, mmxManagerStub);
            done();
        });

    });

    describe('setDB', function(){

        it("should fail if missing a required db config", function(done) {
            var obj = {
                dbName   : 'mydb',
//                host     : 'localhost',
                username : 'foo',
                port     : 3306
            };
            ConfigManager.setDB(obj, function(e){
                expect(e).toEqual('connect-error');
                done();
            });
        });

        it("should fail if unable to connect to db", function(done) {
            var obj = {
                dbName   : 'mydb',
                host     : 'invalid-host',
                username : 'foo',
                port     : 3306
            };
            ConfigManager.setDB(obj, function(e){
                expect(e).toEqual('ENOTFOUND');
                done();
            });
        });

        it("should fail to connect given incorrect credentials", function(done) {
            var obj = {
                dbName   : 'magnetmessagedb',
                host     : 'localhost',
                username : 'nonexist-user',
                port     : 3306
            };
            ConfigManager.setDB(obj, function(e){
                expect(e).toEqual('ER_DBACCESS_DENIED_ERROR');
                done();<commit_msg>make test pass against jenkins version of mysql<commit_after>var ConfigManager = require("../lib/ConfigManager")
 , Helper = require('./Helper')
 , orm = require('../lib/orm')
, fs = require('fs')
, sinon = require('sinon')
, mysql = require('mysql')
, magnetId = require('node-uuid')
, _ = require('underscore');

jasmine.getEnv().defaultTimeoutInterval = 5000;

var configStatic = Helper.deepCopy(require('../lib/config/config.json'));

describe('ConfigManager', function(){

    beforeAll(function(done){
        ENV_CONFIG.DatabaseLog.enabled = false;
        var connection =  mysql.createConnection({
            host     : ENV_CONFIG.Database.host,
            port     : ENV_CONFIG.Database.port,
            user     : ENV_CONFIG.Database.username,
            password : ENV_CONFIG.Database.password
        });
        connection.query('CREATE DATABASE IF NOT EXISTS '+ENV_CONFIG.Database.dbName+';', function(e){
            if(e){
                expect(e).toEqual('failed-test');
            }
            orm.setup('./lib/models', function(){
                done();
            });
        });
    });

    var mmxManagerStub = {
        getServerStatus : function(obj, cb){
            cb(null, {"setupComplete":true,"mmxAdminPort":"6060","mmxAdminPortSecure":"6061","mmxPublicPort":"5220","mmxPublicPortSecure":"5221"}, 200);
        },
        getConfigs : function(section, cb){
            cb(null, {});
        },
        provisionServer: function(connect, config, cb){
            cb(null, 'test-status', 201);
        }
    };

    var fsStub  = {
        readFile: function(configPath, encoding, cb){
            cb(null, {});
        },
        readFileSync: function(configPath, encoding){
            if(configPath == './lib/config/auth.json')
                return {
                    email    : '<EMAIL>',
                    password : '<PASSWORD>'
                };
            if(configPath == './lib/config/config.json')
                return JSON.stringify(Helper.deepCopy(configStatic));
        },
        writeFile: function(configPath, str, cb){
            (cb || function(){})();
        }
    };

    var redisClientStub = function(){
        this.evts = {};
        this.auth = function(password){
        };
        this.on = function(evt, cb){
            this.evts[evt] = cb;
        };
        this.get = function(tag, cb){
            if(tag == 'redis-init')
                this.evts['connect'](null, this);
            else
                cb(null, JSON.stringify(Helper.deepCopy(configStatic)));
        }
        this.set = function(tag, cfg, cb){
            cb();
        }
    };
    var redisStub = {
        createClient: function(port, host){
            return new redisClientStub();
        }
    };

    describe('init', function(){

        beforeAll(function(done){
            ConfigManager.define(fsStub, null, null, null, mmxManagerStub);
            done();
        });

        beforeEach(function(done){
            ConfigManager.configs = Helper.deepCopy(configStatic);
            done();
        });

        it("should callback if server is not configured", function(done) {
            ConfigManager.configs.App.configured = false;
            ConfigManager.init(function(e){
                expect(e).toBeUndefined();
                done();
            });
        });

        it("should callback if server is configured", function(done) {
            ConfigManager.configs.App.configured = true;
            ConfigManager.init(function(e){
                expect(e).toBeUndefined();
                done();
            });
        });

        describe('redis', function(){

            it("should fail to connect if redis not available", function(done) {
                ConfigManager.configs.App.configured = true;
                ConfigManager.configs.Redis.enabled = true;
                ConfigManager.init(function(e){
                    expect(e).toEqual('connect-error');
                    done();
                });
            });

            it("should connect if redis available", function(done) {
                ConfigManager.configs.App.configured = true;
                ConfigManager.configs.Redis.enabled = true;
                ConfigManager.define(fsStub, null, redisStub, null, mmxManagerStub);
                ConfigManager.init(function(e){
                    expect(e).toBeUndefined();
                    done();
                });
            });

            it("should connect and storeConfig", function(done) {
                ConfigManager.configs.App.configured = true;
                ConfigManager.configs.Redis.enabled = true;
                ConfigManager.configs.Redis.storeConfig = true;
                ConfigManager.define(fsStub, null, redisStub, null, mmxManagerStub);
                ConfigManager.init(function(e){
                    expect(e).toBeUndefined();
                    done();
                });
            });

        });

        afterAll(function(done){
            ConfigManager.configs = Helper.deepCopy(configStatic);
            ConfigManager.define(fsStub, null, null, null, mmxManagerStub);
            done();
        });

    });

    describe('initComplete', function(){

        it("should connect to messaging server", function(done) {
            ConfigManager.initComplete(function(e){
                expect(e).toBeUndefined();
                done();
            });
        });

    });

    describe('get', function(){

        it("should obtain cached config", function(done) {
            ConfigManager.configs.Redis.enabled = false;
            ConfigManager.define(fsStub, null, null, null, mmxManagerStub);
            ConfigManager.get('App', function(e, config){
                expect(e).toBeNull();
                expect(config.port).toEqual(3000);
                done();
            }, true);
        });

        it("should obtain redis config", function(done) {
            ConfigManager.configs.App.configured = true;
            ConfigManager.configs.Redis.enabled = true;
            ConfigManager.configs.Redis.storeConfig = true;
            ConfigManager.define(fsStub, null, redisStub, null, mmxManagerStub);
            ConfigManager.get('App', function(e, config){
                expect(e).toBeNull();
                expect(config.port).toEqual(3000);
                done();
            });
        });

    });

    describe('retrieveLocal', function(){

        it("should return config", function(done) {
            var config = ConfigManager.retrieveLocal();
            expect(config.App.port).toEqual(3000);
            done();
        });

        it("should obtain redis config", function(done) {
            var fsStubInst = Helper.deepCopy(fsStub);
            fsStubInst.readFileSync = function(){
                throw 'test-error';
            };
            ConfigManager.define(fsStubInst, null, null, null, mmxManagerStub);
            expect(function(){ConfigManager.retrieveLocal()}).toThrow();
            done();
        });

        afterAll(function(done){
            ConfigManager.define(fsStub, null, null, null, mmxManagerStub);
            done();
        });

    });

    describe('set', function(){

        beforeEach(function(done){
            ConfigManager.configs = Helper.deepCopy(configStatic);
            done();
        });

        it("should fail given invalid feature", function(done) {
            ConfigManager.set('invalid', {
                'foo' : 'bar'
            }, function(e){
                expect(e).toEqual('invalid-feature');
                done();
            });
        });

        it("should fail given invalid data types", function(done) {
            ConfigManager.set('App', {
                'port' : 'should-be-int'
            }, function(e){
                expect(e).toEqual([ 'port' ]);
                done();
            });
        });

    });

    describe('storeLocal', function(){

        beforeEach(function(done){
            ConfigManager.configs = Helper.deepCopy(configStatic);
            done();
        });

        it("should fail given invalid feature", function(done) {
            var fsStubInst = {
                writeFile: function(configPath, str, cb){
                    cb('test-error');
                }
            };
            ConfigManager.define(fsStubInst, null, null, null, mmxManagerStub);
            ConfigManager.storeLocal(function(e){
                expect(e).toEqual('save-error');
                done();
            });
        });

        it("should set config after storing", function(done) {
            expect(ENV_CONFIG.App.port).toEqual(3000);
            ConfigManager.configs.App.port = 3001;
            ConfigManager.storeLocal(function(e){
                expect(ConfigManager.configs.App.port).toEqual(3001);
                done();
            });
        });

        afterAll(function(done){
            ConfigManager.configs = Helper.deepCopy(configStatic);
            ConfigManager.define(fsStub, null, null, null, mmxManagerStub);
            done();
        });

    });

    describe('setDB', function(){

        it("should fail if missing a required db config", function(done) {
            var obj = {
                dbName   : 'mydb',
//                host     : 'localhost',
                username : 'foo',
                port     : 3306
            };
            ConfigManager.setDB(obj, function(e){
                expect(e).toEqual('connect-error');
                done();
            });
        });

        it("should fail if unable to connect to db", function(done) {
            var obj = {
                dbName   : 'mydb',
                host     : 'invalid-host',
                username : 'foo',
                port     : 3306
            };
            ConfigManager.setDB(obj, function(e){
                expect(e).toEqual('ENOTFOUND');
                done();
            });
        });

        it("should fail to connect given incorrect credentials", function(done) {
            var obj = {
                dbName   : 'magnetmessagedb',
                host     : 'localhost',
                username : 'nonexist-user',
                port     : 3306
            };
            ConfigManager.setDB(obj, function(e){
                expect(e).toContain('ACCESS_DENIED_ERROR');
                done();
{'fraction_non_alphanumeric': 0.13183947457381562, 'fraction_numerical': 0.005988313130825325, 'mean_word_length': 1.6146464646464647, 'pattern_counts': {'":': 10, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5801248', 'n_tokens_mistral': 5535, 'n_tokens_neox': 5283, 'n_words': 950}