Skip to content

Commit d7c450c

Browse files
committed
chore: adding-tests
added unit tests using msw to the client
1 parent ebf651b commit d7c450c

File tree

10 files changed

+1191
-15
lines changed

10 files changed

+1191
-15
lines changed

.changeset/config.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77
}
88
],
99
"commit": false,
10-
"fixed": [],
10+
"fixed": [["@forgerock/*"]],
1111
"linked": [],
1212
"access": "public",
1313
"baseBranch": "main",
1414
"updateInternalDependencies": "patch",
1515
"ignore": [
16-
"@forgerock/*",
1716
"@forgerock/davinci-app",
1817
"@forgerock/davinci-suites",
1918
"@forgerock/mock-api-v2"

.changeset/old-phones-explain.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@forgerock/device-client': patch
3+
---
4+
5+
small changes to the device api. align with other platforms.

packages/device-client/README.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,7 @@ apiClient.push
163163
### WebAuthn Management Example
164164

165165
```typescript
166-
const webAuthnQuery: WebAuthnQuery = {
167-
/* your query parameters */
168-
};
169-
170-
apiClient.webauthn
166+
apiClient.webAuthn
171167
.get(webAuthnQuery)
172168
.then((response) => {
173169
console.log('WebAuthn Devices:', response);
@@ -180,7 +176,7 @@ const updateWebAuthnQuery: WebAuthnQueryWithUUID & WebAuthnBody = {
180176
/* your update query */
181177
};
182178

183-
apiClient.webauthn
179+
apiClient.webAuthn
184180
.update(updateWebAuthnQuery)
185181
.then((response) => {
186182
console.log('Updated WebAuthn Device:', response);
@@ -193,7 +189,7 @@ const deleteWebAuthnQuery: WebAuthnQueryWithUUID & WebAuthnBody = {
193189
/* your delete query */
194190
};
195191

196-
apiClient.webauthn
192+
apiClient.webAuthn
197193
.delete(deleteWebAuthnQuery)
198194
.then((response) => {
199195
console.log('Deleted WebAuthn Device:', response);
@@ -206,7 +202,7 @@ apiClient.webauthn
206202
### Bound Devices Management Example
207203

208204
```typescript const bindingQuery: BindingDeviceQuery = { /* your query parameters */ };
209-
apiClient.boundDevices
205+
apiClient.bound
210206
.get(bindingQuery)
211207
.then((response) => {
212208
console.log('Bound Devices:', response);
@@ -219,7 +215,7 @@ const deleteBindingQuery: BindingDeviceQuery = {
219215
/* your delete query */
220216
};
221217

222-
apiClient.boundDevices
218+
apiClient.bound
223219
.delete(deleteBindingQuery)
224220
.then((response) => {
225221
console.log('Deleted Bound Device:', response);
@@ -232,7 +228,7 @@ const updateBindingQuery: BindingDeviceQuery = {
232228
/* your update query */
233229
};
234230

235-
apiClient.boundDevices
231+
apiClient.bound
236232
.update(updateBindingQuery)
237233
.then((response) => {
238234
console.log('Updated Bound Device:', response);

packages/device-client/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
},
1515
"files": ["./dist"],
1616
"dependencies": {
17-
"@reduxjs/toolkit": "catalog:",
1817
"@forgerock/javascript-sdk": "4.6.0",
18+
"@reduxjs/toolkit": "catalog:",
1919
"immer": "catalog:"
2020
},
2121
"devDependencies": {
22+
"@testing-library/react": "16.1.0",
23+
"msw": "2.7.0",
2224
"vitest": "^1.4.0"
2325
},
2426
"scripts": {
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
import { http, HttpResponse } from 'msw';
2+
import { setupServer } from 'msw/node';
3+
import { deviceClient } from './device.store';
4+
5+
import {
6+
MOCK_PUSH_DEVICES,
7+
MOCK_BINDING_DEVICES,
8+
MOCK_OATH_DEVICES,
9+
MOCK_DELETED_OATH_DEVICE,
10+
MOCK_WEBAUTHN_DEVICES,
11+
} from './mock-data/msw-mock-data';
12+
13+
// Create handlers
14+
export const handlers = [
15+
// OATH Devices
16+
http.get('*/devices/2fa/oath', () => {
17+
return HttpResponse.json(MOCK_OATH_DEVICES);
18+
}),
19+
20+
http.delete('*/devices/2fa/oath/:uuid', () => {
21+
return HttpResponse.json(MOCK_DELETED_OATH_DEVICE);
22+
}),
23+
24+
// Push Devices
25+
http.get('*/devices/2fa/push', () => {
26+
return HttpResponse.json(MOCK_PUSH_DEVICES);
27+
}),
28+
29+
http.delete('*/devices/2fa/push/:uuid', () => {
30+
return HttpResponse.json(MOCK_PUSH_DEVICES[0]);
31+
}),
32+
33+
// WebAuthn Devices
34+
http.get('*/devices/2fa/webauthn', () => {
35+
return HttpResponse.json(MOCK_WEBAUTHN_DEVICES);
36+
}),
37+
38+
http.put('*/devices/2fa/webauthn/:uuid', () => {
39+
return HttpResponse.json({
40+
...MOCK_WEBAUTHN_DEVICES.result[0],
41+
deviceName: 'Updated WebAuthn Device',
42+
});
43+
}),
44+
45+
http.delete('*/devices/2fa/webauthn/:uuid', () => {
46+
return HttpResponse.json(MOCK_WEBAUTHN_DEVICES.result[0]);
47+
}),
48+
49+
// Binding Devices
50+
http.get('*/devices/2fa/binding', () => {
51+
return HttpResponse.json(MOCK_BINDING_DEVICES);
52+
}),
53+
54+
http.put('*/devices/2fa/binding/:uuid', () => {
55+
return HttpResponse.json({
56+
...MOCK_BINDING_DEVICES.result[0],
57+
deviceName: 'Updated Binding Device',
58+
});
59+
}),
60+
61+
http.delete('*/devices/2fa/binding/:uuid', () => {
62+
return HttpResponse.json(MOCK_BINDING_DEVICES.result[0]);
63+
}),
64+
];
65+
66+
export const server = setupServer(...handlers);
67+
68+
// Establish API mocking before all tests.
69+
beforeAll(() => server.listen({ onUnhandledRequest: 'error' }));
70+
71+
// Reset any request handlers that we may add during the tests,
72+
// so they don't affect other tests.
73+
afterEach(() => server.resetHandlers());
74+
75+
// Clean up after the tests are finished.
76+
afterAll(() => server.close());
77+
78+
describe('Device Client Store', () => {
79+
const config = {
80+
serverConfig: {
81+
baseUrl: 'https://api.example.com',
82+
},
83+
realmPath: 'test-realm',
84+
};
85+
86+
describe('OATH Device Management', () => {
87+
const client = deviceClient(config);
88+
89+
it('should fetch OATH devices', async () => {
90+
const result = await client.oath.get({
91+
userId: 'test-user',
92+
});
93+
94+
expect(result).toEqual(MOCK_OATH_DEVICES);
95+
});
96+
97+
it('should delete OATH device', async () => {
98+
const result = await client.oath.delete({
99+
userId: 'test-user',
100+
uuid: 'oath-uuid-1',
101+
deviceName: 'Test OATH Device',
102+
id: 'test-id',
103+
createdDate: new Date(1705555555555),
104+
lastAccessDate: new Date(1705555555555),
105+
});
106+
107+
expect(result).toEqual(MOCK_DELETED_OATH_DEVICE);
108+
});
109+
});
110+
111+
describe('Push Device Management', () => {
112+
const client = deviceClient(config);
113+
114+
it('should fetch push devices', async () => {
115+
const result = await client.push.get({
116+
userId: 'test-user',
117+
});
118+
119+
expect(result).toEqual(MOCK_PUSH_DEVICES);
120+
});
121+
122+
it('should delete push device', async () => {
123+
const result = await client.push.delete({
124+
userId: 'test-user',
125+
uuid: 'push-uuid-1',
126+
});
127+
128+
expect(result).toEqual(MOCK_PUSH_DEVICES[0]);
129+
});
130+
});
131+
describe('WebAuthn Device Management', () => {
132+
const client = deviceClient(config);
133+
134+
it('should fetch webauthn devices', async () => {
135+
const result = await client.webAuthn.get({
136+
userId: 'test-user',
137+
});
138+
139+
expect(result).toEqual(MOCK_WEBAUTHN_DEVICES);
140+
});
141+
142+
it('should update webauthn device name', async () => {
143+
const mockDevice = MOCK_WEBAUTHN_DEVICES.result[0];
144+
const result = await client.webAuthn.update({
145+
userId: 'test-user',
146+
uuid: mockDevice.uuid,
147+
id: mockDevice._id,
148+
deviceName: 'Updated WebAuthn Device',
149+
credentialId: mockDevice.credentialId,
150+
createdDate: mockDevice.createdDate,
151+
lastAccessDate: mockDevice.lastAccessDate,
152+
});
153+
154+
expect(result).toEqual({
155+
...mockDevice,
156+
deviceName: 'Updated WebAuthn Device',
157+
});
158+
});
159+
160+
it('should delete webauthn device', async () => {
161+
const mockDevice = MOCK_WEBAUTHN_DEVICES.result[0];
162+
const result = await client.webAuthn.delete({
163+
userId: 'test-user',
164+
uuid: mockDevice.uuid,
165+
id: mockDevice._id,
166+
deviceName: mockDevice.deviceName,
167+
credentialId: mockDevice.credentialId,
168+
createdDate: mockDevice.createdDate,
169+
lastAccessDate: mockDevice.lastAccessDate,
170+
});
171+
172+
expect(result).toEqual(mockDevice);
173+
});
174+
});
175+
176+
describe('Bound Device Management', () => {
177+
const client = deviceClient(config);
178+
const mockDevice = MOCK_BINDING_DEVICES.result[0];
179+
180+
it('should fetch bound devices', async () => {
181+
const result = await client.bound.get({
182+
userId: 'test-user',
183+
...mockDevice,
184+
});
185+
186+
expect(result).toEqual(MOCK_BINDING_DEVICES);
187+
});
188+
189+
it('should update bound device name', async () => {
190+
const result = await client.bound.update({
191+
userId: 'test-user',
192+
...mockDevice,
193+
});
194+
195+
expect(result).toEqual({
196+
...mockDevice,
197+
deviceName: 'Updated Binding Device',
198+
});
199+
});
200+
201+
it('should delete bound device', async () => {
202+
const result = await client.bound.delete({
203+
userId: 'test-user',
204+
...mockDevice,
205+
});
206+
207+
expect(result).toEqual(mockDevice);
208+
});
209+
});
210+
});

0 commit comments

Comments
 (0)