|
| 1 | +import { |
| 2 | + assertInput, |
| 3 | + assertInputString, |
| 4 | + assertInputStringLength, |
| 5 | + assertInputStringArrayEntryLength, |
| 6 | + assertInputStringNumberEnumLike |
| 7 | +} from '../server.assertions'; |
| 8 | + |
| 9 | +describe('assertInput', () => { |
| 10 | + it.each([ |
| 11 | + { |
| 12 | + description: 'basic string validation', |
| 13 | + param: '', |
| 14 | + condition: (value: any) => typeof value === 'string' && value.trim().length > 0 |
| 15 | + }, |
| 16 | + { |
| 17 | + description: 'pattern in string validation', |
| 18 | + param: 'patternfly://lorem-ipsum', |
| 19 | + condition: (value: any) => new RegExp('patternfly://', 'i').test(value) |
| 20 | + }, |
| 21 | + { |
| 22 | + description: 'array entry length validation', |
| 23 | + param: ['lorem', 'ipsum'], |
| 24 | + condition: (value: any) => Array.isArray(value) && value.length > 2 |
| 25 | + } |
| 26 | + ])('should throw an error for validation, $description', ({ param, condition }) => { |
| 27 | + const errorMessage = `Lorem ipsum error message for ${param} validation.`; |
| 28 | + |
| 29 | + expect(() => assertInput( |
| 30 | + condition, |
| 31 | + errorMessage |
| 32 | + )).toThrow(errorMessage); |
| 33 | + }); |
| 34 | +}); |
| 35 | + |
| 36 | +describe('assertInputString', () => { |
| 37 | + it.each([ |
| 38 | + { |
| 39 | + description: 'empty string', |
| 40 | + param: '' |
| 41 | + }, |
| 42 | + { |
| 43 | + description: 'undefined', |
| 44 | + param: undefined |
| 45 | + }, |
| 46 | + { |
| 47 | + description: 'number', |
| 48 | + param: 1 |
| 49 | + }, |
| 50 | + { |
| 51 | + description: 'null', |
| 52 | + param: null |
| 53 | + } |
| 54 | + ])('should throw an error for validation, $description', ({ param }) => { |
| 55 | + const errorMessage = '"Input" must be a string'; |
| 56 | + |
| 57 | + expect(() => assertInputString( |
| 58 | + param |
| 59 | + )).toThrow(errorMessage); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +describe('assertInputStringLength', () => { |
| 64 | + it.each([ |
| 65 | + { |
| 66 | + description: 'empty string', |
| 67 | + param: '' |
| 68 | + }, |
| 69 | + { |
| 70 | + description: 'undefined', |
| 71 | + param: undefined |
| 72 | + }, |
| 73 | + { |
| 74 | + description: 'number', |
| 75 | + param: 1 |
| 76 | + }, |
| 77 | + { |
| 78 | + description: 'null', |
| 79 | + param: null |
| 80 | + }, |
| 81 | + { |
| 82 | + description: 'max', |
| 83 | + param: 'lorem ipsum', |
| 84 | + options: { max: 5 } |
| 85 | + }, |
| 86 | + { |
| 87 | + description: 'min', |
| 88 | + param: 'lorem ipsum', |
| 89 | + options: { min: 15 } |
| 90 | + }, |
| 91 | + { |
| 92 | + description: 'max and min', |
| 93 | + param: 'lorem ipsum', |
| 94 | + options: { min: 1, max: 10 } |
| 95 | + }, |
| 96 | + { |
| 97 | + description: 'max and min and display name', |
| 98 | + param: 'lorem ipsum', |
| 99 | + options: { min: 1, max: 10, inputDisplayName: 'lorem ipsum' } |
| 100 | + }, |
| 101 | + { |
| 102 | + description: 'max and min and description', |
| 103 | + param: 'lorem ipsum', |
| 104 | + options: { min: 1, max: 10, message: 'dolor sit amet, consectetur adipiscing elit.' } |
| 105 | + } |
| 106 | + ])('should throw an error for validation, $description', ({ param, options }) => { |
| 107 | + const errorMessage = options?.message || `"${options?.inputDisplayName || 'Input'}" must be a string`; |
| 108 | + |
| 109 | + expect(() => assertInputStringLength( |
| 110 | + param, |
| 111 | + { min: 1, max: 100, ...options } as any |
| 112 | + )).toThrow(errorMessage); |
| 113 | + }); |
| 114 | +}); |
| 115 | + |
| 116 | +describe('assertInputStringArrayEntryLength', () => { |
| 117 | + it.each([ |
| 118 | + { |
| 119 | + description: 'empty string', |
| 120 | + param: '' |
| 121 | + }, |
| 122 | + { |
| 123 | + description: 'undefined', |
| 124 | + param: undefined |
| 125 | + }, |
| 126 | + { |
| 127 | + description: 'number', |
| 128 | + param: 1 |
| 129 | + }, |
| 130 | + { |
| 131 | + description: 'null', |
| 132 | + param: null |
| 133 | + }, |
| 134 | + { |
| 135 | + description: 'max', |
| 136 | + param: ['lorem ipsum'], |
| 137 | + options: { max: 5 } |
| 138 | + }, |
| 139 | + { |
| 140 | + description: 'min', |
| 141 | + param: ['lorem ipsum'], |
| 142 | + options: { min: 15 } |
| 143 | + }, |
| 144 | + { |
| 145 | + description: 'max and min', |
| 146 | + param: ['lorem ipsum'], |
| 147 | + options: { min: 1, max: 10 } |
| 148 | + }, |
| 149 | + { |
| 150 | + description: 'max and min and display name', |
| 151 | + param: ['lorem ipsum'], |
| 152 | + options: { min: 1, max: 10, inputDisplayName: 'lorem ipsum' } |
| 153 | + }, |
| 154 | + { |
| 155 | + description: 'max and min and description', |
| 156 | + param: ['lorem ipsum'], |
| 157 | + options: { min: 1, max: 10, message: 'dolor sit amet, consectetur adipiscing elit.' } |
| 158 | + } |
| 159 | + ])('should throw an error for validation, $description', ({ param, options }) => { |
| 160 | + const errorMessage = options?.message || `"${options?.inputDisplayName || 'Input'}" array must contain strings`; |
| 161 | + |
| 162 | + expect(() => assertInputStringArrayEntryLength( |
| 163 | + param as any, |
| 164 | + { min: 1, max: 100, ...options } as any |
| 165 | + )).toThrow(errorMessage); |
| 166 | + }); |
| 167 | +}); |
| 168 | + |
| 169 | +describe('assertInputStringNumberEnumLike', () => { |
| 170 | + it.each([ |
| 171 | + { |
| 172 | + description: 'empty string', |
| 173 | + param: '', |
| 174 | + compare: [2, 3] |
| 175 | + }, |
| 176 | + { |
| 177 | + description: 'undefined', |
| 178 | + param: undefined, |
| 179 | + compare: [2, 3] |
| 180 | + }, |
| 181 | + { |
| 182 | + description: 'null', |
| 183 | + param: null, |
| 184 | + compare: [2, 3] |
| 185 | + }, |
| 186 | + { |
| 187 | + description: 'number', |
| 188 | + param: 1, |
| 189 | + compare: [2, 3] |
| 190 | + }, |
| 191 | + { |
| 192 | + description: 'string', |
| 193 | + param: 'lorem ipsum', |
| 194 | + compare: ['amet', 'dolor sit'] |
| 195 | + }, |
| 196 | + { |
| 197 | + description: 'string and display name', |
| 198 | + param: 'lorem ipsum', |
| 199 | + compare: ['amet', 'dolor sit'], |
| 200 | + options: { inputDisplayName: 'lorem ipsum' } |
| 201 | + }, |
| 202 | + { |
| 203 | + description: 'string and description', |
| 204 | + param: 'lorem ipsum', |
| 205 | + compare: [1, 2], |
| 206 | + options: { message: 'dolor sit amet, consectetur adipiscing elit.' } |
| 207 | + } |
| 208 | + ])('should throw an error for validation, $description', ({ param, compare, options }) => { |
| 209 | + const errorMessage = options?.message || `"${options?.inputDisplayName || 'Input'}" must be one of the following values`; |
| 210 | + |
| 211 | + expect(() => assertInputStringNumberEnumLike( |
| 212 | + param as any, |
| 213 | + compare as any, |
| 214 | + { ...options } as any |
| 215 | + )).toThrow(errorMessage); |
| 216 | + }); |
| 217 | + |
| 218 | + it('should throw an internal error for validation when missing comparison values', () => { |
| 219 | + const errorMessage = 'List of allowed values is empty'; |
| 220 | + |
| 221 | + expect(() => assertInputStringNumberEnumLike( |
| 222 | + 1, |
| 223 | + [] |
| 224 | + )).toThrow(errorMessage); |
| 225 | + }); |
| 226 | +}); |
0 commit comments