voice.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. process.env.NODE_ENV = 'test';
  2. const sqlite3 = require('sqlite3').verbose();
  3. const db = new sqlite3.Database('./db/data.db');
  4. const config = require('../config');
  5. let server = require('../app');
  6. let chai = require('chai');
  7. let chaiHttp = require('chai-http');
  8. const {
  9. expect
  10. } = require('chai');
  11. let should = chai.should();
  12. chai.use(chaiHttp);
  13. describe('Voice.js', () => {
  14. beforeEach((done) => { //Before each test we empty the database
  15. db.run(`DELETE FROM calls WHERE 1`, function(err) {
  16. if (err) return console.log(err.message);
  17. });
  18. db.run(`INSERT INTO calls(itsfrom, itsto, callSid, digits, status, date, user, service) VALUES(?, ?, ?, ?, ?, ?, ?, ?)`, ['33123456789', '33123456789', 'fakecallsid', '123456', 'test', 'testdate', 'test', 'paypal'], function(err) {
  19. if (err) {
  20. return console.log(err.message);
  21. }
  22. db.run(`INSERT INTO calls(itsfrom, itsto, callSid, digits, status, date, user) VALUES(?, ?, ?, ?, ?, ?, ?)`, ['33123456789', '33123456789', 'fakecallsid2', '123456', 'test', 'testdate', 'test'], function(err) {
  23. if (err) {
  24. return console.log(err.message);
  25. }
  26. db.run(`INSERT INTO calls(itsfrom, itsto, callSid, digits, status, date, user, service) VALUES(?, ?, ?, ?, ?, ?, ?, ?)`, ['33123456789', '33123456789', 'fakecallsid3', '123456', 'test', 'testdate', 'test', 'fakeservice'], function(err) {
  27. if (err) {
  28. return console.log(err.message);
  29. }
  30. done();
  31. });
  32. });
  33. });
  34. });
  35. /*
  36. * Test the /GET route
  37. */
  38. describe('/voice POST', () => {
  39. it('it should return an ERROR "Please give us the callSid." : we are not sending any callSid', (done) => {
  40. chai.request(server)
  41. .post('/voice/' + config.apipassword)
  42. .set('content-type', 'application/x-www-form-urlencoded')
  43. .end((err, res) => {
  44. res.should.have.status(200);
  45. res.should.to.be.json;
  46. res.body.should.have.property('error').eql('Please give us the callSid.');
  47. done();
  48. });
  49. });
  50. it('it should return the twiML code for the paypal service : we are not sending any callSid or digit', (done) => {
  51. chai.request(server)
  52. .post('/voice/' + config.apipassword)
  53. .set('content-type', 'application/x-www-form-urlencoded')
  54. .send({
  55. CallSid: 'fakecallsid'
  56. })
  57. .end((err, res) => {
  58. res.should.have.status(200);
  59. res.should.to.have.header('content-type', 'text/xml; charset=utf-8');
  60. res.text.should.equal('<?xml version="1.0" encoding="UTF-8"?><Response><Gather timeout="8" numDigits="6"><Say>Bonjour ,</Say><Play loop="4">' + config.serverurl + '/stream/paypal</Play></Gather></Response>');
  61. done();
  62. });
  63. });
  64. it('it should return the twiML code for the default service : the service is not precised in the db', (done) => {
  65. chai.request(server)
  66. .post('/voice/' + config.apipassword)
  67. .set('content-type', 'application/x-www-form-urlencoded')
  68. .send({
  69. CallSid: 'fakecallsid2'
  70. })
  71. .end((err, res) => {
  72. res.should.have.status(200);
  73. res.should.to.have.header('content-type', 'text/xml; charset=utf-8');
  74. res.text.should.equal('<?xml version="1.0" encoding="UTF-8"?><Response><Gather timeout="8" numDigits="6"><Say>Bonjour ,</Say><Play loop="4">' + config.serverurl + '/stream/default' + '</Play></Gather></Response>');
  75. done();
  76. });
  77. });
  78. it('it should return the twiML code for the default service : the service asked doesn\'t exist', (done) => {
  79. chai.request(server)
  80. .post('/voice/' + config.apipassword)
  81. .set('content-type', 'application/x-www-form-urlencoded')
  82. .send({
  83. CallSid: 'fakecallsid3'
  84. })
  85. .end((err, res) => {
  86. res.should.have.status(200);
  87. res.should.to.have.header('content-type', 'text/xml; charset=utf-8');
  88. res.text.should.equal('<?xml version="1.0" encoding="UTF-8"?><Response><Gather timeout="8" numDigits="6"><Say>Bonjour ,</Say><Play loop="4">' + config.serverurl + '/stream/default' + '</Play></Gather></Response>');
  89. done();
  90. });
  91. });
  92. it('it should return the twiML code for the end service : we provided the 6 digits code', (done) => {
  93. chai.request(server)
  94. .post('/voice/' + config.apipassword)
  95. .set('content-type', 'application/x-www-form-urlencoded')
  96. .send({
  97. CallSid: 'fakecallsid3',
  98. Digits: '123456'
  99. })
  100. .end((err, res) => {
  101. res.should.have.status(200);
  102. res.should.to.have.header('content-type', 'text/xml; charset=utf-8');
  103. res.text.should.equal('<?xml version="1.0" encoding="UTF-8"?><Response><Play>' + config.serverurl + '/stream/end</Play></Response>');
  104. done();
  105. });
  106. });
  107. });
  108. });