1
0
mirror of synced 2024-11-21 20:16:03 +03:00
mg-bot-api-client-js/tests/test_v1_client.js

444 lines
14 KiB
JavaScript
Raw Permalink Normal View History

2019-03-01 13:46:30 +03:00
import nock from 'nock'
import chai from 'chai'
import https from 'https'
2019-03-06 14:03:41 +03:00
import MgBotApiClient from '../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();
});
2019-03-06 14:03:41 +03:00
const api = new MgBotApiClient({
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-03-01 13:46:30 +03:00
}).client;
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
}]);
2019-03-06 14:03:41 +03:00
api.getBots().then(function (value) {
2019-02-06 14:54:15 +03:00
chai.expect(value).to.be.an('array');
chai.expect(value).to.be.not.empty;
});
});
it('Get empty bots list', function () {
nock('https://api.example.com/api/bot/v1').get('/bots?id=1').reply(200, []);
2019-02-06 14:54:15 +03:00
api.getBots({id: 1}).then(function (value) {
2019-02-06 14:54:15 +03:00
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
}]);
2019-03-06 14:03:41 +03:00
api.getChannels().then(function (value) {
2019-02-06 14:54:15 +03:00
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
2019-03-06 14:03:41 +03:00
api.getChannels().then(function (value) {
2019-02-06 14:54:15 +03:00
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
}]);
2019-03-06 14:03:41 +03:00
api.getChats().then(function (value) {
2019-02-06 14:54:15 +03:00
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
2019-03-06 14:03:41 +03:00
api.getChats().then(function (value) {
2019-02-06 14:54:15 +03:00
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
}]);
2019-03-06 14:03:41 +03:00
api.getCustomers().then(function (value) {
2019-02-06 14:54:15 +03:00
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
2019-03-06 14:03:41 +03:00
api.getCustomers().then(function (value) {
2019-02-06 14:54:15 +03:00
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
}]);
2019-03-06 14:03:41 +03:00
api.getDialogs().then(function (value) {
2019-02-06 14:54:15 +03:00
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
2019-03-06 14:03:41 +03:00
api.getDialogs().then(function (value) {
2019-02-06 14:54:15 +03:00
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
}]);
2019-03-06 14:03:41 +03:00
api.getMembers().then(function (value) {
2019-02-06 14:54:15 +03:00
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
2019-03-06 14:03:41 +03:00
api.getMembers().then(function (value) {
2019-02-06 14:54:15 +03:00
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
}
});
2019-03-06 14:03:41 +03:00
api.assignDialog(1, {
2019-02-06 14:54:15 +03:00
manager_id: 1
}).then(function (value) {
chai.expect(value).to.be.an('object');
});
});
it('Assign dialog incorrect', function () {
2019-03-06 14:03:41 +03:00
chai.expect(api.assignDialog.bind(api)).to.throw('Parameter `dialog_id` is required');
2019-02-06 14:54:15 +03:00
});
it('Unassign dialog', function () {
nock('https://api.example.com/api/bot/v1').patch('/dialogs/1/unassign').reply(200, {
previous_responsible: {type: 'user', id: 1, assigned_at: '2019-01-22T11:50:13Z'}
});
api.unassignDialog(1).then(function (value) {
chai.expect(value).to.be.an('object');
});
});
it('Unassign dialog incorrect', function () {
chai.expect(api.unassignDialog.bind(api)).to.throw('Parameter `dialog_id` is required');
});
2019-02-06 14:54:15 +03:00
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
2019-03-06 14:03:41 +03:00
api.closeDialog(1).then(function (value) {
2019-02-06 14:54:15 +03:00
chai.expect(value).to.be.empty;
});
});
it('Close dialog incorrect', function () {
2019-03-06 14:03:41 +03:00
chai.expect(api.closeDialog.bind(api)).to.throw('Parameter `dialog_id` is required');
2019-02-06 14:54:15 +03:00
});
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
});
2019-03-06 14:03:41 +03:00
api.sendMessage({
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
}).then(function (value) {
chai.expect(value).to.be.an('object');
chai.expect(value).to.be.not.empty;
});
});
it('Send message incorrect', function () {
2019-03-06 14:03:41 +03:00
chai.expect(api.sendMessage.bind(api)).to.throw('Body is not be empty');
2019-02-06 14:54:15 +03:00
});
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
}
}]);
2019-03-06 14:03:41 +03:00
api.getMessages().then(function (value) {
2019-02-06 14:54:15 +03:00
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
2019-03-06 14:03:41 +03:00
api.getMessages().then(function (value) {
2019-02-06 14:54:15 +03:00
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
2019-03-06 14:03:41 +03:00
api.deleteMessage(1).then(function (value) {
2019-02-06 14:54:15 +03:00
chai.expect(value).to.be.empty;
});
});
it('Delete message incorrect', function () {
2019-03-06 14:03:41 +03:00
chai.expect(api.deleteMessage.bind(api)).to.throw('Parameter `message_id` is required');
2019-02-06 14:54:15 +03:00
});
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, {});
2019-03-06 14:03:41 +03:00
api.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 () {
2019-03-06 14:03:41 +03:00
chai.expect(api.editMessage.bind(api)).to.throw('Parameter `message_id` is required');
2019-02-06 14:54:15 +03:00
});
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'
}]);
2019-03-06 14:03:41 +03:00
api.getCommands().then(function (value) {
2019-02-06 14:54:15 +03:00
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
2019-03-06 14:03:41 +03:00
api.getCommands().then(function (value) {
2019-02-06 14:54:15 +03:00
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, {});
2019-03-06 14:03:41 +03:00
api.editCommand('command', {
2019-02-06 14:54:15 +03:00
description: 'Desc',
name: 'name'
}).then(function (value) {
chai.expect(value).to.be.empty;
});
});
it('Edit command incorrect', function () {
2019-03-06 14:03:41 +03:00
chai.expect(api.editCommand.bind(api, 'command')).to.throw('Body is not be empty');
chai.expect(api.editCommand.bind(api)).to.throw('Parameter `command_name` is required');
2019-02-06 14:54:15 +03:00
});
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
2019-03-06 14:03:41 +03:00
api.deleteCommand('command').then(function (value) {
2019-02-06 14:54:15 +03:00
chai.expect(value).to.be.empty;
});
});
it('Delete command incorrect', function () {
2019-03-06 14:03:41 +03:00
chai.expect(api.deleteCommand.bind(api)).to.throw('Parameter `command_name` is required');
2019-02-06 14:54:15 +03:00
});
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, {});
2019-03-06 14:03:41 +03:00
api.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 () {
2019-03-06 14:03:41 +03:00
chai.expect(api.info.bind(api)).to.throw('Body is not be empty');
2019-02-06 14:54:15 +03:00
});
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'
}]);
2019-03-06 14:03:41 +03:00
api.getUsers().then(function (value) {
2019-02-06 14:54:15 +03:00
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
2019-03-06 14:03:41 +03:00
api.getUsers().then(function (value) {
2019-02-06 14:54:15 +03:00
chai.expect(value).to.be.an('array');
chai.expect(value).to.be.empty;
});
});
it('Get file', function () {
nock('https://api.example.com/api/bot/v1').get('/files/1').reply(200, {
id: '1',
size: 100,
type: 'image',
url: 'https://file.url'
});
api.getFile('1').then(function (value) {
chai.expect(value).to.be.an('object');
chai.expect(value.id).to.be.equal('1');
chai.expect(value.size).to.be.equal(100);
chai.expect(value.type).to.be.equal('image');
chai.expect(value.url).to.be.equal('https://file.url');
});
});
it('Get file incorrect', function () {
chai.expect(api.getFile.bind(api)).to.throw('Parameter `file_id` is required');
});
it('File upload', function () {
const options = {
host: 'via.placeholder.com',
path: '/300'
};
const req = https.get(options, function (res) {
let data = Buffer.from('', 'binary');
res.on('data', function (chunk) {
data = Buffer.concat([data, Buffer.from(chunk, 'binary')])
});
res.on('end', function () {
nock('https://api.example.com/api/bot/v1').post('/files/upload', data).reply(200, {
id: '1',
size: 1132,
type: 'image'
});
api.filesUpload(data).then(function (value) {
chai.expect(value).to.be.an('object');
chai.expect(value.id).to.be.equal('1');
chai.expect(value.size).to.be.equal(1132);
chai.expect(value.type).to.be.equal('image');
});
});
});
req.end();
});
it('File upload incorrect', function () {
chai.expect(api.filesUpload.bind(api)).to.throw('Body is not be empty');
});
it('File upload by url', function () {
nock('https://api.example.com/api/bot/v1').post('/files/upload_by_url', {url: 'https://fileurl.com'}).reply(200, {
id: '123',
size: 1132,
type: 'image',
url: 'https://file.url'
});
api.filesUploadByUrl('https://fileurl.com').then(function (value) {
chai.expect(value).to.be.an('object');
chai.expect(value.id).to.be.equal('123');
chai.expect(value.size).to.be.equal(1132);
chai.expect(value.type).to.be.equal('image');
chai.expect(value.url).to.be.equal('https://file.url');
});
});
it('File upload by url incorrect', function () {
chai.expect(api.filesUploadByUrl.bind(api)).to.throw('Parameter `url` is required');
});
2019-03-01 13:46:30 +03:00
it('Get websocket data', function () {
2019-03-06 14:03:41 +03:00
const wsData = api.getWebsocketData([MgBotApiClient.types().wsMessageNew, MgBotApiClient.types().wsMessageUpdated]);
2019-03-01 13:46:30 +03:00
const expectedUrl = 'wss://api.example.com/api/bot/v1/ws?events=message_new,message_updated';
const expectedHeaders = {'X-Bot-Token': 'test_token'};
2019-02-06 14:54:15 +03:00
2019-03-01 13:46:30 +03:00
chai.assert.equal(wsData.get('url'), expectedUrl);
chai.assert.equal(wsData.get('headers')["X-Bot-Token"], expectedHeaders["X-Bot-Token"]);
2019-02-06 14:54:15 +03:00
});
it('Get websocket url incorrect', function () {
2019-03-06 14:03:41 +03:00
chai.expect(api.getWebsocketData.bind(api)).to.throw('Events is required');
2019-02-06 14:54:15 +03:00
});
});