diff --git a/test/dial-tests.js b/test/dial-tests.js new file mode 100644 index 00000000..048faad2 --- /dev/null +++ b/test/dial-tests.js @@ -0,0 +1,215 @@ +const test = require('tape'); +const { sippUac } = require('./sipp')('test_fs'); +const bent = require('bent'); +const getJSON = bent('json') +const clearModule = require('clear-module'); +const {provisionCallHook} = require('./utils') + +process.on('unhandledRejection', (reason, p) => { + console.log('Unhandled Rejection at: Promise', p, 'reason:', reason); +}); + +function connect(connectable) { + return new Promise((resolve, reject) => { + connectable.on('connect', () => { + return resolve(); + }); + }); +} + +test('\'dial-phone\'', async(t) => { + clearModule.all(); + const {srf, disconnect} = require('../app'); + try { + await connect(srf); + + // GIVEN + const from = "dial_success"; + let verbs = [ + { + "verb": "dial", + "callerId": from, + "actionHook": "/actionHook", + "timeLimit": 5, + "target": [ + { + "type": "phone", + "number": "15083084809" + } + ] + } + ]; + + provisionCallHook(from, verbs); + + // THEN + const p = sippUac('uas-dial.xml', '172.38.0.10', undefined, undefined, 2); + + let account_sid = '622f62e4-303a-49f2-bbe0-eb1e1714e37a'; + + let post = bent('http://127.0.0.1:3000/', 'POST', 'json', 201); + post('v1/createCall', { + 'account_sid':account_sid, + "call_hook": { + "url": "http://127.0.0.1:3100/", + "method": "POST", + "username": "username", + "password": "password" + }, + "from": from, + "to": { + "type": "phone", + "number": "15583084808" + }}); + + await p; + + obj = await getJSON(`http://127.0.0.1:3100/lastRequest/${from}_actionHook`); + t.ok(obj.body.from === from, + 'dial: succeeds actionHook'); + + disconnect(); + } catch (err) { + console.log(`error received: ${err}`); + disconnect(); + t.error(err); + } +}); + + +test('\'dial-sip\'', async(t) => { + clearModule.all(); + const {srf, disconnect} = require('../app'); + try { + await connect(srf); + + // GIVEN + const from = "dial_sip"; + let verbs = [ + { + "verb": "dial", + "callerId": from, + "actionHook": "/actionHook", + "dtmfCapture":["*2", "*3"], + "target": [ + { + "type": "sip", + "sipUri": "sip:15083084809@jambonz.com" + } + ] + } + ]; + + provisionCallHook(from, verbs); + + // THEN + const p = sippUac('uas-dial.xml', '172.38.0.10', undefined, undefined, 2); + + let account_sid = '622f62e4-303a-49f2-bbe0-eb1e1714e37a'; + + let post = bent('http://127.0.0.1:3000/', 'POST', 'json', 201); + post('v1/createCall', { + 'account_sid':account_sid, + "call_hook": { + "url": "http://127.0.0.1:3100/", + "method": "POST", + "username": "username", + "password": "password" + }, + "from": from, + "to": { + "type": "phone", + "number": "15583084808" + }}); + + await new Promise(r => setTimeout(r, 2000)); + + let obj = await getJSON(`http://127.0.0.1:3100/lastRequest/${from}`); + const callSid = obj.body.call_sid; + + post = bent('http://127.0.0.1:3000/', 'POST', 202); + await post(`v1/updateCall/${callSid}`, { + "call_status": "completed" + }); + + await p; + + obj = await getJSON(`http://127.0.0.1:3100/lastRequest/${from}_actionHook`); + t.ok(obj.body.from === from, + 'dial: succeeds actionHook'); + + disconnect(); + } catch (err) { + console.log(`error received: ${err}`); + disconnect(); + t.error(err); + } +}); + +test('\'dial-user\'', async(t) => { + clearModule.all(); + const {srf, disconnect} = require('../app'); + try { + await connect(srf); + + // GIVEN + const from = "dial_user"; + let verbs = [ + { + "verb": "dial", + "callerId": from, + "actionHook": "/actionHook", + "target": [ + { + "type": "user", + "name": "user110@jambonz.com" + } + ] + } + ]; + + provisionCallHook(from, verbs); + + // THEN + const p = sippUac('uas-dial.xml', '172.38.0.10', undefined, undefined, 2); + + let account_sid = '622f62e4-303a-49f2-bbe0-eb1e1714e37a'; + + let post = bent('http://127.0.0.1:3000/', 'POST', 'json', 201); + post('v1/createCall', { + 'account_sid':account_sid, + "call_hook": { + "url": "http://127.0.0.1:3100/", + "method": "POST", + "username": "username", + "password": "password" + }, + "from": from, + "to": { + "type": "phone", + "number": "15583084808" + }}); + + await new Promise(r => setTimeout(r, 2000)); + + let obj = await getJSON(`http://127.0.0.1:3100/lastRequest/${from}`); + const callSid = obj.body.call_sid; + + post = bent('http://127.0.0.1:3000/', 'POST', 202); + await post(`v1/updateCall/${callSid}`, { + "call_status": "completed" + }); + + await p; + + obj = await getJSON(`http://127.0.0.1:3100/lastRequest/${from}_actionHook`); + t.ok(obj.body.from === from, + 'dial: succeeds actionHook'); + + disconnect(); + } catch (err) { + console.log(`error received: ${err}`); + disconnect(); + t.error(err); + } +}); \ No newline at end of file diff --git a/test/index.js b/test/index.js index 094e3ed0..bc28d4b9 100644 --- a/test/index.js +++ b/test/index.js @@ -2,6 +2,7 @@ require('./unit-tests'); require('./docker_start'); require('./create-test-db'); require('./account-validation-tests'); +require('./dial-tests'); require('./webhooks-tests'); require('./say-tests'); require('./gather-tests'); diff --git a/test/listen-tests.js b/test/listen-tests.js index 93ce0dcb..7f259739 100644 --- a/test/listen-tests.js +++ b/test/listen-tests.js @@ -40,7 +40,7 @@ test('\'listen-success\'', async(t) => { // THEN await sippUac('uac-gather-account-creds-success-send-bye.xml', '172.38.0.10', from); let obj = await getJSON(`http://127.0.0.1:3100/ws_packet_count/${from}`); - t.ok(38500 <= obj.count && obj.count <= 40100, 'listen: success incomming call audio'); + t.ok(38000 <= obj.count, 'listen: success incomming call audio'); obj = await getJSON(`http://127.0.0.1:3100/ws_metadata/${from}`); t.ok(obj.metadata.from === from && obj.metadata.sampleRate === 8000, 'listen: success metadata'); @@ -80,7 +80,7 @@ test('\'listen-maxLength\'', async(t) => { // THEN await sippUac('uac-gather-account-creds-success.xml', '172.38.0.10', from); let obj = await getJSON(`http://127.0.0.1:3100/ws_packet_count/${from}`); - t.ok(31680 <= obj.count && obj.count <= 32500, 'listen: success maxLength incomming call audio'); + t.ok(30000 <= obj.count, 'listen: success maxLength incomming call audio'); obj = await getJSON(`http://127.0.0.1:3100/ws_metadata/${from}`); t.ok(obj.metadata.from === from && obj.metadata.sampleRate === 8000, 'listen: success maxLength metadata'); diff --git a/test/scenarios/uas-dial.xml b/test/scenarios/uas-dial.xml new file mode 100644 index 00000000..f394bf4d --- /dev/null +++ b/test/scenarios/uas-dial.xml @@ -0,0 +1,164 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Content-Type: application/sdp + Content-Length: [len] + + v=0 + o=user1 53655765 2353687637 IN IP[local_ip_type] [local_ip] + s=- + c=IN IP[media_ip_type] [media_ip] + t=0 0 + m=audio [media_port] RTP/AVP 0 + a=rtpmap:0 PCMU/8000 + + ]]> + + + + + + + + + + + + + + Content-Type: application/sdp + Content-Length: [len] + + v=0 + o=user1 53655765 2353687637 IN IP[local_ip_type] [local_ip] + s=- + c=IN IP[media_ip_type] [media_ip] + t=0 0 + m=audio [media_port] RTP/AVP 0 + a=rtpmap:0 PCMU/8000 + + ]]> + + + + + + + + + + + Content-Length: 0 + + ]]> + + + + diff --git a/test/sipp.js b/test/sipp.js index d36508d4..f3b666b4 100644 --- a/test/sipp.js +++ b/test/sipp.js @@ -24,13 +24,13 @@ obj.output = () => { return output; }; -obj.sippUac = (file, bindAddress, from='sipp', to='16174000000') => { +obj.sippUac = (file, bindAddress, from='sipp', to='16174000000', loop=1) => { const cmd = 'docker'; const args = [ 'run', '-t', '--rm', '--net', `${network}`, '-v', `${__dirname}/scenarios:/tmp/scenarios`, 'drachtio/sipp', 'sipp', '-sf', `/tmp/scenarios/${file}`, - '-m', '1', + '-m', loop, '-sleep', '250ms', '-nostdin', '-cid_str', `%u-%p@%s-${idx++}`,