UNPKG

racker

Version:

Robust Rackspace cloudfiles client

296 lines (259 loc) 7.83 kB
// lib var racker = require('..') , Racker = racker.Racker , Request = racker.Request , Auth = racker.Auth , Upload = racker.Upload , fs = require('fs') , ms = require('ms') , ups = [] , container , auth , cdn; // auth.json must exist. try { auth = require('./auth.json'); container = auth.container; cdn = auth['cdn container']; racker.set(auth); } catch (e) { console.error('To run the test suite you must ' + 'create `' + __dirname + '/auth.json` with ' + 'your details'); process.exit(1); } // auth describe('Auth', function (){ describe('new Auth', function (){ it('should default expire to `23.5h`', function (){ new Auth().expire.should.eql(ms('23.5h')); }) }) describe('.expired()', function (){ it('should be true', function (){ new Auth().expired().should.eql(true); }) it('should emit `expired` if expired', function (done){ new Auth().on('expired', done).expired(); }) }) describe('.send()', function (){ it('should emit success if authenticated successfully', function (done){ var a = new Auth(auth); a.on('success', function (data){ data.token.should.be.a('string'); data.store.should.be.a('string'); data.cdn.should.be.a('string'); a.expired().should.eql(false); a.expires.should.be.above(Date.now()); done(); }).send(); }) it('should emit error with unauthorized message if creds arent ok', function (done){ var a = new Auth({ user: 'foo', key: 'bar' }); a.on('error', function (err){ err.should.be.instanceOf(Error); done(); }).send(); }) }) }) describe('Upload', function(){ describe('new Upload', function (){ it('should set the src if its a string', function(){ var up = new Upload(racker, '/bar/foo.png'); up._as.should.eql('foo.png'); }) }) describe('.set()', function (){ it('should set header to val', function (){ var up = new Upload(racker); up.set('Content-Type', 'foo'); up.headers['Content-Type'].should.eql('foo'); }) it('should merge an object', function (){ var up = new Upload(racker); up.set({ foo: 'bar' }).headers.foo.should.eql('bar'); }) }) describe('.as()', function () { it('should set `._as`', function (){ new Upload(racker).as('foo.png')._as.should.eql('foo.png'); }) }) describe('.to()', function (){ it('should set `._to`', function (){ new Upload(racker).to('foo.png')._to.should.eql('foo.png'); }) }) }) describe('racker', function () { describe('.authenticated', function(){ it('should be false', function (){ racker.authenticated.should.eql(false); }) }) describe('.authenticate()', function () { it('should emit success on success', function (done){ var client = new Racker(auth); client.on('success', function (){ client.get('token').should.be.a('string'); client.get('cdn').should.match(/^http/); client.get('store').should.match(/^http/); client.target.should.eql(client.get('store')); client.enable('service net'); client.authenticated.should.eql(true); done(); }).authenticate(); }) it('should emit `error` on error', function (done){ var client = new Racker({ user: '', key: '' }); client.on('error', function (err){ err.should.be.instanceOf(Error); client.authenticated.should.eql(false); done(); }).authenticate(); }) }) describe('.meta()', function (){ it('should get account metadata', function (done){ racker.meta(function (err, res){ if (err) return done(err); var bytes = parseInt(res.headers['x-account-bytes-used']); bytes.should.be.a('number'); done(); }) }) }) describe('.create()', function (){ it('should create a container', function (done){ racker.create(container) .end(function (err, res) { if (err) return done(err); res.status.should.eql(201); done(); }) }) }) describe('.update()', function (){ it('should update a container', function (done){ racker.update(container) .set('X-Container-Meta-Type', 'test') .end(function (err, res){ if (err) return done(err); res.ok.should.be.ok; done(); }); }) }) describe('.meta(container)', function(){ it('should get container metas', function (done){ racker.meta(container) .end(function (err, res){ if (err) return done(err); res.headers['x-container-meta-type'] .should.eql('test'); done(); }); }) }) describe('.upload(Buffer)', function (){ it('should upload the buffer', function (done) { ups.push('buffer.json'); racker.upload(new Buffer('{ "buffer": "test" }')) .set('Content-Type', 'application/json') .to(container) .as('buffer.json') .end(function (err, res){ if (err) return done(err); res.status.should.eql(201); done(); }); }) }) describe('.upload(stream)', function (){ it('should upload a stream', function (done){ var f = fs.createReadStream(__dirname + '/uploads/upload.json'); ups.push('stream.json'); racker.upload(f) .set('Content-Type', 'application/json') .set('X-Object-Meta-Type', 'json') .to(container) .as('stream.json') .end(function (err, res){ if (err) return done(err); res.status.should.eql(201) done(); }); }) }) describe('.upload(filename)', function (){ it('should upload a filename', function (done){ ups.push('file.json'); var fn = __dirname + '/uploads/upload.json'; racker.upload(fn) .to(container) .as('file.json') .end(function (err, res){ if (err) return done(err); res.status.should.eql(201); done(); }); }) }) describe('.list(fn)', function (){ it('should list all containers if a function is given', function (done){ racker.list(function (err, res) { if (err) return done(err); res.body.should.be.instanceOf(Array); done(); }) }) }) describe('.list(name)', function (){ it('should list all objects in the given container', function(done){ racker.list(container) .end(function (err, res) { if (err) return done(err); res.body.should.be.instanceOf(Array); res.body.should.be.lengthOf(3); done(); }); }) }) describe('.meta(container, obj)', function (){ it('should get object metadata', function (done){ racker.meta(container, 'stream.json') .end(function (err, res){ if (err) return done(err); res.headers.should.have.property('x-object-meta-type', 'json'); done(); }); }) }) describe('.del(container, obj)', function (){ it('should delete the object', function (done){ var len = ups.length, dels = []; (function next(){ racker.del(container, ups.pop()) .end(function(err, res){ if (err) return done(err); dels.push(res.status || 500); if (--len) return next(); dels.should.eql([204, 204, 204]); done(); }); })(); }) }) describe('.del(container)', function(){ it('should delete the container', function(done){ racker.del(container) .end(function (err, res){ if (err) return done(err); res.status.should.eql(204); done(); }); }) }) })