|
| 1 | +/********************************************************************* |
| 2 | + * Copyright (c) Intel Corporation 2026 |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + **********************************************************************/ |
| 5 | + |
| 6 | +import { validationResult } from 'express-validator' |
| 7 | +import { describe, expect, it } from '@jest/globals' |
| 8 | +import { validator } from './deviceValidator.js' |
| 9 | + |
| 10 | +async function getValidationErrors(body: any): Promise<any[]> { |
| 11 | + const req: any = { body, query: {}, params: {} } |
| 12 | + const chains = validator() |
| 13 | + |
| 14 | + for (const chain of chains) { |
| 15 | + await chain.run(req) |
| 16 | + } |
| 17 | + |
| 18 | + return validationResult(req).array() |
| 19 | +} |
| 20 | + |
| 21 | +describe('device validator dnsSuffix checks', () => { |
| 22 | + it('accepts a valid dnsSuffix', async () => { |
| 23 | + const errors = await getValidationErrors({ |
| 24 | + guid: '123e4567-e89b-12d3-a456-426614174000', |
| 25 | + dnsSuffix: 'os.suffix.com' |
| 26 | + }) |
| 27 | + |
| 28 | + expect(errors).toHaveLength(0) |
| 29 | + }) |
| 30 | + |
| 31 | + it('accepts null dnsSuffix', async () => { |
| 32 | + const errors = await getValidationErrors({ |
| 33 | + guid: '123e4567-e89b-12d3-a456-426614174000', |
| 34 | + dnsSuffix: null |
| 35 | + }) |
| 36 | + |
| 37 | + expect(errors).toHaveLength(0) |
| 38 | + }) |
| 39 | + |
| 40 | + it("accepts empty string dnsSuffix", async () => { |
| 41 | + const errors = await getValidationErrors({ |
| 42 | + guid: '123e4567-e89b-12d3-a456-426614174000', |
| 43 | + dnsSuffix: '' |
| 44 | + }) |
| 45 | + |
| 46 | + expect(errors).toHaveLength(0) |
| 47 | + }) |
| 48 | + |
| 49 | + it('rejects dnsSuffix containing query string characters', async () => { |
| 50 | + const errors = await getValidationErrors({ |
| 51 | + guid: '123e4567-e89b-12d3-a456-426614174000', |
| 52 | + dnsSuffix: 'None?injected_query_string=123' |
| 53 | + }) |
| 54 | + |
| 55 | + expect(errors.some((error) => error.path === 'dnsSuffix')).toBeTruthy() |
| 56 | + const dnsSuffixError = errors.find((error) => error.path === 'dnsSuffix') |
| 57 | + expect(dnsSuffixError).toBeDefined() |
| 58 | + expect(dnsSuffixError?.msg).toContain('dnsSuffix') |
| 59 | + }) |
| 60 | +}) |
0 commit comments