|
| 1 | +import { SdkResponse } from '@descope/core-js-sdk'; |
| 2 | +import withManagement from '.'; |
| 3 | +import apiPaths from './paths'; |
| 4 | +import { Engine, EngineSecretResponse } from './types'; |
| 5 | +import { mockHttpClient, resetMockHttpClient } from './testutils'; |
| 6 | + |
| 7 | +const management = withManagement(mockHttpClient); |
| 8 | + |
| 9 | +// The management Engine exposes only id/name/secret/createdTime; createdTime is an int32 |
| 10 | +// epoch-seconds JSON number. |
| 11 | +const mockEngine: Engine = { |
| 12 | + id: 'eng1', |
| 13 | + name: 'my-engine', |
| 14 | + secret: 's3cret', |
| 15 | + createdTime: 1719571200, |
| 16 | +}; |
| 17 | + |
| 18 | +describe('Management Engine', () => { |
| 19 | + afterEach(() => { |
| 20 | + jest.clearAllMocks(); |
| 21 | + resetMockHttpClient(); |
| 22 | + }); |
| 23 | + |
| 24 | + describe('create', () => { |
| 25 | + it('should send the correct request and return the engine with its secret', async () => { |
| 26 | + const httpResponse = { |
| 27 | + ok: true, |
| 28 | + json: () => ({ engine: mockEngine }), |
| 29 | + clone: () => ({ json: () => Promise.resolve({ engine: mockEngine }) }), |
| 30 | + status: 200, |
| 31 | + }; |
| 32 | + mockHttpClient.post.mockResolvedValue(httpResponse); |
| 33 | + |
| 34 | + const resp: SdkResponse<Engine> = await management.engine.create('my-engine'); |
| 35 | + |
| 36 | + expect(mockHttpClient.post).toHaveBeenCalledWith(apiPaths.engine.create, { |
| 37 | + name: 'my-engine', |
| 38 | + }); |
| 39 | + expect(resp.data).toEqual(mockEngine); |
| 40 | + expect(resp.data?.secret).toBe('s3cret'); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('update', () => { |
| 45 | + it('should send the correct request', async () => { |
| 46 | + const updated = { id: 'eng1', name: 'renamed' }; |
| 47 | + const httpResponse = { |
| 48 | + ok: true, |
| 49 | + json: () => ({ engine: updated }), |
| 50 | + clone: () => ({ json: () => Promise.resolve({ engine: updated }) }), |
| 51 | + status: 200, |
| 52 | + }; |
| 53 | + mockHttpClient.post.mockResolvedValue(httpResponse); |
| 54 | + |
| 55 | + const resp = await management.engine.update('eng1', 'renamed'); |
| 56 | + |
| 57 | + expect(mockHttpClient.post).toHaveBeenCalledWith(apiPaths.engine.update, { |
| 58 | + id: 'eng1', |
| 59 | + name: 'renamed', |
| 60 | + }); |
| 61 | + expect(resp.data).toEqual(updated); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('delete', () => { |
| 66 | + it('should send the correct request', async () => { |
| 67 | + const httpResponse = { |
| 68 | + ok: true, |
| 69 | + json: () => ({}), |
| 70 | + clone: () => ({ json: () => Promise.resolve({}) }), |
| 71 | + status: 200, |
| 72 | + }; |
| 73 | + mockHttpClient.post.mockResolvedValue(httpResponse); |
| 74 | + |
| 75 | + await management.engine.delete('eng1'); |
| 76 | + |
| 77 | + expect(mockHttpClient.post).toHaveBeenCalledWith(apiPaths.engine.delete, { |
| 78 | + id: 'eng1', |
| 79 | + }); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe('load', () => { |
| 84 | + it('should send the correct request with the id query param', async () => { |
| 85 | + const loaded = { id: 'eng1', name: 'my-engine' }; |
| 86 | + const httpResponse = { |
| 87 | + ok: true, |
| 88 | + json: () => ({ engine: loaded }), |
| 89 | + clone: () => ({ json: () => Promise.resolve({ engine: loaded }) }), |
| 90 | + status: 200, |
| 91 | + }; |
| 92 | + mockHttpClient.get.mockResolvedValue(httpResponse); |
| 93 | + |
| 94 | + const resp = await management.engine.load('eng1'); |
| 95 | + |
| 96 | + expect(mockHttpClient.get).toHaveBeenCalledWith(apiPaths.engine.load, { |
| 97 | + queryParams: { id: 'eng1' }, |
| 98 | + }); |
| 99 | + expect(resp.data).toEqual(loaded); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + describe('loadAll', () => { |
| 104 | + it('should send the correct request and return all engines', async () => { |
| 105 | + const engines = [{ id: 'eng1' }, { id: 'eng2' }]; |
| 106 | + const httpResponse = { |
| 107 | + ok: true, |
| 108 | + json: () => ({ engines }), |
| 109 | + clone: () => ({ json: () => Promise.resolve({ engines }) }), |
| 110 | + status: 200, |
| 111 | + }; |
| 112 | + mockHttpClient.get.mockResolvedValue(httpResponse); |
| 113 | + |
| 114 | + const resp: SdkResponse<Engine[]> = await management.engine.loadAll(); |
| 115 | + |
| 116 | + expect(mockHttpClient.get).toHaveBeenCalledWith(apiPaths.engine.loadAll, {}); |
| 117 | + expect(resp.data).toEqual(engines); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + describe('rotateSecret', () => { |
| 122 | + it('should send the correct request and return the new secret', async () => { |
| 123 | + const secretResp: EngineSecretResponse = { secret: 'newS3cret' }; |
| 124 | + const httpResponse = { |
| 125 | + ok: true, |
| 126 | + json: () => secretResp, |
| 127 | + clone: () => ({ json: () => Promise.resolve(secretResp) }), |
| 128 | + status: 200, |
| 129 | + }; |
| 130 | + mockHttpClient.post.mockResolvedValue(httpResponse); |
| 131 | + |
| 132 | + const resp = await management.engine.rotateSecret('eng1'); |
| 133 | + |
| 134 | + expect(mockHttpClient.post).toHaveBeenCalledWith(apiPaths.engine.rotate, { |
| 135 | + id: 'eng1', |
| 136 | + }); |
| 137 | + expect(resp.data?.secret).toBe('newS3cret'); |
| 138 | + }); |
| 139 | + }); |
| 140 | +}); |
0 commit comments