<commit_before>require('dotenv').load({ silent: true });

var Bluebird = require('bluebird');
var Code = require('code');
var Lab = require('lab');
var Sandbox = require('../');
var Url = require('url');
var _ = require('lodash');

var lab = exports.lab = Lab.script();
var expect = Code.expect;

var sandboxParams = {
    url: process.env.SANDBOX_URL,
    token: process.env.SANDBOX_TOKEN,
    container: process.env.SANDBOX_CONTAINER,
};


lab.experiment('Sandbox instance', {parallel: true, timeout: 10000}, function () {
    var googleTestCodeUrl = 'https://cdn.auth0.com/webtasks/test_cron_google.js';

    lab.test('test pre-requisites are met', function (done) {

        expect(sandboxParams.url).to.exist();
        expect(sandboxParams.token).to.exist();
        expect(sandboxParams.container).to.exist();

        done();
    });

    lab.test('can be used to create a Webtask using Promise syntax', function (done) {
        var sandbox = Sandbox.init(sandboxParams);

        sandbox.create(googleTestCodeUrl)
            .then(function (webtask) {
                expect(webtask).to.be.an.object();
                expect(webtask.constructor).to.be.a.function();
                expect(webtask.constructor.name).to.equal('Webtask');
                expect(webtask.container).to.equal(sandbox.container);
                expect(webtask.token).to.be.a.string();
                expect(webtask.url).to.be.a.string();
                expect(webtask.url).to.match(/^https:\/\//);
            })
            .nodeify(done);
    });

    lab.test('can be used to create a Webtask using node-style callback', function (done) {
        var sandbox = Sandbox.init(sandboxParams);

        sandbox.create(googleTestCodeUrl, function (err, webtask) {
            expect(webtask).to.be.an.object();
            expect(webtask.constructor).to.be.a.function();
            expect(webtask.constructor.name).to.equal('Webtask');
            expect(webtask.container).to.equal(sandbox.container);
            expect(webtask.token).to.be.a.string();
            expect(webtask.url).to.be.a.string();
            expect(webtask.url).to.match(/^https:\/\//);

            done(err);
        });
    });

    lab.test('can be used to create a named Webtask', function (done) {
        var sandbox = Sandbox.init(sandboxParams);
        var tokenOptions = {
            name: 'pinggoogle',
        };

        Bluebird.join(
            sandbox.create(googleTestCodeUrl),
            sandbox.create(googleTestCodeUrl, tokenOptions),
            function (webtask1, webtask2) {
                var url1 = Url.parse(webtask1.url, true);
                var url2 = Url.parse(webtask2.url, true);

                expect(webtask1.url).to.match(/^https:\/\//);
                expect(webtask2.url).to.match(/^https:\/\//);
                expect(url1.pathname + '/' + tokenOptions.name).to.equal(url2.pathname);
                expect(url1.query.key).to.be.a.string();
                expect(url2.query.key).to.not.exist();
            })
            .nodeify(done);
    });

    lab.test('can be used to run a webtask', function (done) {
        var sandbox = Sandbox.init(sandboxParams);

        sandbox.run(googleTestCodeUrl)
            .then(function (res) {
                expect(res.statusCode).to.be.at.least(200).and.below(300);
                expect(res.text).to.match(/^\d+$/);
            })
            .nodeify(done);
    });

    lab.test('can be used to run a named Webtask', function (done) {
        var sandbox = Sandbox.init(sandboxParams);

        sandbox.run(googleTestCodeUrl, { name: 'pinggoogle' })
            .then(function (res) {
                expect(res.statusCode).to.be.at.least(200).and.below(300);
                expect(res.text).to.match(/^\d+$/);
            })
            .nodeify(done);
    });

    lab.test('can be used to run code', function (done) {
        var sandbox = Sandbox.init(sandboxParams);
        var code = 'module.exports = function (ctx, cb) { cb(null, ctx.data.id); }';
        var query = {id: 'test'};

        sandbox.run(code, {query: query})
            .then(function (res, body) {
                expect(res.statusCode).to.be.at.least(200).and.below(300);
                expect(res.body).to.equal(query.id);
            })
            .nodeify(done);
    });
});<commit_msg>Add tests for 4XX and 5XX responses<commit_after>require('dotenv').load({ silent: true });

var Bluebird = require('bluebird');
var Code = require('code');
var Lab = require('lab');
var Sandbox = require('../');
var Url = require('url');
var _ = require('lodash');

var lab = exports.lab = Lab.script();
var expect = Code.expect;

var sandboxParams = {
    url: process.env.SANDBOX_URL,
    token: process.env.SANDBOX_TOKEN,
    container: process.env.SANDBOX_CONTAINER,
};


lab.experiment('Sandbox instance', {parallel: true, timeout: 10000}, function () {
    var googleTestCodeUrl = 'https://cdn.auth0.com/webtasks/test_cron_google.js';

    lab.test('test pre-requisites are met', function (done) {

        expect(sandboxParams.url).to.exist();
        expect(sandboxParams.token).to.exist();
        expect(sandboxParams.container).to.exist();

        done();
    });

    lab.test('can be used to create a Webtask using Promise syntax', function (done) {
        var sandbox = Sandbox.init(sandboxParams);

        sandbox.create(googleTestCodeUrl)
            .then(function (webtask) {
                expect(webtask).to.be.an.object();
                expect(webtask.constructor).to.be.a.function();
                expect(webtask.constructor.name).to.equal('Webtask');
                expect(webtask.container).to.equal(sandbox.container);
                expect(webtask.token).to.be.a.string();
                expect(webtask.url).to.be.a.string();
                expect(webtask.url).to.match(/^https:\/\//);
            })
            .nodeify(done);
    });

    lab.test('can be used to create a Webtask using node-style callback', function (done) {
        var sandbox = Sandbox.init(sandboxParams);

        sandbox.create(googleTestCodeUrl, function (err, webtask) {
            expect(webtask).to.be.an.object();
            expect(webtask.constructor).to.be.a.function();
            expect(webtask.constructor.name).to.equal('Webtask');
            expect(webtask.container).to.equal(sandbox.container);
            expect(webtask.token).to.be.a.string();
            expect(webtask.url).to.be.a.string();
            expect(webtask.url).to.match(/^https:\/\//);

            done(err);
        });
    });

    lab.test('can be used to create a named Webtask', function (done) {
        var sandbox = Sandbox.init(sandboxParams);
        var tokenOptions = {
            name: '<PASSWORD>',
        };

        Bluebird.join(
            sandbox.create(googleTestCodeUrl),
            sandbox.create(googleTestCodeUrl, tokenOptions),
            function (webtask1, webtask2) {
                var url1 = Url.parse(webtask1.url, true);
                var url2 = Url.parse(webtask2.url, true);

                expect(webtask1.url).to.match(/^https:\/\//);
                expect(webtask2.url).to.match(/^https:\/\//);
                expect(url1.pathname + '/' + tokenOptions.name).to.equal(url2.pathname);
                expect(url1.query.key).to.be.a.string();
                expect(url2.query.key).to.not.exist();
            })
            .nodeify(done);
    });

    lab.test('can be used to run a webtask', function (done) {
        var sandbox = Sandbox.init(sandboxParams);

        sandbox.run(googleTestCodeUrl)
            .then(function (res) {
                expect(res.statusCode).to.be.at.least(200).and.below(300);
                expect(res.text).to.match(/^\d+$/);
            })
            .nodeify(done);
    });

    lab.test('can be used to run a named Webtask', function (done) {
        var sandbox = Sandbox.init(sandboxParams);

        sandbox.run(googleTestCodeUrl, { name: 'pinggoogle' })
            .then(function (res) {
                expect(res.statusCode).to.be.at.least(200).and.below(300);
                expect(res.text).to.match(/^\d+$/);
            })
            .nodeify(done);
    });

    lab.test('can be used to run code', function (done) {
        var sandbox = Sandbox.init(sandboxParams);
        var code = 'module.exports = function (ctx, cb) { cb(null, ctx.data.id); }';
        var query = {id: 'test'};

        sandbox.run(code, {query: query})
            .then(function (res, body) {
                expect(res.statusCode).to.be.at.least(200).and.below(300);
                expect(res.body).to.equal(query.id);
            })
            .nodeify(done);
    });

    lab.test('4XX errors do not reject promises', function (done) {
        var sandbox = Sandbox.init(sandboxParams);
        var code = 'module.exports = function (ctx, req, res) { res.writeHead(404); res.end(); }';

        sandbox.run(code)
            .then(function (res, body) {
                expect(res.statusCode).to.equal(404);
                expect(res.clientError).to.equal(true);
                expect(res.serverError).to.equal(false);
            })
            .nodeify(done);
    });

    lab.test('5XX errors do not reject promises', function (done) {
        var sandbox = Sandbox.init(sandboxParams);
        var code = 'module.exports = function (ctx, req, res) { res.writeHead(500); res.end(); }';

        sandbox.run(code)
            .then(function (res, body) {
                expect(res.statusCode).to.equal(500);
                expect(res.clientError).to.equal(false);
                expect(res.serverError).to.equal(true);
            })
            .nodeify(done);
    });
});
{'fraction_non_alphanumeric': 0.1574933003504432, 'fraction_numerical': 0.009070294784580499, 'mean_word_length': 2.1009907318632153, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '5767475', 'n_tokens_mistral': 3165, 'n_tokens_neox': 2977, 'n_words': 498}