Completed
Push — master ( a6cbbe...9c91a4 )
by Callum
30s
created

server-spec.js ➔ ???   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 88

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
cc 1
c 5
b 1
f 1
nc 1
nop 0
dl 0
loc 88
rs 8.6012

1 Function

Rating   Name   Duplication   Size   Complexity  
B server-spec.js ➔ ... ➔ ??? 0 24 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import chai, { expect } from 'chai'
2
import chaiHttp from 'chai-http'
3
import xmldoc from 'xmldoc'
4
import fs from 'fs'
5
import server from './server'
6
7
chai.use(chaiHttp)
8
9
// describe('Runtime arguments', () => {
10
//   it('should clean the output directory when called with --clean', (done) => {
11
//     process.argv = ['--clean']
12
//     chai.request(server)
13
//   })
14
// })
15
16
describe('/POST email', () => {
17
  const toAddress = '[email protected]'
18
  const ccAddress = '[email protected]'
19
  const bccAddress = '[email protected]'
20
  const replyToAddress = '[email protected]'
21
  const htmlEmail = '<p>HTML Email</p>'
22
  const textEmail = 'Text Email'
23
  const emailSubject = 'Email Subject 😊'
24
  const fromEmail = '[email protected]'
25
  it('should succeed if email has all params', (done) => {
26
    chai.request(server)
27
      .post('/')
28
      .send({
29
        Action: 'SendEmail',
30
        'Destination.ToAddresses.member.1': toAddress,
31
        'Message.Body.Html.Data': htmlEmail,
32
        'Message.Body.Text.Data': textEmail,
33
        'Message.Subject.Data': emailSubject,
34
        'Destination.CcAddresses.member.1': ccAddress,
35
        'Destination.BccAddresses.member.1': bccAddress,
36
        'ReplyToAddresses.member.1': replyToAddress,
37
        Source: fromEmail,
38
      })
39
      .end((err, res) => {
40
        expect(res).to.have.status(200)
41
        const response = new xmldoc.XmlDocument(res.text)
42
        const path = response.valueWithPath('SendEmailResult.MessageId')
43
        expect(fs.readFileSync(path, 'utf8')).to.eq(htmlEmail)
44
        expect(fs.readFileSync(path.replace('body.html', 'body.txt'), 'utf8')).to.eq(textEmail)
45
        expect(fs.readFileSync(path.replace('body.html', 'headers.txt'), 'utf8')).to.eq(`Subject: ${emailSubject}\nTo Address: ${toAddress}\nCc Address: ${ccAddress}\nBcc Address: ${bccAddress}\nReply To Address: ${replyToAddress}\nSource: ${fromEmail}`)
46
        done()
47
      })
48
  })
49
  it('should succeed if only HTML body is missing', (done) => {
50
    chai.request(server)
51
      .post('/')
52
      .send({
53
        Action: 'SendEmail',
54
        'Destination.ToAddresses.member.1': toAddress,
55
        'Message.Body.Text.Data': textEmail,
56
        'Message.Subject.Data': emailSubject,
57
        'Destination.CcAddresses.member.1': ccAddress,
58
        'Destination.BccAddresses.member.1': bccAddress,
59
        'ReplyToAddresses.member.1': replyToAddress,
60
        Source: fromEmail,
61
      })
62
      .end((err, res) => {
63
        expect(res).to.have.status(200)
64
        done()
65
      })
66
  })
67
  it('should succeed if only Text body is missing', (done) => {
68
    chai.request(server)
69
      .post('/')
70
      .send({
71
        Action: 'SendEmail',
72
        'Destination.ToAddresses.member.1': toAddress,
73
        'Message.Body.Html.Data': htmlEmail,
74
        'Message.Subject.Data': emailSubject,
75
        'Destination.CcAddresses.member.1': ccAddress,
76
        'Destination.BccAddresses.member.1': bccAddress,
77
        'ReplyToAddresses.member.1': replyToAddress,
78
        Source: fromEmail,
79
      })
80
      .end((err, res) => {
81
        expect(res).to.have.status(200)
82
        done()
83
      })
84
  })
85
  it('should fail if one param is not sent', (done) => {
86
    chai.request(server)
87
      .post('/')
88
      .send({
89
        Action: 'SendEmail',
90
        'Message.Body.Html.Data': htmlEmail,
91
        'Message.Body.Text.Data': textEmail,
92
        'Message.Subject.Data': emailSubject,
93
        Source: fromEmail,
94
      })
95
      .end((err, res) => {
96
        expect(res).to.have.status(500)
97
        const response = new xmldoc.XmlDocument(res.text)
98
        expect(response.valueWithPath('Code')).to.eq('MessageRejected')
99
        expect(response.valueWithPath('Message')).to.eq('One or more required fields was not sent')
100
        done()
101
      })
102
  })
103
})