1 |
|
2 | const FILE = 'FILE'
|
3 | const EMAIL = 'test@mailjet.com'
|
4 | const EMAIL2 = 'test2@mailjet.com'
|
5 | const NAME = 'name'
|
6 | const SUBJECT = 'subject'
|
7 | const TEXT_PART = 'text'
|
8 | const VAR = {'Key1': 'Value1', 'Key2': 'Value2'}
|
9 | const SIMPLE_RECIPIENTS = [{email: EMAIL}, {email: EMAIL2}]
|
10 | const UNIQUE_RECIPIENT = [{email: EMAIL}]
|
11 | const RECIPIENTS_NAME = [{email: EMAIL, name: NAME}, {email: EMAIL2, name: NAME}]
|
12 | const RECIPIENTS_VARS = [{email: EMAIL, vars: VAR}]
|
13 | const API_KEY = process.env.MJ_APIKEY_PUBLIC
|
14 | const API_SECRET = process.env.MJ_APIKEY_PRIVATE
|
15 |
|
16 | var Mailjet = require('../mailjet-client')
|
17 | var chai = require('chai')
|
18 | var expect = chai.expect
|
19 | var should = chai.should()
|
20 | var Promise = require('bluebird')
|
21 | var nock = require('nock')
|
22 |
|
23 | if (typeof API_KEY === 'undefined' || typeof API_SECRET === 'undefined') {
|
24 | throw new Error('Mailjet API_KEY and API_SECRET are required, respectively ' + API_KEY + ' and ' + API_SECRET + ' given ')
|
25 | }
|
26 |
|
27 | var emailOptions = {
|
28 | version: 'v3'
|
29 | }
|
30 |
|
31 | describe('Basic Usage', function () {
|
32 | var client = Mailjet.connect(API_KEY, API_SECRET, emailOptions)
|
33 |
|
34 | describe('connection', function () {
|
35 | it('creates an instance of the client', function () {
|
36 | var connectionType1 = new Mailjet(API_KEY, API_SECRET)
|
37 | var connectionType2 = new Mailjet().connect(API_KEY, API_SECRET)
|
38 | var connectionType3 = Mailjet.connect(API_KEY, API_SECRET)
|
39 |
|
40 | expect('' + connectionType1.apiKey + connectionType1.apiSecret).to.equal('' + API_KEY + API_SECRET)
|
41 | expect('' + connectionType2.apiKey + connectionType2.apiSecret).to.equal('' + API_KEY + API_SECRET)
|
42 | expect('' + connectionType3.apiKey + connectionType3.apiSecret).to.equal('' + API_KEY + API_SECRET)
|
43 | })
|
44 |
|
45 | it('creates an instance of the client with options', function () {
|
46 | var options = {
|
47 | proxyUrl: 'http://localhost:3128',
|
48 | timeout: 10000
|
49 | }
|
50 |
|
51 | var connectionType1 = new Mailjet(API_KEY, API_SECRET, options)
|
52 | var connectionType2 = new Mailjet().connect(API_KEY, API_SECRET, options)
|
53 | var connectionType3 = Mailjet.connect(API_KEY, API_SECRET, options)
|
54 |
|
55 | var connections = [connectionType1, connectionType2, connectionType3]
|
56 | connections.forEach(function (connection) {
|
57 | expect(connection).to.have.property('apiKey', API_KEY)
|
58 | expect(connection).to.have.property('apiSecret', API_SECRET)
|
59 | expect(connection.options).to.have.property('proxyUrl', options.proxyUrl)
|
60 | expect(connection.options).to.have.property('timeout', 10000)
|
61 | })
|
62 | })
|
63 | })
|
64 |
|
65 | describe('method request', function () {
|
66 | describe('get', function () {
|
67 | var contact = client.get('contact')
|
68 |
|
69 | it('calls the contact ressource instance whith no parameters', function (done) {
|
70 | contact.request()
|
71 | .then(function (result) {
|
72 | result.body.should.be.a('object')
|
73 | expect(result.response.statusCode).to.equal(200)
|
74 | done()
|
75 | })
|
76 | .catch(function (reason) {
|
77 |
|
78 | expect(reason).to.equal(undefined)
|
79 | done()
|
80 | })
|
81 | })
|
82 |
|
83 | it('calls the contact ressource instance whith parameters', function (done) {
|
84 | var promise = contact.request({Name: 'Guillaume Badi'})
|
85 | .then(function (result) {
|
86 | result.body.should.be.a('object')
|
87 | expect(result.response.statusCode).to.be.within(200, 201)
|
88 | done()
|
89 | })
|
90 | .catch(function (reason) {
|
91 |
|
92 | expect(reason).to.equal(undefined)
|
93 | done()
|
94 | })
|
95 | expect(Promise.prototype.isPrototypeOf(promise)).to.equal(true)
|
96 | })
|
97 |
|
98 | it('calls the contact ressource instance with empty parameters', function (done) {
|
99 | contact.request({}).then(function (result) {
|
100 | result.body.should.be.a('object')
|
101 | expect(result.response.statusCode).to.be.within(200, 201)
|
102 | done()
|
103 | })
|
104 | })
|
105 | })
|
106 |
|
107 | describe('post', function () {
|
108 | var sender = client.post('sender')
|
109 |
|
110 | var deletedErrorMessage = 'There is an already existing deleted sender with the same email. ' +
|
111 | 'You can use "validate" action in order to activate it.'
|
112 |
|
113 | var inactiveErrorMessage = 'There is an already existing inactive sender with the same email. ' +
|
114 | 'You can use "validate" action in order to activate it.'
|
115 |
|
116 | it('calls the sender ressource instance whith no parameters', function (done) {
|
117 | sender.request().catch(function (reason) {
|
118 |
|
119 | reason.statusCode.should.equal(400)
|
120 | done()
|
121 | })
|
122 | })
|
123 |
|
124 | it('calls the sender ressource instance whith invalid parameters', function (done) {
|
125 | sender.request({Name: 'Guillaume Badi'}).catch(function (reason) {
|
126 |
|
127 | expect(reason.statusCode).to.equal(400)
|
128 | done()
|
129 | })
|
130 | })
|
131 |
|
132 | it('calls the sender ressource instance whith valid parameters', function (done) {
|
133 | sender.request({email: 'gbadi@mailjet.com'})
|
134 | .then(function (result) {
|
135 | expect(result.response.statusCode).to.equal(201)
|
136 | done()
|
137 | })
|
138 | .catch(function (reason) {
|
139 |
|
140 |
|
141 | expect(reason.statusCode).to.equal(400)
|
142 | done()
|
143 | })
|
144 | })
|
145 |
|
146 | it('calls the sender resource with empty parameters', function (done) {
|
147 | sender.request({}).catch(function (reason) {
|
148 |
|
149 | expect(reason.statusCode).to.equal(400)
|
150 | done()
|
151 | })
|
152 | })
|
153 | })
|
154 | })
|
155 | })
|
156 |
|
157 | describe('Advanced API Calls', function () {
|
158 | function Example (fn, payload) {
|
159 | this.fn = fn
|
160 | this.payload = payload
|
161 | this.format = function (obj) { return JSON.stringify(obj).match(/\S+/g).join('') }
|
162 | var self = this
|
163 | this.call = function () {
|
164 | var res = this.fn.request(this.payload)
|
165 | if(res[0]) {
|
166 | return res[0].replace(/\\/g, '/') + ' ' + this.format(res[1])
|
167 | } else {
|
168 | return res.then(function(result) {
|
169 | return result.url.replace(/\\/g, '/') + ' ' + self.format(result.body)
|
170 | })
|
171 | }
|
172 | }
|
173 | }
|
174 |
|
175 | var client2 = new Mailjet(API_KEY, API_SECRET, emailOptions, true)
|
176 |
|
177 | const EXAMPLES_SET = [
|
178 | new Example(client2.get('contact')),
|
179 | new Example(client2.get('contact').id(2)),
|
180 | new Example(client2.get('contact/2')),
|
181 | new Example(client2.get('contact').id(3).action('getcontactslist')),
|
182 | new Example(client2.get('contact'), {countOnly: 1}),
|
183 | new Example(client2.get('contact'), {limit: 2}),
|
184 | new Example(client2.get('contact'), {offset: 233}),
|
185 | new Example(client2.get('contact'), {contatctList: 34}),
|
186 | new Example(client2.post('contactslist').id(34).action('managecontact'), {email: EMAIL}),
|
187 | new Example(client2.post('contactslist').id(34).action('csvdata'), FILE),
|
188 | new Example(client2.get('newsletter'), {filters: {CountOnly: 1}}),
|
189 | new Example(client2.get('batchjob').action('csverror')),
|
190 | new Example(client2.post('contact'), {email: EMAIL}),
|
191 | new Example(client2.post('send'), {'FromName': NAME, 'FromEmail': EMAIL, 'Subject': SUBJECT, 'Text-Part': TEXT_PART, 'Recipients': UNIQUE_RECIPIENT}),
|
192 | new Example(client2.post('send'), {'FromName': NAME, 'FromEmail': EMAIL, 'Subject': SUBJECT, 'Text-Part': TEXT_PART, 'Recipients': SIMPLE_RECIPIENTS}),
|
193 | new Example(client2.post('send'), {'FromName': NAME, 'FromEmail': EMAIL, 'Subject': SUBJECT, 'Text-Part': TEXT_PART, 'Recipients': RECIPIENTS_NAME}),
|
194 | new Example(client2.post('send'), {'FromName': NAME, 'FromEmail': EMAIL, 'Subject': SUBJECT, 'Text-Part': TEXT_PART, 'Recipients': RECIPIENTS_VARS})
|
195 | ]
|
196 |
|
197 | const EXPECTED_SET = [
|
198 | 'https://api.mailjet.com/v3/REST/contact {}',
|
199 | 'https://api.mailjet.com/v3/REST/contact/2 {}',
|
200 | 'https://api.mailjet.com/v3/REST/contact/2 {}',
|
201 | 'https://api.mailjet.com/v3/REST/contact/3/getcontactslist {}',
|
202 | 'https://api.mailjet.com/v3/REST/contact/?countOnly=1 {}',
|
203 | 'https://api.mailjet.com/v3/REST/contact/?limit=2 {}',
|
204 | 'https://api.mailjet.com/v3/REST/contact/?offset=233 {}',
|
205 | 'https://api.mailjet.com/v3/REST/contact/?contatctList=34 {}',
|
206 | 'https://api.mailjet.com/v3/REST/contactslist/34/managecontact {"email":"test@mailjet.com"}',
|
207 | 'https://api.mailjet.com/v3/DATA/contactslist/34/csvdata "FILE"',
|
208 | 'https://api.mailjet.com/v3/REST/newsletter/?CountOnly=1 {}',
|
209 | 'https://api.mailjet.com/v3/DATA/batchjob/csverror {}',
|
210 | 'https://api.mailjet.com/v3/REST/contact {"email":"test@mailjet.com"}',
|
211 | 'https://api.mailjet.com/v3/send {"FromName":"name","FromEmail":"test@mailjet.com","Subject":"subject","Text-Part":"text","Recipients":[{"email":"test@mailjet.com"}]}',
|
212 | 'https://api.mailjet.com/v3/send {"FromName":"name","FromEmail":"test@mailjet.com","Subject":"subject","Text-Part":"text","Recipients":[{"email":"test@mailjet.com"},{"email":"test2@mailjet.com"}]}',
|
213 | 'https://api.mailjet.com/v3/send {"FromName":"name","FromEmail":"test@mailjet.com","Subject":"subject","Text-Part":"text","Recipients":[{"email":"test@mailjet.com","name":"name"},{"email":"test2@mailjet.com","name":"name"}]}',
|
214 | 'https://api.mailjet.com/v3/send {"FromName":"name","FromEmail":"test@mailjet.com","Subject":"subject","Text-Part":"text","Recipients":[{"email":"test@mailjet.com","vars":{"Key1":"Value1","Key2":"Value2"}}]}'
|
215 | ]
|
216 |
|
217 | EXPECTED_SET.forEach(function (test, index) {
|
218 | it('should output: ' + test, function () {
|
219 | var call = EXAMPLES_SET[index].call()
|
220 | if(call instanceof Promise) {
|
221 | call.then(function(response) {
|
222 | response.should.equal(test)
|
223 | })
|
224 | } else {
|
225 | call.should.equal(test) }
|
226 |
|
227 | })
|
228 | })
|
229 | })
|
230 |
|
231 |
|
232 | describe('Mocked API calls', function () {
|
233 |
|
234 | var client = Mailjet.connect(API_KEY, API_SECRET, { timeout: 10, version: 'v3' })
|
235 |
|
236 | describe('method request', function () {
|
237 | describe('get', function () {
|
238 | var contact = client.get('contact')
|
239 |
|
240 | it('calls the contact resource instance and the request times out', function (done) {
|
241 |
|
242 | nock('https://api.mailjet.com')
|
243 | .get('/v3/REST/contact')
|
244 | .delayConnection(1000)
|
245 | .reply(200, {})
|
246 |
|
247 | contact.request({})
|
248 | .then(function (result) {
|
249 |
|
250 | expect(result).to.equal(undefined)
|
251 | done()
|
252 | })
|
253 | .catch(function (reason) {
|
254 | expect(reason.ErrorMessage).to.equal('Timeout of 10ms exceeded')
|
255 | expect(reason.statusCode).to.equal(null)
|
256 | expect(reason.response).to.equal(null)
|
257 | done()
|
258 | })
|
259 | })
|
260 | })
|
261 | })
|
262 | })
|