Changeset View
Changeset View
Standalone View
Standalone View
lib/types/validation.test.js
// @flow | // @flow | ||||
import _findKey from 'lodash/fp/findKey.js'; | import _findKey from 'lodash/fp/findKey.js'; | ||||
import { rawEntryInfoValidator } from './entry-types.js'; | import { rawEntryInfoValidator } from './entry-types.js'; | ||||
import { | import { | ||||
imageValidator, | imageValidator, | ||||
videoValidator, | videoValidator, | ||||
mediaValidator, | mediaValidator, | ||||
} from './media-types.js'; | } from './media-types.js'; | ||||
import { messageTypes } from './message-types-enum.js'; | import { messageTypes } from './message-types-enum.js'; | ||||
import { | |||||
activityUpdateResponseServerSocketMessageValidator, | |||||
apiResponseServerSocketMessageValidator, | |||||
authErrorServerSocketMessageValidator, | |||||
errorServerSocketMessageValidator, | |||||
messagesServerSocketMessageValidator, | |||||
pongServerSocketMessageValidator, | |||||
serverRequestsServerSocketMessageValidator, | |||||
serverSocketMessageTypes, | |||||
serverStateSyncServerSocketMessageValidator, | |||||
serverUpdatesServerSocketMessageValidator, | |||||
} from './socket-types.js'; | |||||
import { threadTypes, rawThreadInfoValidator } from './thread-types.js'; | import { threadTypes, rawThreadInfoValidator } from './thread-types.js'; | ||||
import { | import { | ||||
updateTypes, | updateTypes, | ||||
accountDeletionUpdateInfoValidator, | accountDeletionUpdateInfoValidator, | ||||
badDeviceTokenUpdateInfoValidator, | badDeviceTokenUpdateInfoValidator, | ||||
entryUpdateInfoValidator, | entryUpdateInfoValidator, | ||||
serverCurrentUserUpdateInfoValidator, | serverCurrentUserUpdateInfoValidator, | ||||
threadDeletionUpdateInfoValidator, | threadDeletionUpdateInfoValidator, | ||||
▲ Show 20 Lines • Show All 642 Lines • ▼ Show 20 Lines | describe('entry validation', () => { | ||||
it('should validate correct entry', () => { | it('should validate correct entry', () => { | ||||
expect(rawEntryInfoValidator.is(entry)).toBe(true); | expect(rawEntryInfoValidator.is(entry)).toBe(true); | ||||
}); | }); | ||||
it('should not validate incorrect entry', () => { | it('should not validate incorrect entry', () => { | ||||
expect(rawEntryInfoValidator.is({ ...entry, threadID: 0 })).toBe(false); | expect(rawEntryInfoValidator.is({ ...entry, threadID: 0 })).toBe(false); | ||||
}); | }); | ||||
}); | }); | ||||
describe('server update validation', () => { | |||||
const updates = [ | const updates = [ | ||||
{ | { | ||||
type: updateTypes.DELETE_ACCOUNT, | type: updateTypes.DELETE_ACCOUNT, | ||||
id: '98424', | id: '98424', | ||||
time: 1640870111106, | time: 1640870111106, | ||||
deletedUserID: '98262', | deletedUserID: '98262', | ||||
}, | }, | ||||
{ | { | ||||
type: updateTypes.UPDATE_THREAD, | type: updateTypes.UPDATE_THREAD, | ||||
id: '97948', | id: '97948', | ||||
time: 1640868525494, | time: 1640868525494, | ||||
threadInfo: thread, | threadInfo: thread, | ||||
}, | }, | ||||
{ | { | ||||
type: updateTypes.UPDATE_THREAD_READ_STATUS, | type: updateTypes.UPDATE_THREAD_READ_STATUS, | ||||
id: '98002', | id: '98002', | ||||
time: 1640869373326, | time: 1640869373326, | ||||
threadID: '83794', | threadID: '83794', | ||||
unread: true, | unread: true, | ||||
}, | }, | ||||
{ | { | ||||
type: updateTypes.DELETE_THREAD, | type: updateTypes.DELETE_THREAD, | ||||
id: '98208', | id: '98208', | ||||
time: 1640869773339, | time: 1640869773339, | ||||
threadID: '97852', | threadID: '97852', | ||||
}, | }, | ||||
{ | { | ||||
type: updateTypes.JOIN_THREAD, | type: updateTypes.JOIN_THREAD, | ||||
id: '98126', | id: '98126', | ||||
time: 1640869494461, | time: 1640869494461, | ||||
threadInfo: thread, | threadInfo: thread, | ||||
rawMessageInfos: messages, | rawMessageInfos: messages, | ||||
truncationStatus: 'exhaustive', | truncationStatus: 'exhaustive', | ||||
rawEntryInfos: [entry], | rawEntryInfos: [entry], | ||||
}, | }, | ||||
{ | { | ||||
type: updateTypes.BAD_DEVICE_TOKEN, | type: updateTypes.BAD_DEVICE_TOKEN, | ||||
id: '98208', | id: '98208', | ||||
time: 1640869773495, | time: 1640869773495, | ||||
deviceToken: 'some-device-token', | deviceToken: 'some-device-token', | ||||
}, | }, | ||||
{ | { | ||||
type: updateTypes.UPDATE_ENTRY, | type: updateTypes.UPDATE_ENTRY, | ||||
id: '98233', | id: '98233', | ||||
time: 1640869844908, | time: 1640869844908, | ||||
entryInfo: entry, | entryInfo: entry, | ||||
}, | }, | ||||
{ | { | ||||
type: updateTypes.UPDATE_CURRENT_USER, | type: updateTypes.UPDATE_CURRENT_USER, | ||||
id: '98237', | id: '98237', | ||||
time: 1640869934058, | time: 1640869934058, | ||||
currentUserInfo: { | currentUserInfo: { | ||||
id: '256', | id: '256', | ||||
username: 'ashoat', | username: 'ashoat', | ||||
}, | }, | ||||
}, | }, | ||||
{ | { | ||||
type: updateTypes.UPDATE_USER, | type: updateTypes.UPDATE_USER, | ||||
id: '97988', | id: '97988', | ||||
time: 1640869211822, | time: 1640869211822, | ||||
updatedUserID: '86565', | updatedUserID: '86565', | ||||
}, | }, | ||||
]; | ]; | ||||
describe('server update validation', () => { | |||||
const validatorByUpdateType = { | const validatorByUpdateType = { | ||||
[updateTypes.DELETE_ACCOUNT]: accountDeletionUpdateInfoValidator, | [updateTypes.DELETE_ACCOUNT]: accountDeletionUpdateInfoValidator, | ||||
[updateTypes.UPDATE_THREAD]: threadUpdateInfoValidator, | [updateTypes.UPDATE_THREAD]: threadUpdateInfoValidator, | ||||
[updateTypes.UPDATE_THREAD_READ_STATUS]: | [updateTypes.UPDATE_THREAD_READ_STATUS]: | ||||
threadReadStatusUpdateInfoValidator, | threadReadStatusUpdateInfoValidator, | ||||
[updateTypes.DELETE_THREAD]: threadDeletionUpdateInfoValidator, | [updateTypes.DELETE_THREAD]: threadDeletionUpdateInfoValidator, | ||||
[updateTypes.JOIN_THREAD]: threadJoinUpdateInfoValidator, | [updateTypes.JOIN_THREAD]: threadJoinUpdateInfoValidator, | ||||
[updateTypes.BAD_DEVICE_TOKEN]: badDeviceTokenUpdateInfoValidator, | [updateTypes.BAD_DEVICE_TOKEN]: badDeviceTokenUpdateInfoValidator, | ||||
Show All 18 Lines | for (const update of updates) { | ||||
} else { | } else { | ||||
it(`${validatorUpdateTypeName} shouldn't validate ${updateTypeName}`, () => { | it(`${validatorUpdateTypeName} shouldn't validate ${updateTypeName}`, () => { | ||||
expect(validator.is(update)).toBe(false); | expect(validator.is(update)).toBe(false); | ||||
}); | }); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
}); | }); | ||||
describe('socket message validation', () => { | |||||
const socketMessages = [ | |||||
{ | |||||
type: serverSocketMessageTypes.STATE_SYNC, | |||||
responseTo: 0, | |||||
payload: { | |||||
type: 1, | |||||
messagesResult: { | |||||
rawMessageInfos: messages, | |||||
truncationStatuses: { '86033': 'unchanged' }, | |||||
currentAsOf: 1683296863468, | |||||
}, | |||||
updatesResult: { | |||||
newUpdates: updates, | |||||
currentAsOf: 1683296863489, | |||||
}, | |||||
deltaEntryInfos: [], | |||||
deletedEntryIDs: [], | |||||
userInfos: [], | |||||
}, | |||||
}, | |||||
{ | |||||
type: serverSocketMessageTypes.REQUESTS, | |||||
payload: { | |||||
serverRequests: [ | |||||
{ | |||||
type: 6, | |||||
hashesToCheck: { | |||||
threadInfos: 3311950643, | |||||
entryInfos: 3191324567, | |||||
currentUserInfo: 820850779, | |||||
userInfos: 707653884, | |||||
}, | |||||
}, | |||||
], | |||||
}, | |||||
}, | |||||
{ | |||||
type: serverSocketMessageTypes.ACTIVITY_UPDATE_RESPONSE, | |||||
responseTo: 194, | |||||
payload: { unfocusedToUnread: [] }, | |||||
}, | |||||
{ type: serverSocketMessageTypes.PONG, responseTo: 190 }, | |||||
{ | |||||
type: 6, | |||||
payload: { | |||||
updatesResult: { | |||||
currentAsOf: 1683298141720, | |||||
newUpdates: [ | |||||
{ | |||||
type: 1, | |||||
id: '94428', | |||||
time: 1683298141720, | |||||
threadInfo: thread, | |||||
}, | |||||
], | |||||
}, | |||||
userInfos: [], | |||||
}, | |||||
}, | |||||
{ | |||||
type: serverSocketMessageTypes.MESSAGES, | |||||
payload: { | |||||
messagesResult: { | |||||
rawMessageInfos: messages, | |||||
truncationStatuses: { '86033': 'unchanged' }, | |||||
currentAsOf: 1683298141707, | |||||
}, | |||||
}, | |||||
}, | |||||
{ | |||||
type: serverSocketMessageTypes.API_RESPONSE, | |||||
responseTo: 209, | |||||
payload: { | |||||
rawMessageInfos: messages, | |||||
truncationStatuses: { '1': 'exhaustive' }, | |||||
userInfos: {}, | |||||
}, | |||||
}, | |||||
]; | |||||
const validatorByMessageType = { | |||||
[serverSocketMessageTypes.STATE_SYNC]: | |||||
serverStateSyncServerSocketMessageValidator, | |||||
[serverSocketMessageTypes.REQUESTS]: | |||||
serverRequestsServerSocketMessageValidator, | |||||
[serverSocketMessageTypes.ERROR]: errorServerSocketMessageValidator, | |||||
[serverSocketMessageTypes.AUTH_ERROR]: | |||||
authErrorServerSocketMessageValidator, | |||||
[serverSocketMessageTypes.ACTIVITY_UPDATE_RESPONSE]: | |||||
activityUpdateResponseServerSocketMessageValidator, | |||||
[serverSocketMessageTypes.PONG]: pongServerSocketMessageValidator, | |||||
[serverSocketMessageTypes.UPDATES]: | |||||
serverUpdatesServerSocketMessageValidator, | |||||
[serverSocketMessageTypes.MESSAGES]: messagesServerSocketMessageValidator, | |||||
[serverSocketMessageTypes.API_RESPONSE]: | |||||
apiResponseServerSocketMessageValidator, | |||||
}; | |||||
for (const validatorMessageType in validatorByMessageType) { | |||||
const validator = validatorByMessageType[validatorMessageType]; | |||||
const validatorMessageTypeName = _findKey( | |||||
e => e === Number(validatorMessageType), | |||||
)(serverSocketMessageTypes); | |||||
for (const message of socketMessages) { | |||||
const messageTypeName = _findKey(e => e === message.type)( | |||||
serverSocketMessageTypes, | |||||
); | |||||
if (Number(validatorMessageType) === message.type) { | |||||
it(`${validatorMessageTypeName} should validate ${messageTypeName}`, () => { | |||||
expect(validator.is(message)).toBe(true); | |||||
}); | |||||
} else { | |||||
it(`${validatorMessageTypeName} shouldn't validate ${messageTypeName}`, () => { | |||||
expect(validator.is(message)).toBe(false); | |||||
}); | |||||
} | |||||
} | |||||
} | |||||
}); |