Skip to content

Commit 3cb91e7

Browse files
authored
Merge pull request #349 from ForgeRock/update-protect-api
Update Protect module and E2E tests
2 parents 5a1d97b + 65f7bfd commit 3cb91e7

6 files changed

Lines changed: 326 additions & 254 deletions

File tree

e2e/davinci-app/main.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,16 @@ const urlParams = new URLSearchParams(window.location.search);
7979

8080
(async () => {
8181
const davinciClient: DavinciClient = await davinci({ config, logger, requestMiddleware });
82-
const protectAPI = await protect({ envId: '02fb4743-189a-4bc7-9d6c-a919edfe6447' });
82+
const protectAPI = protect({ envId: '02fb4743-189a-4bc7-9d6c-a919edfe6447' });
8383
const continueToken = urlParams.get('continueToken');
8484
const formEl = document.getElementById('form') as HTMLFormElement;
8585
let resumed: any;
8686

8787
// Initialize Protect
88-
await protectAPI.start();
88+
const error = await protectAPI.start();
89+
if (error?.error) {
90+
console.error('Error starting Protect:', error.error);
91+
}
8992

9093
if (continueToken) {
9194
resumed = await davinciClient.resume({ continueToken });
@@ -274,6 +277,11 @@ const urlParams = new URLSearchParams(window.location.search);
274277

275278
async function updateProtectCollector(protectCollector: ProtectCollector) {
276279
const data = await protectAPI.getData();
280+
if (typeof data !== 'string' && 'error' in data) {
281+
console.error(`Failed to retrieve data from PingOne Protect: ${data.error}`);
282+
return;
283+
}
284+
277285
const updater = davinciClient.update(protectCollector);
278286
const error = updater(data);
279287
if (error && 'error' in error) {

e2e/davinci-suites/src/middleware.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
import { expect, test } from '@playwright/test';
88
import { asyncEvents } from './utils/async-events.js';
9+
import { password, username } from './utils/demo-user.js';
910

1011
test('Test middleware on test page', async ({ page }) => {
1112
const networkArray = [];
@@ -24,6 +25,13 @@ test('Test middleware on test page', async ({ page }) => {
2425

2526
await expect(page.getByText('Username/Password Form')).toBeVisible();
2627

28+
await page.getByLabel('Username').fill(username);
29+
await page.getByLabel('Password').fill(password);
30+
31+
await page.getByRole('button', { name: 'Sign On' }).click();
32+
33+
await expect(page.getByText('Complete')).toBeVisible();
34+
2735
const startRequest = networkArray.find((req) => req.url.includes('/authorize'));
2836
const nextRequest = networkArray.find((req) => req.url.includes('/customHTMLTemplate'));
2937

e2e/davinci-suites/src/phone-number-field.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ test('Login - add phone device - authenticate with phone device', async ({ page
6565
* Go to page
6666
***/
6767
expect(page.url()).toContain(
68-
'http://localhost:5829/?clientId=20dd0ed0-bb9b-4c8f-9a60-9ebeb4b348e',
68+
'http://localhost:5829/?clientId=20dd0ed0-bb9b-4c8f-9a60-9ebeb4b348e0',
6969
);
7070

7171
/**

packages/protect/src/lib/protect.test.ts

Lines changed: 93 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const config: ProtectConfig = {
2929
disableHub: false,
3030
};
3131

32-
describe('protect (success tests)', () => {
32+
describe('protect (with successfully loaded signals sdk)', () => {
3333
beforeAll(() => {
3434
vi.doMock('./signals-sdk.js', () => {
3535
return {
@@ -67,99 +67,117 @@ describe('protect (success tests)', () => {
6767
expect(protect).toBeDefined();
6868
});
6969

70-
it('should return protect methods', async () => {
71-
const protectAPI = await protect(config);
72-
expect(protectAPI).toBeDefined();
73-
assertType<Protect>(protectAPI);
74-
expect(protectAPI.start).toBeDefined();
75-
expect(protectAPI.getData).toBeDefined();
76-
expect(protectAPI.pauseBehavioralData).toBeDefined();
77-
expect(protectAPI.resumeBehavioralData).toBeDefined();
78-
expect(protectAPI.getPauseBehavioralData).toBeDefined();
79-
expect(protectAPI.getNodeConfig).toBeDefined();
80-
expect(protectAPI.getProtectType).toBeDefined();
81-
expect(protectAPI.setNodeClientError).toBeDefined();
82-
expect(protectAPI.setNodeInputValue).toBeDefined();
70+
it('should return protect methods', () => {
71+
const protectApi = protect(config);
72+
expect(protectApi).toBeDefined();
73+
assertType<Protect>(protectApi);
74+
expect(protectApi.start).toBeDefined();
75+
expect(protectApi.getData).toBeDefined();
76+
expect(protectApi.pauseBehavioralData).toBeDefined();
77+
expect(protectApi.resumeBehavioralData).toBeDefined();
78+
expect(protectApi.getPauseBehavioralData).toBeDefined();
79+
expect(protectApi.getNodeConfig).toBeDefined();
80+
expect(protectApi.getProtectType).toBeDefined();
81+
expect(protectApi.setNodeClientError).toBeDefined();
82+
expect(protectApi.setNodeInputValue).toBeDefined();
8383
});
8484

8585
describe('native node methods', () => {
8686
it('should call start', async () => {
87-
const protectAPI = await protect(config);
88-
const protectMock = vi.spyOn(protectAPI, 'start');
89-
await protectAPI.start();
87+
const protectApi = protect(config);
88+
const protectMock = vi.spyOn(protectApi, 'start');
89+
await protectApi.start();
9090
expect(protectMock).toHaveBeenCalled();
9191
expect(window._pingOneSignals.init).toHaveBeenCalledWith(config);
9292
});
9393

9494
it('should call getData', async () => {
95-
const protectAPI = await protect(config);
96-
const protectMock = vi.spyOn(protectAPI, 'getData');
97-
await protectAPI.getData();
95+
const protectApi = protect(config);
96+
const protectMock = vi.spyOn(protectApi, 'getData');
97+
await protectApi.getData();
9898
expect(protectMock).toHaveBeenCalled();
9999
});
100100

101-
it('should call pauseBehavioralData', async () => {
102-
const protectAPI = await protect(config);
103-
const protectMock = vi.spyOn(protectAPI, 'pauseBehavioralData');
104-
protectAPI.pauseBehavioralData();
101+
it('should call pauseBehavioralData', () => {
102+
const protectApi = protect(config);
103+
const protectMock = vi.spyOn(protectApi, 'pauseBehavioralData');
104+
protectApi.pauseBehavioralData();
105105
expect(protectMock).toHaveBeenCalled();
106106
});
107107

108-
it('should call resume behavioralData', async () => {
109-
const protectAPI = await protect(config);
110-
const protectMock = vi.spyOn(protectAPI, 'resumeBehavioralData');
111-
protectAPI.resumeBehavioralData();
108+
it('should call resumeBehavioralData', () => {
109+
const protectApi = protect(config);
110+
const protectMock = vi.spyOn(protectApi, 'resumeBehavioralData');
111+
protectApi.resumeBehavioralData();
112112
expect(protectMock).toHaveBeenCalled();
113113
});
114+
115+
it('getData should error if start has not been called', async () => {
116+
const protectApi = protect(config);
117+
const error = await protectApi.getData();
118+
expect(error).toEqual({ error: 'PingOne Signals SDK is not initialized' });
119+
});
120+
121+
it('pauseBehavioralData should error if start has not been called', async () => {
122+
const protectApi = protect(config);
123+
const error = await protectApi.pauseBehavioralData();
124+
expect(error).toEqual({ error: 'PingOne Signals SDK is not initialized' });
125+
});
126+
127+
it('resumeBehavioralData should error if start has not been called', async () => {
128+
const protectApi = protect(config);
129+
const error = await protectApi.resumeBehavioralData();
130+
expect(error).toEqual({ error: 'PingOne Signals SDK is not initialized' });
131+
});
114132
});
115133

116134
describe('marketplace node methods', () => {
117-
it('should test getPauseBehavioralData with marketplace data', async () => {
118-
const protectAPI = await protect(config);
119-
const result = protectAPI.getPauseBehavioralData(standardPingProtectEvaluationStep);
135+
it('should test getPauseBehavioralData with marketplace data', () => {
136+
const protectApi = protect(config);
137+
const result = protectApi.getPauseBehavioralData(standardPingProtectEvaluationStep);
120138
expect(result).toEqual(false);
121139

122-
const secondResult = protectAPI.getPauseBehavioralData(standardPingProtectInitializeStep);
140+
const secondResult = protectApi.getPauseBehavioralData(standardPingProtectInitializeStep);
123141
expect(secondResult).toEqual(true);
124142
});
125143

126-
it('should get the node config', async () => {
127-
const protectAPI = await protect(config);
128-
const result = protectAPI.getNodeConfig(standardPingProtectInitializeStep);
144+
it('should get the node config', () => {
145+
const protectApi = protect(config);
146+
const result = protectApi.getNodeConfig(standardPingProtectInitializeStep);
129147
expect(result).toEqual(
130148
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
131149
standardPingProtectInitializeStep!.payload.callbacks![0].output[0].value,
132150
);
133151

134-
const result2 = protectAPI.getNodeConfig(noProtectType);
152+
const result2 = protectApi.getNodeConfig(noProtectType);
135153
expect(result2).toBeUndefined();
136154
});
137155

138-
it('should test the getPingProtectType method', async () => {
139-
const protectAPI = await protect(config);
140-
const result = protectAPI.getProtectType(standardPingProtectInitializeStep);
156+
it('should test the getPingProtectType method', () => {
157+
const protectApi = protect(config);
158+
const result = protectApi.getProtectType(standardPingProtectInitializeStep);
141159
expect(result).toEqual('initialize');
142160

143-
const result2 = protectAPI.getProtectType(standardPingProtectEvaluationStep);
161+
const result2 = protectApi.getProtectType(standardPingProtectEvaluationStep);
144162
expect(result2).toEqual('evaluate');
145163

146-
const result3 = protectAPI.getProtectType(noProtectType);
164+
const result3 = protectApi.getProtectType(noProtectType);
147165
expect(result3).toEqual('none');
148166
});
149167

150-
it('should set the input with marketplace nodes', async () => {
151-
const protectAPI = await protect(config);
168+
it('should set the input with marketplace nodes', () => {
169+
const protectApi = protect(config);
152170
const step = standardPingProtectEvaluationStep as FRStep;
153171

154-
protectAPI.setNodeInputValue(step, 'the value');
172+
protectApi.setNodeInputValue(step, 'the value');
155173
const [hc] = step.getCallbacksOfType<HiddenValueCallback>(CallbackType.HiddenValueCallback);
156174

157175
expect(hc.getInputValue()).toEqual('the value');
158176
});
159177

160-
it('should set an error with marketplace nodes', async () => {
161-
const protectAPI = await protect(config);
162-
protectAPI.setNodeClientError(standardPingProtectEvaluationStep, 'we errored!');
178+
it('should set an error with marketplace nodes', () => {
179+
const protectApi = protect(config);
180+
protectApi.setNodeClientError(standardPingProtectEvaluationStep, 'we errored!');
163181

164182
const [, err] = (
165183
standardPingProtectEvaluationStep as FRStep
@@ -170,16 +188,41 @@ describe('protect (success tests)', () => {
170188
});
171189
});
172190

173-
describe('protect (error tests)', () => {
191+
describe('protect (with failed signals sdk load)', () => {
174192
beforeAll(() => {
175193
vi.doMock('./signals-sdk.js', () => {
176194
throw new Error('Failed to load PingOne Signals SDK');
177195
});
178196
});
197+
179198
afterAll(() => {
180199
vi.doUnmock('./signals-sdk.js');
181200
});
182-
it('should error on failed signals sdk load', async () => {
183-
await expect(protect(config)).rejects.toThrowError('Failed to load PingOne Signals SDK');
201+
202+
it('start method should error', async () => {
203+
const protectApi = protect(config);
204+
const error = await protectApi.start();
205+
await expect(error).toEqual({ error: 'Failed to load PingOne Signals SDK' });
206+
});
207+
208+
it('getData method should error', async () => {
209+
const protectApi = protect(config);
210+
await protectApi.start();
211+
const error = await protectApi.getData();
212+
await expect(error).toEqual({ error: 'PingOne Signals SDK is not initialized' });
213+
});
214+
215+
it('pauseBehavioralData method should error', async () => {
216+
const protectApi = protect(config);
217+
await protectApi.start();
218+
const error = await protectApi.pauseBehavioralData();
219+
await expect(error).toEqual({ error: 'PingOne Signals SDK is not initialized' });
220+
});
221+
222+
it('resumeBehavioralData method should error', async () => {
223+
const protectApi = protect(config);
224+
await protectApi.start();
225+
const error = await protectApi.resumeBehavioralData();
226+
await expect(error).toEqual({ error: 'PingOne Signals SDK is not initialized' });
184227
});
185228
});

0 commit comments

Comments
 (0)