UNPKG

8.11 kBJavaScriptView Raw
1const FILE = 'FILE'
2const EMAIL = 'test@mailjet.com'
3const EMAIL2 = 'test2@mailjet.com'
4const NAME = 'name'
5const SUBJECT = 'subject'
6const TEXT_PART = 'text'
7const VAR = {'Key1': 'Value1', 'Key2': 'Value2'}
8const SIMPLE_RECIPIENTS = [{email: EMAIL}, {email: EMAIL2}]
9const UNIQUE_RECIPIENT = [{email: EMAIL}]
10const RECIPIENTS_NAME = [{email: EMAIL, name: NAME}, {email: EMAIL2, name: NAME}]
11const RECIPIENTS_VARS = [{email: EMAIL, vars: VAR}]
12const API_KEY = process.env.MJ_APIKEY_PUBLIC
13const API_SECRET = process.env.MJ_APIKEY_PRIVATE
14
15var Mailjet = require('../mailjet-client')
16var chai = require('chai')
17var expect = chai.expect
18var should = chai.should() // eslint-disable-line no-unused-vars
19var Promise = require('bluebird')
20if (typeof API_KEY === 'undefined' || typeof API_SECRET === 'undefined') {
21 throw new Error(`Mailjet API_KEY and API_SECRET are required, respectively "${API_KEY}" and "${API_SECRET}" given`)
22}
23
24describe('Basic Usage', function () {
25 var client = Mailjet.connect(API_KEY, API_SECRET)
26
27 describe('connection', function () {
28 it('creates an instance of the client', function () {
29 var connectionType1 = new Mailjet(API_KEY, API_SECRET)
30 var connectionType2 = new Mailjet().connect(API_KEY, API_SECRET)
31 var connectionType3 = Mailjet.connect(API_KEY, API_SECRET)
32
33 expect('' + connectionType1.apiKey + connectionType1.apiSecret).to.equal('' + API_KEY + API_SECRET)
34 expect('' + connectionType2.apiKey + connectionType2.apiSecret).to.equal('' + API_KEY + API_SECRET)
35 expect('' + connectionType3.apiKey + connectionType3.apiSecret).to.equal('' + API_KEY + API_SECRET)
36 })
37 })
38
39 describe('method request', function () {
40 describe('get', function () {
41 var contact = client.get('contact')
42
43 it('calls the contact ressource instance whith no parameters', function (done) {
44 contact.request()
45 .then(function (result) {
46 result.body.should.be.a('object')
47 expect(result.response.statusCode).to.equal(200)
48 done()
49 })
50 .catch(function (reason) {
51 // We want it to raise an error if it gets here
52 expect(reason).to.equal(undefined)
53 done()
54 })
55 })
56
57 it('calls the contact ressource instance whith parameters', function (done) {
58 var promise = contact.request({Name: 'Guillaume Badi'})
59 .then(function (result) {
60 result.body.should.be.a('object')
61 expect(result.response.statusCode).to.be.within(200, 201)
62 done()
63 })
64 .catch(function (reason) {
65 // We want it to raise an error if it gets here
66 expect(reason).to.equal(undefined)
67 done()
68 })
69 expect(Promise.prototype.isPrototypeOf(promise)).to.equal(true)
70 })
71
72 it('calls the contact ressource instance with empty parameters', function (done) {
73 contact.request({}).then(function (result) {
74 result.body.should.be.a('object')
75 expect(result.response.statusCode).to.be.within(200, 201)
76 done()
77 })
78 })
79 })
80
81 describe('post', function () {
82 var sender = client.post('sender')
83
84 it('calls the sender ressource instance whith no parameters', function (done) {
85 sender.request().catch(function (reason) {
86 reason.statusCode.should.equal(400)
87 done()
88 })
89 })
90
91 it('calls the sender ressource instance whith invalid parameters', function (done) {
92 sender.request({Name: 'Guillaume Badi'}).catch(function (reason) {
93 expect(reason.statusCode).to.equal(400)
94 done()
95 })
96 })
97
98 it('calls the sender ressource instance whith valid parameters', function (done) {
99 sender.request({email: 'gbadi@mailjet.com'})
100 .then(function (result) {
101 expect(result.response.statusCode).to.equal(201)
102 done()
103 })
104 .catch(function (reason) {
105 // if it fails because the sender already exist. should be 400
106 expect(reason.statusCode).to.equal(400)
107 done()
108 })
109 })
110
111 it('calls the sender resource with empty parameters', function (done) {
112 sender.request({}).catch(function (reason) {
113 expect(reason.statusCode).to.equal(400)
114 done()
115 })
116 })
117 })
118 })
119})
120
121describe('Advanced API Calls', function () {
122 function Example (fn, payload) {
123 this.fn = fn
124 this.payload = payload
125 this.format = function (obj) { return JSON.stringify(obj).match(/\S+/g).join('') }
126 this.call = function () {
127 var res = this.fn.request(this.payload)
128 var ret = res[0] + ' ' + this.format(res[1])
129 return ret
130 }
131 }
132
133 var client2 = new Mailjet(API_KEY, API_SECRET, true)
134
135 const EXAMPLES_SET = [
136 new Example(client2.get('contact')),
137 new Example(client2.get('contact').id(2)),
138 new Example(client2.get('contact/2')),
139 new Example(client2.get('contact').id(3).action('getcontactslist')),
140 new Example(client2.get('contact'), {countOnly: 1}),
141 new Example(client2.get('contact'), {limit: 2}),
142 new Example(client2.get('contact'), {offset: 233}),
143 new Example(client2.get('contact'), {contatctList: 34}),
144 new Example(client2.post('contactslist').id(34).action('managecontact'), {email: EMAIL}),
145 new Example(client2.post('contactslist').id(34).action('csvdata'), FILE),
146 new Example(client2.get('newsletter'), {filters: {CountOnly: 1}}),
147 new Example(client2.get('batchjob').action('csverror')),
148 new Example(client2.post('contact'), {email: EMAIL}),
149 new Example(client2.post('send'), {'FromName': NAME, 'FromEmail': EMAIL, 'Subject': SUBJECT, 'Text-Part': TEXT_PART, 'Recipients': UNIQUE_RECIPIENT}),
150 new Example(client2.post('send'), {'FromName': NAME, 'FromEmail': EMAIL, 'Subject': SUBJECT, 'Text-Part': TEXT_PART, 'Recipients': SIMPLE_RECIPIENTS}),
151 new Example(client2.post('send'), {'FromName': NAME, 'FromEmail': EMAIL, 'Subject': SUBJECT, 'Text-Part': TEXT_PART, 'Recipients': RECIPIENTS_NAME}),
152 new Example(client2.post('send'), {'FromName': NAME, 'FromEmail': EMAIL, 'Subject': SUBJECT, 'Text-Part': TEXT_PART, 'Recipients': RECIPIENTS_VARS})
153 ]
154
155 const EXPECTED_SET = [
156 'https://api.mailjet.com/v3/REST/contact {}',
157 'https://api.mailjet.com/v3/REST/contact/2 {}',
158 'https://api.mailjet.com/v3/REST/contact/2 {}',
159 'https://api.mailjet.com/v3/REST/contact/3/getcontactslist {}',
160 'https://api.mailjet.com/v3/REST/contact/?countOnly=1 {}',
161 'https://api.mailjet.com/v3/REST/contact/?limit=2 {}',
162 'https://api.mailjet.com/v3/REST/contact/?offset=233 {}',
163 'https://api.mailjet.com/v3/REST/contact/?contatctList=34 {}',
164 'https://api.mailjet.com/v3/REST/contactslist/34/managecontact {"email":"test@mailjet.com"}',
165 'https://api.mailjet.com/v3/DATA/contactslist/34/csvdata "FILE"',
166 'https://api.mailjet.com/v3/REST/newsletter/?CountOnly=1 {}',
167 'https://api.mailjet.com/v3/DATA/batchjob/csverror {}',
168 'https://api.mailjet.com/v3/REST/contact {"email":"test@mailjet.com"}',
169 'https://api.mailjet.com/v3/send {"FromName":"name","FromEmail":"test@mailjet.com","Subject":"subject","Text-Part":"text","Recipients":[{"email":"test@mailjet.com"}]}',
170 '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"}]}',
171 '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"}]}',
172 '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"}}]}'
173 ]
174
175 EXPECTED_SET.forEach(function (test, index) {
176 it('should output: ' + test, function () {
177 EXAMPLES_SET[index].call().should.equal(test)
178 })
179 })
180})