|
| 1 | +const assert = require('assert'); |
| 2 | + |
| 3 | +const { errors } = require('arsenal'); |
| 4 | +const { bucketPut } = require('../../../lib/api/bucketPut'); |
| 5 | +const bucketPutVersioning = require('../../../lib/api/bucketPutVersioning'); |
| 6 | + |
| 7 | +const { cleanup, |
| 8 | + DummyRequestLogger, |
| 9 | + makeAuthInfo } = require('../helpers'); |
| 10 | +const metadata = require('../../../lib/metadata/wrapper'); |
| 11 | + |
| 12 | +const xmlEnableVersioning = |
| 13 | +'<VersioningConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">' + |
| 14 | +'<Status>Enabled</Status>' + |
| 15 | +'</VersioningConfiguration>'; |
| 16 | + |
| 17 | +const xmlSuspendVersioning = |
| 18 | +'<VersioningConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">' + |
| 19 | +'<Status>Suspended</Status>' + |
| 20 | +'</VersioningConfiguration>'; |
| 21 | + |
| 22 | +const locConstraintVersioned = |
| 23 | +'<CreateBucketConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">' + |
| 24 | +'<LocationConstraint>withversioning</LocationConstraint>' + |
| 25 | +'</CreateBucketConfiguration>'; |
| 26 | + |
| 27 | +const locConstraintNonVersioned = |
| 28 | +'<CreateBucketConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">' + |
| 29 | +'<LocationConstraint>withoutversioning</LocationConstraint>' + |
| 30 | +'</CreateBucketConfiguration>'; |
| 31 | + |
| 32 | +const externalVersioningErrorMessage = 'We do not currently support putting ' + |
| 33 | +'a versioned object to a location-constraint of type Azure or GCP.'; |
| 34 | + |
| 35 | +const log = new DummyRequestLogger(); |
| 36 | +const bucketName = 'bucketname'; |
| 37 | +const authInfo = makeAuthInfo('accessKey1'); |
| 38 | + |
| 39 | +function _getPutBucketRequest(xml) { |
| 40 | + const request = { |
| 41 | + bucketName, |
| 42 | + headers: { host: `${bucketName}.s3.amazonaws.com` }, |
| 43 | + url: '/', |
| 44 | + }; |
| 45 | + request.post = xml; |
| 46 | + return request; |
| 47 | +} |
| 48 | + |
| 49 | +function _putVersioningRequest(xml) { |
| 50 | + const request = { |
| 51 | + bucketName, |
| 52 | + headers: { host: `${bucketName}.s3.amazonaws.com` }, |
| 53 | + url: '/?versioning', |
| 54 | + query: { versioning: '' }, |
| 55 | + }; |
| 56 | + request.post = xml; |
| 57 | + return request; |
| 58 | +} |
| 59 | + |
| 60 | +describe('bucketPutVersioning API', () => { |
| 61 | + before(() => cleanup()); |
| 62 | + afterEach(() => cleanup()); |
| 63 | + |
| 64 | + describe('with version enabled location constraint', () => { |
| 65 | + beforeEach(done => { |
| 66 | + const request = _getPutBucketRequest(locConstraintVersioned); |
| 67 | + bucketPut(authInfo, request, log, done); |
| 68 | + }); |
| 69 | + |
| 70 | + const tests = [ |
| 71 | + { |
| 72 | + msg: 'should successfully enable versioning on location ' + |
| 73 | + 'constraint with supportsVersioning set to true', |
| 74 | + input: xmlEnableVersioning, |
| 75 | + output: { Status: 'Enabled' }, |
| 76 | + }, |
| 77 | + { |
| 78 | + msg: 'should successfully suspend versioning on location ' + |
| 79 | + 'constraint with supportsVersioning set to true', |
| 80 | + input: xmlSuspendVersioning, |
| 81 | + output: { Status: 'Suspended' }, |
| 82 | + }, |
| 83 | + ]; |
| 84 | + tests.forEach(test => it(test.msg, done => { |
| 85 | + const request = _putVersioningRequest(test.input); |
| 86 | + bucketPutVersioning(authInfo, request, log, err => { |
| 87 | + assert.ifError(err, |
| 88 | + `Expected success, but got err: ${err}`); |
| 89 | + metadata.getBucket(bucketName, log, (err, bucket) => { |
| 90 | + assert.ifError(err, |
| 91 | + `Expected success, but got err: ${err}`); |
| 92 | + assert.deepStrictEqual(bucket._versioningConfiguration, |
| 93 | + test.output); |
| 94 | + done(); |
| 95 | + }); |
| 96 | + }); |
| 97 | + })); |
| 98 | + }); |
| 99 | + |
| 100 | + describe('with version disabled location constraint', () => { |
| 101 | + beforeEach(done => { |
| 102 | + const request = _getPutBucketRequest(locConstraintNonVersioned); |
| 103 | + bucketPut(authInfo, request, log, done); |
| 104 | + }); |
| 105 | + |
| 106 | + const tests = [ |
| 107 | + { |
| 108 | + msg: 'should return error if enabling versioning on location ' + |
| 109 | + 'constraint with supportsVersioning set to false', |
| 110 | + input: xmlEnableVersioning, |
| 111 | + output: { error: errors.NotImplemented.customizeDescription( |
| 112 | + externalVersioningErrorMessage) }, |
| 113 | + }, |
| 114 | + { |
| 115 | + msg: 'should return error if suspending versioning on ' + |
| 116 | + ' location constraint with supportsVersioning set to false', |
| 117 | + input: xmlSuspendVersioning, |
| 118 | + output: { error: errors.NotImplemented.customizeDescription( |
| 119 | + externalVersioningErrorMessage) }, |
| 120 | + }, |
| 121 | + ]; |
| 122 | + tests.forEach(test => it(test.msg, done => { |
| 123 | + const putBucketVersioningRequest = |
| 124 | + _putVersioningRequest(test.input); |
| 125 | + bucketPutVersioning(authInfo, putBucketVersioningRequest, log, |
| 126 | + err => { |
| 127 | + assert.deepStrictEqual(err, test.output.error); |
| 128 | + done(); |
| 129 | + }); |
| 130 | + })); |
| 131 | + }); |
| 132 | +}); |
0 commit comments