<commit_before>const assert = require('assert');
const { S3 } = require('aws-sdk');

const checkError = require('../../lib/utility/checkError');
const getConfig = require('../support/config');
const BucketUtility = require('../../lib/utility/bucket-util');

const bucket = 'mock-bucket';

function getObjectLockParams(status, mode, days, years) {
    const objectLockConfig = {
        ObjectLockEnabled: status,
        Rule: {
            DefaultRetention: {
                Mode: mode,
            },
        },
    };
    if (days) {
        objectLockConfig.Rule.DefaultRetention.Days = days;
    }
    if (years) {
        objectLockConfig.Rule.DefaultRetention.Years = years;
    }
    return {
        Bucket: bucket,
        ObjectLockConfiguration: objectLockConfig,
    };
}

describe('aws-sdk test put object lock configuration', () => {
    let s3;
    let otherAccountS3;

    before(done => {
        const config = getConfig('default', { signatureVersion: 'v4' });
        s3 = new S3(config);
        otherAccountS3 = new BucketUtility('lisa', {}).s3;
        return done();
    });

    it('should return NoSuchBucket error if bucket does not exist', done => {
        const params = getObjectLockParams('Enabled', 'GOVERNANCE', 1);
        s3.putObjectLockConfiguration(params, err => {
            checkError(err, 'NoSuchBucket', 404);
            done();
        });
    });

    describe('on object lock disabled bucket', () => {
        beforeEach(done => s3.createBucket({
            Bucket: bucket,
        }, done));

        afterEach(done => s3.deleteBucket({ Bucket: bucket }, done));

        it('should return InvalidBucketState error', done => {
            const params = getObjectLockParams('Enabled', 'GOVERNANCE', 1);
            s3.putObjectLockConfiguration(params, err => {
                checkError(err, 'InvalidBucketState', 409);
                done();<commit_msg>bf: S3C-3130 put config without rule throws exception<commit_after>const assert = require('assert');
const { S3 } = require('aws-sdk');

const checkError = require('../../lib/utility/checkError');
const getConfig = require('../support/config');
const BucketUtility = require('../../lib/utility/bucket-util');

const bucket = 'mock-bucket';

function getObjectLockParams(status, mode, days, years) {
    const objectLockConfig = {
        ObjectLockEnabled: status,
        Rule: {
            DefaultRetention: {
                Mode: mode,
            },
        },
    };
    if (days) {
        objectLockConfig.Rule.DefaultRetention.Days = days;
    }
    if (years) {
        objectLockConfig.Rule.DefaultRetention.Years = years;
    }
    return {
        Bucket: bucket,
        ObjectLockConfiguration: objectLockConfig,
    };
}

describe('aws-sdk test put object lock configuration', () => {
    let s3;
    let otherAccountS3;

    before(done => {
        const config = getConfig('default', { signatureVersion: 'v4' });
        s3 = new S3(config);
        otherAccountS3 = new BucketUtility('lisa', {}).s3;
        return done();
    });

    it('should return NoSuchBucket error if bucket does not exist', done => {
        const params = getObjectLockParams('Enabled', 'GOVERNANCE', 1);
        s3.putObjectLockConfiguration(params, err => {
            checkError(err, 'NoSuchBucket', 404);
            done();
        });
    });

    describe('on object lock disabled bucket', () => {
        beforeEach(done => s3.createBucket({
            Bucket: bucket,
        }, done));

        afterEach(done => s3.deleteBucket({ Bucket: bucket }, done));

        it('should return InvalidBucketState error', done => {
            const params = getObjectLockParams('Enabled', 'GOVERNANCE', 1);
            s3.putObjectLockConfiguration(params, err => {
                checkError(err, 'InvalidBucketState', 409);
                done();
            });
        });

        it('should return InvalidBucketState error without Rule', done => {
            const params = {
                Bucket: bucket,
                ObjectLockConfiguration: {
                    ObjectLockEnabled: 'Enabled',
                },
            };
            s3.putObjectLockConfiguration(params, err => {
                checkError(err, 'InvalidBucketState', 409);
                done();
{'fraction_non_alphanumeric': 0.13344201210992082, 'fraction_numerical': 0.011411271541686073, 'mean_word_length': 2.263677811550152, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '7249433', 'n_tokens_mistral': 1198, 'n_tokens_neox': 1150, 'n_words': 268}