Skip to content

Commit

Permalink
test: getBucketTags/getObjectTagging/calculatePostSignature (#739)
Browse files Browse the repository at this point in the history
* test: getBucketTags/getObjectTagging/calculatePostSignature

* fix: trafficLimit error
  • Loading branch information
weiyie authored Mar 25, 2020
1 parent 2d2b33a commit 92b0880
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
12 changes: 10 additions & 2 deletions lib/common/object/calculatePostSignature.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

const policy2Str = require('../utils/policy2Str');
const signHelper = require('../signUtils');
const isObject = require('../utils/isObject');

const proto = exports;

Expand All @@ -12,8 +13,15 @@ const proto = exports;
* {String} params.policy JSON text encoded with UTF-8 and Base64.
*/
proto.calculatePostSignature = function calculatePostSignature(policy) {
if (!policy) {
throw new Error('policy must be JSON or Object');
if (!isObject(policy) && typeof policy !== 'string') {
throw new Error('policy must be JSON string or Object');
}
if (!isObject(policy)) {
try {
JSON.stringify(JSON.parse(policy));
} catch (error) {
throw new Error('policy must be JSON string or Object');
}
}
policy = Buffer.from(policy2Str(policy), 'utf8').toString('base64');

Expand Down
2 changes: 1 addition & 1 deletion lib/common/signUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ exports._signatureForURL = function _signatureForURL(accessKeySecret, options, r
headers[key] = value;
} else if (lowerKey.indexOf('content-type') === 0) {
headers[key] = value;
} else if (lowerKey !== 'expires' && lowerKey !== 'response' && lowerKey !== 'process' && lowerKey !== 'method' && lowerKey !== 'trafficLimit') {
} else if (lowerKey !== 'expires' && lowerKey !== 'response' && lowerKey !== 'process' && lowerKey !== 'method' && lowerKey !== 'trafficlimit') {
subResource[lowerKey] = value;
}
});
Expand Down
13 changes: 13 additions & 0 deletions test/node/bucket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,18 @@ describe('test/bucket.test.js', () => {
} catch (error) {
assert(false, error);
}

try {
const tag = { a: '1' };
result = await store.putBucketTags(bucket, tag);
assert.strictEqual(result.status, 200);

result = await store.getBucketTags(bucket);
assert.strictEqual(result.status, 200);
assert.deepEqual(result.tag, tag);
} catch (error) {
assert(false, error);
}
});

it('maximum of 20 tags for a bucket', async () => {
Expand Down Expand Up @@ -641,6 +653,7 @@ describe('test/bucket.test.js', () => {
}
});
});

describe('putBucketEncryption(), getBucketEncryption(), deleteBucketEncryption()', () => {
it('should create, get and delete the bucket encryption', async () => {
// put with AES256
Expand Down
30 changes: 30 additions & 0 deletions test/node/object.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1916,6 +1916,24 @@ describe('test/object.test.js', () => {
const headRes = await store.head(name);
assert.equal(headRes.status, 200);
});

it('should throw error when policy is not JSON or Object', async () => {
let policy = 'string'
const errorMessage = 'policy must be JSON string or Object'
try {
store.calculatePostSignature(policy)
assert(false)
} catch (error) {
assert.strictEqual(errorMessage, error.message)
}
try {
policy = 123
store.calculatePostSignature(policy)
assert(false)
} catch (error) {
assert.strictEqual(errorMessage, error.message)
}
});
});

describe('getObjectTagging() putObjectTagging() deleteObjectTagging()', () => {
Expand Down Expand Up @@ -1948,6 +1966,18 @@ describe('test/object.test.js', () => {
} catch (error) {
assert(false, error);
}

try {
const tag = { a: '1' };
result = await store.putObjectTagging(name, tag);
assert.strictEqual(result.status, 200);

result = await store.getObjectTagging(name);
assert.strictEqual(result.status, 200);
assert.deepEqual(result.tag, tag);
} catch (error) {
assert(false, error);
}
});

it('maximum of 10 tags for a object', async () => {
Expand Down

0 comments on commit 92b0880

Please sign in to comment.