1
0
mirror of synced 2024-11-25 13:56:03 +03:00
mg-bot-api-client-js/tests/test_v1_client.js

350 lines
11 KiB
JavaScript
Raw Normal View History

2019-02-06 14:54:15 +03:00
var nock = require('nock');
var chai = require('chai');
2019-02-08 13:31:38 +03:00
var RetailcrmBotApiClient = require('../index');
2019-02-06 14:54:15 +03:00
2019-02-08 13:31:38 +03:00
describe('#API client v1', function() {
2019-02-06 14:54:15 +03:00
beforeEach(function() {
nock.cleanAll();
});
var retailcrm = new RetailcrmBotApiClient({
2019-02-08 13:31:38 +03:00
host: 'https://api.example.com',
2019-02-06 14:54:15 +03:00
token: 'test_token'
2019-02-08 13:31:38 +03:00
}).getClient();
2019-02-06 14:54:15 +03:00
it('Get bots list', function() {
2019-02-08 13:31:38 +03:00
nock('https://api.example.com/api/bot/v1').get('/bots').reply(200, [{
2019-02-06 14:54:15 +03:00
id: 1,
isActive: true
}]);
retailcrm.getBots().then(function (value) {
chai.expect(value).to.be.an('array');
chai.expect(value).to.be.not.empty;
});
});
it('Get empty bots list', function () {
2019-02-08 13:31:38 +03:00
nock('https://api.example.com/api/bot/v1').get('/bots').reply(200, []);
2019-02-06 14:54:15 +03:00
retailcrm.getBots().then(function (value) {
chai.expect(value).to.be.an('array');
chai.expect(value).to.be.empty;
});
});
it('Get channels list', function () {
2019-02-08 13:31:38 +03:00
nock('https://api.example.com/api/bot/v1').get('/channels').reply(200, [{
2019-02-06 14:54:15 +03:00
id: 1
}]);
retailcrm.getChannels().then(function (value) {
chai.expect(value).to.be.an('array');
chai.expect(value).to.be.not.empty;
});
});
it('Get empty channels list', function () {
2019-02-08 13:31:38 +03:00
nock('https://api.example.com/api/bot/v1').get('/channels').reply(200, []);
2019-02-06 14:54:15 +03:00
retailcrm.getChannels().then(function (value) {
chai.expect(value).to.be.an('array');
chai.expect(value).to.be.empty;
});
});
it('Get chats list', function () {
2019-02-08 13:31:38 +03:00
nock('https://api.example.com/api/bot/v1').get('/chats').reply(200, [{
2019-02-06 14:54:15 +03:00
author_id: 1,
id: 1
}]);
retailcrm.getChats().then(function (value) {
chai.expect(value).to.be.an('array');
chai.expect(value).to.be.not.empty;
});
});
it('Get empty chats list', function () {
2019-02-08 13:31:38 +03:00
nock('https://api.example.com/api/bot/v1').get('/chats').reply(200, []);
2019-02-06 14:54:15 +03:00
retailcrm.getChats().then(function (value) {
chai.expect(value).to.be.an('array');
chai.expect(value).to.be.empty;
});
});
it('Get customers list', function () {
2019-02-08 13:31:38 +03:00
nock('https://api.example.com/api/bot/v1').get('/customers').reply(200, [{
2019-02-06 14:54:15 +03:00
external_id: 1,
channel_id: 1,
id: 1
}]);
retailcrm.getCustomers().then(function (value) {
chai.expect(value).to.be.an('array');
chai.expect(value).to.be.not.empty;
});
});
it('Get empty customers list', function () {
2019-02-08 13:31:38 +03:00
nock('https://api.example.com/api/bot/v1').get('/customers').reply(200, []);
2019-02-06 14:54:15 +03:00
retailcrm.getCustomers().then(function (value) {
chai.expect(value).to.be.an('array');
chai.expect(value).to.be.empty;
});
});
it('Get dialogs list', function () {
2019-02-08 13:31:38 +03:00
nock('https://api.example.com/api/bot/v1').get('/dialogs').reply(200, [{
2019-02-06 14:54:15 +03:00
begin_message_id: 1,
id: 1
}]);
retailcrm.getDialogs().then(function (value) {
chai.expect(value).to.be.an('array');
chai.expect(value).to.be.not.empty;
});
});
it('Get empty dialogs list', function () {
2019-02-08 13:31:38 +03:00
nock('https://api.example.com/api/bot/v1').get('/dialogs').reply(200, []);
2019-02-06 14:54:15 +03:00
retailcrm.getDialogs().then(function (value) {
chai.expect(value).to.be.an('array');
chai.expect(value).to.be.empty;
});
});
it('Get members list', function () {
2019-02-08 13:31:38 +03:00
nock('https://api.example.com/api/bot/v1').get('/members').reply(200, [{
2019-02-06 14:54:15 +03:00
id: 1
}]);
retailcrm.getMembers().then(function (value) {
chai.expect(value).to.be.an('array');
chai.expect(value).to.be.not.empty;
});
});
it('Get empty members list', function () {
2019-02-08 13:31:38 +03:00
nock('https://api.example.com/api/bot/v1').get('/members').reply(200, []);
2019-02-06 14:54:15 +03:00
retailcrm.getMembers().then(function (value) {
chai.expect(value).to.be.an('array');
chai.expect(value).to.be.empty;
});
});
it('Assign dialog', function () {
2019-02-08 13:31:38 +03:00
nock('https://api.example.com/api/bot/v1').patch('/dialogs/1/assign').reply(200, {
2019-02-06 14:54:15 +03:00
is_reassign: true,
responsible: {
id: 1
}
});
retailcrm.assignDialog(1, {
manager_id: 1
}).then(function (value) {
chai.expect(value).to.be.an('object');
});
});
it('Assign dialog incorrect', function () {
chai.expect(retailcrm.assignDialog.bind(retailcrm)).to.throw('Body is not be empty');
});
it('Close dialog', function () {
2019-02-08 13:31:38 +03:00
nock('https://api.example.com/api/bot/v1').delete('/dialogs/1/close').reply(200, {});
2019-02-06 14:54:15 +03:00
retailcrm.closeDialog(1).then(function (value) {
chai.expect(value).to.be.empty;
});
});
it('Close dialog incorrect', function () {
chai.expect(retailcrm.closeDialog.bind(retailcrm)).to.throw('dialog_id is required');
});
it('Send message', function () {
2019-02-08 13:31:38 +03:00
nock('https://api.example.com/api/bot/v1').post('/messages', {
2019-02-06 14:54:15 +03:00
chat_id: 1,
scope: 'public',
type: 'text',
2019-02-08 13:31:38 +03:00
content: 'tests message'
2019-02-06 14:54:15 +03:00
}).reply(200, {
message_id: 1
});
retailcrm.sendMessage({
chat_id: 1,
scope: 'public',
type: 'text',
2019-02-08 13:31:38 +03:00
content: 'tests message'
2019-02-06 14:54:15 +03:00
}).then(function (value) {
chai.expect(value).to.be.an('object');
chai.expect(value).to.be.not.empty;
});
});
it('Send message incorrect', function () {
chai.expect(retailcrm.sendMessage.bind(retailcrm)).to.throw('Body is not be empty');
});
it('Get messages', function() {
2019-02-08 13:31:38 +03:00
nock('https://api.example.com/api/bot/v1').get('/messages').reply(200, [{
2019-02-06 14:54:15 +03:00
id: 1,
chat_id: 1,
from: {
id: 1
}
}]);
retailcrm.getMessages().then(function (value) {
chai.expect(value).to.be.an('array');
chai.expect(value).to.be.not.empty;
});
});
it('Get empty messages', function () {
2019-02-08 13:31:38 +03:00
nock('https://api.example.com/api/bot/v1').get('/messages').reply(200, []);
2019-02-06 14:54:15 +03:00
retailcrm.getMessages().then(function (value) {
chai.expect(value).to.be.an('array');
chai.expect(value).to.be.empty;
});
});
it('Delete message', function () {
2019-02-08 13:31:38 +03:00
nock('https://api.example.com/api/bot/v1').delete('/messages/1').reply(200, {});
2019-02-06 14:54:15 +03:00
retailcrm.deleteMessage(1).then(function (value) {
chai.expect(value).to.be.empty;
});
});
it('Delete message incorrect', function () {
chai.expect(retailcrm.deleteMessage.bind(retailcrm)).to.throw('message_id is required');
});
it('Edit message', function () {
2019-02-08 13:31:38 +03:00
nock('https://api.example.com/api/bot/v1').patch('/messages/1', {
content: 'tests message'
2019-02-06 14:54:15 +03:00
}).reply(200, {});
retailcrm.editMessage(1, {
2019-02-08 13:31:38 +03:00
content: 'tests message'
2019-02-06 14:54:15 +03:00
}).then(function (value) {
chai.expect(value).to.be.empty;
});
});
it('Edit message incorrect', function () {
chai.expect(retailcrm.editMessage.bind(retailcrm)).to.throw('Body is not be empty');
});
it('Get commands', function () {
2019-02-08 13:31:38 +03:00
nock('https://api.example.com/api/bot/v1').get('/my/commands').reply(200, [{
2019-02-06 14:54:15 +03:00
id: 1,
name: 'Command name'
}]);
retailcrm.getCommands().then(function (value) {
chai.expect(value).to.be.an('array');
chai.expect(value).to.be.not.empty;
});
});
it('Get empty commands', function () {
2019-02-08 13:31:38 +03:00
nock('https://api.example.com/api/bot/v1').get('/my/commands').reply(200, []);
2019-02-06 14:54:15 +03:00
retailcrm.getCommands().then(function (value) {
chai.expect(value).to.be.an('array');
chai.expect(value).to.be.empty;
});
});
it('Edit command', function () {
2019-02-08 13:31:38 +03:00
nock('https://api.example.com/api/bot/v1').put('/my/commands/command', {
2019-02-06 14:54:15 +03:00
description: 'Desc',
name: 'name'
}).reply(200, {});
retailcrm.editCommand('command', {
description: 'Desc',
name: 'name'
}).then(function (value) {
chai.expect(value).to.be.empty;
});
});
it('Edit command incorrect', function () {
chai.expect(retailcrm.editCommand.bind(retailcrm, 'command')).to.throw('Body is not be empty');
chai.expect(retailcrm.editCommand.bind(retailcrm)).to.throw('Parameter command name is required');
});
it('Delete command', function () {
2019-02-08 13:31:38 +03:00
nock('https://api.example.com/api/bot/v1').delete('/my/commands/command').reply(200, {});
2019-02-06 14:54:15 +03:00
retailcrm.deleteCommand('command').then(function (value) {
chai.expect(value).to.be.empty;
});
});
it('Delete command incorrect', function () {
chai.expect(retailcrm.deleteCommand.bind(retailcrm)).to.throw('command_name is required');
});
it('Update bot info', function () {
2019-02-08 13:31:38 +03:00
nock('https://api.example.com/api/bot/v1').patch('/my/info', {
avatar_url: 'http://tests.ru/avatar.png',
2019-02-06 14:54:15 +03:00
name: 'Bot'
}).reply(200, {});
retailcrm.info({
2019-02-08 13:31:38 +03:00
avatar_url: 'http://tests.ru/avatar.png',
2019-02-06 14:54:15 +03:00
name: 'Bot'
}).then(function (value) {
chai.expect(value).to.be.empty;
});
});
it('Update bot info incorrect', function () {
chai.expect(retailcrm.info.bind(retailcrm)).to.throw('Body is not be empty');
});
it('Get users', function () {
2019-02-08 13:31:38 +03:00
nock('https://api.example.com/api/bot/v1').get('/users').reply(200, [{
2019-02-06 14:54:15 +03:00
id: 1,
name: 'Username'
}]);
retailcrm.getUsers().then(function (value) {
chai.expect(value).to.be.an('array');
chai.expect(value).to.be.not.empty;
});
});
it('Get empty users', function () {
2019-02-08 13:31:38 +03:00
nock('https://api.example.com/api/bot/v1').get('/users').reply(200, []);
2019-02-06 14:54:15 +03:00
retailcrm.getUsers().then(function (value) {
chai.expect(value).to.be.an('array');
chai.expect(value).to.be.empty;
});
});
it('Get websocket url', function () {
var url = retailcrm.getWebsocketUrl(['message_new', 'message_updated']);
2019-02-08 13:31:38 +03:00
var expected = 'wss://api.example.com/api/bot/v1/ws?events=message_new,message_updated';
2019-02-06 14:54:15 +03:00
chai.assert.equal(url, expected);
});
it('Get websocket url incorrect', function () {
chai.expect(retailcrm.getWebsocketUrl.bind(retailcrm)).to.throw('Events is required');
});
});