<commit_before>// Copyright 2012 Joyent, Inc. All rights reserved.
var assert = require('assert');
var crypto = require('crypto');
var path = require('path');
var Logger = require('bunyan');
var restify = require('restify');
var uuid = require('node-uuid');
var d = require('dtrace-provider');
var UFDS = require('sdc-clients').UFDS;
var VMAPI = require('sdc-clients').VMAPI;
var Package = require('sdc-clients').Package;
var app = require('../lib').app;
var util = require('util');
var fs = require('fs');

// --- Globals

var PASSWD = '<PASSWORD>';
var DEFAULT_CFG = path.join(__dirname, '..', '/etc/cloudapi.config.json');
var LOG =  new Logger({
    level: process.env.LOG_LEVEL || 'info',
    name: 'cloudapi_unit_test',
    stream: process.stderr,
    serializers: {
        err: Logger.stdSerializers.err,
        req: Logger.stdSerializers.req,
        res: restify.bunyan.serializers.response
    }
});
var DTP = d.createDTraceProvider('cloudapi_test');
var config = {};
try {
    config = JSON.parse(fs.readFileSync(DEFAULT_CFG, 'utf8'));
} catch (e) {}

// --- Library

module.exports = {

    setup: function (callback) {
        assert.ok(callback);

        var user = 'a' + uuid().substr(0, 7) + '@joyent.com',
            ufds, napi, client,
            server = app.createServer({
                config: DEFAULT_CFG,
                log: LOG,
                dtrace: DTP,
                overrides: {},
                test: true
            }, function (s) {
                server = s;
                server.start(function () {
                    LOG.info('CloudAPI listening at %s', server.url);

                    client = restify.createJsonClient({
                        url: server.url,
                        version: '*',
                        retryOptions: {
                            retry: 0
                        },
                        log: LOG
                    });

                    client.basicAuth(user, PASSWD);
                    client.testUser = user;

                    // We need vmapi client to check jobs on tests, given if we
                    // just wait for vmachine status change, we'll be just
                    // hanging forever.
                    client.vmapi = new VMAPI({
                        url: process.env.VMAPI_URL || config.vmapi.url || 'http://10.99.99.21',
                        retry: {
                            retries: 1,
                            minTimeout: 1000
                        },
                        log: client.log
                    });

                    ufds = new UFDS({
                        url: (process.env.UFDS_URL || config.ufds.url || 'ldaps://10.99.99.14'),
                        bindDN: 'cn=root',
                        bindPassword: '<PASSWORD>'
                    });

                    ufds.on('error', function (err) {
                        return callback(err);
                    });

                    ufds.on('ready', function () {
                        var entry = {
                            login: client.testUser,
                            email: client.testUser,
                            userpassword: <PASSWORD>
                        };
                        return ufds.addUser(entry, function (err) {
                            if (err) {
                                return callback(err);
                            }

                            client.ufds = ufds;
                            client.pkg = new Package(ufds);
                            client.teardown = function teardown(cb) {
                                client.ufds.deleteUser(client.testUser,
                                    function (err2) {
                                    if (err2) {
                                        // blindly ignore
                                        // return cb(err2);
                                    }

                                    return ufds.close(function () {
                                        return cb(null);
                                    });
                                });
                            };

                            return callback(null, client, server);
                        });
                    });

                });

            });

    },

    checkHeaders: function (t, headers) {
        assert.ok(t);

        t.ok(headers, 'headers ok');
        t.ok(headers['access-control-allow-origin'], 'headers allow-origin');
        t.ok(headers['access-control-allow-methods'], 'headers allow-methods');
        t.ok(headers.date, 'headers date');
        t.ok(headers['x-request-id'], 'headers x-request-id');
        t.ok(headers['x-response-time'] >= 0, 'headers response time');
        t.equal(headers.server, 'Joyent SmartDataCenter 7.0.0',
                'headers server');
        t.equal(headers.connection, 'Keep-Alive', 'headers keep alive');
        t.equal(headers['x-api-version'], '7.0.0', 'headers x-api-version');
    }
};
<commit_msg>Allow UFDS bind DN & Password to be specified through cfg file.<commit_after>// Copyright 2012 Joyent, Inc. All rights reserved.
var assert = require('assert');
var crypto = require('crypto');
var path = require('path');
var Logger = require('bunyan');
var restify = require('restify');
var uuid = require('node-uuid');
var d = require('dtrace-provider');
var UFDS = require('sdc-clients').UFDS;
var VMAPI = require('sdc-clients').VMAPI;
var Package = require('sdc-clients').Package;
var app = require('../lib').app;
var util = require('util');
var fs = require('fs');

// --- Globals

var PASSWD = '<PASSWORD>';
var DEFAULT_CFG = path.join(__dirname, '..', '/etc/cloudapi.config.json');
var LOG =  new Logger({
    level: process.env.LOG_LEVEL || 'info',
    name: 'cloudapi_unit_test',
    stream: process.stderr,
    serializers: {
        err: Logger.stdSerializers.err,
        req: Logger.stdSerializers.req,
        res: restify.bunyan.serializers.response
    }
});
var DTP = d.createDTraceProvider('cloudapi_test');
var config = {};
try {
    config = JSON.parse(fs.readFileSync(DEFAULT_CFG, 'utf8'));
} catch (e) {}

// --- Library

module.exports = {

    setup: function (callback) {
        assert.ok(callback);

        var user = 'a' + uuid().substr(0, 7) + '@joyent.com',
            ufds, napi, client,
            server = app.createServer({
                config: DEFAULT_CFG,
                log: LOG,
                dtrace: DTP,
                overrides: {},
                test: true
            }, function (s) {
                server = s;
                server.start(function () {
                    LOG.info('CloudAPI listening at %s', server.url);

                    client = restify.createJsonClient({
                        url: server.url,
                        version: '*',
                        retryOptions: {
                            retry: 0
                        },
                        log: LOG
                    });

                    client.basicAuth(user, PASSWD);
                    client.testUser = user;

                    // We need vmapi client to check jobs on tests, given if we
                    // just wait for vmachine status change, we'll be just
                    // hanging forever.
                    client.vmapi = new VMAPI({
                        url: process.env.VMAPI_URL || config.vmapi.url || 'http://10.99.99.21',
                        retry: {
                            retries: 1,
                            minTimeout: 1000
                        },
                        log: client.log
                    });

                    ufds = new UFDS({
                        url: (process.env.UFDS_URL || config.ufds.url || 'ldaps://10.99.99.14'),
                        bindDN: (config.ufds.bindDN || 'cn=root'),
                        bindPassword: (config.ufds.bindPassword || '<PASSWORD>')
                    });

                    ufds.on('error', function (err) {
                        return callback(err);
                    });

                    ufds.on('ready', function () {
                        var entry = {
                            login: client.testUser,
                            email: client.testUser,
                            userpassword: <PASSWORD>
                        };
                        return ufds.addUser(entry, function (err) {
                            if (err) {
                                return callback(err);
                            }

                            client.ufds = ufds;
                            client.pkg = new Package(ufds);
                            client.teardown = function teardown(cb) {
                                client.ufds.deleteUser(client.testUser,
                                    function (err2) {
                                    if (err2) {
                                        // blindly ignore
                                        // return cb(err2);
                                    }

                                    return ufds.close(function () {
                                        return cb(null);
                                    });
                                });
                            };

                            return callback(null, client, server);
                        });
                    });

                });

            });

    },

    checkHeaders: function (t, headers) {
        assert.ok(t);

        t.ok(headers, 'headers ok');
        t.ok(headers['access-control-allow-origin'], 'headers allow-origin');
        t.ok(headers['access-control-allow-methods'], 'headers allow-methods');
        t.ok(headers.date, 'headers date');
        t.ok(headers['x-request-id'], 'headers x-request-id');
        t.ok(headers['x-response-time'] >= 0, 'headers response time');
        t.equal(headers.server, 'Joyent SmartDataCenter 7.0.0',
                'headers server');
        t.equal(headers.connection, 'Keep-Alive', 'headers keep alive');
        t.equal(headers['x-api-version'], '7.0.0', 'headers x-api-version');
    }
};

{'fraction_non_alphanumeric': 0.12295566502463054, 'fraction_numerical': 0.007684729064039409, 'mean_word_length': 1.1366028204588507, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2680060', 'n_tokens_mistral': 2687, 'n_tokens_neox': 2500, 'n_words': 552}