1
+ import { describe , beforeAll , it , vi , Mock } from 'vitest'
2
+ import { ConsentApi } from 'obp-api-typescript'
3
+
4
+ const mockGetOAuthHeader = vi . fn ( async ( ) => ( `OAuth oauth_consumer_key="jgaawf2fnj4yixqdsfaq4gipt4v1wvgsxgre",oauth_nonce="JiGDBWA3MAyKtsd9qkfWCxfju36bMjsA",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1741364123",oauth_version="1.0",oauth_signature="sa%2FRylnsdfLK8VPZI%2F2WkGFlTKs%3D"` ) ) ;
5
+ const mockGetDirectLoginToken = vi . fn ( async ( ) => {
6
+ return "eyJhbGciOisdReI1NiJ9.eyIiOiIifQ.neaNv-ltBoEEyErvmhmEbYIG8KLdjqRfT7hA7uKPdvs"
7
+ } ) ;
8
+
9
+ vi . mock ( '../services/OBPClientService' , ( ) => {
10
+ return {
11
+ default : vi . fn ( ) . mockImplementation ( ( ) => {
12
+ return {
13
+ // mock getOAuthHeader
14
+ getOBPClientConfig : vi . fn ( ( ) => ( { baseUri : 'https://test.openbankproject.com' } ) ) ,
15
+ getOAuthHeader : mockGetOAuthHeader ,
16
+ getDirectLoginToken : mockGetDirectLoginToken ,
17
+ }
18
+ } ) ,
19
+ }
20
+ } )
21
+
22
+ import OBPConsentsService from '../services/OBPConsentsService' ;
23
+
24
+ describe ( 'OBPConsentsService.createConsentClient' , ( ) => {
25
+ let obpConsentsService : OBPConsentsService ;
26
+ let mockedOAuthHeaders : string ;
27
+
28
+ beforeEach ( async ( ) => {
29
+
30
+ vi . clearAllMocks ( ) ;
31
+
32
+ mockGetOAuthHeader . mockImplementation ( async ( ) => `OAuth oauth_consumer_key="jgaawf2fnj4yixqdsfaq4gipt4v1wvgsxgre",oauth_nonce="JiGDBWA3MAyKtsd9qkfWCxfju36bMjsA",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1741364123",oauth_version="1.0",oauth_signature="sa%2FRylnsdfLK8VPZI%2F2WkGFlTKs%3D"` ) ;
33
+ // Mock the OBP Client service for getting the OAuth and direct login headers
34
+ obpConsentsService = new OBPConsentsService ( ) ;
35
+ } ) ;
36
+
37
+ it ( 'should return a ConsentApi client for logged in user' , async ( ) => {
38
+ const consentClient = await obpConsentsService . createConsentClient ( '/consents' , 'POST' , 'logged_in_user' ) ;
39
+ expect ( consentClient ) . toBeDefined ( ) ;
40
+ expect ( obpConsentsService . consentsClient ) . toBe ( consentClient ) ;
41
+ expect ( consentClient ) . toBeInstanceOf ( ConsentApi ) ;
42
+ } )
43
+
44
+ it ( 'should return a ConsentApi client for API Explorer' , async ( ) => {
45
+ const consentClient = await obpConsentsService . createConsentClient ( '/consents' , 'POST' , 'API_Explorer' ) ;
46
+ expect ( consentClient ) . toBeDefined ( ) ;
47
+ expect ( obpConsentsService . consentsClient ) . toBe ( consentClient ) ;
48
+ expect ( consentClient ) . toBeInstanceOf ( ConsentApi ) ;
49
+ } )
50
+
51
+ it ( 'should throw an error if the client type is not recognized' , async ( ) => {
52
+ await expect ( obpConsentsService . createConsentClient ( '/consents' , 'POST' , 'unknown' ) ) . rejects . toThrow ( ) ;
53
+ } )
54
+
55
+ it ( 'should throw correct error if OBPClientService.getOAuthHeader fails for logged in user' , async ( ) => {
56
+
57
+ mockGetOAuthHeader . mockImplementationOnce ( async ( ) => {
58
+ throw new Error ( 'OAuth header error' ) ;
59
+ } ) ;
60
+
61
+ await expect ( obpConsentsService . createConsentClient ( '/consents' , 'POST' , 'logged_in_user' ) )
62
+ . rejects . toThrow ( `Could not create Consents API client for logged in user, Error: OAuth header error` ) ;
63
+ } )
64
+
65
+ it ( 'should throw correct error if OBPClientService.getDirectLoginToken fails for API Explorer' , async ( ) => {
66
+
67
+ mockGetDirectLoginToken . mockImplementationOnce ( async ( ) => {
68
+ throw new Error ( 'Direct login token error' ) ;
69
+ } ) ;
70
+
71
+ await expect ( obpConsentsService . createConsentClient ( '/consents' , 'POST' , 'API_Explorer' ) )
72
+ . rejects . toThrow ( `Could not create Consents API client for API Explorer, Error: Direct login token error` ) ;
73
+ } )
74
+ } )
0 commit comments