call.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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('Call.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. done();
  18. });
  19. });
  20. /*
  21. * Test the /GET route
  22. */
  23. describe('/call POST', () => {
  24. it('it should return an ERROR with "Please post all the informations needed." : we sent no post data', (done) => {
  25. chai.request(server)
  26. .post('/call')
  27. .set('content-type', 'application/x-www-form-urlencoded')
  28. .send({
  29. password: config.apipassword
  30. })
  31. .end((err, res) => {
  32. res.should.have.status(200);
  33. res.should.to.be.json;
  34. res.body.should.have.property('error').eql('Please post all the informations needed.');
  35. done();
  36. });
  37. });
  38. it('it should return an ERROR with "Please post all the informations needed." : we sent 1 post data', (done) => {
  39. chai.request(server)
  40. .post('/call')
  41. .set('content-type', 'application/x-www-form-urlencoded')
  42. .send({
  43. password: config.apipassword,
  44. to: '3312345678'
  45. })
  46. .end((err, res) => {
  47. res.should.have.status(200);
  48. res.should.to.be.json;
  49. res.body.should.have.property('error').eql('Please post all the informations needed.');
  50. done();
  51. });
  52. });
  53. it('it should return an ERROR with "Please post all the informations needed." : we sent 2 post data', (done) => {
  54. chai.request(server)
  55. .post('/call')
  56. .set('content-type', 'application/x-www-form-urlencoded')
  57. .send({
  58. password: config.apipassword,
  59. to: '3312345678',
  60. user: 'test'
  61. })
  62. .end((err, res) => {
  63. res.should.have.status(200);
  64. res.should.to.be.json;
  65. res.body.should.have.property('error').eql('Please post all the informations needed.');
  66. done();
  67. });
  68. });
  69. it("it should return an ERROR with \"The service wasn't recognised.\" : we sent 3 post data (but bad service)", (done) => {
  70. chai.request(server)
  71. .post('/call')
  72. .set('content-type', 'application/x-www-form-urlencoded')
  73. .send({
  74. password: config.apipassword,
  75. to: '3312345678',
  76. user: 'test',
  77. service: 'test'
  78. })
  79. .end((err, res) => {
  80. res.should.have.status(200);
  81. res.should.to.be.json;
  82. res.body.should.have.property('error').eql("The service wasn't recognised.");
  83. done();
  84. });
  85. });
  86. it("it should return an ERROR with \"Bad phone number.\" : we sent 3 post data (but bad phone number)", (done) => {
  87. chai.request(server)
  88. .post('/call')
  89. .set('content-type', 'application/x-www-form-urlencoded')
  90. .send({
  91. password: config.apipassword,
  92. to: '33123',
  93. user: 'test',
  94. service: 'default'
  95. })
  96. .end((err, res) => {
  97. res.should.have.status(200);
  98. res.should.to.be.json;
  99. res.body.should.have.property('error').eql("Bad phone number.");
  100. done();
  101. });
  102. });
  103. it("it should return the callSid : everything's fine", (done) => {
  104. chai.request(server)
  105. .post('/call')
  106. .set('content-type', 'application/x-www-form-urlencoded')
  107. .send({
  108. password: config.apipassword,
  109. to: '33612345678',
  110. user: 'test',
  111. service: 'default'
  112. })
  113. .end((err, res) => {
  114. res.should.have.status(200);
  115. res.should.to.be.json;
  116. res.body.should.not.have.property('error');
  117. res.body.should.have.property('callSid');
  118. done();
  119. });
  120. });
  121. });
  122. });