<commit_before>/*jshint node:true, mocha:true*/
/**
 * @author pmeijer / https://github.com/pmeijer
 */

describe('standalone http server with authentication turned on', function () {
    'use strict';
    var testFixture = require('../_globals.js'),
        WebGME = testFixture.WebGME,
        safeStorage,
        webgmeToken,
        gmeAuth,
        serverBaseUrl,
        expect = testFixture.expect,
        should = testFixture.should,
        superagent = testFixture.superagent,
        Q = testFixture.Q,
        logger,
        agent,
        server,
        authorizer,
        projectAuthParams,
        logIn = function (callback) {
            testFixture.logIn(server, agent, 'user', 'plaintext').nodeify(callback);
        },
        openSocketIo = function (callback) {
            return testFixture.openSocketIo(server, agent, 'user', 'plaintext')
                .then(function (result) {
                    webgmeToken = result.webgmeToken;
                    return result.socket;
                })
                .nodeify(callback);
        };

    beforeEach(function () {
        agent = superagent.agent();
    });

    before(function (done) {
        // we have to set the config here
        var dbConn,
            serverReady = Q.defer(),
            project = 'project',
            unauthorizedProject = 'unauthorized_project',
            gmeConfig = testFixture.getGmeConfig();

        logger = testFixture.logger.fork('standalone.auth.spec');
        gmeConfig.authentication.enable = true;
        gmeConfig.authentication.allowGuests = false;

        dbConn = testFixture.clearDBAndGetGMEAuth(gmeConfig)
            .then(function (gmeAuth_) {
                gmeAuth = gmeAuth_;
                authorizer = gmeAuth.authorizer;
                projectAuthParams = {
                    entityType: authorizer.ENTITY_TYPES.PROJECT,
                };
                safeStorage = testFixture.getMongoStorage(logger, gmeConfig, gmeAuth);
                return safeStorage.openDatabase();

            })
            .then(function () {
                return Q.allDone([
                    testFixture.importProject(safeStorage, {
                        projectSeed: 'seeds/EmptyProject.webgmex',
                        projectName: project,
                        gmeConfig: gmeConfig,
                        logger: logger
                    }),
                    testFixture.importProject(safeStorage, {
                        projectSeed: 'seeds/EmptyProject.webgmex',
                        projectName: unauthorizedProject,
                        gmeConfig: gmeConfig,
                        logger: logger
                    })
                ]);
            });

        server = WebGME.standaloneServer(gmeConfig);
        serverBaseUrl = server.getUrl();
        server.start(serverReady.makeNodeResolver());

        Q.allDone([serverReady, dbConn])
            .then(function () {
                return gmeAuth.addUser('user', '<EMAIL>', 'plaintext', true, {overwrite: true});
            })
            .then(function () {
                return gmeAuth.authorizeByUserId('user', testFixture.projectName2Id('project'),
                    'create', {
                        read: true,
                        write: true,
                        delete: false
                    }
                );
            })
            .then(function () {
                return gmeAuth.authorizeByUserId('user', testFixture.projectName2Id('unauthorized_project'),
                    'create', {
                        read: false,
                        write: false,
                        delete: false
                    }
                );
            })
            .nodeify(done);
    });

    after(function (done) {
        server.stop(function (err) {
            if (err) {
                logger.error(err);
            }
            gmeAuth.unload()
                .nodeify(done);
        });
    });
<commit_msg>WIP Don't drop the database during server start. (#1034)<commit_after>/*jshint node:true, mocha:true*/
/**
 * @author pmeijer / https://github.com/pmeijer
 */

describe('standalone http server with authentication turned on', function () {
    'use strict';
    var testFixture = require('../_globals.js'),
        WebGME = testFixture.WebGME,
        safeStorage,
        webgmeToken,
        gmeAuth,
        serverBaseUrl,
        expect = testFixture.expect,
        should = testFixture.should,
        superagent = testFixture.superagent,
        Q = testFixture.Q,
        logger,
        agent,
        server,
        authorizer,
        projectAuthParams,
        logIn = function (callback) {
            testFixture.logIn(server, agent, 'user', 'plaintext').nodeify(callback);
        },
        openSocketIo = function (callback) {
            return testFixture.openSocketIo(server, agent, 'user', 'plaintext')
                .then(function (result) {
                    webgmeToken = result.webgmeToken;
                    return result.socket;
                })
                .nodeify(callback);
        };

    beforeEach(function () {
        agent = superagent.agent();
    });

    before(function (done) {
        // we have to set the config here
        var project = 'project',
            unauthorizedProject = 'unauthorized_project',
            gmeConfig = testFixture.getGmeConfig();

        logger = testFixture.logger.fork('standalone.auth.spec');
        gmeConfig.authentication.enable = true;
        gmeConfig.authentication.allowGuests = false;

        testFixture.clearDBAndGetGMEAuth(gmeConfig)
            .then(function (gmeAuth_) {
                gmeAuth = gmeAuth_;
                authorizer = gmeAuth.authorizer;
                projectAuthParams = {
                    entityType: authorizer.ENTITY_TYPES.PROJECT,
                };

                safeStorage = testFixture.getMongoStorage(logger, gmeConfig, gmeAuth);
                return safeStorage.openDatabase();
            })
            .then(function () {
                return Q.allDone([
                    testFixture.importProject(safeStorage, {
                        projectSeed: 'seeds/EmptyProject.webgmex',
                        projectName: project,
                        gmeConfig: gmeConfig,
                        logger: logger
                    }),
                    testFixture.importProject(safeStorage, {
                        projectSeed: 'seeds/EmptyProject.webgmex',
                        projectName: unauthorizedProject,
                        gmeConfig: gmeConfig,
                        logger: logger
                    })
                ]);
            })
            .then(function () {
                return gmeAuth.addUser('user', '<EMAIL>', 'plaintext', true, {overwrite: true});
            })
            .then(function () {
                return gmeAuth.authorizeByUserId('user', testFixture.projectName2Id('project'),
                    'create', {
                        read: true,
                        write: true,
                        delete: false
                    }
                );
            })
            .then(function () {
                return gmeAuth.authorizeByUserId('user', testFixture.projectName2Id('unauthorized_project'),
                    'create', {
                        read: false,
                        write: false,
                        delete: false
                    }
                );
            })
            .then(function () {
                server = WebGME.standaloneServer(gmeConfig);
                serverBaseUrl = server.getUrl();

                return Q.ninvoke(server, 'start');
            })
            .nodeify(done);
    });

    after(function (done) {
        server.stop(function (err) {
            if (err) {
                logger.error(err);
            }
            gmeAuth.unload()
                .nodeify(done);
        });
    });

{'fraction_non_alphanumeric': 0.10077519379844961, 'fraction_numerical': 0.001000250062515629, 'mean_word_length': 1.3464359049574655, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '5198761', 'n_tokens_mistral': 2082, 'n_tokens_neox': 1968, 'n_words': 351}