test/integration/message.js   A
last analyzed

Complexity

Total Complexity 14
Complexity/F 1

Size

Lines of Code 136
Function Count 14

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
cc 0
c 5
b 1
f 0
nc 1
dl 0
loc 136
rs 10
wmc 14
mnd 0
bc 14
fnc 14
bpm 1
cpm 1
noi 5

2 Functions

Rating   Name   Duplication   Size   Complexity  
A message.js ➔ describe(ꞌTest Email APIꞌ) 0 58 1
A message.js ➔ describe(ꞌTest SMS APIꞌ) 0 67 1
1
var chai = require('chai')
2
var server = require('../../app')
3
/* eslint-disable no-unused-vars */
4
var should = chai.should()
5
var nock = require('nock')
6
var nodemailer = require('nodemailer')
7
var sinon = require('sinon')
8
var expect = require('chai').expect
9
var prefix = '/api/messages'
10
11
describe('Test Email API', function () {
12
  it('it should send an email', function (done) {
13
    var transportStub = {
14
      sendMail: function (options) {
0 ignored issues
show
Unused Code introduced by
The parameter options is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
15
        return new Promise((resolve, reject) => { resolve(true, false) })
0 ignored issues
show
Unused Code introduced by
The parameter reject is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
16
      }
17
    }
18
19
    var sendMailSpy = sinon.spy(transportStub, 'sendMail')
0 ignored issues
show
Unused Code introduced by
The variable sendMailSpy seems to be never used. Consider removing it.
Loading history...
20
    var mailerStub = sinon.stub(nodemailer, 'createTransport').returns(transportStub)
21
22
    var mail = {
23
      to: '[email protected]',
24
      subject: 'Hello ✔',
25
      text: 'Hello world 🐴',
26
      html: '<b>Hello world 🐴</b>'
27
    }
28
    chai.request(server)
29
      .post(prefix + '/email')
30
      .send(mail)
31
      .end(function (err, res) {
32
        should.not.exist(err)
33
        res.should.have.status(200)
34
        res.body.should.be.a('object')
35
        res.body.should.have.property('message').eql('Message sent')
36
        mailerStub.restore()
37
        done()
38
      })
39
  })
40
41
  it('it should send an email', function (done) {
42
    var transportStub = {
43
      sendMail: function (options) {
0 ignored issues
show
Unused Code introduced by
The parameter options is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
44
        return new Promise((resolve, reject) => { reject(new Error('Something went wrong with mail service')) })
45
      }
46
    }
47
    var sendMailSpy = sinon.spy(transportStub, 'sendMail')
0 ignored issues
show
Unused Code introduced by
The variable sendMailSpy seems to be never used. Consider removing it.
Loading history...
48
    var mailerStub = sinon.stub(nodemailer, 'createTransport').returns(transportStub)
49
50
    var mail = {
51
      to: '[email protected]',
52
      subject: 'Hello ✔',
53
      text: 'Hello world 🐴',
54
      html: '<b>Hello world 🐴</b>'
55
    }
56
    chai.request(server)
57
      .post(prefix + '/email')
58
      .send(mail)
59
      .end(function (err, res) {
60
        //should.exist(err)
61
        res.should.have.status(404)
62
        res.body.should.be.a('object')
63
        err.should.have.property('message').eql('Not Found')
64
        mailerStub.restore()
65
        done()
66
      })
67
  })
68
})
69
70
describe('Test SMS API', function () {
71
  xit('it should send an sms', function (done) {
72
    nock('https://www.my-cool-sms.com')
73
      .get('/api-socket.php')
74
      .reply(200, {
75
        'success': true,
76
        'smsid': 'ce184cc0a6d1714d1ac763f4fe89f521',
77
        'body': 'Have a nice day!',
78
        'bodyucs2': '0048006100760065002000610020006E00690063006500200064',
79
        'bodygsm7': '486176652061206E6963652064617921',
80
        'number': '+491234567890',
81
        'senderid': '+449876543210',
82
        'senderidenabled': true,
83
        'unicode': false,
84
        'numchars': 321,
85
        'escapenumchars': 0,
86
        'smscount': 3,
87
        'charge': 0.112,
88
        'balance': 752.121,
89
        'countrycode': 'IE',
90
        'prefix': '+353',
91
        'timestamp': '2017-04-02T22:27:22-07:00',
92
        'callbackurl': 'https://www.groganburners.ie/api/sms/callback'
93
      })
94
95
    var mail = {
96
      number: '+353873791474',
97
      message: 'Hello'
98
    }
99
    chai.request(server)
100
      .post(prefix + '/sms')
101
      .send(mail)
102
      .end(function (err, res) {
103
        should.not.exist(err)
104
        res.should.have.status(200)
105
        res.body.should.be.a('object')
106
        res.body.should.have.property('message').eql('Message sent')
107
        nock.cleanAll()
108
        done()
109
      })
110
  })
111
112
  it('it should fail to send an sms to invalid number', function (done) {
113
    nock('https://www.my-cool-sms.com')
114
      .get('/api-socket.php')
115
      .reply(200, {
116
        success: false,
117
        errorcode: '210',
118
        description: 'The number seems to be invalid'
119
      })
120
    var mail = {
121
      number: '+35386',
122
      message: 'Hello'
123
    }
124
    chai.request(server)
125
      .post(prefix + '/sms')
126
      .send(mail)
127
      .end(function (err, res) {
128
        should.exist(err)
129
        res.should.have.status(404)
130
        res.body.should.be.a('object')
131
        err.should.have.property('message').eql('Not Found')
132
        nock.cleanAll()
133
        done()
134
      })
135
  })
136
})
137