Skip to content

Commit f8b313a

Browse files
committed
add test for OBPConsentsService
1 parent e5df920 commit f8b313a

File tree

3 files changed

+136
-1
lines changed

3 files changed

+136
-1
lines changed

server/services/OBPConsentsService.ts

+62
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,72 @@
11
import { Service } from 'typedi'
2+
import { Configuration, ConsentApi} from 'obp-api-typescript'
3+
import OBPClientService from './OBPClientService'
24

35
@Service()
46
export default class OBPConsentsService {
7+
private consentApiConfig: Configuration
8+
public obpClientService: OBPClientService
9+
public consentsClient: ConsentApi // This needs to be changed once we migrate away from the old OBP SDK
510
constructor() {
11+
this.obpClientService = new OBPClientService()
12+
}
13+
/**
14+
* Function to create a OBP Consents API client
15+
* at differnt times in the consent flow we will either need to be acting as the logged in user, or the API Explorer II consumer
16+
*
17+
* @param path
18+
* @param method
19+
* @param as_client
20+
* @returns
21+
*/
22+
async createConsentClient(path: string, method: string, as_consumer: "logged_in_user" | "API_Explorer"): Promise<ConsentApi | undefined> {
23+
// This function creates a Consents API client as the logged in user, using their OAuth1 headers
24+
if (as_consumer === "logged_in_user") {
25+
try {
26+
27+
// Get the OAuth1 headers for the logged in user to use in the API call
28+
const oauth1Headers = await this.obpClientService.getOAuthHeader(path, method)
29+
const authHeader = "OAuth " + oauth1Headers
30+
// Set config for the Consents API client from the new typescript SDK
31+
this.consentApiConfig = new Configuration({
32+
basePath: this.obpClientService.getOBPClientConfig().baseUri,
33+
accessToken: authHeader
34+
})
35+
36+
// Create the Consents API client
37+
this.consentsClient = new ConsentApi(this.consentApiConfig)
38+
return this.consentsClient
39+
40+
} catch (error) {
41+
console.error(error)
42+
throw new Error(`Could not create Consents API client for logged in user, ${error}`)
43+
}
44+
45+
} else if (as_consumer === "API_Explorer") {
646

47+
try {
48+
// Get direct Login token from OBP for API Explorer II
49+
const directLoginToken = await this.obpClientService.getDirectLoginToken()
50+
const directLoginHeader = "DirectLogin token=" + directLoginToken
51+
// Set config for the Consents API client from the new typescript SDK
52+
this.consentApiConfig = new Configuration({
53+
basePath: this.obpClientService.getOBPClientConfig().baseUri,
54+
accessToken: directLoginHeader
55+
})
56+
57+
this.consentsClient = new ConsentApi(this.consentApiConfig)
58+
return this.consentsClient
59+
60+
} catch (error) {
61+
console.error(error)
62+
throw new Error(`Could not create Consents API client for API Explorer, ${error}`)
63+
}
64+
} else {
65+
throw new Error("Invalid client type, must be 'logged_in_user' or 'API_Explorer'")
66+
}
767
}
68+
69+
870
async createConsentRequest(): Promise<any> {
971

1072
}
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
})

server/test/opey-controller.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ describe('OpeyController consents flow', () => {
133133
let opeyController: OpeyController
134134

135135
beforeAll(() => {
136-
137136
mockOBPClientService = {
138137
get: vi.fn(async () => {
139138
Promise.resolve({})

0 commit comments

Comments
 (0)