Changeset View
Changeset View
Standalone View
Standalone View
keyserver/src/utils/validation-utils.test.js
// @flow | // @flow | ||||
import t from 'tcomb'; | import t from 'tcomb'; | ||||
import { tPassword, tShape } from 'lib/utils/validation-utils.js'; | import { tPassword, tShape, tID } from 'lib/utils/validation-utils.js'; | ||||
import { sanitizeInput, redactedString } from './validation-utils.js'; | import { | ||||
convertServerIDsToClientIDs, | |||||
sanitizeInput, | |||||
redactedString, | |||||
convertClientIDsToServerIDs, | |||||
} from './validation-utils.js'; | |||||
describe('sanitization', () => { | describe('sanitization', () => { | ||||
it('should redact a string', () => { | it('should redact a string', () => { | ||||
expect(sanitizeInput(tPassword, 'password')).toStrictEqual(redactedString); | expect(sanitizeInput(tPassword, 'password')).toStrictEqual(redactedString); | ||||
}); | }); | ||||
it('should redact a string inside an object', () => { | it('should redact a string inside an object', () => { | ||||
const validator = tShape({ password: tPassword }); | const validator = tShape({ password: tPassword }); | ||||
▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | it('should redact a string inside an object array', () => { | ||||
const validator = tShape({ | const validator = tShape({ | ||||
passwords: t.list(tShape({ password: tPassword })), | passwords: t.list(tShape({ password: tPassword })), | ||||
}); | }); | ||||
const object = { passwords: [{ password: 'password' }] }; | const object = { passwords: [{ password: 'password' }] }; | ||||
const redacted = { passwords: [{ password: redactedString }] }; | const redacted = { passwords: [{ password: redactedString }] }; | ||||
expect(sanitizeInput(validator, object)).toStrictEqual(redacted); | expect(sanitizeInput(validator, object)).toStrictEqual(redacted); | ||||
}); | }); | ||||
}); | }); | ||||
describe('id conversion', () => { | |||||
it('should convert string id', () => { | |||||
const validator = tShape({ id: tID }); | |||||
const serverData = { id: '1' }; | |||||
const clientData = { id: '0|1' }; | |||||
expect( | |||||
convertServerIDsToClientIDs('0', validator, serverData), | |||||
).toStrictEqual(clientData); | |||||
expect( | |||||
convertClientIDsToServerIDs('0', validator, clientData), | |||||
).toStrictEqual(serverData); | |||||
}); | |||||
it('should convert a complex type', () => { | |||||
const validator = tShape({ ids: t.dict(tID, t.list(tID)) }); | |||||
const serverData = { ids: { '1': ['11', '12'], '2': [], '3': ['13'] } }; | |||||
const clientData = { | |||||
ids: { '0|1': ['0|11', '0|12'], '0|2': [], '0|3': ['0|13'] }, | |||||
}; | |||||
expect( | |||||
convertServerIDsToClientIDs('0', validator, serverData), | |||||
).toStrictEqual(clientData); | |||||
expect( | |||||
convertClientIDsToServerIDs('0', validator, clientData), | |||||
).toStrictEqual(serverData); | |||||
}); | |||||
}); |