const assert = require('assert');

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

const date = Date.now();
const bucket = `abortmpu${date}`;
const key = 'key';
const bodyFirstPart = Buffer.allocUnsafe(10).fill(0);

function checkError(err, code, message) {
    assert.notEqual(err, null, 'Expected failure but got success');
    assert.strictEqual(err.code, code);
    assert.strictEqual(err.message, message);
}

describe('Abort MPU', () => {
    withV4(sigCfg => {
        let bucketUtil;
        let s3;
        let uploadId;

        beforeEach(() => {
            bucketUtil = new BucketUtility('default', sigCfg);
            s3 = bucketUtil.s3;
            return s3.createBucketAsync({ Bucket: bucket })
            .then(() => s3.createMultipartUploadAsync({
                Bucket: bucket, Key: key }))
            .then(res => {
                uploadId = res.UploadId;
                return s3.uploadPartAsync({ Bucket: bucket, Key: key,
                    PartNumber: 1, UploadId: uploadId, Body: bodyFirstPart });
            })
            .catch(err => {
                process.stdout.write(`Error in beforeEach: ${err}\n`);
                throw err;
            });
        });

        afterEach(() =>
            s3.abortMultipartUploadAsync({
                Bucket: bucket,
                Key: key,
                UploadId: uploadId,
            })
            .then(() => bucketUtil.empty(bucket))
            .then(() => bucketUtil.deleteOne(bucket))
        );

        it('should return InvalidRequest error if aborting without key',
        done => {
            s3.abortMultipartUpload({
                Bucket: bucket,
                Key: '',
                UploadId: uploadId },
            err => {
                checkError(err, 'InvalidRequest', 'A key must be specified');
                done();
            });
        });
    });
});

@@ -23,24 +23,56 @@
 ('assert');%0A
+const uuid = require('uuid/v4');
 %0Aconst withV
@@ -1963,12 +1963,849 @@
     %7D);%0A%7D);%0A
+%0Adescribe('Abort MPU - No Such Upload', () =%3E %7B%0A    withV4(sigCfg =%3E %7B%0A        let bucketUtil;%0A        let s3;%0A%0A        beforeEach(() =%3E %7B%0A            bucketUtil = new BucketUtility('default', sigCfg);%0A            s3 = bucketUtil.s3;%0A            return s3.createBucketAsync(%7B Bucket: bucket %7D);%0A        %7D);%0A%0A        afterEach(() =%3E bucketUtil.deleteOne(bucket));%0A%0A        it('should return NoSuchUpload error when aborting non-existent mpu',%0A        done =%3E %7B%0A            s3.abortMultipartUpload(%7B%0A                Bucket: bucket,%0A                Key: key,%0A                UploadId: uuid().replace(/-/g, '') %7D,%0A            err =%3E %7B%0A                assert.notEqual(err, null, 'Expected failure but got success');%0A                assert.strictEqual(err.code, 'NoSuchUpload');%0A                done();%0A            %7D);%0A        %7D);%0A    %7D);%0A%7D);%0A

add test for aborting non-existent mpu