|
| 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