1
- import axios , { AxiosError } from 'axios' ;
1
+ import axios from 'axios' ;
2
2
import {
3
3
mockRdi ,
4
4
mockRdiClientMetadata ,
@@ -7,13 +7,12 @@ import {
7
7
mockRdiPipeline ,
8
8
mockRdiSchema ,
9
9
mockRdiStatisticsData ,
10
+ mockRdiUnauthorizedError ,
10
11
} from 'src/__mocks__' ;
11
- import errorMessages from 'src/constants/error-messages' ;
12
12
import { sign } from 'jsonwebtoken' ;
13
13
import { ApiRdiClient } from './api.rdi.client' ;
14
14
import { RdiDyRunJobStatus , RdiPipeline , RdiStatisticsStatus } from '../models' ;
15
15
import { RdiUrl , TOKEN_THRESHOLD } from '../constants' ;
16
- import { wrapRdiPipelineError } from '../exceptions' ;
17
16
18
17
const mockedAxios = axios as jest . Mocked < typeof axios > ;
19
18
jest . mock ( 'axios' ) ;
@@ -40,17 +39,10 @@ describe('ApiRdiClient', () => {
40
39
} ) ;
41
40
42
41
it ( 'should throw error if request fails' , async ( ) => {
43
- const error = {
44
- message : errorMessages . UNAUTHORIZED ,
45
- response : {
46
- status : 401 ,
47
- } ,
48
- } ;
49
-
50
42
mockedAxios . get . mockResolvedValueOnce ( { data : mockRdiConfigSchema } ) ;
51
- mockedAxios . get . mockRejectedValueOnce ( error ) ;
43
+ mockedAxios . get . mockRejectedValueOnce ( mockRdiUnauthorizedError ) ;
52
44
53
- await expect ( client . getSchema ( ) ) . rejects . toThrow ( errorMessages . UNAUTHORIZED ) ;
45
+ await expect ( client . getSchema ( ) ) . rejects . toThrow ( mockRdiUnauthorizedError . message ) ;
54
46
} ) ;
55
47
} ) ;
56
48
@@ -70,10 +62,9 @@ describe('ApiRdiClient', () => {
70
62
} ) ;
71
63
72
64
it ( 'should throw error if request fails' , async ( ) => {
73
- const error = new Error ( 'test error' ) ;
74
- mockedAxios . get . mockRejectedValueOnce ( error ) ;
65
+ mockedAxios . get . mockRejectedValueOnce ( mockRdiUnauthorizedError ) ;
75
66
76
- await expect ( client . getPipeline ( ) ) . rejects . toThrow ( errorMessages . INTERNAL_SERVER_ERROR ) ;
67
+ await expect ( client . getPipeline ( ) ) . rejects . toThrow ( mockRdiUnauthorizedError . message ) ;
77
68
} ) ;
78
69
} ) ;
79
70
@@ -89,10 +80,9 @@ describe('ApiRdiClient', () => {
89
80
} ) ;
90
81
91
82
it ( 'should throw an error when API call fails' , async ( ) => {
92
- const mockError = new Error ( 'API call failed' ) ;
93
- mockedAxios . get . mockRejectedValueOnce ( mockError ) ;
83
+ mockedAxios . get . mockRejectedValueOnce ( mockRdiUnauthorizedError ) ;
94
84
95
- await expect ( client . getStrategies ( ) ) . rejects . toThrow ( errorMessages . INTERNAL_SERVER_ERROR ) ;
85
+ await expect ( client . getStrategies ( ) ) . rejects . toThrow ( mockRdiUnauthorizedError . message ) ;
96
86
expect ( axios . get ) . toHaveBeenCalledWith ( RdiUrl . GetStrategies ) ;
97
87
} ) ;
98
88
} ) ;
@@ -112,11 +102,10 @@ describe('ApiRdiClient', () => {
112
102
} ) ;
113
103
114
104
it ( 'should throw an error when the API call fails' , async ( ) => {
115
- const expectedError = new Error ( 'API call failed' ) ;
116
- mockedAxios . get . mockRejectedValueOnce ( expectedError ) ;
105
+ mockedAxios . get . mockRejectedValueOnce ( mockRdiUnauthorizedError ) ;
117
106
118
107
await expect ( client . getConfigTemplate ( pipelineType , dbType ) )
119
- . rejects . toThrowError ( errorMessages . INTERNAL_SERVER_ERROR ) ;
108
+ . rejects . toThrowError ( mockRdiUnauthorizedError . message ) ;
120
109
} ) ;
121
110
} ) ;
122
111
@@ -141,10 +130,9 @@ describe('ApiRdiClient', () => {
141
130
} ) ;
142
131
143
132
it ( 'should throw an error when the API call fails' , async ( ) => {
144
- const expectedError = new Error ( 'API call failed' ) ;
145
- mockedAxios . get . mockRejectedValueOnce ( expectedError ) ;
133
+ mockedAxios . get . mockRejectedValueOnce ( mockRdiUnauthorizedError ) ;
146
134
147
- await expect ( client . getJobTemplate ( pipelineType ) ) . rejects . toThrowError ( errorMessages . INTERNAL_SERVER_ERROR ) ;
135
+ await expect ( client . getJobTemplate ( pipelineType ) ) . rejects . toThrowError ( mockRdiUnauthorizedError . message ) ;
148
136
} ) ;
149
137
} ) ;
150
138
@@ -170,11 +158,9 @@ describe('ApiRdiClient', () => {
170
158
} ) ;
171
159
172
160
it ( 'should throw an error if the deployment fails' , async ( ) => {
173
- const errorMessage = 'Deployment failed' ;
174
- const errorResponse = { response : { data : { message : errorMessage } } } ;
175
- mockedAxios . post . mockRejectedValueOnce ( errorResponse ) ;
161
+ mockedAxios . post . mockRejectedValueOnce ( mockRdiUnauthorizedError ) ;
176
162
177
- await expect ( client . deploy ( mockRdiPipeline ) ) . rejects . toThrow ( errorMessage ) ;
163
+ await expect ( client . deploy ( mockRdiPipeline ) ) . rejects . toThrow ( mockRdiUnauthorizedError . message ) ;
178
164
} ) ;
179
165
} ) ;
180
166
@@ -197,10 +183,9 @@ describe('ApiRdiClient', () => {
197
183
} ) ;
198
184
199
185
it ( 'should throw an error if the client call fails' , async ( ) => {
200
- const mockError = new Error ( 'mock error' ) ;
201
- mockedAxios . post . mockRejectedValueOnce ( mockError ) ;
186
+ mockedAxios . post . mockRejectedValueOnce ( mockRdiUnauthorizedError ) ;
202
187
203
- await expect ( client . dryRunJob ( mockRdiDryRunJob ) ) . rejects . toThrow ( errorMessages . INTERNAL_SERVER_ERROR ) ;
188
+ await expect ( client . dryRunJob ( mockRdiDryRunJob ) ) . rejects . toThrow ( mockRdiUnauthorizedError . message ) ;
204
189
} ) ;
205
190
} ) ;
206
191
@@ -219,10 +204,9 @@ describe('ApiRdiClient', () => {
219
204
220
205
it ( 'should throw an error if the request fails' , async ( ) => {
221
206
const config = { } ;
222
- const error = new Error ( 'Test error' ) ;
223
- mockedAxios . post . mockRejectedValueOnce ( error ) ;
207
+ mockedAxios . post . mockRejectedValueOnce ( mockRdiUnauthorizedError ) ;
224
208
225
- await expect ( client . testConnections ( config ) ) . rejects . toThrow ( errorMessages . INTERNAL_SERVER_ERROR ) ;
209
+ await expect ( client . testConnections ( config ) ) . rejects . toThrow ( mockRdiUnauthorizedError . message ) ;
226
210
expect ( mockedAxios . post ) . toHaveBeenCalledWith ( RdiUrl . TestConnections , config ) ;
227
211
} ) ;
228
212
} ) ;
@@ -239,10 +223,9 @@ describe('ApiRdiClient', () => {
239
223
} ) ;
240
224
241
225
it ( 'should throw an error when API call fails' , async ( ) => {
242
- const mockError = new Error ( 'API call failed' ) ;
243
- mockedAxios . get . mockRejectedValue ( mockError ) ;
226
+ mockedAxios . get . mockRejectedValue ( mockRdiUnauthorizedError ) ;
244
227
245
- await expect ( client . getPipelineStatus ( ) ) . rejects . toThrow ( errorMessages . INTERNAL_SERVER_ERROR ) ;
228
+ await expect ( client . getPipelineStatus ( ) ) . rejects . toThrow ( mockRdiUnauthorizedError . message ) ;
246
229
expect ( mockedAxios . get ) . toHaveBeenCalledWith ( RdiUrl . GetPipelineStatus ) ;
247
230
} ) ;
248
231
} ) ;
@@ -262,14 +245,13 @@ describe('ApiRdiClient', () => {
262
245
} ) ;
263
246
264
247
it ( 'should return fail status and error message when API call fails' , async ( ) => {
265
- const errorMessage = 'API call failed' ;
266
- mockedAxios . get . mockRejectedValue ( new Error ( errorMessage ) ) ;
248
+ mockedAxios . get . mockRejectedValue ( mockRdiUnauthorizedError ) ;
267
249
268
250
const result = await client . getStatistics ( ) ;
269
251
270
252
expect ( mockedAxios . get ) . toHaveBeenCalledWith ( RdiUrl . GetStatistics , { params : { } } ) ;
271
253
expect ( result . status ) . toBe ( RdiStatisticsStatus . Fail ) ;
272
- expect ( result . error ) . toBe ( errorMessage ) ;
254
+ expect ( result . error ) . toBe ( mockRdiUnauthorizedError . message ) ;
273
255
} ) ;
274
256
} ) ;
275
257
@@ -285,9 +267,9 @@ describe('ApiRdiClient', () => {
285
267
} ) ;
286
268
287
269
it ( 'should throw an error if the API call fails' , async ( ) => {
288
- mockedAxios . get . mockRejectedValueOnce ( new Error ( 'API error' ) ) ;
270
+ mockedAxios . get . mockRejectedValueOnce ( mockRdiUnauthorizedError ) ;
289
271
290
- await expect ( client . getJobFunctions ( ) ) . rejects . toThrow ( errorMessages . INTERNAL_SERVER_ERROR ) ;
272
+ await expect ( client . getJobFunctions ( ) ) . rejects . toThrow ( mockRdiUnauthorizedError . message ) ;
291
273
} ) ;
292
274
} ) ;
293
275
@@ -313,10 +295,9 @@ describe('ApiRdiClient', () => {
313
295
} ) ;
314
296
315
297
it ( 'should throw an error if login fails' , async ( ) => {
316
- const error = new AxiosError ( 'Login failed' ) ;
317
- mockedAxios . post . mockRejectedValueOnce ( error ) ;
298
+ mockedAxios . post . mockRejectedValueOnce ( mockRdiUnauthorizedError ) ;
318
299
319
- await expect ( client . connect ( ) ) . rejects . toThrow ( wrapRdiPipelineError ( error ) ) ;
300
+ await expect ( client . connect ( ) ) . rejects . toThrow ( mockRdiUnauthorizedError . message ) ;
320
301
} ) ;
321
302
} ) ;
322
303
@@ -346,7 +327,6 @@ describe('ApiRdiClient', () => {
346
327
347
328
describe ( 'pollActionStatus' , ( ) => {
348
329
const responseData = 'some data' ;
349
- const error = new Error ( 'Test error' ) ;
350
330
const actionId = 'test-action-id' ;
351
331
352
332
it ( 'should return response data on success' , async ( ) => {
@@ -365,9 +345,9 @@ describe('ApiRdiClient', () => {
365
345
} ) ;
366
346
367
347
it ( 'should throw an error if an error occurs during polling' , async ( ) => {
368
- mockedAxios . get . mockRejectedValueOnce ( error ) ;
348
+ mockedAxios . get . mockRejectedValueOnce ( mockRdiUnauthorizedError ) ;
369
349
370
- await expect ( client [ 'pollActionStatus' ] ( actionId ) ) . rejects . toThrow ( errorMessages . INTERNAL_SERVER_ERROR ) ;
350
+ await expect ( client [ 'pollActionStatus' ] ( actionId ) ) . rejects . toThrow ( mockRdiUnauthorizedError . message ) ;
371
351
expect ( mockedAxios . get ) . toHaveBeenCalledWith ( `${ RdiUrl . Action } /${ actionId } ` , { signal : undefined } ) ;
372
352
} ) ;
373
353
} ) ;
0 commit comments