From f15d89283d3b52cf43de2014f742a161d9d4e8f8 Mon Sep 17 00:00:00 2001 From: lilleman Date: Wed, 3 Jun 2015 18:11:29 +0200 Subject: [PATCH] Added good tests --- README.md | 26 +++++++++++---- lib/session.js | 4 +-- test/04_session.js | 83 +++++++++++++++++++++++++++++++++++----------- 3 files changed, 86 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 61171bd..e5f19f0 100644 --- a/README.md +++ b/README.md @@ -126,24 +126,38 @@ Example code below: }); }); -### Events +## Session Events #### connect -Triggered when the socket is connected to a client +Triggered when the socket is connected to a client. This is server specific. #### data -Triggered when data is comming in on the socket +Triggered when data is comming in on the socket. #### close -Triggered when the socket is closed +Triggered when the socket is closed. #### error -Generic error event +Generic error event. #### sms -Incoming SMS \ No newline at end of file +Incoming SMS. + +#### incomingPdu + +Incoming PDU. + +#### incomingPduObj + +Incoming PDU Object. Same as incomingPdu, but it have been converted into an object instead of a buffer. + +## Session commands + +### send + +Send a PDU to the remote. \ No newline at end of file diff --git a/lib/session.js b/lib/session.js index c9a4c26..f32ba3f 100644 --- a/lib/session.js +++ b/lib/session.js @@ -51,7 +51,7 @@ function smsResp(status, callback) { } if (sms.pduObjs === undefined) { - sms.pduObjs = [sms.pduObj]; + sms.pduObjs = [{'pduObj': sms.pduObj}]; } // Build async tasks to run the responses in parallel @@ -355,7 +355,7 @@ function session(sock) { * to - international format * message - string * dlr - boolean defaults to false - * @param func callback(err, smsId, retPduObj) + * @param func callback(err, smsIds, retPduObj) */ returnObj.sendSms = function(smsOptions, callback) { var pduObj = {}; diff --git a/test/04_session.js b/test/04_session.js index 4ba904c..fd2d36a 100644 --- a/test/04_session.js +++ b/test/04_session.js @@ -17,12 +17,12 @@ describe('Sessions', function() { it('should setup a basic server and client and then directly unbinding again', function(done) { portfinder.getPort(function(err, freePort) { - assert( ! err, 'Error should be negative'); + assert( ! err, 'Error should not be negative'); larvitsmpp.server({ 'port': freePort }, function(err, serverSession) { - assert( ! err, 'Error should be negative'); + assert( ! err, 'Error should not be negative'); serverSession.on('close', function() { // Manually destroy the server socket @@ -33,7 +33,7 @@ describe('Sessions', function() { larvitsmpp.client({ 'port': freePort }, function(err, clientSession) { - assert( ! err, 'Error should be negative'); + assert( ! err, 'Error should not be negative'); // Gracefully close connection clientSession.unbind(); @@ -45,13 +45,13 @@ describe('Sessions', function() { it('should setup a server with auth and client trying to connect with wrong username and password', function(done) { portfinder.getPort(function(err, freePort) { - assert( ! err, 'Error should be negative'); + assert( ! err, 'Error should not be negative'); larvitsmpp.server({ 'port': freePort, 'checkuserpass': checkuserpass }, function(err, serverSession) { - assert( ! err, 'Error should be negative'); + assert( ! err, 'Error should not be negative'); serverSession.on('close', function() { // Manually destroy the server socket @@ -71,13 +71,13 @@ describe('Sessions', function() { it('should setup a server with auth and client trying to connect with correct username and password', function(done) { portfinder.getPort(function(err, freePort) { - assert( ! err, 'Error should be negative'); + assert( ! err, 'Error should not be negative'); larvitsmpp.server({ 'port': freePort, 'checkuserpass': checkuserpass }, function(err, serverSession) { - assert( ! err, 'Error should be negative'); + assert( ! err, 'Error should not be negative'); serverSession.on('close', function() { // Manually destroy the server socket @@ -90,7 +90,7 @@ describe('Sessions', function() { 'username': 'foo', 'password': 'bar' }, function(err, clientSession) { - assert( ! err, 'Error should be negative'); + assert( ! err, 'Error should not be negative'); // Gracefully close connection clientSession.unbind(); @@ -100,20 +100,65 @@ describe('Sessions', function() { }); }); - /* - it('should try to send submit_sm while not logged in and get a failure return PDU back', function(done) { + it('should try sending a simple sms', function(done) { + portfinder.getPort(function(err, freePort) { + assert( ! err, 'Error should not be negative'); + larvitsmpp.server({ + 'port': freePort + }, function(err, serverSession) { + assert( ! err, 'Error should not be negative'); + + serverSession.on('sms', function(sms) { + sms.smsId = 2343; + + assert(sms.from === 'foo', 'SMS from should be "foo"'); + assert(sms.to === '46709771337', 'SMS to should be "46709771337"'); + assert(sms.message === 'hello world', 'SMS message should be "hello world"'); + assert(sms.dlr === false, 'DLR should be boolean false'); + assert(sms.pduObj.cmdId === 4, 'SMS pduObj cmdId should be 4'); + + sms.sendResp(function(err, retPdus) { + assert( ! err, 'Error should not be negative'); + + assert(retPdus[0].toString('hex') === '000000158000000400000000000000023233343300', 'The return PDU should be "000000158000000400000000000000023233343300"'); + }); + }); + + serverSession.on('close', function() { + // Manually destroy the server socket + serverSession.sock.destroy(); + }); + }); + + larvitsmpp.client({ + 'port': freePort + }, function(err, clientSession) { + assert( ! err, 'Error should not be negative'); + + clientSession.sendSms({ + 'from': 'foo', + 'to': '46709771337', + 'message': 'hello world' + }, function(err, smsIds, retPduObj) { + assert( ! err, 'Error should not be negative'); + + assert(smsIds instanceof Array, 'smsIds should be an Array'); + assert(smsIds[0] === '2343', 'Given smsId should be "2343"'); + assert(retPduObj.cmdStatus === 'ESME_ROK', 'Command status should be "ESME_ROK"'); + assert(retPduObj.cmdName === 'submit_sm_resp', 'Command name should be "submit_sm_resp"'); + + // Gracefully close connection + clientSession.unbind(); + + done(); + }); + }); + }); }); - it('should send a bind_transceiver to a new session and get logged in', function(done) { - - }); - - it('should send a bind_transceiver to a new session with a login-method and fail due to wrong username and password', function(done) { - - }); - - it('should send a bind_transceiver to a new session with a login-method and succeed with correct username and password', function(done) { + /*it('should try sending a long sms', function(done) { });*/ + }); \ No newline at end of file