1
0
mirror of synced 2024-11-25 05:46:02 +03:00

Restructuring

This commit is contained in:
Akolzin Dmitry 2019-02-08 13:31:38 +03:00
parent 9643ebd309
commit 1b70739ae1
9 changed files with 473 additions and 453 deletions

View File

@ -3,4 +3,4 @@ node_js:
- "11" - "11"
- "10" - "10"
- "8" - "8"
script: npm test script: npm tests

View File

@ -17,7 +17,7 @@ var api = new RetailcrmBotApiClient({
host: 'https://api.example.com', host: 'https://api.example.com',
token: 'your bot token', token: 'your bot token',
apiVersion: 'v1' // optional apiVersion: 'v1' // optional
}); }).getClient();
api.getUsers() api.getUsers()
.then(function (users) { .then(function (users) {
@ -34,14 +34,14 @@ var api = new RetailcrmBotApiClient({
host: 'https://api.example.com', host: 'https://api.example.com',
token: 'your bot token', token: 'your bot token',
apiVersion: 'v1' // optional apiVersion: 'v1' // optional
}); }).getClient();
var message = { var message = {
chat_id: 1, chat_id: 1,
content: 'Text message', content: 'Text message',
scope: 'public', scope: 'public',
type: 'text' type: 'text'
} };
api.sendMessage(message) api.sendMessage(message)
.then(function (result) { .then(function (result) {

388
index.js
View File

@ -1,9 +1,7 @@
'use strict'; 'use strict';
var https = require('https'); var v1 = require('./lib/v1/client');
var querystring = require('querystring'); var request = require('./lib/request');
var url = require('url');
var lastApiVersion = 'v1';
module.exports = RetailcrmBotApiClient; module.exports = RetailcrmBotApiClient;
@ -25,380 +23,26 @@ function RetailcrmBotApiClient(options) {
throw new Error('Token is required'); throw new Error('Token is required');
} }
this._setDefaultOptions(options) var currentVersion;
} var lastApiVersion = 'v1';
/** var clients = {
* @param {Object} options 'v1': v1.Client
* @private };
*/
RetailcrmBotApiClient.prototype._setDefaultOptions = function (options) { if (options.apiVersion) {
if (!options.apiVersion) { currentVersion = options.apiVersion;
this.apiVersion = lastApiVersion;
} else { } else {
this.apiVersion = options.apiVersion; currentVersion = lastApiVersion;
} }
this.host = url.parse(options.host).host; this._client = new clients[currentVersion](new request.Request(options));
this.token = options.token;
};
/**
* Get request path
* @param {string} endpoint
* @returns {string}
* @private
*/
RetailcrmBotApiClient.prototype._getPath = function (endpoint) {
return '/api/bot/' + this.apiVersion + endpoint;
};
/**
* Make request
* @param {string} endpoint
* @param {string} method
* @param {Object} data
* @returns {Promise}
* @throws {Error}
* @private
*/
RetailcrmBotApiClient.prototype._request = function (endpoint, method, data) {
var path= this._getPath(endpoint);
var response = '';
if (method === 'GET' && data.length > 0) {
path += '?' + querystring.stringify(data);
} }
var options = {
host: this.host,
method: method,
path: path,
headers: {
'x-bot-token': this.token
}
};
return new Promise(function(resolve, reject) {
var request = https.request(options, function (res) {
res.on('data', function (chunk) {
response += chunk;
});
res.on('end', function () {
try {
var result = JSON.parse(response);
if (res.statusCode < 400) {
resolve(result);
} else {
reject(new Error(result.errors.join(',')));
}
} catch (e) {
reject(e);
}
});
res.on('error', function (e) {
reject(e);
})
});
if (['POST', 'PUT', 'PATCH'].includes(method)) {
request.write(JSON.stringify(data));
}
request.end();
request.on('error', function(e) {
reject(e);
});
});
};
/** /**
* Method GET * Get API client
* @param {string} endpoint * @returns {Client}
* @param {Object} params
* @returns {Promise}
* @private
*/ */
RetailcrmBotApiClient.prototype._get = function (endpoint, params) { RetailcrmBotApiClient.prototype.getClient = function () {
if (params === undefined) { return this._client;
params = {};
}
return this._request(endpoint, 'GET', params);
};
/**
* Method POST
* @param {string} endpoint
* @param {Object} data
* @returns {Promise}
* @throws {Error}
* @private
*/
RetailcrmBotApiClient.prototype._post = function (endpoint, data) {
if (!data) {
throw new Error('Body is not be empty');
}
return this._request(endpoint, 'POST', data);
};
/**
* Method PATCH
* @param {string} endpoint
* @param {Object} data
* @returns {Promise}
* @throws {Error}
* @private
*/
RetailcrmBotApiClient.prototype._patch = function (endpoint, data) {
if (!data) {
throw new Error('Body is not be empty');
}
return this._request(endpoint, 'PATCH', data);
};
/**
* Method PUT
* @param {string} endpoint
* @param {Object} data
* @returns {Promise}
* @throws {Error}
* @private
*/
RetailcrmBotApiClient.prototype._put = function (endpoint, data) {
if (!data) {
throw new Error('Body is not be empty');
}
return this._request(endpoint, 'PUT', data);
};
/**
* Method DELETE
* @param {string} endpoint
* @returns {Promise}
* @private
*/
RetailcrmBotApiClient.prototype._delete = function (endpoint) {
return this._request(endpoint, 'DELETE', {});
};
/**
* Set API version
* @param {string} api_version
*/
RetailcrmBotApiClient.prototype.setApiVersion = function (api_version) {
this.apiVersion = api_version;
};
/**
* Get bot token
* @returns {*|string}
*/
RetailcrmBotApiClient.prototype.getToken = function () {
return this.token;
};
/**
* Get bots
* @param {string} params
* @returns {Promise}
*/
RetailcrmBotApiClient.prototype.getBots = function (params) {
return this._get('/bots', params);
};
/**
* Get channels
* @param {string} params
* @returns {Promise}
*/
RetailcrmBotApiClient.prototype.getChannels = function (params) {
return this._get('/channels', params);
};
/**
* Get chats
* @param {string} params
* @returns {Promise}
*/
RetailcrmBotApiClient.prototype.getChats = function (params) {
return this._get('/chats', params);
};
/**
* Get customers
* @param {string} params
* @returns {Promise}
*/
RetailcrmBotApiClient.prototype.getCustomers = function (params) {
return this._get('/customers', params);
};
/**
* Get dialogs
* @param {string} params
* @returns {Promise}
*/
RetailcrmBotApiClient.prototype.getDialogs = function (params) {
return this._get('/dialogs', params);
};
/**
* Get members
* @param {string} params
* @returns {Promise}
*/
RetailcrmBotApiClient.prototype.getMembers = function (params) {
return this._get('/members', params);
};
/**
* Assign dialog
* @param {Number} dialog_id
* @param {Object} dialog
* @returns {Promise}
*/
RetailcrmBotApiClient.prototype.assignDialog = function (dialog_id, dialog) {
return this._patch('/dialogs/'+ dialog_id + '/assign', dialog);
};
/**
* Close dialog
* @param {Number} dialog_id
* @returns {Promise}
* @throws {Error}
*/
RetailcrmBotApiClient.prototype.closeDialog = function (dialog_id) {
if (!dialog_id) {
throw new Error('dialog_id is required');
}
return this._delete('/dialogs/'+ dialog_id + '/close');
};
/**
* Send message
* @param {Object} data
* @returns {Promise}
*/
RetailcrmBotApiClient.prototype.sendMessage = function (data) {
return this._post('/messages', data);
};
/**
* Get messages
* @param {Object} params
* @returns {Promise}
*/
RetailcrmBotApiClient.prototype.getMessages = function (params) {
return this._get('/messages', params);
};
/**
* Delete message
* @param {Number} message_id
* @returns {Promise}
* @throws {Error}
*/
RetailcrmBotApiClient.prototype.deleteMessage = function (message_id) {
if (!message_id) {
throw new Error('message_id is required');
}
return this._delete('/messages/' + message_id);
};
/**
* Edit message
* @param {Number} message_id
* @param {Object} message
* @returns {Promise}
*/
RetailcrmBotApiClient.prototype.editMessage = function (message_id, message) {
return this._patch('/messages/' + message_id, message);
};
/**
* Get bot commands
* @param {Object} params
* @returns {Promise}
*/
RetailcrmBotApiClient.prototype.getCommands = function (params) {
return this._get('/my/commands', params);
};
/**
* Edit bot command
* @param {string} command_name
* @param {Object} data
* @returns {Promise}
* @throws {Error}
*/
RetailcrmBotApiClient.prototype.editCommand = function (command_name, data) {
if (!command_name) {
throw new Error('Parameter command name is required');
}
return this._put('/my/commands/' + command_name, data);
};
/**
* Delete bot command
* @param {string} command_name
* @returns {Promise}
* @throws {Error}
*/
RetailcrmBotApiClient.prototype.deleteCommand = function (command_name) {
if (!command_name) {
throw new Error('command_name is required');
}
return this._delete('/my/commands/' + command_name);
};
/**
* Bot information update
* @param {Object} data
* @returns {Promise}
*/
RetailcrmBotApiClient.prototype.info = function (data) {
return this._patch('/my/info', data);
};
/**
* Get users
* @param {Object} params
* @returns {Promise}
*/
RetailcrmBotApiClient.prototype.getUsers = function (params) {
return this._get('/users', params);
};
/**
* Get websocket url
* @param {array<string>} events
* @returns {string}
* @throws {Error}
*/
RetailcrmBotApiClient.prototype.getWebsocketUrl = function (events) {
if (!events) {
throw new Error('Events is required');
}
var url = 'wss://' + this.host + '/api/bot/' + this.apiVersion + '/ws?events=';
events.forEach(function (event) {
url += event + ',';
});
url = url.slice(0, -1);
return url;
}; };

161
lib/request.js Normal file
View File

@ -0,0 +1,161 @@
'use strict';
var url = require('url');
var https = require('https');
var querystring = require('querystring');
exports.Request = Request;
/**
* @param {Object} options
* @constructor
*/
function Request(options) {
this._host = url.parse(options.host).host;
this._token = options.token;
}
/**
* Get request path
* @param {string} endpoint
* @returns {string}
* @private
*/
Request.prototype._getPath = function (endpoint) {
return '/api/bot/' + endpoint;
};
/**
* Make request
* @param {string} endpoint
* @param {string} method
* @param {Object} data
* @returns {Promise}
* @throws {Error}
* @private
*/
Request.prototype._request = function (endpoint, method, data) {
var path = this._getPath(endpoint);
var response = '';
if (method === 'GET' && data.length > 0) {
path += '?' + querystring.stringify(data);
}
var options = {
host: this._host,
method: method,
path: path,
headers: {
'x-bot-token': this._token
}
};
return new Promise(function(resolve, reject) {
var request = https.request(options, function (res) {
res.on('data', function (chunk) {
response += chunk;
});
res.on('end', function () {
try {
var result = JSON.parse(response);
if (res.statusCode < 400) {
resolve(result);
} else {
reject(new Error(result.errors.join(',')));
}
} catch (e) {
reject(e);
}
});
res.on('error', function (e) {
reject(e);
})
});
if (['POST', 'PUT', 'PATCH'].includes(method)) {
request.write(JSON.stringify(data));
}
request.end();
request.on('error', function(e) {
reject(e);
});
});
};
/**
* Method GET
* @param {string} endpoint
* @param {Object} params
* @returns {Promise}
*/
Request.prototype.get = function (endpoint, params) {
if (params === undefined) {
params = {};
}
return this._request(endpoint, 'GET', params);
};
/**
* Method POST
* @param {string} endpoint
* @param {Object} data
* @returns {Promise}
* @throws {Error}
*/
Request.prototype.post = function (endpoint, data) {
if (!data) {
throw new Error('Body is not be empty');
}
return this._request(endpoint, 'POST', data);
};
/**
* Method PATCH
* @param {string} endpoint
* @param {Object} data
* @returns {Promise}
* @throws {Error}
*/
Request.prototype.patch = function (endpoint, data) {
if (!data) {
throw new Error('Body is not be empty');
}
return this._request(endpoint, 'PATCH', data);
};
/**
* Method PUT
* @param {string} endpoint
* @param {Object} data
* @returns {Promise}
* @throws {Error}
*/
Request.prototype.put = function (endpoint, data) {
if (!data) {
throw new Error('Body is not be empty');
}
return this._request(endpoint, 'PUT', data);
};
/**
* Method DELETE
* @param {string} endpoint
* @returns {Promise}
*/
Request.prototype.delete = function (endpoint) {
return this._request(endpoint, 'DELETE', {});
};
Request.prototype.getHost = function () {
return this._host;
};

210
lib/v1/client.js Normal file
View File

@ -0,0 +1,210 @@
'use strict';
exports.Client = Client;
/**
* @param {Request} request
* @constructor
*/
function Client(request) {
this._version = 'v1';
this._request = request;
}
/**
* Get bots
* @param {string} params
* @returns {Promise}
*/
Client.prototype.getBots = function (params) {
return this._request.get(this._version + '/bots', params);
};
/**
* Get channels
* @param {string} params
* @returns {Promise}
*/
Client.prototype.getChannels = function (params) {
return this._request.get(this._version + '/channels', params);
};
/**
* Get chats
* @param {string} params
* @returns {Promise}
*/
Client.prototype.getChats = function (params) {
return this._request.get(this._version + '/chats', params);
};
/**
* Get customers
* @param {string} params
* @returns {Promise}
*/
Client.prototype.getCustomers = function (params) {
return this._request.get(this._version + '/customers', params);
};
/**
* Get dialogs
* @param {string} params
* @returns {Promise}
*/
Client.prototype.getDialogs = function (params) {
return this._request.get(this._version + '/dialogs', params);
};
/**
* Get members
* @param {string} params
* @returns {Promise}
*/
Client.prototype.getMembers = function (params) {
return this._request.get(this._version + '/members', params);
};
/**
* Assign dialog
* @param {Number} dialog_id
* @param {Object} dialog
* @returns {Promise}
*/
Client.prototype.assignDialog = function (dialog_id, dialog) {
return this._request.patch(this._version + '/dialogs/'+ dialog_id + '/assign', dialog);
};
/**
* Close dialog
* @param {Number} dialog_id
* @returns {Promise}
* @throws {Error}
*/
Client.prototype.closeDialog = function (dialog_id) {
if (!dialog_id) {
throw new Error('dialog_id is required');
}
return this._request.delete(this._version + '/dialogs/'+ dialog_id + '/close');
};
/**
* Send message
* @param {Object} data
* @returns {Promise}
*/
Client.prototype.sendMessage = function (data) {
return this._request.post(this._version + '/messages', data);
};
/**
* Get messages
* @param {Object} params
* @returns {Promise}
*/
Client.prototype.getMessages = function (params) {
return this._request.get(this._version + '/messages', params);
};
/**
* Delete message
* @param {Number} message_id
* @returns {Promise}
* @throws {Error}
*/
Client.prototype.deleteMessage = function (message_id) {
if (!message_id) {
throw new Error('message_id is required');
}
return this._request.delete(this._version + '/messages/' + message_id);
};
/**
* Edit message
* @param {Number} message_id
* @param {Object} message
* @returns {Promise}
*/
Client.prototype.editMessage = function (message_id, message) {
return this._request.patch(this._version + '/messages/' + message_id, message);
};
/**
* Get bot commands
* @param {Object} params
* @returns {Promise}
*/
Client.prototype.getCommands = function (params) {
return this._request.get(this._version + '/my/commands', params);
};
/**
* Edit bot command
* @param {string} command_name
* @param {Object} data
* @returns {Promise}
* @throws {Error}
*/
Client.prototype.editCommand = function (command_name, data) {
if (!command_name) {
throw new Error('Parameter command name is required');
}
return this._request.put(this._version + '/my/commands/' + command_name, data);
};
/**
* Delete bot command
* @param {string} command_name
* @returns {Promise}
* @throws {Error}
*/
Client.prototype.deleteCommand = function (command_name) {
if (!command_name) {
throw new Error('command_name is required');
}
return this._request.delete(this._version + '/my/commands/' + command_name);
};
/**
* Bot information update
* @param {Object} data
* @returns {Promise}
*/
Client.prototype.info = function (data) {
return this._request.patch(this._version + '/my/info', data);
};
/**
* Get users
* @param {Object} params
* @returns {Promise}
*/
Client.prototype.getUsers = function (params) {
return this._request.get(this._version + '/users', params);
};
/**
* Get websocket url
* @param {array<string>} events
* @returns {string}
* @throws {Error}
*/
Client.prototype.getWebsocketUrl = function (events) {
if (!events) {
throw new Error('Events is required');
}
var url = 'wss://' + this._request.getHost() + '/api/bot/' + this._version + '/ws?events=';
events.forEach(function (event) {
url += event + ',';
});
url = url.slice(0, -1);
return url;
};

View File

@ -4,7 +4,7 @@
"tags": ["API", "retailCRM", "Bot", "Chat"], "tags": ["API", "retailCRM", "Bot", "Chat"],
"version": "1.0.0", "version": "1.0.0",
"scripts": { "scripts": {
"test": " ./node_modules/.bin/_mocha test.js" "test": "./node_modules/.bin/_mocha ./tests/."
}, },
"author": "retailCRM", "author": "retailCRM",
"license": "MIT", "license": "MIT",

26
tests/test_index.js Normal file
View File

@ -0,0 +1,26 @@
var nock = require('nock');
var chai = require('chai');
var RetailcrmBotApiClient = require('../index');
describe('#Constructor', function () {
it('Empty url', function () {
chai.expect(function() {
new RetailcrmBotApiClient({token: 'test_token'});
}).to.throw('Url is required');
});
it('Incorrect url', function () {
chai.expect(function() {
new RetailcrmBotApiClient({
host: 'http://api.example.com',
token: 'test_token'
});
}).to.throw('HTTPS required');
});
it('Empty token', function () {
chai.expect(function() {
new RetailcrmBotApiClient({host: 'https://api.example.com'});
}).to.throw('Token is required');
});
});

14
tests/test_request.js Normal file
View File

@ -0,0 +1,14 @@
var chai = require('chai');
var request = require('../lib/request');
describe('#Request', function () {
var req = new request.Request({
host: 'http://api.example.com',
token: 'test_token'
});
it('Request parameters', function () {
chai.expect(req._host).to.equal('api.example.com');
chai.expect(req._token).to.equal('test_token');
})
});

View File

@ -1,54 +1,19 @@
var nock = require('nock'); var nock = require('nock');
var chai = require('chai'); var chai = require('chai');
var RetailcrmBotApiClient = require('./index'); var RetailcrmBotApiClient = require('../index');
describe('#Constructor', function () { describe('#API client v1', function() {
it('Empty url', function () {
chai.expect(function() {
new RetailcrmBotApiClient({token: 'test_token'});
}).to.throw('Url is required');
});
it('Incorrect url', function () {
chai.expect(function() {
new RetailcrmBotApiClient({
host: 'http://test.retailcrm.ru',
token: 'test_token'
});
}).to.throw('HTTPS required');
});
it('Empty token', function () {
chai.expect(function() {
new RetailcrmBotApiClient({host: 'https://test.retailcrm.ru',});
}).to.throw('Token is required');
});
it('Set the default options', function () {
var api = new RetailcrmBotApiClient({
host: 'https://test.retailcrm.ru',
token: 'xxxxxxxxxxxxxxxxxxxxxxxx'
});
chai.expect(api.apiVersion).to.equal('v1');
chai.expect(api.token).to.equal('xxxxxxxxxxxxxxxxxxxxxxxx');
chai.expect(api.getToken()).to.equal('xxxxxxxxxxxxxxxxxxxxxxxx');
chai.expect(api.host).to.equal('test.retailcrm.ru');
});
});
describe('#Requests', function() {
beforeEach(function() { beforeEach(function() {
nock.cleanAll(); nock.cleanAll();
}); });
var retailcrm = new RetailcrmBotApiClient({ var retailcrm = new RetailcrmBotApiClient({
host: 'https://test.retailcrm.ru', host: 'https://api.example.com',
token: 'test_token' token: 'test_token'
}); }).getClient();
it('Get bots list', function() { it('Get bots list', function() {
nock('https://test.retailcrm.ru/api/bot/v1').get('/bots').reply(200, [{ nock('https://api.example.com/api/bot/v1').get('/bots').reply(200, [{
id: 1, id: 1,
isActive: true isActive: true
}]); }]);
@ -60,7 +25,7 @@ describe('#Requests', function() {
}); });
it('Get empty bots list', function () { it('Get empty bots list', function () {
nock('https://test.retailcrm.ru/api/bot/v1').get('/bots').reply(200, []); nock('https://api.example.com/api/bot/v1').get('/bots').reply(200, []);
retailcrm.getBots().then(function (value) { retailcrm.getBots().then(function (value) {
chai.expect(value).to.be.an('array'); chai.expect(value).to.be.an('array');
@ -69,7 +34,7 @@ describe('#Requests', function() {
}); });
it('Get channels list', function () { it('Get channels list', function () {
nock('https://test.retailcrm.ru/api/bot/v1').get('/channels').reply(200, [{ nock('https://api.example.com/api/bot/v1').get('/channels').reply(200, [{
id: 1 id: 1
}]); }]);
@ -80,7 +45,7 @@ describe('#Requests', function() {
}); });
it('Get empty channels list', function () { it('Get empty channels list', function () {
nock('https://test.retailcrm.ru/api/bot/v1').get('/channels').reply(200, []); nock('https://api.example.com/api/bot/v1').get('/channels').reply(200, []);
retailcrm.getChannels().then(function (value) { retailcrm.getChannels().then(function (value) {
chai.expect(value).to.be.an('array'); chai.expect(value).to.be.an('array');
@ -89,7 +54,7 @@ describe('#Requests', function() {
}); });
it('Get chats list', function () { it('Get chats list', function () {
nock('https://test.retailcrm.ru/api/bot/v1').get('/chats').reply(200, [{ nock('https://api.example.com/api/bot/v1').get('/chats').reply(200, [{
author_id: 1, author_id: 1,
id: 1 id: 1
}]); }]);
@ -101,7 +66,7 @@ describe('#Requests', function() {
}); });
it('Get empty chats list', function () { it('Get empty chats list', function () {
nock('https://test.retailcrm.ru/api/bot/v1').get('/chats').reply(200, []); nock('https://api.example.com/api/bot/v1').get('/chats').reply(200, []);
retailcrm.getChats().then(function (value) { retailcrm.getChats().then(function (value) {
chai.expect(value).to.be.an('array'); chai.expect(value).to.be.an('array');
@ -110,7 +75,7 @@ describe('#Requests', function() {
}); });
it('Get customers list', function () { it('Get customers list', function () {
nock('https://test.retailcrm.ru/api/bot/v1').get('/customers').reply(200, [{ nock('https://api.example.com/api/bot/v1').get('/customers').reply(200, [{
external_id: 1, external_id: 1,
channel_id: 1, channel_id: 1,
id: 1 id: 1
@ -123,7 +88,7 @@ describe('#Requests', function() {
}); });
it('Get empty customers list', function () { it('Get empty customers list', function () {
nock('https://test.retailcrm.ru/api/bot/v1').get('/customers').reply(200, []); nock('https://api.example.com/api/bot/v1').get('/customers').reply(200, []);
retailcrm.getCustomers().then(function (value) { retailcrm.getCustomers().then(function (value) {
chai.expect(value).to.be.an('array'); chai.expect(value).to.be.an('array');
@ -132,7 +97,7 @@ describe('#Requests', function() {
}); });
it('Get dialogs list', function () { it('Get dialogs list', function () {
nock('https://test.retailcrm.ru/api/bot/v1').get('/dialogs').reply(200, [{ nock('https://api.example.com/api/bot/v1').get('/dialogs').reply(200, [{
begin_message_id: 1, begin_message_id: 1,
id: 1 id: 1
}]); }]);
@ -144,7 +109,7 @@ describe('#Requests', function() {
}); });
it('Get empty dialogs list', function () { it('Get empty dialogs list', function () {
nock('https://test.retailcrm.ru/api/bot/v1').get('/dialogs').reply(200, []); nock('https://api.example.com/api/bot/v1').get('/dialogs').reply(200, []);
retailcrm.getDialogs().then(function (value) { retailcrm.getDialogs().then(function (value) {
chai.expect(value).to.be.an('array'); chai.expect(value).to.be.an('array');
@ -153,7 +118,7 @@ describe('#Requests', function() {
}); });
it('Get members list', function () { it('Get members list', function () {
nock('https://test.retailcrm.ru/api/bot/v1').get('/members').reply(200, [{ nock('https://api.example.com/api/bot/v1').get('/members').reply(200, [{
id: 1 id: 1
}]); }]);
@ -164,7 +129,7 @@ describe('#Requests', function() {
}); });
it('Get empty members list', function () { it('Get empty members list', function () {
nock('https://test.retailcrm.ru/api/bot/v1').get('/members').reply(200, []); nock('https://api.example.com/api/bot/v1').get('/members').reply(200, []);
retailcrm.getMembers().then(function (value) { retailcrm.getMembers().then(function (value) {
chai.expect(value).to.be.an('array'); chai.expect(value).to.be.an('array');
@ -173,7 +138,7 @@ describe('#Requests', function() {
}); });
it('Assign dialog', function () { it('Assign dialog', function () {
nock('https://test.retailcrm.ru/api/bot/v1').patch('/dialogs/1/assign').reply(200, { nock('https://api.example.com/api/bot/v1').patch('/dialogs/1/assign').reply(200, {
is_reassign: true, is_reassign: true,
responsible: { responsible: {
id: 1 id: 1
@ -192,7 +157,7 @@ describe('#Requests', function() {
}); });
it('Close dialog', function () { it('Close dialog', function () {
nock('https://test.retailcrm.ru/api/bot/v1').delete('/dialogs/1/close').reply(200, {}); nock('https://api.example.com/api/bot/v1').delete('/dialogs/1/close').reply(200, {});
retailcrm.closeDialog(1).then(function (value) { retailcrm.closeDialog(1).then(function (value) {
chai.expect(value).to.be.empty; chai.expect(value).to.be.empty;
@ -204,11 +169,11 @@ describe('#Requests', function() {
}); });
it('Send message', function () { it('Send message', function () {
nock('https://test.retailcrm.ru/api/bot/v1').post('/messages', { nock('https://api.example.com/api/bot/v1').post('/messages', {
chat_id: 1, chat_id: 1,
scope: 'public', scope: 'public',
type: 'text', type: 'text',
content: 'test message' content: 'tests message'
}).reply(200, { }).reply(200, {
message_id: 1 message_id: 1
}); });
@ -217,7 +182,7 @@ describe('#Requests', function() {
chat_id: 1, chat_id: 1,
scope: 'public', scope: 'public',
type: 'text', type: 'text',
content: 'test message' content: 'tests message'
}).then(function (value) { }).then(function (value) {
chai.expect(value).to.be.an('object'); chai.expect(value).to.be.an('object');
chai.expect(value).to.be.not.empty; chai.expect(value).to.be.not.empty;
@ -229,7 +194,7 @@ describe('#Requests', function() {
}); });
it('Get messages', function() { it('Get messages', function() {
nock('https://test.retailcrm.ru/api/bot/v1').get('/messages').reply(200, [{ nock('https://api.example.com/api/bot/v1').get('/messages').reply(200, [{
id: 1, id: 1,
chat_id: 1, chat_id: 1,
from: { from: {
@ -244,7 +209,7 @@ describe('#Requests', function() {
}); });
it('Get empty messages', function () { it('Get empty messages', function () {
nock('https://test.retailcrm.ru/api/bot/v1').get('/messages').reply(200, []); nock('https://api.example.com/api/bot/v1').get('/messages').reply(200, []);
retailcrm.getMessages().then(function (value) { retailcrm.getMessages().then(function (value) {
chai.expect(value).to.be.an('array'); chai.expect(value).to.be.an('array');
@ -253,7 +218,7 @@ describe('#Requests', function() {
}); });
it('Delete message', function () { it('Delete message', function () {
nock('https://test.retailcrm.ru/api/bot/v1').delete('/messages/1').reply(200, {}); nock('https://api.example.com/api/bot/v1').delete('/messages/1').reply(200, {});
retailcrm.deleteMessage(1).then(function (value) { retailcrm.deleteMessage(1).then(function (value) {
chai.expect(value).to.be.empty; chai.expect(value).to.be.empty;
@ -265,12 +230,12 @@ describe('#Requests', function() {
}); });
it('Edit message', function () { it('Edit message', function () {
nock('https://test.retailcrm.ru/api/bot/v1').patch('/messages/1', { nock('https://api.example.com/api/bot/v1').patch('/messages/1', {
content: 'test message' content: 'tests message'
}).reply(200, {}); }).reply(200, {});
retailcrm.editMessage(1, { retailcrm.editMessage(1, {
content: 'test message' content: 'tests message'
}).then(function (value) { }).then(function (value) {
chai.expect(value).to.be.empty; chai.expect(value).to.be.empty;
}); });
@ -281,7 +246,7 @@ describe('#Requests', function() {
}); });
it('Get commands', function () { it('Get commands', function () {
nock('https://test.retailcrm.ru/api/bot/v1').get('/my/commands').reply(200, [{ nock('https://api.example.com/api/bot/v1').get('/my/commands').reply(200, [{
id: 1, id: 1,
name: 'Command name' name: 'Command name'
}]); }]);
@ -293,7 +258,7 @@ describe('#Requests', function() {
}); });
it('Get empty commands', function () { it('Get empty commands', function () {
nock('https://test.retailcrm.ru/api/bot/v1').get('/my/commands').reply(200, []); nock('https://api.example.com/api/bot/v1').get('/my/commands').reply(200, []);
retailcrm.getCommands().then(function (value) { retailcrm.getCommands().then(function (value) {
chai.expect(value).to.be.an('array'); chai.expect(value).to.be.an('array');
@ -302,7 +267,7 @@ describe('#Requests', function() {
}); });
it('Edit command', function () { it('Edit command', function () {
nock('https://test.retailcrm.ru/api/bot/v1').put('/my/commands/command', { nock('https://api.example.com/api/bot/v1').put('/my/commands/command', {
description: 'Desc', description: 'Desc',
name: 'name' name: 'name'
}).reply(200, {}); }).reply(200, {});
@ -321,7 +286,7 @@ describe('#Requests', function() {
}); });
it('Delete command', function () { it('Delete command', function () {
nock('https://test.retailcrm.ru/api/bot/v1').delete('/my/commands/command').reply(200, {}); nock('https://api.example.com/api/bot/v1').delete('/my/commands/command').reply(200, {});
retailcrm.deleteCommand('command').then(function (value) { retailcrm.deleteCommand('command').then(function (value) {
chai.expect(value).to.be.empty; chai.expect(value).to.be.empty;
@ -333,13 +298,13 @@ describe('#Requests', function() {
}); });
it('Update bot info', function () { it('Update bot info', function () {
nock('https://test.retailcrm.ru/api/bot/v1').patch('/my/info', { nock('https://api.example.com/api/bot/v1').patch('/my/info', {
avatar_url: 'http://test.ru/avatar.png', avatar_url: 'http://tests.ru/avatar.png',
name: 'Bot' name: 'Bot'
}).reply(200, {}); }).reply(200, {});
retailcrm.info({ retailcrm.info({
avatar_url: 'http://test.ru/avatar.png', avatar_url: 'http://tests.ru/avatar.png',
name: 'Bot' name: 'Bot'
}).then(function (value) { }).then(function (value) {
chai.expect(value).to.be.empty; chai.expect(value).to.be.empty;
@ -351,7 +316,7 @@ describe('#Requests', function() {
}); });
it('Get users', function () { it('Get users', function () {
nock('https://test.retailcrm.ru/api/bot/v1').get('/users').reply(200, [{ nock('https://api.example.com/api/bot/v1').get('/users').reply(200, [{
id: 1, id: 1,
name: 'Username' name: 'Username'
}]); }]);
@ -363,7 +328,7 @@ describe('#Requests', function() {
}); });
it('Get empty users', function () { it('Get empty users', function () {
nock('https://test.retailcrm.ru/api/bot/v1').get('/users').reply(200, []); nock('https://api.example.com/api/bot/v1').get('/users').reply(200, []);
retailcrm.getUsers().then(function (value) { retailcrm.getUsers().then(function (value) {
chai.expect(value).to.be.an('array'); chai.expect(value).to.be.an('array');
@ -373,7 +338,7 @@ describe('#Requests', function() {
it('Get websocket url', function () { it('Get websocket url', function () {
var url = retailcrm.getWebsocketUrl(['message_new', 'message_updated']); var url = retailcrm.getWebsocketUrl(['message_new', 'message_updated']);
var expected = 'wss://test.retailcrm.ru/api/bot/v1/ws?events=message_new,message_updated'; var expected = 'wss://api.example.com/api/bot/v1/ws?events=message_new,message_updated';
chai.assert.equal(url, expected); chai.assert.equal(url, expected);
}); });