UNPKG

ks3

Version:

本代码库为`金山云存储KS3`服务.主要提供`KS3 nodejs SDK`和`命令行工具`.

474 lines (431 loc) 12.3 kB
var KS3 = require('..'); var should = require('should'); require('should-http'); const { ak, sk, targetBucket} = require('./config') describe('API bucket', function() { var bucketName = 'test-sdk-' + (+new Date()); var client = new KS3(ak, sk, bucketName); var acl = 'public-read'; before(function (done) { client.bucket.put({ Bucket: bucketName, ACL: acl }, function(err, data, res) { console.log('bucket created:', bucketName, err, data, res.statusCode) should.not.exist(err); res.should.have.status(200); done() }); }) describe('create bucket', function() { it('require Bucket not pass verify', function() { (function() { client.bucket.put({Bucket: 'test_tes'}, function() {}); }).should.throw('the illegal bucketName'); }); it('require ACL not pass verify', function() { (function() { client.bucket.put({Bucket: bucketName, ACL: 'testacl'}, function() {}); }).should.throw('the illegal acl'); }); }); describe('other handle for bucket', function() { describe('bucket ACL', function(done) { it('Put ACL && get ACL', function(done) { client.bucket.putACL({ACL: acl}, function(err, data, res) { should.not.exist(err); res.should.have.status(200); client.bucket.getACL(function(err, data, res) { should.not.exist(err); res.should.have.status(200); done(); }); }); }); it('Put ACL without params', function() { (function() { client.bucket.putACL(function(err, data, res) {}); }).should.throw('require the acl') }); it('Put ACL params verify fail', function() { (function() { client.bucket.putACL({ACL: 'testacl'}, function(err, data, res) {}); }).should.throw('the illegal acl') }); }); describe('bucket location', function() { it('get location should get a 2XX statuscode', function(done) { client.bucket.getLocation(function(err, data, res) { should.not.exist(err); res.should.have.status(200); done(); }); }); }); // NOTICE: need main account ak/sk describe('bucket logging', function() { it('put logging should get a 2XX statusCode', function (done) { client.bucket.putLogging({ Logging: { targetBucket } }, function (err, data, res) { should.not.exist(err) res.should.have.status(200) done() }) }) it('get logging should get a 2XX statuscode', function(done) { client.bucket.getLogging(function(err, data, res) { should.not.exist(err); res.should.have.status(200); done(); }); }); it('close logging should get a 2XX statuscode', function (done) { client.bucket.putLogging(function (err, data, res) { should.not.exist(err); res.should.have.status(200); done(); }) }) }); describe('list objects', function() { it('get objects without params', function(done) { this.timeout(300000); client.bucket.get(function(err, data, res) { should.not.exist(err); res.should.have.status(200); done(); }); }); it('get objects with params', function(done) { client.bucket.get({ MaxKeys: 30, Marker: '', Prefix: '' }, function(err, data, res) { should.not.exist(err); res.should.have.status(200); done(); }); }); /** * 禁止传递空值 delimiter, * sdk会做处理 */ it('get objects with empty delimiter', function(done) { client.bucket.get({ MaxKeys: 30, Delimiter:'', Marker: '', Prefix: '' }, function(err, data, res) { should.not.exist(err); res.should.have.status(200); done(); }); }); }); describe('head bucket', function() { it('should get a 2XX statuscode for head bucket', function(done) { client.bucket.head({Bucket: bucketName}, function(err, data, res) { should.not.exist(err); res.should.have.status(200); done(); }); }); }); describe('bucket name is null', function() { var client = new KS3(ak, sk); it('return a error', function() { (function(){ client.bucket.getACL(function(err, data, res){}) }).should.throw('require the bucketName'); }) }); describe('bucket policy', function () { it ('set bucket policy require policy params', function () { (function () { client.bucket.putBucketPolicy(function(err, data, res){}) }).should.throw('require the Policy') }) it ('set bucket error policy', function (done) { client.bucket.putBucketPolicy({ Policy: { "Version":"2008-10-17", "Statement":[ { "Sid":"1", "Effect":"Allow", "Principal":{ "KSC":[ "krn:ksc:iam::accountID:root" ] }, "Action":[ "ks3:*" ], "Resource":[ "krn:ksc:ks3:::ks3-example", "krn:ksc:ks3:::ks3-example/*" ], "Condition":{ "IpAddress":{ "ksc:SourceIp":"54.240.143.1" } } } ] } }, function (err, data, res) { (err).should.have.property('statusCode', 400) done() }) }) it ('get bucket error policy', function (done) { client.bucket.getBucketPolicy(function (err, data, res) { (err).should.have.property('statusCode', 404) done() }) }) it ('set bucket correct policy', function (done) { const policy = { "Statement": [ { "Resource": [ "krn:ksc:ks3:::" + bucketName, "krn:ksc:ks3:::"+ bucketName +"/*" ], "Principal": { "KSC": [ "*" ] }, "Action": [ "ks3:*" ], "Effect": "Allow" } ] } client.bucket.putBucketPolicy({ Policy: policy }, function (err, data, res) { res.should.have.status(204) done() }) }) it ('get bucket policy', function (done) { client.bucket.getBucketPolicy(function (err, data, res) { should.not.exist(err) res.should.have.status(200) done() }) }) it ('delete bucket policy', function (done) { client.bucket.deleteBucketPolicy(function (err, data, res) { res.should.have.status(204) done() }) }) }) describe('bucket lifecycle', function () { it ('set bucket lifecycle require Rules', function() { (function () { client.bucket.putBucketLifecycle({}, function(err,data, res){}) }).should.throw() }) it('set bucket lifecycle', function (done) { client.bucket.putBucketLifecycle({ Rules: [{ "id":"id1", "filter":{ "prefix":"documents测试" }, "expiration":{ "date":"2016-12-31T00:00:00+08:00" }, "status":"Enabled" }] }, function (err, data, res) { should.not.exist(err) res.should.have.status(200) done() }) }) it('get bucket lifecycle', function (done) { client.bucket.getBucketLifecycle(function (err, data, res) { should.equal(err, null) res.should.have.status(200) done() }) }) it('delete bucket lifecycle', function (done) { client.bucket.deleteBucketLifecycle(function(err, data, res){ should.equal(err, null) res.should.have.status(204) done() }) }) }) describe('bucket CORS', function () { it ('set bucket cors require Rules', function () { (function () { client.bucket.putBucketCors({}, function (err, data, res){}) }).should.throw() }) it ('check bucket cors legal', function () { (function () { client.bucket.putBucketCors({ allowedMethod: '*'}, function (){}) }).should.throw() }) it('set bucket cors', function (done) { client.bucket.putBucketCors({ Rules: [{ allowedMethod: ['POST', 'GET'], allowedOrigin: ['http://ks3.console.ksyun.com', 'http://ks3.ksyun.com'], allowedHeader: ['x-kss-test1:nodesdk-test1', 'x-kss-test2:nodesdk-test2'], maxAgeSeconds: 3000, exposeHeader: 'x-kss-test1:nodesdk-test1, x-kss-test2:nodesdk-test2' }] }, function(err, data, res) { should.equal(err, null) res.should.have.status(200) done() }) }) it('get bucket cors', function (done) { client.bucket.getBucketCors(function (err, data, res) { res.should.have.status(200) done() }) }) it('delete bucket cors', function (done) { client.bucket.deleteBucketCors(function (err, data, res) { res.should.have.status(204) done() }) }) }) describe('bucket mirror', function () { this.timeout(10000) it('set bucket mirror require Mirror', function () { (function(){ client.bucket.putBucketMirror(function () {})}).should.throw() }) it('set bucket mirror', function (done) { client.bucket.putBucketMirror({ Mirror: { "version":"V3",// 回源类型,目前只支持V3 "use_default_robots":false, // 是否使用默认的robots.txt "sync_mirror_rules":[ // 一组同步回源规则,最多可配置20个。该字段与async_mirror_rule必须至少有一个,可同时存在。 { "mirror_url":"http://v-ks-a-i.originalvod.com",// 源站url,必须以http或者https开头 "saving_setting":{ // 从源站回源得到的文件在上传ks3时的配置。 "acl":"private" } } ] } }, function (err, data, res) { should.not.exist(err) res.should.have.status(200) done() }) }) it('get bucket mirror', function (done) { client.bucket.getBucketMirror(function(err,data, res){ should.not.exist(err) res.should.have.status(200) done() }) }) it('delete bucket mirror', function (done) { client.bucket.deleteBucketMirror(function (err, data, res){ should.not.exist(err) res.should.have.status(204) done() }) }) }) // getACLType describe('bucket acl', function () { it('bucket acl', function (done) { client.config({ dataType: 'json' }); client.bucket.getACLType(function(err, data, res) { should.not.exist(err) should.equal(data, acl) res.should.have.status(200) done() }) }) }) // listMultipartUploads describe('bucket listMultipartUploads', function () { it ('bucket listMultipartUploads', function (done) { client.bucket.listMultipartUploads(function(err, data, res){ should.not.exist(err) res.should.have.status(200) done() }) }) }) describe('bucket replication configuration', function () { it('set bucket replication require rule', function () { (function(){ client.bucket.putBucketReplicationConfiguration(function(){}) }).should.throw() }) it('set bucket replication config', function (done) { this.timeout(0) setTimeout(function () { console.log('set replication:', targetBucket) client.bucket.putBucketReplicationConfiguration({ Rule: { prefix: ['abc', 'xyz'], deleteMarkerStatus: 'Disabled', targetBucket, historicalObjectReplication: 'Enabled', } }, function (err, data, res) { should.equal(err, null) res.should.have.status(200) done() }) }, 5000) }) it('get bucket replication config', function (done) { client.bucket.getBucketReplicationConfiguration(function (err, data, res) { should.equal(err, null) res.should.have.status(200) done() }) }) it('delete bucket replication config', function (done) { client.bucket.deleteBucketReplicationConfiguration(function(err, data, res) { should.not.exist(err) res.should.have.status(200) done() }) }) }) }); // describe('delete bucket', function () { // it ('delete bucket', function (done) { // client.bucket.del(function(err, data, res) { // should.not.exist(err); // res.should.have.status(204); // 删除bucket,成功的状态码为204 // done(); // }) // }) // }) after(function (done) { client.bucket.del(function(err, data, res) { should.not.exist(err); res.should.have.status(204); // 删除bucket,成功的状态码为204 done(); }) }) });