From 2ca5886f453bc075725273c09cbb965c4c0c662c Mon Sep 17 00:00:00 2001 From: Rafael Garbin Date: Sat, 12 Aug 2017 23:55:26 -0300 Subject: [PATCH 1/7] Added test to send msg using flash msg. --- test/api.test.js | 83 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/test/api.test.js b/test/api.test.js index 00d5632..d6609c5 100644 --- a/test/api.test.js +++ b/test/api.test.js @@ -211,6 +211,89 @@ describe('API', () => { }); }).timeout(10000); + it('should sendSMS(Flash Msg) function return success', (done) => { + const payload = { + sendSmsRequest: { + from: 'Zenvia API', + to: phoneNumber, + schedule: null, + msg: 'Hello from Zenvia API from NodeJS!!!', + callbackOption: 'NONE', + id: parseInt(Math.random() * 10000).toString(), + aggregateId: '777', + flashSms: true + }, + }; + + zapi + .sendSMS(payload) + .then((res) => { + expect(JSON.stringify(res)) + .to.equal(JSON.stringify({ + statusCode: 200, + body: { + sendSmsResponse: { + statusCode: '00', + statusDescription: 'Ok', + detailCode: '000', + detailDescription: 'Message Sent', + }, + }, + } + )); + + done(); + }) + .catch((err) => { + console.log(err); + done(); + }); + }).timeout(10000); + +it('should sendSMS Multiple(Flash Msg) function return success', (done) => { + const payload = { + sendSmsMultiRequest: { + aggregateId: '777', + sendSmsRequestList: [{ + from: "remetente", + to: phoneNumber, + msg: "uma mensagem", + callbackOption: "NONE", + flashSms: true + }], + }, + }; + + zapi + .sendSMS(payload) + .then((res) => { + expect(JSON.stringify(res)) + .to.equal(JSON.stringify({ + statusCode: 200, + body: { + sendSmsMultiResponse: { + sendSmsResponseList: [{ + statusCode: "00", + statusDescription: "Ok", + detailCode: "000", + detailDescription: "Message Sent", + parts: [{ + partId: "b940441e-22c4-4865-8db6-92cecb4be4ef", + order: 1 + }], + }], + }, + }, + } + )); + done(); + }) + .catch((err) => { + console.log(err); + done(); + }); + }).timeout(10000); + it('should cancelScheduledSMS function return success', (done) => { const smsId = parseInt(Math.random() * 10000).toString(); From 4c02cc0fc64248fcf61d62a103ee07e7af06f615 Mon Sep 17 00:00:00 2001 From: Rafael Garbin Date: Sat, 12 Aug 2017 23:58:11 -0300 Subject: [PATCH 2/7] Code review --- test/api.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/api.test.js b/test/api.test.js index d6609c5..b61b536 100644 --- a/test/api.test.js +++ b/test/api.test.js @@ -217,7 +217,7 @@ describe('API', () => { from: 'Zenvia API', to: phoneNumber, schedule: null, - msg: 'Hello from Zenvia API from NodeJS!!!', + msg: 'Hello from Zenvia API from NodeJS - Flash Msg', callbackOption: 'NONE', id: parseInt(Math.random() * 10000).toString(), aggregateId: '777', @@ -257,7 +257,7 @@ it('should sendSMS Multiple(Flash Msg) function return success', (done) => { sendSmsRequestList: [{ from: "remetente", to: phoneNumber, - msg: "uma mensagem", + msg: 'Hello from Zenvia API from NodeJS - Flash Msg Multiple', callbackOption: "NONE", flashSms: true }], From aa01aee85370b8c32e2684033d0a132977b04a30 Mon Sep 17 00:00:00 2001 From: Rafael Garbin Date: Tue, 15 Aug 2017 11:50:35 -0300 Subject: [PATCH 3/7] Code review --- test/api.test.js | 283 ++++++++++++++++++++--------------------------- 1 file changed, 123 insertions(+), 160 deletions(-) diff --git a/test/api.test.js b/test/api.test.js index b61b536..105637f 100644 --- a/test/api.test.js +++ b/test/api.test.js @@ -14,7 +14,6 @@ describe('API', () => { it('should set credentials', () => { zapi.setCredentials('abc', '123'); - expect(JSON.stringify(zapi.CREDENTIALS)) .to .equal( @@ -111,65 +110,56 @@ describe('API', () => { }); }).timeout(10000); - // TODO: refactor this it('should getSMSStatus function return catch 401', (done) => { zapi.setCredentials('abc', '123'); - - zapi - .getSMSStatus(0) - .then((res) => {}) - .catch((err) => { - expect(JSON.stringify(err)) - .to - .equal(JSON.stringify({ statusCode: 400, body: { exception: { message: 'Cannot consume content type' } } })); - - done(); - }); + zapi.getSMSStatus(0) + .then((res) => {}) + .catch((err) => { + expect(JSON.stringify(err)) + .to + .equal(JSON.stringify({ statusCode: 400, body: { exception: { message: 'Cannot consume content type' } } })); + + done(); + }); }).timeout(10000); it('should getSMSReceivedList function return catch 401', (done) => { zapi.setCredentials('abc', '123'); - - zapi - .getSMSReceivedList() - .then((res) => {}) - .catch((err) => { - expect(JSON.stringify(err)) - .to - .equal(JSON.stringify({ statusCode: 401, body: 'Bad credentials' })); - - done(); - }); + zapi.getSMSReceivedList() + .then((res) => {}) + .catch((err) => { + expect(JSON.stringify(err)) + .to + .equal(JSON.stringify({ statusCode: 401, body: 'Bad credentials' })); + + done(); + }); }).timeout(10000); it('should getSMSReceivedListSearch function return catch 401', (done) => { zapi.setCredentials('abc', '123'); - - zapi - .getSMSReceivedListSearch(Date.now(), Date.now()) - .then((res) => {}) - .catch((err) => { - expect(JSON.stringify(err)) - .to - .equal(JSON.stringify({ statusCode: 401, body: 'Bad credentials' })); - - done(); - }); + zapi.getSMSReceivedListSearch(Date.now(), Date.now()) + .then((res) => {}) + .catch((err) => { + expect(JSON.stringify(err)) + .to + .equal(JSON.stringify({ statusCode: 401, body: 'Bad credentials' })); + + done(); + }); }).timeout(10000); it('should cancelScheduledSMS function return catch 401', (done) => { zapi.setCredentials('abc', '123'); - - zapi - .cancelScheduledSMS(Date.now(), Date.now()) - .then((res) => {}) - .catch((err) => { - expect(JSON.stringify(err)) - .to - .equal(JSON.stringify({ statusCode: 401, body: 'Bad credentials' })); - - done(); - }); + zapi.cancelScheduledSMS(Date.now(), Date.now()) + .then((res) => {}) + .catch((err) => { + expect(JSON.stringify(err)) + .to + .equal(JSON.stringify({ statusCode: 401, body: 'Bad credentials' })); + + done(); + }); }).timeout(10000); @@ -186,29 +176,28 @@ describe('API', () => { }, }; - zapi - .sendSMS(payload) - .then((res) => { - expect(JSON.stringify(res)) - .to.equal(JSON.stringify({ - statusCode: 200, - body: { - sendSmsResponse: { - statusCode: '00', - statusDescription: 'Ok', - detailCode: '000', - detailDescription: 'Message Sent', - }, - }, - } - )); - - done(); - }) - .catch((err) => { - console.log(err); - done(); - }); + zapi.sendSMS(payload) + .then((res) => { + expect(JSON.stringify(res)) + .to.equal(JSON.stringify({ + statusCode: 200, + body: { + sendSmsResponse: { + statusCode: '00', + statusDescription: 'Ok', + detailCode: '000', + detailDescription: 'Message Sent', + }, + }, + } + )); + + done(); + }) + .catch((err) => { + console.log(err, "sendSMS Simple SMS"); + done(); + }); }).timeout(10000); it('should sendSMS(Flash Msg) function return success', (done) => { @@ -220,78 +209,55 @@ describe('API', () => { msg: 'Hello from Zenvia API from NodeJS - Flash Msg', callbackOption: 'NONE', id: parseInt(Math.random() * 10000).toString(), - aggregateId: '777', + aggregateId: 777, flashSms: true - }, + } }; - zapi - .sendSMS(payload) - .then((res) => { - expect(JSON.stringify(res)) - .to.equal(JSON.stringify({ - statusCode: 200, - body: { - sendSmsResponse: { - statusCode: '00', - statusDescription: 'Ok', - detailCode: '000', - detailDescription: 'Message Sent', - }, - }, - } - )); - - done(); - }) - .catch((err) => { - console.log(err); - done(); - }); + zapi.sendSMS(payload) + .then((res) => { + const response = res.body.sendSmsResponse; + expect(res.statusCode).to.equal(200); + expect(response.statusCode).to.equal('00'); + expect(response.statusDescription).to.equal('Ok'); + expect(response.detailCode).to.equal('000'); + expect(response.detailDescription).to.equal('Message Sent'); + done(); + }) + .catch((err) => { + console.log(err, 'sendSMS Simple(Flash Msg) - SMS'); + done(); + }); }).timeout(10000); -it('should sendSMS Multiple(Flash Msg) function return success', (done) => { + it('should sendSMS Multiple(Flash Msg) function return success', (done) => { const payload = { sendSmsMultiRequest: { - aggregateId: '777', + aggregateId: 777, sendSmsRequestList: [{ - from: "remetente", - to: phoneNumber, - msg: 'Hello from Zenvia API from NodeJS - Flash Msg Multiple', - callbackOption: "NONE", - flashSms: true - }], - }, + from: 'remetente', + to: phoneNumber, + msg: 'Hello from Zenvia API from NodeJS - Flash Msg Multiple', + callbackOption: 'NONE', + flashSms: true + }] + } }; - zapi - .sendSMS(payload) - .then((res) => { - expect(JSON.stringify(res)) - .to.equal(JSON.stringify({ - statusCode: 200, - body: { - sendSmsMultiResponse: { - sendSmsResponseList: [{ - statusCode: "00", - statusDescription: "Ok", - detailCode: "000", - detailDescription: "Message Sent", - parts: [{ - partId: "b940441e-22c4-4865-8db6-92cecb4be4ef", - order: 1 - }], - }], - }, - }, - } - )); - done(); - }) - .catch((err) => { - console.log(err); - done(); - }); + zapi.sendSMS(payload) + .then((res) => { + const response = res.body.sendSmsMultiResponse.sendSmsResponseList[0]; + expect(res.statusCode).to.equal(200); + expect(response.statusCode).to.equal('00'); + expect(response.statusDescription).to.equal('Ok'); + expect(response.detailCode).to.equal('000'); + expect(response.detailDescription).to.equal('Message Sent'); + done(); + }) + .catch((err) => { + console.log(res, "sendSMS Multiple(Flash Msg)") + done(); + }); }).timeout(10000); it('should cancelScheduledSMS function return success', (done) => { @@ -309,34 +275,31 @@ it('should sendSMS Multiple(Flash Msg) function return success', (done) => { }, }; - zapi - .sendSMS(payload) - .then((res) => { - zapi - .cancelScheduledSMS(smsId) - .then(res => { - expect(JSON.stringify(res)) - .to.equal(JSON.stringify({ - statusCode: 200, - body: { - cancelSmsResp: { - statusCode: '09', - statusDescription: 'Blocked', - detailCode: '002', - detailDescription: 'Message successfully canceled', - }, - }, - } - )); - - done(); - }); - }) - .catch(err => { - done(); - console.log(err); - }); + zapi.sendSMS(payload) + .then((res) => { + zapi + .cancelScheduledSMS(smsId) + .then(res => { + expect(JSON.stringify(res)) + .to.equal(JSON.stringify({ + statusCode: 200, + body: { + cancelSmsResp: { + statusCode: '09', + statusDescription: 'Blocked', + detailCode: '002', + detailDescription: 'Message successfully canceled', + }, + }, + } + )); + + done(); + }); + }) + .catch(err => { + done(); + console.log(err, "cancelScheduledSMS"); + }); }).timeout(10000); - - // TODO: make success tests for getSMSReceivedList and getSMSReceivedListSearch }); From 8efbfc7be3e7bd83ee897fdab72046f57493d989 Mon Sep 17 00:00:00 2001 From: Rafael Garbin Date: Wed, 16 Aug 2017 10:51:45 -0300 Subject: [PATCH 4/7] Added dotenv because is helpful to run fast tests --- .env-dist | 3 +++ package.json | 1 + test/api.test.js | 8 +++++--- 3 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 .env-dist diff --git a/.env-dist b/.env-dist new file mode 100644 index 0000000..d527bb7 --- /dev/null +++ b/.env-dist @@ -0,0 +1,3 @@ +ZENVIA_PHONENUMBER='' +ZENVIA_ACCOUNT='' +ZENVIA_PASSWORD='' diff --git a/package.json b/package.json index 928c46e..1d32079 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "devDependencies": { "chai": "^4.1.0", "coveralls": "^2.13.1", + "dotenv": "^4.0.0", "eslint": "^3.3.0", "eslint-config-airbnb": "^10.0.1", "eslint-plugin-import": "^1.13.0", diff --git a/test/api.test.js b/test/api.test.js index 105637f..74a8741 100644 --- a/test/api.test.js +++ b/test/api.test.js @@ -1,12 +1,14 @@ 'use strict'; +require('dotenv').config() + const expect = require('chai').expect; const zapi = require('../index').api; describe('API', () => { - const phoneNumber = process.env.zenvia_phonenumber; - const zenviaAccount = process.env.zenviaAccount; - const zenviaPassword = process.env.zenviaPassword; + const phoneNumber = process.env.ZENVIA_PHONENUMBER; + const zenviaAccount = process.env.ZENVIA_ACCOUNT; + const zenviaPassword = process.env.ZENVIA_PASSWORD; beforeEach(() => { zapi.setCredentials(zenviaAccount, zenviaPassword); From 466aa9df8bd3c7f7c4d3b81f7ac55066e277d976 Mon Sep 17 00:00:00 2001 From: Rafael Garbin Date: Sun, 20 Aug 2017 16:14:21 -0300 Subject: [PATCH 5/7] RFT to tests use by default zenvia apis mock on heroku --- lib/api.js | 4 + test/api.test.js | 202 ++++++++++++++++++++++++++--------------------- 2 files changed, 114 insertions(+), 92 deletions(-) diff --git a/lib/api.js b/lib/api.js index 195778c..ad192b6 100644 --- a/lib/api.js +++ b/lib/api.js @@ -37,6 +37,10 @@ module.exports = { password: null, }, + setHostName(url) { + this.ZENVIA_API.apiHostName = url; + }, + setCredentials(account, password) { this.CREDENTIALS.account = account; this.CREDENTIALS.password = password; diff --git a/test/api.test.js b/test/api.test.js index 74a8741..34daddd 100644 --- a/test/api.test.js +++ b/test/api.test.js @@ -2,16 +2,27 @@ require('dotenv').config() +const use_zenvia_apis_mock = true; const expect = require('chai').expect; const zapi = require('../index').api; describe('API', () => { - const phoneNumber = process.env.ZENVIA_PHONENUMBER; - const zenviaAccount = process.env.ZENVIA_ACCOUNT; - const zenviaPassword = process.env.ZENVIA_PASSWORD; + + const hostName = 'https://zenvia-apis-mock.herokuapp.com/api-rest'; + const phoneNumber = '5551999999200'; + const zenviaAccount = 'user'; + const zenviaPassword = 'pass'; + + if (!use_zenvia_apis_mock) { + hostName = 'https://api-rest.zenvia360.com.br'; + phoneNumber = process.env.ZENVIA_PHONENUMBER; + zenviaAccount = process.env.ZENVIA_ACCOUNT; + zenviaPassword = process.env.ZENVIA_PASSWORD; + } beforeEach(() => { zapi.setCredentials(zenviaAccount, zenviaPassword); + zapi.setHostName(hostName); }); it('should set credentials', () => { @@ -39,7 +50,7 @@ describe('API', () => { .to .equal( JSON.stringify({ - apiHostName: 'https://api-rest.zenvia360.com.br', + apiHostName: hostName, postSmsPath: '/services/send-sms', postSmsMultiple: '/services/send-sms-multiple', getSmsStatus: '/services/get-sms-status', @@ -70,107 +81,25 @@ describe('API', () => { it('should exist getSMSReceivedList function', () => { const fName = zapi.getSMSReceivedList.name; - - expect(fName) - .to - .equal('getSMSReceivedList'); + expect(fName).to.equal('getSMSReceivedList'); }); it('should exist getSMSReceivedListSearch function', () => { const fName = zapi.getSMSReceivedListSearch.name; - - expect(fName) - .to - .equal('getSMSReceivedListSearch'); + expect(fName).to.equal('getSMSReceivedListSearch'); }); it('should exist cancelScheduledSMS function', () => { const fName = zapi.cancelScheduledSMS.name; - - expect(fName) - .to - .equal('cancelScheduledSMS'); + expect(fName).to.equal('cancelScheduledSMS'); }); - - it('should sendSMS function return catch 401', (done) => { - zapi.setCredentials('abc', '123'); - - zapi - .sendSMS({}) - .then((res) => {}) - .catch((err) => { - expect(JSON.stringify(err)) - .to - .equal(JSON.stringify({ statusCode: 401, body: 'Bad credentials' })); - - done(); - }) - .catch(err => { - - - }); - }).timeout(10000); - - it('should getSMSStatus function return catch 401', (done) => { - zapi.setCredentials('abc', '123'); - zapi.getSMSStatus(0) - .then((res) => {}) - .catch((err) => { - expect(JSON.stringify(err)) - .to - .equal(JSON.stringify({ statusCode: 400, body: { exception: { message: 'Cannot consume content type' } } })); - - done(); - }); - }).timeout(10000); - - it('should getSMSReceivedList function return catch 401', (done) => { - zapi.setCredentials('abc', '123'); - zapi.getSMSReceivedList() - .then((res) => {}) - .catch((err) => { - expect(JSON.stringify(err)) - .to - .equal(JSON.stringify({ statusCode: 401, body: 'Bad credentials' })); - - done(); - }); - }).timeout(10000); - - it('should getSMSReceivedListSearch function return catch 401', (done) => { - zapi.setCredentials('abc', '123'); - zapi.getSMSReceivedListSearch(Date.now(), Date.now()) - .then((res) => {}) - .catch((err) => { - expect(JSON.stringify(err)) - .to - .equal(JSON.stringify({ statusCode: 401, body: 'Bad credentials' })); - - done(); - }); - }).timeout(10000); - - it('should cancelScheduledSMS function return catch 401', (done) => { - zapi.setCredentials('abc', '123'); - zapi.cancelScheduledSMS(Date.now(), Date.now()) - .then((res) => {}) - .catch((err) => { - expect(JSON.stringify(err)) - .to - .equal(JSON.stringify({ statusCode: 401, body: 'Bad credentials' })); - - done(); - }); - }).timeout(10000); - - it('should sendSMS function return success', (done) => { const payload = { sendSmsRequest: { from: 'Zenvia API', to: phoneNumber, - schedule: null, + schedule: '2017-08-09T14:00:00', msg: 'Hello from Zenvia API from NodeJS!!!', callbackOption: 'NONE', id: parseInt(Math.random() * 10000).toString(), @@ -202,12 +131,13 @@ describe('API', () => { }); }).timeout(10000); + it('should sendSMS(Flash Msg) function return success', (done) => { const payload = { sendSmsRequest: { from: 'Zenvia API', to: phoneNumber, - schedule: null, + schedule: '2017-08-09T14:00:00', msg: 'Hello from Zenvia API from NodeJS - Flash Msg', callbackOption: 'NONE', id: parseInt(Math.random() * 10000).toString(), @@ -241,7 +171,9 @@ describe('API', () => { to: phoneNumber, msg: 'Hello from Zenvia API from NodeJS - Flash Msg Multiple', callbackOption: 'NONE', - flashSms: true + flashSms: true, + schedule: '2017-08-09T14:00:00', + id: parseInt(Math.random() * 10000).toString() }] } }; @@ -262,6 +194,91 @@ describe('API', () => { }); }).timeout(10000); + it('should sendSMS function return catch 401', (done) => { + const payload = { + sendSmsRequest: { + from: 'Zenvia API', + to: phoneNumber, + schedule: '2017-08-09T14:00:00', + msg: 'Hello from Zenvia API from NodeJS - Flash Msg', + callbackOption: 'NONE', + id: parseInt(Math.random() * 10000).toString(), + aggregateId: 777, + flashSms: true + } + }; + + // Invalid credentials + zapi.setCredentials('abc', '123'); + zapi.sendSMS(payload) + .then((res) => {}) + .catch((err) => { + expect(JSON.stringify(err)) + .to + .equal(JSON.stringify({ statusCode: 401, body: 'Bad credentials' })); + + done(); + }) + .catch(err => { + console.log(res, "sendSMS function return catch 401") + done(); + }); + }).timeout(10000); + +/* + it('should getSMSStatus function return catch 401', (done) => { + zapi.setCredentials('abc', '123'); + zapi.getSMSStatus(0) + .then((res) => {}) + .catch((err) => { + expect(JSON.stringify(err)) + .to + .equal(JSON.stringify({ statusCode: 400, body: { exception: { message: 'Cannot consume content type' } } })); + + done(); + }); + }).timeout(10000); + + it('should getSMSReceivedList function return catch 401', (done) => { + zapi.setCredentials('abc', '123'); + zapi.getSMSReceivedList() + .then((res) => {}) + .catch((err) => { + expect(JSON.stringify(err)) + .to + .equal(JSON.stringify({ statusCode: 401, body: 'Bad credentials' })); + + done(); + }); + }).timeout(10000); + + it('should getSMSReceivedListSearch function return catch 401', (done) => { + zapi.setCredentials('abc', '123'); + zapi.getSMSReceivedListSearch(Date.now(), Date.now()) + .then((res) => {}) + .catch((err) => { + expect(JSON.stringify(err)) + .to + .equal(JSON.stringify({ statusCode: 401, body: 'Bad credentials' })); + + done(); + }); + }).timeout(10000); + + it('should cancelScheduledSMS function return catch 401', (done) => { + zapi.setCredentials('abc', '123'); + zapi.cancelScheduledSMS(Date.now(), Date.now()) + .then((res) => {}) + .catch((err) => { + expect(JSON.stringify(err)) + .to + .equal(JSON.stringify({ statusCode: 401, body: 'Bad credentials' })); + + done(); + }); + }).timeout(10000); + + it('should cancelScheduledSMS function return success', (done) => { const smsId = parseInt(Math.random() * 10000).toString(); @@ -304,4 +321,5 @@ describe('API', () => { console.log(err, "cancelScheduledSMS"); }); }).timeout(10000); + */ }); From 1f05faa5aafaf4ab40e882bc75720d2d1c076878 Mon Sep 17 00:00:00 2001 From: Rafael Garbin Date: Sun, 20 Aug 2017 19:38:29 -0300 Subject: [PATCH 6/7] RFT to use mock on get-sms-status --- lib/api.js | 2 +- test/api.test.js | 74 ++++++++++++++++++++++++++++++------------------ 2 files changed, 48 insertions(+), 28 deletions(-) diff --git a/lib/api.js b/lib/api.js index ad192b6..c5c420e 100644 --- a/lib/api.js +++ b/lib/api.js @@ -79,7 +79,7 @@ module.exports = { unirest .get(url) - .headers({ Accept: 'application/octet-stream', 'Content-Type': 'application/octet-stream' }) + .headers({ Accept: 'application/json', 'Content-Type': 'application/json' }) .auth({ user: this.CREDENTIALS.account, pass: this.CREDENTIALS.password, diff --git a/test/api.test.js b/test/api.test.js index 34daddd..bcd61da 100644 --- a/test/api.test.js +++ b/test/api.test.js @@ -8,12 +8,16 @@ const zapi = require('../index').api; describe('API', () => { + const smsId = 0; + const timeout = 15000; const hostName = 'https://zenvia-apis-mock.herokuapp.com/api-rest'; const phoneNumber = '5551999999200'; const zenviaAccount = 'user'; const zenviaPassword = 'pass'; - if (!use_zenvia_apis_mock) { + if (!use_zenvia_apis_mock) { + smsId = parseInt(Math.random() * 10000).toString(); + timeout = 10000; hostName = 'https://api-rest.zenvia360.com.br'; phoneNumber = process.env.ZENVIA_PHONENUMBER; zenviaAccount = process.env.ZENVIA_ACCOUNT; @@ -62,36 +66,24 @@ describe('API', () => { ); }); - it('should exist sendSMS function', () => { - const fName = zapi.sendSMS.name; - - expect(fName) - .to - .equal('sendSMS'); + expect(zapi.sendSMS.name).to.equal('sendSMS'); }); it('should exist getSMSStatus function', () => { - const fName = zapi.getSMSStatus.name; - - expect(fName) - .to - .equal('getSMSStatus'); + expect(zapi.getSMSStatus.name).to.equal('getSMSStatus'); }); it('should exist getSMSReceivedList function', () => { - const fName = zapi.getSMSReceivedList.name; - expect(fName).to.equal('getSMSReceivedList'); + expect(zapi.getSMSReceivedList.name).to.equal('getSMSReceivedList'); }); it('should exist getSMSReceivedListSearch function', () => { - const fName = zapi.getSMSReceivedListSearch.name; - expect(fName).to.equal('getSMSReceivedListSearch'); + expect(zapi.getSMSReceivedListSearch.name).to.equal('getSMSReceivedListSearch'); }); it('should exist cancelScheduledSMS function', () => { - const fName = zapi.cancelScheduledSMS.name; - expect(fName).to.equal('cancelScheduledSMS'); + expect(zapi.cancelScheduledSMS.name).to.equal('cancelScheduledSMS'); }); it('should sendSMS function return success', (done) => { @@ -129,7 +121,7 @@ describe('API', () => { console.log(err, "sendSMS Simple SMS"); done(); }); - }).timeout(10000); + }).timeout(timeout); it('should sendSMS(Flash Msg) function return success', (done) => { @@ -160,7 +152,7 @@ describe('API', () => { console.log(err, 'sendSMS Simple(Flash Msg) - SMS'); done(); }); - }).timeout(10000); + }).timeout(timeout); it('should sendSMS Multiple(Flash Msg) function return success', (done) => { const payload = { @@ -192,7 +184,7 @@ describe('API', () => { console.log(res, "sendSMS Multiple(Flash Msg)") done(); }); - }).timeout(10000); + }).timeout(timeout); it('should sendSMS function return catch 401', (done) => { const payload = { @@ -208,7 +200,7 @@ describe('API', () => { } }; - // Invalid credentials + // Setting invalid credentials zapi.setCredentials('abc', '123'); zapi.sendSMS(payload) .then((res) => {}) @@ -223,22 +215,50 @@ describe('API', () => { console.log(res, "sendSMS function return catch 401") done(); }); - }).timeout(10000); + }).timeout(timeout); -/* it('should getSMSStatus function return catch 401', (done) => { zapi.setCredentials('abc', '123'); - zapi.getSMSStatus(0) + zapi.getSMSStatus(smsId) .then((res) => {}) .catch((err) => { expect(JSON.stringify(err)) .to - .equal(JSON.stringify({ statusCode: 400, body: { exception: { message: 'Cannot consume content type' } } })); + .equal(JSON.stringify({ statusCode: 401, body: 'Bad credentials' })); + done(); + }); + }).timeout(timeout); + + it('should getSMSStatus function return success', (done) => { + zapi.getSMSStatus(smsId) + .then((res) => { + expect(JSON.stringify(res)) + .to.equal(JSON.stringify({ + statusCode: 200, + body: { + getSmsStatusResp: { + id: '0', + received: '2017-08-20T18:56:57', + shortcode: null, + mobileOperatorName: 'vivo', + statusCode: '02', + statusDescription: 'Sent', + detailCode: '133', + detailDescription: 'Message content in analysis' + }, + }, + } + )); done(); + }) + .catch((err) => { + console.log(err, "getSMSStatus function return success"); + done(); }); - }).timeout(10000); + }).timeout(timeout); +/* it('should getSMSReceivedList function return catch 401', (done) => { zapi.setCredentials('abc', '123'); zapi.getSMSReceivedList() From 5683a4113bce31d325d0ac250d976ddd33d79627 Mon Sep 17 00:00:00 2001 From: Rafael Garbin Date: Sun, 20 Aug 2017 21:24:01 -0300 Subject: [PATCH 7/7] RFT to cancel-sms endpoint use zenvia-apis-mock --- test/api.test.js | 61 ++++++++++++++---------------------------------- 1 file changed, 17 insertions(+), 44 deletions(-) diff --git a/test/api.test.js b/test/api.test.js index bcd61da..bc6eaa2 100644 --- a/test/api.test.js +++ b/test/api.test.js @@ -258,6 +258,23 @@ describe('API', () => { }); }).timeout(timeout); + it('should cancelScheduledSMS function return success', (done) => { + zapi.cancelScheduledSMS(smsId) + .then(res => { + const response = res.body.cancelSmsResp; + expect(res.statusCode).to.equal(200); + expect(response.statusCode).to.equal('09'); + expect(response.statusDescription).to.equal('Blocked'); + expect(response.detailCode).to.equal('002'); + expect(response.detailDescription).to.equal('Message successfully canceled'); + done(); + }) + .catch(err => { + done(); + console.log(err, "cancelScheduledSMS"); + }); + }).timeout(timeout); + /* it('should getSMSReceivedList function return catch 401', (done) => { zapi.setCredentials('abc', '123'); @@ -297,49 +314,5 @@ describe('API', () => { done(); }); }).timeout(10000); - - - it('should cancelScheduledSMS function return success', (done) => { - const smsId = parseInt(Math.random() * 10000).toString(); - - const payload = { - sendSmsRequest: { - from: 'Zenvia API', - to: phoneNumber, - schedule: Date.now() + 10000, - msg: 'Hello from Zenvia API from NodeJS!!!', - callbackOption: 'NONE', - id: smsId, - aggregateId: '777', - }, - }; - - zapi.sendSMS(payload) - .then((res) => { - zapi - .cancelScheduledSMS(smsId) - .then(res => { - expect(JSON.stringify(res)) - .to.equal(JSON.stringify({ - statusCode: 200, - body: { - cancelSmsResp: { - statusCode: '09', - statusDescription: 'Blocked', - detailCode: '002', - detailDescription: 'Message successfully canceled', - }, - }, - } - )); - - done(); - }); - }) - .catch(err => { - done(); - console.log(err, "cancelScheduledSMS"); - }); - }).timeout(10000); */ });