UNPKG

imf-data-nodejs-sdk

Version:
424 lines (367 loc) 11.3 kB
/** * Copyright 2015 IBM Corp. All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ var events = require('events'); var assert = require('assert'); var IMFData = require('../lib/imfdata.js'); var config = require('./utils/config.js'); var tools = require('./utils/assert-tools.js'); var imf = require('imf-oauth-user-sdk'); describe('account.db', function() { var data; var dbName = 'az09_$()+-/'; /** * Gets data module */ before(function(done) { this.timeout(10000); IMFData.initialize(config, function(err, imfdata) { if (err) { done(err); } else { data = imfdata; done(); } }); }); describe('.create(name, [callback])', function() { it('should successfully create a az09_$()+-/ database', function(done) { this.timeout(10000); data.db.create(dbName, function(err, body, headers) { if (err) { done(err); } else { assert.notEqual(body, null); assert.notEqual(headers, null); done(); } }); }); it('should return error in callback for null db name', function(done) { this.timeout(10000); data.db.create(null, function(err, response, headers) { assert.notEqual(err, null); done(); }); }); it('should return error in callback for invalid db name', function(done) { this.timeout(10000); data.db.create('THISISABADDBNAME', function(err, response, headers) { assert.notEqual(err, null); done(); }); }); }); describe('.get(name, [callback])', function() { it('should successfully get az09_$()+-/ database after setting permissions for appId', function(done) { this.timeout(10000); data.set_permissions({ identity: config.appId, database: dbName, access: 'admins' }, function(err, body, headers) { if (err) { done(err); } else { data.db.get(dbName, function(err, body, headers) { if (err) { done(err); } else { assert.notEqual(body, null); assert.notEqual(headers, null); done(); } }); } }); }); it('should return error in callback for nonexistant database', function(done) { this.timeout(10000); data.db.get('myfakedb', function(err, response, headers) { tools.assertExists(err); done(); }); }); }); describe('.destroy(name, [callback])', function() { this.timeout(10000); it('should successfully delete a az09_$()+- database', function(done) { data.db.destroy(dbName, function(err, body, headers) { if (err) { done(err); } else { assert.notEqual(body, null); assert.notEqual(headers, null); done(); } }); }); it('should return error in callback for null db name', function(done) { data.db.destroy(null, function(err, response, headers) { tools.assertExists(err); done(); }); }); it('should return error in callback for invalid db name', function(done) { data.db.destroy('THISISFAKE!!!', function(err, response, headers) { tools.assertExists(err); done(); }); }); }); describe('.list([callback])', function() { before(function(done) { this.timeout(10000); data.db.create('testlistdbs', function(err, body, headers) { if (err) { done(err); } else { done(); } }); }); after(function(done) { this.timeout(10000); data.db.destroy('testlistdbs', function(err, body, headers) { if (err) { done(err); } else { done(); } }); }); it('should forbid application from listing all databases', function(done) { this.timeout(10000); data.db.list(function(err, response, headers) { assert.notEqual(err, null); assert.equal(err.status_code, 403); done(); }); }); }); describe('.use(name)', function() { it('should return a database object with all db_module functions', function(done) { var db_module = data.db.use('test'); tools.assertDbModuleFunctionsExist(db_module); done(); }); }); describe('.scope(name)', function() { it('should return a database object with all db_module functions', function(done) { var db_module = data.db.scope('test'); tools.assertDbModuleFunctionsExist(db_module); done(); }); }); describe('.compact(db_name, [ddoc_name], [callback])', function() { before(function(done) { this.timeout(10000); data.db.create('testcompactiondb', function(err, body, headers) { if (err) { done(err); } else { done(); } }); }); after(function(done) { this.timeout(10000); data.db.destroy('testcompactiondb', function(err, body, headers) { if (err) { done(err); } else { done(); } }); }); it('should produce error on database compaction', function(done) { this.timeout(10000); data.db.compact('testcompactiondb', function(err, response, headers) { tools.assertExists(err); done(); }); }); }); describe('.replicate(source, target, [opts], [callback])', function() { after(function(done) { this.timeout(10000); data.db.destroy('testreplicatedb', function(err, response, headers) { if (err) done(err); else done(); }); }); it('should successfully replicate example database', function(done) { this.timeout(40000); data.db.replicate('http://examples.cloudant.com/animaldb', 'testreplicatedb', { create_target: true }, function(err, body, headers) { assert.ifError(err); tools.assertExists(body); tools.assertExists(headers); done(); }); }); }); describe('.changes(name, [params], [callback])', function() { before(function(done) { this.timeout(10000); data.db.create('testchangesdb', function(err, body, headers) { if (err) { done(err); } else { data.set_permissions({ identity: config.appId, database: 'testchangesdb', access: 'admins' }, function(err, body, headers) { if (err) done(err); else done(); }); } }); }); after(function(done) { this.timeout(10000); data.db.destroy('testchangesdb', function(err, body, headers) { if (err) { done(err); } else { done(); } }); }); it('should successfully pull database changes', function(done) { this.timeout(10000); data.db.changes('testchangesdb', function(err, body, headers) { assert.ifError(err); tools.assertExists(body); tools.assertExists(headers); done(); }); }); }); describe('.follow(name, [params], [callback])', function() { before(function(done) { this.timeout(20000); data.db.create('testfollowdb', function(err, body, headers) { if (err) { done(err); } else { data.set_permissions({ identity: config.appId, database: 'testfollowdb', access: 'admins' }, function(err, body, headers) { if (err) done(err); else done(); }); } }); }); after(function(done) { this.timeout(20000); data.db.destroy('testfollowdb', function(err, body, headers) { if (err) { done(err); } else { done(); } }); }); it('should start and stop a database changes feed', function(done) { this.timeout(15000); var feed = data.db.follow('testfollowdb'); assert(feed instanceof events.EventEmitter); feed.on('start', function() { feed.stop(); }); feed.on('stop', function() { setTimeout(done, 1000); }); feed.on('error', function(err) { done(err); }); feed.follow(); }); it('should start database changes feed and listen for a change', function(done) { this.timeout(15000); var feed = data.db.follow('testfollowdb'); assert(feed instanceof events.EventEmitter); feed.on('start', function() { }); feed.on('change', function(change) { assert.ok(change); feed.stop(); }); feed.on('stop', function() { setTimeout(done, 1000); }); feed.on('error', function(err) { done(err); }); feed.follow(); process.nextTick(function() { var db = data.db.use('testfollowdb'); db.insert({ foo: 'bar' }); }); }); it('should create database changes feed with params and a callback and listen for a change', function(done) { this.timeout(20000); var feed = data.db.follow('testfollowdb', { since: 'now' }, function(err, change) { assert.ifError(err); assert.ok(change); feed.stop(); }); assert(feed instanceof events.EventEmitter); feed.on('stop', function() { setTimeout(done, 1000); }); process.nextTick(function() { var db = data.db.use('testfollowdb'); db.insert({ foo: 'bar' }); }); }); it('should create database changes feed with a callback and listen for a change', function(done) { this.timeout(20000); var feed = data.db.follow('testfollowdb', function(err, change) { assert.ifError(err); assert.ok(change); feed.stop(); }); assert(feed instanceof events.EventEmitter); feed.on('stop', function() { setTimeout(done, 1000); }); process.nextTick(function() { var db = data.db.use('testfollowdb'); db.insert({ foo: 'bar' }); }); }); }); });