1
+ /* global describe, it */
2
+ var API_TOKEN = process . env . MJ_API_TOKEN
3
+ var API_PUBLIC_KEY = process . env . MJ_APIKEY_PUBLIC
4
+ var API_PRIVATE_KEY = process . env . MJ_APIKEY_PRIVATE
5
+
6
+ var Mailjet = require ( '../mailjet-client' )
7
+ var chai = require ( 'chai' )
8
+ var expect = chai . expect
9
+ var should = chai . should ( ) // eslint-disable-line no-unused-vars
10
+ var Promise = require ( 'bluebird' )
11
+
12
+ describe ( 'Basic Error Handling' , function ( ) {
13
+ API_TOKEN = API_TOKEN || '#invalidToken'
14
+ API_PUBLIC_KEY = API_PUBLIC_KEY || '#invalidPublicKey'
15
+ API_PRIVATE_KEY = API_PRIVATE_KEY || '#invalidPrivateKey'
16
+
17
+ const AUTH_ERROR_MESSAGE = 'API key authentication/authorization failure. You may be unauthorized to access the API or your API key may be expired. Visit API keys management section to check your keys.'
18
+ const AUTH_V3_ERROR_MESSAGE = 'Unauthorized'
19
+ const AUTH_ERROR_CODE = 401
20
+
21
+ describe ( 'invalid token' , function ( ) {
22
+ var v4Config = {
23
+ 'url' : 'api.mailjet.com' ,
24
+ 'version' : 'v4' ,
25
+ 'output' : 'json' ,
26
+ 'perform_api_call' : true ,
27
+ 'secured' : true
28
+ }
29
+ var v4Client = Mailjet . connect ( API_TOKEN , v4Config )
30
+
31
+ describe ( 'get' , function ( ) {
32
+
33
+ var smsGet = v4Client . get ( 'sms' )
34
+
35
+ it ( 'check error message' , function ( done ) {
36
+ var promise = smsGet
37
+ . request ( { FromTS : + new Date , ToTS : + new Date } )
38
+ . then ( function ( response ) {
39
+ expect ( response . body ) . to . be . a ( 'object' )
40
+ done ( )
41
+ } )
42
+ . catch ( function ( err ) {
43
+ expect ( err . ErrorMessage ) . to . equal ( AUTH_ERROR_MESSAGE )
44
+ done ( )
45
+ } )
46
+ expect ( Promise . prototype . isPrototypeOf ( promise ) ) . to . equal ( true )
47
+ } )
48
+
49
+ it ( 'check status code' , function ( done ) {
50
+ var promise = smsGet
51
+ . request ( { FromTS : + new Date , ToTS : + new Date } )
52
+ . then ( function ( response ) {
53
+ expect ( response . body ) . to . be . a ( 'object' )
54
+ expect ( response . body . Data . length ) . to . equal ( 0 )
55
+ done ( )
56
+ } )
57
+ . catch ( function ( err ) {
58
+ expect ( err . statusCode ) . to . equal ( AUTH_ERROR_CODE )
59
+ done ( )
60
+ } )
61
+ expect ( Promise . prototype . isPrototypeOf ( promise ) ) . to . equal ( true )
62
+ } )
63
+
64
+ it ( 'check response body is not null on error' , function ( done ) {
65
+ var promise = smsGet
66
+ . request ( { FromTS : + new Date , ToTS : + new Date } )
67
+ . then ( function ( response ) {
68
+ expect ( response . body ) . to . be . a ( 'object' )
69
+ expect ( response . body . Data . length ) . to . equal ( 0 )
70
+ done ( )
71
+ } )
72
+ . catch ( function ( err ) {
73
+ expect ( err . response ) . to . not . equal ( null )
74
+ done ( )
75
+ } )
76
+ expect ( Promise . prototype . isPrototypeOf ( promise ) ) . to . equal ( true )
77
+ } )
78
+
79
+ it ( 'check error identitfier is not empty string' , function ( done ) {
80
+ var promise = smsGet
81
+ . request ( { FromTS : + new Date , ToTS : + new Date } )
82
+ . then ( function ( response ) {
83
+ expect ( response . body ) . to . be . a ( 'object' )
84
+ expect ( response . body . Data . length ) . to . equal ( 0 )
85
+ done ( )
86
+ } )
87
+ . catch ( function ( err ) {
88
+ expect ( err . ErrorIdentifier ) . to . not . equal ( '' )
89
+ done ( )
90
+ } )
91
+ expect ( Promise . prototype . isPrototypeOf ( promise ) ) . to . equal ( true )
92
+ } )
93
+ } )
94
+
95
+ describe ( 'invalid public/private keys' , function ( ) {
96
+ var v3Config = {
97
+ url : 'api.mailjet.com' ,
98
+ version : 'v3' ,
99
+ output : 'json' ,
100
+ perform_api_call : true ,
101
+ secured : true
102
+ }
103
+ var v3Client = Mailjet . connect ( API_PUBLIC_KEY , API_PRIVATE_KEY , v3Config )
104
+
105
+ describe ( 'get' , function ( ) {
106
+
107
+ var contact = v3Client . get ( 'contact' )
108
+
109
+ it ( 'check v3 error message' , function ( done ) {
110
+ contact . request ( )
111
+ . then ( function ( result ) {
112
+ result . body . should . be . a ( 'object' )
113
+ expect ( result . response . statusCode ) . to . equal ( 200 )
114
+ done ( )
115
+ } )
116
+ . catch ( function ( err ) {
117
+ expect ( err . ErrorMessage ) . to . equal ( AUTH_V3_ERROR_MESSAGE )
118
+ done ( )
119
+ } )
120
+ } )
121
+
122
+ it ( 'check v3 error status code' , function ( done ) {
123
+ contact . request ( )
124
+ . then ( function ( result ) {
125
+ result . body . should . be . a ( 'object' )
126
+ expect ( result . response . statusCode ) . to . equal ( 200 )
127
+ done ( )
128
+ } )
129
+ . catch ( function ( err ) {
130
+ expect ( err . statusCode ) . to . equal ( AUTH_ERROR_CODE )
131
+ done ( )
132
+ } )
133
+ } )
134
+
135
+ it ( 'check v3 response body is not null on error' , function ( done ) {
136
+ contact . request ( )
137
+ . then ( function ( result ) {
138
+ result . body . should . be . a ( 'object' )
139
+ expect ( result . response . statusCode ) . to . equal ( 200 )
140
+ done ( )
141
+ } )
142
+ . catch ( function ( err ) {
143
+ expect ( err . response ) . to . not . equal ( null )
144
+ done ( )
145
+ } )
146
+ } )
147
+
148
+ it ( 'check v3 error identitfier is not empty string' , function ( done ) {
149
+ contact . request ( )
150
+ . then ( function ( result ) {
151
+ result . body . should . be . a ( 'object' )
152
+ expect ( result . response . statusCode ) . to . equal ( 200 )
153
+ done ( )
154
+ } )
155
+ . catch ( function ( err ) {
156
+ expect ( err . ErrorIdentifier ) . to . not . equal ( '' )
157
+ done ( )
158
+ } )
159
+ } )
160
+
161
+ } )
162
+ } )
163
+ } )
164
+ } )
0 commit comments