<commit_before>/*global describe, it, expect, example, before, after, beforeEach, afterEach, mocha, sinon*/
'use strict';
var $ = require('jquery');
var bootbox = require('bootbox');

var assert = require('chai').assert;

var utils = require('tests/utils');
var faker = require('faker');

var s3NodeConfig = require('../s3NodeConfig');
var s3NodeConfigVM = s3NodeConfig._S3NodeConfigViewModel;
var isValidBucketName = s3NodeConfig._isValidBucketName;

var API_BASE = '/api/v1/12345/s3';
var SETTINGS_URL = [API_BASE, 'settings', ''].join('/');
var URLS = {
    create_bucket: [API_BASE, 'newbucket', ''].join('/'),
    import_auth: [API_BASE, 'import-auth', ''].join('/'),
    create_auth: [API_BASE, 'authorize', ''].join('/'),
    deauthorize: SETTINGS_URL,
    bucket_list: [API_BASE, 'buckets', ''].join('/'),
    set_bucket: SETTINGS_URL,
    settings: '/settings/addons/'
};
var makeSettingsEndpoint = function(result, urls) {
    return {
        method: 'GET',
        url: SETTINGS_URL,
        response: {
            result: $.extend({}, {
                urls: $.extend({}, URLS, urls)
            }, result)
        }
    };
};

var noop = function() {};
var APITestCase = function(cfg) {
    this.description = cfg.description || '';
    this.endpoint = cfg.endpoint || {
        response: {
            result: {}
        }
    };
    this.expected = this.endpoint.response.result;
    $.extend(this.expected, cfg.data || {});
};
APITestCase.prototype.run = function(test) {
    var tc = this;
    var server;
    tc.before = () => {
        server = utils.createServer(sinon, [tc.endpoint]);
    };
    tc.after = () => {
        server.restore();
    };
    test(tc);
};
var APITestCases = function(test, cases) {
    for (var i = 0; i < cases.length; i++) {
        new APITestCase(cases[i]).run(test);
    }
};

var s3ViewModelSettings = {
    url: '/api/v1/12345/s3/settings/',
    encryptUploads: false,
    bucketLocations: {
        '': 'US',
        'EU': 'ES',
        'us-west-1': 'CA',
        'us-west-2': 'OR',
        'ap-northeast-1': 'TO',
        'ap-southeast-1': 'SI',
        'ap-southeast-2': 'SY',
        'cn-north-1': 'BE'
    }
};

describe('s3NodeConfigViewModel', () => {
    describe('isValidBucketName', () => {
    var chars = [];
    for (var i = 0; i < 63; i++) {
        chars.push('a');
    }
        var longName = chars.join('');

    var moreChars = [];
    for (var j = 0; j < 255; j++) {
        moreChars.push('a');
    }
    var reallyLongName = moreChars.join('');

        it('allows these names in strict mode', (done) => {
            assert.isTrue(isValidBucketName('valid'), 'basic label');
            assert.isTrue(isValidBucketName('also.valid'), 'two labels');
            assert.isTrue(isValidBucketName('pork.beef.chicken'), 'three labels');
            assert.isTrue(isValidBucketName('a-o.valid'), 'label w/ hyphen');
            assert.isTrue(isValidBucketName('11.12.m'), 'numbers as labels');
            assert.isTrue(isValidBucketName('aa.22.cc'), 'mixed label types');
            assert.isTrue(isValidBucketName('a------a'), 'multiple hypens');
            assert.isTrue(isValidBucketName(longName), 'name up to 63 characters');
            done();
        });

        it('DOES NOT allow these names in strict mode', (done) => {
            assert.isFalse(isValidBucketName(''), 'empty');
            assert.isFalse(isValidBucketName('no'), 'too short');
            assert.isFalse(isValidBucketName(longName + 'a'), '64 characters, too long');
            assert.isFalse(isValidBucketName(' aaa'), 'leading space');
            assert.isFalse(isValidBucketName('aaa '), 'trailing space');
            assert.isFalse(isValidBucketName('aaa.'), 'trailing period');
            assert.isFalse(isValidBucketName('.aaa'), 'leading period');
            assert.isFalse(isValidBucketName('aaa-'), 'trailing hyphen');
            assert.isFalse(isValidBucketName('-aaa'), 'leading hyphen');
            assert.isFalse(isValidBucketName('aAa'), 'mixed case');
            assert.isFalse(isValidBucketName('Aaa'), 'capital first letter');
            assert.isFalse(isValidBucketName('aaA'), 'capital last letter');
            assert.isFalse(isValidBucketName('a..b'), 'empty label');
            assert.isFalse(isValidBucketName('a-.b'), 'label cannot end with hyphen');
            assert.isFalse(isValidBucketName('a.-b'), 'label cannot begin with hyphen');
            assert.isFalse(isValidBucketName('8.8.8.8'), 'label cannot look like IP addr');
            assert.isFalse(isValidBucketName('600.9000.0.28'), '  ..not even a fake IP addr');<commit_msg>add additional tests for strict checking<commit_after>/*global describe, it, expect, example, before, after, beforeEach, afterEach, mocha, sinon*/
'use strict';
var $ = require('jquery');
var bootbox = require('bootbox');

var assert = require('chai').assert;

var utils = require('tests/utils');
var faker = require('faker');

var s3NodeConfig = require('../s3NodeConfig');
var s3NodeConfigVM = s3NodeConfig._S3NodeConfigViewModel;
var isValidBucketName = s3NodeConfig._isValidBucketName;

var API_BASE = '/api/v1/12345/s3';
var SETTINGS_URL = [API_BASE, 'settings', ''].join('/');
var URLS = {
    create_bucket: [API_BASE, 'newbucket', ''].join('/'),
    import_auth: [API_BASE, 'import-auth', ''].join('/'),
    create_auth: [API_BASE, 'authorize', ''].join('/'),
    deauthorize: SETTINGS_URL,
    bucket_list: [API_BASE, 'buckets', ''].join('/'),
    set_bucket: SETTINGS_URL,
    settings: '/settings/addons/'
};
var makeSettingsEndpoint = function(result, urls) {
    return {
        method: 'GET',
        url: SETTINGS_URL,
        response: {
            result: $.extend({}, {
                urls: $.extend({}, URLS, urls)
            }, result)
        }
    };
};

var noop = function() {};
var APITestCase = function(cfg) {
    this.description = cfg.description || '';
    this.endpoint = cfg.endpoint || {
        response: {
            result: {}
        }
    };
    this.expected = this.endpoint.response.result;
    $.extend(this.expected, cfg.data || {});
};
APITestCase.prototype.run = function(test) {
    var tc = this;
    var server;
    tc.before = () => {
        server = utils.createServer(sinon, [tc.endpoint]);
    };
    tc.after = () => {
        server.restore();
    };
    test(tc);
};
var APITestCases = function(test, cases) {
    for (var i = 0; i < cases.length; i++) {
        new APITestCase(cases[i]).run(test);
    }
};

var s3ViewModelSettings = {
    url: '/api/v1/12345/s3/settings/',
    encryptUploads: false,
    bucketLocations: {
        '': 'US',
        'EU': 'ES',
        'us-west-1': 'CA',
        'us-west-2': 'OR',
        'ap-northeast-1': 'TO',
        'ap-southeast-1': 'SI',
        'ap-southeast-2': 'SY',
        'cn-north-1': 'BE'
    }
};

describe('s3NodeConfigViewModel', () => {
    describe('isValidBucketName', () => {
    var chars = [];
    for (var i = 0; i < 63; i++) {
        chars.push('a');
    }
        var longName = chars.join('');

    var moreChars = [];
    for (var j = 0; j < 255; j++) {
        moreChars.push('a');
    }
    var reallyLongName = moreChars.join('');

        it('allows these names in strict mode', (done) => {
            assert.isTrue(isValidBucketName('valid'), 'basic label');
            assert.isTrue(isValidBucketName('also.valid'), 'two labels');
            assert.isTrue(isValidBucketName('pork.beef.chicken'), 'three labels');
            assert.isTrue(isValidBucketName('a-o.valid'), 'label w/ hyphen');
            assert.isTrue(isValidBucketName('11.12.m'), 'numbers as labels');
            assert.isTrue(isValidBucketName('aa.22.cc'), 'mixed label types');
            assert.isTrue(isValidBucketName('a------a'), 'multiple hypens');
            assert.isTrue(isValidBucketName(longName), 'name up to 63 characters');
            done();
        });

        it('DOES NOT allow these names in strict mode', (done) => {
            assert.isFalse(isValidBucketName(''), 'empty');
            assert.isFalse(isValidBucketName('no'), 'too short');
            assert.isFalse(isValidBucketName(longName + 'a'), '64 characters, too long');
            assert.isFalse(isValidBucketName(' aaa'), 'leading space');
            assert.isFalse(isValidBucketName('aaa '), 'trailing space');
            assert.isFalse(isValidBucketName('aaa.'), 'trailing period');
            assert.isFalse(isValidBucketName('.aaa'), 'leading period');
            assert.isFalse(isValidBucketName('aaa-'), 'trailing hyphen');
            assert.isFalse(isValidBucketName('-aaa'), 'leading hyphen');
            assert.isFalse(isValidBucketName('aAa'), 'mixed case');
            assert.isFalse(isValidBucketName('Aaa'), 'capital first letter');
            assert.isFalse(isValidBucketName('aaA'), 'capital last letter');
            assert.isFalse(isValidBucketName('a..b'), 'empty label');
            assert.isFalse(isValidBucketName('a-.b'), 'label cannot end with hyphen');
            assert.isFalse(isValidBucketName('a.-b'), 'label cannot begin with hyphen');
            assert.isFalse(isValidBucketName('8.8.8.8'), 'label cannot look like IP addr');
            assert.isFalse(isValidBucketName('600.9000.0.28'), '  ..not even a fake IP addr');
            assert.isFalse(isValidBucketName(':aa', true), 'colon leading character');
            assert.isFalse(isValidBucketName('aa;', true), 'semicolon trailing');
            assert.isFalse(isValidBucketName('space middle', true), 'space in the middle');
{'fraction_non_alphanumeric': 0.17469627147046501, 'fraction_numerical': 0.012568077084206116, 'mean_word_length': 2.8180727708916433, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '821412', 'n_tokens_mistral': 3023, 'n_tokens_neox': 2971, 'n_words': 616}