jsforce
Version:
Salesforce API Library for JavaScript
483 lines (438 loc) • 15 kB
JavaScript
/*global describe, it, before */
var testUtils = require('./helper/test-utils'),
assert = testUtils.assert;
var async = require('async'),
_ = require('underscore'),
authorize = require('./helper/webauth'),
sf = require('../lib/jsforce'),
config = require('./config/salesforce');
/**
*
*/
describe("connection", function() {
this.timeout(40000); // set timeout to 40 sec.
var conn = new testUtils.createConnection(config);
/**
*
*/
before(function(done) {
testUtils.establishConnection(conn, config, done);
});
var accountId, account;
/**
*
*/
describe("create account", function() {
it("should return created obj", function(done) {
conn.sobject('Account').create({ Name : 'Hello' }, function(err, ret) {
if (err) { throw err; }
assert.ok(ret.success);
assert.ok(_.isString(ret.id));
accountId = ret.id;
}.check(done));
});
});
/**
*
*/
describe("retrieve account", function() {
it("should return a record", function(done) {
conn.sobject('Account').retrieve(accountId, function(err, record) {
if (err) { throw err; }
assert.ok(_.isString(record.Id));
assert.ok(_.isObject(record.attributes));
assert.ok(record.Name === 'Hello');
account = record;
}.check(done));
});
});
/**
*
*/
describe("update account", function() {
it("should update successfully", function(done) {
conn.sobject('Account').record(account.Id).update({ Name : "Hello2" }, function(err, ret) {
if (err) { throw err; }
assert.ok(ret.success);
}.check(done));
});
describe("then retrieve the account", function() {
it("should return updated account object", function(done) {
conn.sobject('Account').record(accountId).retrieve(function(err, record) {
if (err) { throw err; }
assert.ok(record.Name === 'Hello2');
assert.ok(_.isObject(record.attributes));
}.check(done));
});
});
});
describe("update account with options headers", function() {
var options = {
headers: {
'SForce-Auto-Assign': 'FALSE'
}
};
it("should update with options headers successfully", function(done) {
conn.sobject('Account').record(account.Id).update({ Name : "Hello3" }, options, function(err, ret) {
if (err) { throw err; }
assert.ok(ret.success);
}.check(done));
});
describe("then retrieve the account", function() {
it("should return updated account object with options headers set", function(done) {
conn.sobject('Account').record(accountId).retrieve(options, function(err, record) {
if (err) { throw err; }
assert.ok(record.Name === 'Hello3');
assert.ok(_.isObject(record.attributes));
}.check(done));
});
});
});
/**
*
*/
describe("delete account", function() {
it("should delete successfully", function(done) {
conn.sobject('Account').record(account.Id).destroy(function(err, ret) {
if (err) { throw err; }
assert.ok(ret.success);
}.check(done));
});
describe("then retrieve the account", function() {
it("should not return any record for deleted account", function(done) {
conn.sobject('Account').retrieve(account.Id, function(err, record) {
assert.ok(err instanceof Error);
assert.ok(err.errorCode === 'NOT_FOUND');
}.check(done));
});
});
});
var accountIds, accounts;
/**
*
*/
describe("create multiple accounts", function() {
it("should return created records", function(done) {
conn.sobject('Account').create([
{ Name : 'Account #1' },
{ Name : 'Account #2' }
], function(err, rets) {
if (err) { throw err; }
assert.ok(_.isArray(rets));
rets.forEach(function(ret) {
assert.ok(ret.success);
assert.ok(_.isString(ret.id));
});
accountIds = rets.map(function(ret){ return ret.id; });
}.check(done));
});
});
/**
*
*/
describe("retrieve multiple accounts", function() {
it("should return specified records", function(done) {
conn.sobject('Account').retrieve(accountIds, function(err, records) {
if (err) { throw err; }
assert.ok(_.isArray(records));
records.forEach(function(record, i) {
assert.ok(_.isString(record.Id));
assert.ok(_.isObject(record.attributes));
assert.ok(record.Name === 'Account #' + (i+1));
});
accounts = records;
}.check(done));
});
});
/**
*
*/
describe("update multiple accounts", function() {
it("should update records successfully", function(done) {
conn.sobject('Account').update(
accounts.map(function(account) {
return { Id : account.Id, Name : "Updated " + account.Name };
}),
function(err, rets) {
if (err) { throw err; }
assert.ok(_.isArray(rets));
rets.forEach(function(ret){
assert.ok(ret.success);
});
}.check(done)
);
});
describe("then retrieve the accounts", function() {
it("sholuld return updated records", function(done) {
conn.sobject('Account').retrieve(accountIds, function(err, records) {
if (err) { throw err; }
assert.ok(_.isArray(records));
records.forEach(function(record, i) {
assert.ok(record.Name === 'Updated Account #' + (i+1));
assert.ok(_.isObject(record.attributes));
});
}.check(done));
});
});
});
/**
*
*/
describe("delete multiple accounts", function() {
it("should delete successfully", function(done) {
conn.sobject('Account').destroy(accountIds, function(err, rets) {
if (err) { throw err; }
assert.ok(_.isArray(rets));
rets.forEach(function(ret){
assert.ok(ret.success);
});
}.check(done));
});
describe("then retrieve the accounts", function() {
it("should not return any records", function(done) {
conn.sobject('Account').retrieve(accountIds, function(err, records) {
assert.ok(err instanceof Error);
assert.ok(err.errorCode === 'NOT_FOUND');
}.check(done));
});
});
});
/**
*
*/
describe("upsert record", function() {
var extId = "ID" + Date.now();
var recId;
describe("for not existing record", function() {
it("should create record successfully", function(done) {
var rec = { Name : 'New Record' };
rec[config.upsertField] = extId;
conn.sobject(config.upsertTable).upsert(rec, config.upsertField, function(err, ret) {
if (err) { throw err; }
assert.ok(ret.success);
assert.ok(_.isString(ret.id));
recId = ret.id;
}.check(done));
});
});
describe("for already existing record", function() {
it("should update record successfully", function(done) {
var rec = { Name : 'Updated Record' };
rec[config.upsertField] = extId;
conn.sobject(config.upsertTable).upsert(rec, config.upsertField, function(err, ret) {
if (err) { throw err; }
assert.ok(ret.success);
assert.ok(_.isUndefined(ret.id));
}.check(done));
});
describe("then retrieve the record", function() {
it("should return updated record", function(done) {
conn.sobject(config.upsertTable).retrieve(recId, function(err, record) {
if (err) { throw err; }
assert.ok(record.Name === "Updated Record");
}.check(done));
});
});
});
describe("for duplicated external id record", function() {
before(function(done) {
var rec = { Name : 'Duplicated Record' };
rec[config.upsertField] = extId;
conn.sobject(config.upsertTable).create(rec, done);
});
it("should throw error and return array of choices", function(done) {
var rec = { Name : 'Updated Record, Twice' };
rec[config.upsertField] = extId;
conn.sobject(config.upsertTable).upsert(rec, config.upsertField, function(err, ret) {
assert.ok(err instanceof Error);
assert.ok(err.name === "MULTIPLE_CHOICES");
assert.ok(_.isArray(err.content));
assert.ok(_.isString(err.content[0]));
}.check(done));
});
});
});
/**
*
*/
describe("describe Account", function() {
it("should return metadata information", function(done) {
conn.sobject('Account').describe(function(err, meta) {
if (err) { throw err; }
assert.ok(meta.name === "Account");
assert.ok(_.isArray(meta.fields));
}.check(done));
});
describe("then describe cached Account", function() {
it("should return metadata information", function(done) {
conn.sobject('Account').describe$(function(err, meta) {
if (err) { throw err; }
assert.ok(meta.name === "Account");
assert.ok(_.isArray(meta.fields));
}.check(done));
});
});
});
/**
*
*/
describe("describe global sobjects", function() {
it("should return whole global sobject list", function(done) {
conn.describeGlobal(function(err, res) {
if (err) { throw err; }
assert.ok(_.isArray(res.sobjects));
assert.ok(_.isString(res.sobjects[0].name));
assert.ok(_.isString(res.sobjects[0].label));
assert.ok(_.isUndefined(res.sobjects[0].fields));
}.check(done));
});
describe("then describe cached global sobjects", function() {
it("should return whole global sobject list", function(done) {
conn.describeGlobal$(function(err, res) {
if (err) { throw err; }
assert.ok(_.isArray(res.sobjects));
assert.ok(_.isString(res.sobjects[0].name));
assert.ok(_.isString(res.sobjects[0].label));
assert.ok(_.isUndefined(res.sobjects[0].fields));
}.check(done));
});
});
});
/**
*
*/
describe("get recently accessed records", function() {
before(function(done) {
conn.query("SELECT Id, Name FROM Account ORDER BY CreatedDate DESC LIMIT 2 FOR VIEW", function(err) {
if (err) { throw err; }
}.check(done));
});
it("should return recently viewed records in all object", function(done) {
conn.recent(2, function(err, records) {
if (err) { throw err; }
assert(_.isArray(records));
records.forEach(function(record) {
assert(_.isString(record.Id));
assert(_.isString(record.Name));
assert(_.isString(record.attributes.type));
assert(record.attributes.type === 'Account');
});
}.check(done));
});
it("should return recently viewed accounts in Account object", function(done) {
conn.sobject('Account').recent(function(err, records) {
if (err) { throw err; }
assert(_.isArray(records));
records.forEach(function(record) {
assert(_.isString(record.Id));
assert(_.isString(record.Name));
assert(_.isString(record.attributes.type));
assert(record.attributes.type === 'Account');
});
}.check(done));
});
});
/**
*
*/
describe("get updated / deleted account", function () {
before(function(done) {
var accs = [{ Name: 'Hello' }, { Name: 'World' }];
conn.sobject('Account').create(accs, function(err, rets) {
if (err) { throw err; }
var id1 = rets[0].id, id2 = rets[1].id;
async.parallel([
function(cb) {
conn.sobject('Account').record(id1).update({ Name: "Hello2" }, cb);
},
function(cb) {
conn.sobject('Account').record(id2).destroy(cb);
}
], function (err, ret) {
if (err) { throw err; }
}.check(done));
});
});
/**
*
*/
describe("get updated accounts", function () {
it("should return updated account object", function (done) {
var end = new Date();
var start = new Date(end.getTime() - 2 * 24 * 60 * 60 * 1000); // 2 days before
conn.sobject('Account').updated(start, end, function (err, result) {
if (err) { throw err; }
assert.ok(_.isArray(result.ids));
}.check(done));
});
});
/**
*
*/
describe("get updated account with string input", function () {
it("should return updated account object", function (done) {
var end = new Date();
var start = new Date(end.getTime() - 2 * 24 * 60 * 60 * 1000); // 2 days before
conn.sobject('Account').updated(start.toString(), end.toString(), function (err, result) {
if (err) { throw err; }
assert.ok(_.isArray(result.ids));
}.check(done));
});
});
/**
*
*/
describe("get deleted account", function () {
it("should return deleted account object", function (done) {
var end = new Date();
var start = new Date(end.getTime() - 2 * 24 * 60 * 60 * 1000); // 2 days before
conn.sobject('Account').deleted(start, end, function (err, result) {
if (err) { throw err; }
assert.ok(_.isArray(result.deletedRecords));
}.check(done));
});
});
/**
*
*/
describe("get deleted account with string input", function () {
it("should return deleted account object", function (done) {
var end = new Date();
var start = new Date(end.getTime() - 2 * 24 * 60 * 60 * 1000); // 2 days before
conn.sobject('Account').deleted(start.toString(), end.toString(), function (err, result) {
if (err) { throw err; }
assert.ok(_.isArray(result.deletedRecords));
}.check(done));
});
});
});
/**
*
*/
describe("get user identity information", function() {
it("should return user identity information", function (done) {
conn.identity(function(err, res) {
assert.ok(_.isString(res.id));
assert.ok(res.id.indexOf('https://') === 0);
assert.ok(_.isString(res.user_id));
assert.ok(_.isString(res.organization_id));
assert.ok(_.isString(res.email));
assert.ok(_.isObject(res.photos));
assert.ok(_.isObject(res.urls));
}.check(done));
});
});
/**
*
*/
describe("get api limit information", function() {
it("should get api usage and its limit in the org", function() {
var limitInfo = conn.limitInfo;
assert.ok(_.isObject(limitInfo.apiUsage));
assert.ok(_.isNumber(limitInfo.apiUsage.used));
assert.ok(_.isNumber(limitInfo.apiUsage.limit));
assert.ok(limitInfo.apiUsage.used > 0);
assert.ok(limitInfo.apiUsage.limit > limitInfo.apiUsage.used);
});
});
});