@@ -4,9 +4,35 @@ import {
44import fetchMock from "jest-fetch-mock" ;
55import { ClientCredentials } from "simple-oauth2" ;
66
7- describe ( "ServerClient" , ( ) => {
8- let client : ServerClient ;
7+ let client : ServerClient ;
8+ let oauthClientMock : ClientCredentials ;
9+
10+ beforeEach ( ( ) => {
11+ const getTokenMock = jest . fn ( ) . mockResolvedValue ( {
12+ token : {
13+ access_token : "mocked-oauth-token" ,
14+ } ,
15+ expired : jest . fn ( ) . mockReturnValue ( false ) ,
16+ } ) ;
17+
18+ oauthClientMock = {
19+ getToken : getTokenMock ,
20+ } as unknown as ClientCredentials ;
21+
22+ client = new ServerClient (
23+ {
24+ publicKey : "test-public-key" ,
25+ secretKey : "test-secret-key" ,
26+ oauth : {
27+ clientId : "test-client-id" ,
28+ clientSecret : "test-client-secret" ,
29+ } ,
30+ } ,
31+ oauthClientMock ,
32+ ) ;
33+ } ) ;
934
35+ describe ( "ServerClient" , ( ) => {
1036 beforeEach ( ( ) => {
1137 fetchMock . resetMocks ( ) ;
1238 } ) ;
@@ -18,7 +44,7 @@ describe("ServerClient", () => {
1844 secretKey : "test-secret-key" ,
1945 } ;
2046
21- const client = createClient ( params ) ;
47+ client = createClient ( params ) ;
2248 expect ( client ) . toBeInstanceOf ( ServerClient ) ;
2349 } ) ;
2450 } ) ;
@@ -36,11 +62,6 @@ describe("ServerClient", () => {
3662 } ,
3763 ) ;
3864
39- client = new ServerClient ( {
40- publicKey : "test-public-key" ,
41- secretKey : "test-secret-key" ,
42- } ) ;
43-
4465 const result = await client . makeRequest ( "/test-path" , {
4566 method : "GET" ,
4667 } ) ;
@@ -66,11 +87,6 @@ describe("ServerClient", () => {
6687 } ,
6788 ) ;
6889
69- client = new ServerClient ( {
70- publicKey : "test-public-key" ,
71- secretKey : "test-secret-key" ,
72- } ) ;
73-
7490 const result = await client . makeRequest ( "/test-path" , {
7591 method : "POST" ,
7692 body : {
@@ -103,11 +119,6 @@ describe("ServerClient", () => {
103119 } ,
104120 } ) ;
105121
106- client = new ServerClient ( {
107- publicKey : "test-public-key" ,
108- secretKey : "test-secret-key" ,
109- } ) ;
110-
111122 await expect ( client . makeRequest ( "/bad-path" ) ) . rejects . toThrow ( "HTTP error! status: 404, body: Not Found" ) ;
112123 expect ( fetchMock ) . toHaveBeenCalledWith (
113124 "https://api.pipedream.com/v1/bad-path" ,
@@ -118,28 +129,6 @@ describe("ServerClient", () => {
118129
119130 describe ( "makeApiRequest" , ( ) => {
120131 it ( "should include OAuth Authorization header and make an API request" , async ( ) => {
121- const getTokenMock = jest . fn ( ) . mockResolvedValue ( {
122- token : {
123- access_token : "mocked-oauth-token" ,
124- } ,
125- expired : jest . fn ( ) . mockReturnValue ( false ) ,
126- } ) ;
127-
128- const oauthClientMock = {
129- getToken : getTokenMock ,
130- } as unknown as ClientCredentials ;
131-
132- // Inject the mock oauthClient into the ServerClient instance
133- client = new ServerClient (
134- {
135- publicKey : "test-public-key" ,
136- secretKey : "test-secret-key" ,
137- oauthClientId : "test-client-id" ,
138- oauthClientSecret : "test-client-secret" ,
139- } ,
140- oauthClientMock ,
141- ) ;
142-
143132 fetchMock . mockResponseOnce (
144133 JSON . stringify ( {
145134 success : true ,
@@ -168,34 +157,13 @@ describe("ServerClient", () => {
168157 } ) ;
169158
170159 it ( "should handle OAuth token retrieval failure" , async ( ) => {
171- // Create a mock oauthClient that fails to get a token
172- const getTokenMock = jest . fn ( ) . mockRejectedValue ( new Error ( "Invalid credentials" ) ) ;
173-
174- const oauthClientMock = {
175- getToken : getTokenMock ,
176- } as unknown as ClientCredentials ;
177-
178- client = new ServerClient (
179- {
180- publicKey : "test-public-key" ,
181- secretKey : "test-secret-key" ,
182- oauthClientId : "test-client-id" ,
183- oauthClientSecret : "test-client-secret" ,
184- } ,
185- oauthClientMock ,
186- ) ;
187-
160+ oauthClientMock . getToken = jest . fn ( ) . mockRejectedValue ( new Error ( "Invalid credentials" ) ) ;
188161 await expect ( client [ "makeApiRequest" ] ( "/test-path" ) ) . rejects . toThrow ( "Failed to obtain OAuth token: Invalid credentials" ) ;
189162 } ) ;
190163 } ) ;
191164
192165 describe ( "makeConnectRequest" , ( ) => {
193166 it ( "should include Connect Authorization header and make a request" , async ( ) => {
194- client = new ServerClient ( {
195- publicKey : "test-public-key" ,
196- secretKey : "test-secret-key" ,
197- } ) ;
198-
199167 fetchMock . mockResponseOnce (
200168 JSON . stringify ( {
201169 success : true ,
@@ -237,11 +205,6 @@ describe("ServerClient", () => {
237205 } ,
238206 ) ;
239207
240- client = new ServerClient ( {
241- publicKey : "test-public-key" ,
242- secretKey : "test-secret-key" ,
243- } ) ;
244-
245208 const result = await client . connectTokenCreate ( {
246209 external_user_id : "user-id" ,
247210 } ) ;
@@ -279,11 +242,6 @@ describe("ServerClient", () => {
279242 } ,
280243 ) ;
281244
282- client = new ServerClient ( {
283- publicKey : "test-public-key" ,
284- secretKey : "test-secret-key" ,
285- } ) ;
286-
287245 const result = await client . connectTokenCreate ( {
288246 external_user_id : "user-id" ,
289247 success_redirect_uri : "https://example.com/success" ,
@@ -329,11 +287,6 @@ describe("ServerClient", () => {
329287 } ,
330288 ) ;
331289
332- client = new ServerClient ( {
333- publicKey : "test-public-key" ,
334- secretKey : "test-secret-key" ,
335- } ) ;
336-
337290 const result = await client . getAccounts ( {
338291 include_credentials : 1 ,
339292 } ) ;
@@ -365,11 +318,6 @@ describe("ServerClient", () => {
365318 } ,
366319 ) ;
367320
368- client = new ServerClient ( {
369- publicKey : "test-public-key" ,
370- secretKey : "test-secret-key" ,
371- } ) ;
372-
373321 const result = await client . getAccount ( "account-1" ) ;
374322
375323 expect ( result ) . toEqual ( {
@@ -399,11 +347,6 @@ describe("ServerClient", () => {
399347 } ,
400348 ) ;
401349
402- client = new ServerClient ( {
403- publicKey : "test-public-key" ,
404- secretKey : "test-secret-key" ,
405- } ) ;
406-
407350 const result = await client . getAccountsByApp ( "app-1" ) ;
408351
409352 expect ( result ) . toEqual ( [
@@ -435,11 +378,6 @@ describe("ServerClient", () => {
435378 } ,
436379 ) ;
437380
438- client = new ServerClient ( {
439- publicKey : "test-public-key" ,
440- secretKey : "test-secret-key" ,
441- } ) ;
442-
443381 const result = await client . getAccountsByExternalId ( "external-id-1" ) ;
444382
445383 expect ( result ) . toEqual ( [
@@ -461,11 +399,6 @@ describe("ServerClient", () => {
461399 status : 204 ,
462400 } ) ;
463401
464- client = new ServerClient ( {
465- publicKey : "test-public-key" ,
466- secretKey : "test-secret-key" ,
467- } ) ;
468-
469402 await client . deleteAccount ( "account-1" ) ;
470403
471404 expect ( fetchMock ) . toHaveBeenCalledWith (
@@ -483,11 +416,6 @@ describe("ServerClient", () => {
483416 status : 204 ,
484417 } ) ;
485418
486- client = new ServerClient ( {
487- publicKey : "test-public-key" ,
488- secretKey : "test-secret-key" ,
489- } ) ;
490-
491419 await client . deleteAccountsByApp ( "app-1" ) ;
492420
493421 expect ( fetchMock ) . toHaveBeenCalledWith (
@@ -505,11 +433,6 @@ describe("ServerClient", () => {
505433 status : 204 ,
506434 } ) ;
507435
508- client = new ServerClient ( {
509- publicKey : "test-public-key" ,
510- secretKey : "test-secret-key" ,
511- } ) ;
512-
513436 await client . deleteExternalUser ( "external-id-1" ) ;
514437
515438 expect ( fetchMock ) . toHaveBeenCalledWith (
@@ -539,11 +462,6 @@ describe("ServerClient", () => {
539462 } ,
540463 ) ;
541464
542- client = new ServerClient ( {
543- publicKey : "test-public-key" ,
544- secretKey : "test-secret-key" ,
545- } ) ;
546-
547465 const result = await client . getProjectInfo ( ) ;
548466
549467 expect ( result ) . toEqual ( {
@@ -565,31 +483,6 @@ describe("ServerClient", () => {
565483
566484 describe ( "invokeWorkflow" , ( ) => {
567485 it ( "should invoke a workflow with provided URL and body, with no auth type" , async ( ) => {
568- // Create a mock oauthClient
569- const getTokenMock = jest . fn ( ) . mockResolvedValue ( {
570- token : {
571- access_token : "mocked-oauth-token" ,
572- } ,
573- expired : jest . fn ( ) . mockReturnValue ( false ) ,
574- } ) ;
575-
576- const oauthClientMock = {
577- getToken : getTokenMock ,
578- } as unknown as ClientCredentials ;
579-
580- // Inject the mock oauthClient into the ServerClient instance
581- const client = new ServerClient (
582- {
583- publicKey : "test-public-key" ,
584- secretKey : "test" ,
585- oauth : {
586- clientId : "test-client-id" ,
587- clientSecret : "test-client-secret" ,
588- } ,
589- } ,
590- oauthClientMock ,
591- ) ;
592-
593486 fetchMock . mockResponseOnce (
594487 JSON . stringify ( {
595488 result : "workflow-response" ,
0 commit comments