|
| 1 | +/********************************************************************* |
| 2 | + * Copyright (c) Intel Corporation 2025 |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + **********************************************************************/ |
| 5 | + |
| 6 | +import { createSpyObj } from '../../../test/helper/jest.js' |
| 7 | +import { jest } from '@jest/globals' |
| 8 | +import { type SpyInstance, spyOn } from 'jest-mock' |
| 9 | +import { editProxyProfile } from './edit.js' |
| 10 | + |
| 11 | +describe('Proxy - Edit', () => { |
| 12 | + let resSpy |
| 13 | + let req |
| 14 | + let updateSpy: SpyInstance<any> |
| 15 | + let getSpy: SpyInstance<any> |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + resSpy = createSpyObj('Response', [ |
| 19 | + 'status', |
| 20 | + 'json', |
| 21 | + 'end', |
| 22 | + 'send' |
| 23 | + ]) |
| 24 | + req = { |
| 25 | + db: { proxyConfigs: { update: jest.fn(), getByName: jest.fn() } }, |
| 26 | + query: {}, |
| 27 | + params: { proxyConfigAddress: 'proxyConfigAddress' }, |
| 28 | + tenantId: '', |
| 29 | + method: 'PATCH', |
| 30 | + body: { |
| 31 | + address: 'intel.com', |
| 32 | + infoFormat: 201, |
| 33 | + networkDnsSuffix: 'vprodemo', |
| 34 | + port: 443, |
| 35 | + tenantId: 'foo' |
| 36 | + } |
| 37 | + } |
| 38 | + updateSpy = spyOn(req.db.proxyConfigs, 'update').mockResolvedValue({}) |
| 39 | + getSpy = spyOn(req.db.proxyConfigs, 'getByName').mockResolvedValue({}) |
| 40 | + |
| 41 | + resSpy.status.mockReturnThis() |
| 42 | + resSpy.json.mockReturnThis() |
| 43 | + resSpy.send.mockReturnThis() |
| 44 | + }) |
| 45 | + it('should update', async () => { |
| 46 | + getSpy = spyOn(req.db.proxyConfigs, 'getByName').mockResolvedValue({}) |
| 47 | + await editProxyProfile(req, resSpy) |
| 48 | + expect(getSpy).toHaveBeenCalledWith(req.body.address, req.tenantId) |
| 49 | + expect(resSpy.status).toHaveBeenCalledWith(200) |
| 50 | + }) |
| 51 | + it('should handle not found', async () => { |
| 52 | + getSpy = spyOn(req.db.proxyConfigs, 'getByName').mockResolvedValue(null) |
| 53 | + await editProxyProfile(req, resSpy) |
| 54 | + expect(getSpy).toHaveBeenCalledWith(req.body.address, req.tenantId) |
| 55 | + expect(resSpy.status).toHaveBeenCalledWith(404) |
| 56 | + }) |
| 57 | + it('should handle error', async () => { |
| 58 | + getSpy = spyOn(req.db.proxyConfigs, 'getByName').mockResolvedValue({}) |
| 59 | + spyOn(req.db.proxyConfigs, 'update').mockRejectedValue(null) |
| 60 | + await editProxyProfile(req, resSpy) |
| 61 | + expect(getSpy).toHaveBeenCalledWith(req.body.address, req.tenantId) |
| 62 | + expect(resSpy.status).toHaveBeenCalledWith(500) |
| 63 | + }) |
| 64 | +}) |
0 commit comments