imf-data-nodejs-sdk
Version:
IMFData Cloudant Node.js SDK
398 lines (356 loc) • 12.1 kB
JavaScript
/**
* 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 assert = require('assert');
var IMFData = require('../lib/imfdata.js');
var config = require('./utils/config.js');
var Errors = require('../lib/Errors.js');
var tools = require('./utils/assert-tools.js');
describe('account', function(){
var data;
/**
* Gets account module
*/
before(function(done){
this.timeout(10000);
IMFData.initialize(config, function(err, imfdata){
if (err) {
done(err);
} else {
data = imfdata;
done();
}
});
});
describe('.use(name)', function(){
it('should return a database object with all db_module functions', function(done){
var db_module = data.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.scope('test');
tools.assertDbModuleFunctionsExist(db_module);
done();
});
});
describe('.request([opts], [callback])', function(){
it('should successfully make a request to Cloudant account', function(done){
this.timeout(10000);
data.request(function(err, response, headers){
assert.ifError(err);
tools.assertExists(response);
tools.assertExists(headers);
assert.equal(headers['status-code'], 200);
done();
});
});
it('should successfully create testdb', function(done){
this.timeout(10000);
data.request({
db: 'testdb',
method: 'PUT'
}, function(err, response, headers){
assert.ifError(err);
tools.assertExists(response);
tools.assertExists(headers);
assert.equal(headers['status-code'], 201);
done();
});
});
it('should successfully call /_set_permissions', function(done){
this.timeout(10000);
data.request({
path: '_set_permissions',
content_type: 'application/json',
method: 'POST',
body: {
identity: config.appId,
database: 'testdb',
access: IMFData.permissions.ADMINS
}
}, function(err, response, headers){
assert.ifError(err);
tools.assertExists(response);
tools.assertExists(headers);
assert.equal(headers['status-code'], 200);
done();
});
});
it('should successfully get testdb info', function(done){
this.timeout(10000);
data.request({
db: 'testdb'
}, function(err, response, headers){
assert.ifError(err);
tools.assertExists(response);
tools.assertExists(headers);
assert.equal(headers['status-code'], 200);
done();
});
});
it('should successfully delete testdb', function(done){
this.timeout(10000);
data.request({
db: 'testdb',
method: 'DELETE'
}, function(err, response, headers){
assert.ifError(err);
tools.assertExists(response);
tools.assertExists(headers);
assert.equal(headers['status-code'], 200);
done();
});
});
});
describe('.relax(opts, [callback])', function(){
it('should be the same object as request', function(done){
assert.strictEqual(data.relax, data.request);
done();
});
});
describe('.dinosaur(opts, [callback])', function(){
it('should be the same object as request', function(done){
assert.strictEqual(data.dinosaur, data.request);
done();
});
});
describe('.auth(user, pass, [callback])', function(){
it('should return cookie for registered cloudant user/api-key', function(done){
this.timeout(10000);
data.auth(config.cloudant.credentials.username, config.cloudant.credentials.password, function(err, response, headers){
assert.ifError(err);
assert.ok(response);
assert.ok(headers);
assert.ok(headers['set-cookie']);
done();
});
});
it('should return error for unregistered cloudant user/api-key', function(done){
this.timeout(10000);
data.auth('fakeuser', 'fakepass', function(err, response, headers){
assert.ok(err);
done();
});
});
});
describe('.generate_api_key([callback])', function(){
it('should throw error if used', function(done){
try {
data.generate_api_key();
done(new Error('#generate_ap_key() should have thrown an error'));
} catch (e){
done();
}
});
});
describe('.ping([callback])', function(){
it('should ping account', function(done){
this.timeout(10000);
data.ping(function(err, response){
assert.ifError(err);
tools.assertExists(response);
done();
});
});
});
describe('.session([callback])', function(){
it('should return current session', function(done){
this.timeout(10000);
data.session(function(err, response, headers){
assert.ifError(err);
tools.assertExists(response);
tools.assertExists(headers);
done();
});
});
});
describe('.updates([params], [callback])', function(){
it('should return error for call to /_db_updates', function(done){
this.timeout(10000);
data.updates(function(err, response, headers){
tools.assertExists(err);
done();
});
});
});
describe('.follow_updates([params], [callback])', function(){
this.timeout(10000);
it('should return error for starting a _db_updates feed', function(done){
var feed = data.follow_updates({since: 'now'});
feed.on('error', function(err){
done();
});
feed.follow();
});
});
describe('.config', function(){
it('should return the account config object', function(done){
tools.assertExists(data.config);
var domain;
if (/bluemix/.test(config.domain)){
domain = 'mobile.' + config.domain;
}
assert.equal(data.config.url, config.protocol + '://' + config.domain + '/imfdata/api/v1/apps/' + config.appId + '/');
tools.assertExists(data.config.request);
done();
});
});
describe('.set_permissions(payload, [callback])', function(){
var db;
before(function(done){
this.timeout(20000);
data.db.create('testset_permissionsdb', function(err, body, headers){
if (err){
done(err);
} else {
db = data.use('testset_permissionsdb');
done();
}
});
});
after(function(done){
this.timeout(20000);
data.db.destroy('testset_permissionsdb', function(err, body, headers){
if (err){
done(err);
} else {
done();
}
});
});
it('should successfully give permissions to appId for testset_permissionsdb', function(done){
this.timeout(20000);
data.set_permissions({identity: config.appId, database: 'testset_permissionsdb', access: IMFData.permissions.ADMINS}, function(err, body, headers){
assert.ifError(err);
assert.notEqual(body, null);
assert.notEqual(headers, null);
done();
});
});
it('should allow app to create a document in testset_permissionsdb', function(done){
this.timeout(20000);
db.insert({test: 1}, 'test1', function(err, body, headers){
assert.ifError(err);
assert.notEqual(body, null);
assert.notEqual(headers, null);
done();
});
});
it('should allow app to read a document in testset_permissionsdb', function(done){
this.timeout(20000);
db.get('test1', function(err, body, headers){
assert.ifError(err);
assert.notEqual(body, null);
assert.notEqual(headers, null);
done();
});
});
it('should throw error for null payload', function(done){
try {
data.set_permissions();
done(new Error('null payload should throw during set_permissions call'));
} catch (err) {
assert.equal(err.message, new Errors.IMFData_SET_PERMISSIONS().message);
done();
}
});
it('should throw error for invalid payload', function(done){
try {
data.set_permissions({identity: 'me'});
done(new Error('invalid payload should throw during set_permissions call'));
} catch (err) {
assert.equal(err.message, new Errors.IMFData_SET_PERMISSIONS().message);
done();
}
});
});
describe('.remove_permissions(payload, [callback])', function(){
var db;
before(function(done){
this.timeout(20000);
data.db.create('testremove_permissionsdb', function(err, body, headers){
if (err){
done(err);
} else {
db = data.use('testremove_permissionsdb');
data.set_permissions({identity: config.appId, database: 'testremove_permissionsdb', access: IMFData.permissions.ADMINS}, function(err, response, headers){
if (err)
done(err);
else
done();
});
}
});
});
after(function(done){
this.timeout(20000);
data.db.destroy('testremove_permissionsdb', function(err, body, headers){
if (err){
done(err);
} else {
done();
}
});
});
it('should successfully remove permissions to appId for testremove_permissionsdb', function(done){
this.timeout(20000);
data.remove_permissions({identity: config.appId, database: 'testremove_permissionsdb', access: IMFData.permissions.ADMINS}, function(err, body, headers){
assert.ifError(err);
assert.notEqual(body, null);
assert.notEqual(headers, null);
done();
});
});
it('should prevent app from creating a document in testremove_permissionsdb', function(done){
this.timeout(20000);
db.insert({test: 2}, 'test2', function(err, body, headers){
assert.notEqual(err, null);
assert.equal(body, null);
assert.equal(headers, null);
done();
});
});
it('should prevent app from reading a document in testremove_permissionsdb', function(done){
this.timeout(20000);
db.get('test1', function(err, body, headers){
assert.notEqual(err, null);
assert.equal(body, null);
assert.equal(headers, null);
done();
});
});
it('should throw error for null payload', function(done){
try {
data.remove_permissions();
done(new Error('null payload should throw during set_permissions call'));
} catch (err) {
assert.equal(err.message, new Errors.IMFData_REMOVE_PERMISSIONS().message);
done();
}
});
it('should throw error for invalid payload', function(done){
try {
data.remove_permissions({identity: 'me'});
done(new Error('invalid payload should throw during set_permissions call'));
} catch (err) {
assert.equal(err.message, new Errors.IMFData_REMOVE_PERMISSIONS().message);
done();
}
});
});
});