313 lines
9.7 KiB
JavaScript
313 lines
9.7 KiB
JavaScript
/**
|
|
* @fileOverview Unit tests for the schemas for the Image commands
|
|
* @see {@url http://10.0.10.242/w/tricore_architecture/server_interface/image_commands/}
|
|
*/
|
|
var _ = require('lodash');
|
|
var testHelper = require('./testHelpers.js');
|
|
|
|
/**
|
|
* Test data
|
|
*/
|
|
const VALID_SESSION_TOKEN = '01234567890abcdefghijklmnopqrstuvwxyzABCDE';
|
|
const VALID_DEVICE_TOKEN = '01234567890abcdefghijklmnopqrstuvwxyzABCDE';
|
|
const VALID_IMAGE_REF = '0123456789abcdef01234567';
|
|
|
|
// Base 64 encoding of 'address-book' icon from http://p.yusukekamiyamane.com/
|
|
// via http://davidbcalhoun.com/2011/when-to-base64-encode-images-and-when-not-to/
|
|
const VALID_BASE64_IMAGE = 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAA' +
|
|
'GXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAp1JREFUeNqEU21IU1EYfu7' +
|
|
'unW5Ty6aBszYs6MeUjGVYokHYyH5E1B9rZWFEFPQnAwmy6Hc/oqhfJsRKSSZGH1JIIX3MNC' +
|
|
'sqLTD9o1Oj6ebnnDfvvefezrnbdCHhCw/n433P8z7nPe/hBEEAtX0U7hc164uwuvVSXKwZL' +
|
|
'oOmaRDim+7m9vZa0WiEKSUFFpNpCWlmMyypqTDRuYn6t3k8vmQ2gRDCxs0t9fW45F52aBTR' +
|
|
'OJLtZl7nEZad2m+KtoQCQ0FBARyOCGRZ/q92I1WgqqXlfdd95VsrK8/pChIEqqpCkiQsiCI' +
|
|
'I0aBQZZoWl8lzFDwsFjMl0DBLY8Lj41hBwK4jSQrWOIphL6xYyhwJDWGo6wFSaH1Y3PTCAs' +
|
|
'ITE1oyAa8flhWkbSiCLX8vun11eiGIpiJ/z2nYdx5HqLdVV7elrOzsuqysL3rmBIGiKPizK' +
|
|
'CHHWY4PLVeQbnXAdegqdhy+hu8dDTBnbqQJZJ1A7u+vz7RaiymWCZgCRSF6Edk8b9cx+B/W' +
|
|
'6WuVxPaZnyiqXoPpyUmVYvkKTIFClHigEieKjYuSvETUllaF4GAUM1NT6ooaJDKx+aDfC9f' +
|
|
'Byxj90REb+9ppmIoAscH/6leg8MS9DJXPAM9xHCM443K57C6biMjcHDaVVCHw9RmCA2/RGC' +
|
|
'5C00AqXk/m4p20HZK4CM/J3Zk9n0ecMBhDQnJHcrTisyMfdQXOilrdMfxcwoHq/fg5R59Ti' +
|
|
'QV3hYGKo6X2J/c7LyQIjOx9GXhOw/zoJ8wEevRGyp53o/lGMNYsBgPtEwLecwov7/jGDKa1' +
|
|
'twT6o3KpL4MdZgGsWZLtfPr7f1q58k1JNHy7YYaM+J+K3Y2PmAIbRavX66229hrGVvvL5uz' +
|
|
'sHDEUvUu+NT1my78CDAAMK1a8/QaZCgAAAABJRU5ErkJggg==';
|
|
|
|
// Build a string that is 50,001 characters long, which is > than the limit
|
|
const INVALID_BASE64_TOO_LONG = _.repeat('a', 50001);
|
|
|
|
/**
|
|
* Test suite that defines the test command body and expected outcomes for
|
|
* a range of different bodies across different commands.
|
|
*/
|
|
const TEST_SUITE = {
|
|
AddImage: [
|
|
{
|
|
name: '',
|
|
valid: true,
|
|
data: {
|
|
'DeviceToken': VALID_DEVICE_TOKEN,
|
|
'SessionToken': VALID_SESSION_TOKEN,
|
|
'ImageFile': VALID_BASE64_IMAGE,
|
|
'FileType': 'JPEG',
|
|
'ImageType': 'Selfie'
|
|
}
|
|
},
|
|
{
|
|
name: 'with extra param',
|
|
valid: false,
|
|
data: {
|
|
'DeviceToken': VALID_DEVICE_TOKEN,
|
|
'SessionToken': VALID_SESSION_TOKEN,
|
|
'ImageFile': VALID_BASE64_IMAGE,
|
|
'FileType': 'JPEG',
|
|
'ImageType': 'Selfie',
|
|
extra: 'no'
|
|
}
|
|
},
|
|
{
|
|
name: 'no required params',
|
|
valid: false,
|
|
expect: {
|
|
missingRequiredCount: 5
|
|
},
|
|
data: {}
|
|
},
|
|
{
|
|
name: 'mistake in every param',
|
|
valid: false,
|
|
expect: {
|
|
errors: [
|
|
{
|
|
dataPath: '.ImageFile',
|
|
keyword: 'pattern'
|
|
},
|
|
{
|
|
dataPath: '.FileType',
|
|
keyword: 'enum'
|
|
},
|
|
{
|
|
dataPath: '.ImageType',
|
|
keyword: 'enum'
|
|
}
|
|
]
|
|
},
|
|
data: {
|
|
'DeviceToken': VALID_DEVICE_TOKEN,
|
|
'SessionToken': VALID_SESSION_TOKEN,
|
|
'ImageFile': 'a===', // Too many `=` padding chars
|
|
'FileType': 'GIF',
|
|
'ImageType': 'defaultSelfie'
|
|
}
|
|
},
|
|
{
|
|
name: 'image too long',
|
|
valid: false,
|
|
expect: {
|
|
errors: [
|
|
{
|
|
dataPath: '.ImageFile',
|
|
keyword: 'maxLength'
|
|
}
|
|
]
|
|
},
|
|
data: {
|
|
'DeviceToken': VALID_DEVICE_TOKEN,
|
|
'SessionToken': VALID_SESSION_TOKEN,
|
|
'ImageFile': INVALID_BASE64_TOO_LONG,
|
|
'FileType': 'JPEG',
|
|
'ImageType': 'Selfie'
|
|
}
|
|
},
|
|
{
|
|
name: 'image too short (less than 4 chars)',
|
|
valid: false,
|
|
expect: {
|
|
errors: [
|
|
{
|
|
dataPath: '.ImageFile',
|
|
keyword: 'minLength'
|
|
}
|
|
]
|
|
},
|
|
data: {
|
|
'DeviceToken': VALID_DEVICE_TOKEN,
|
|
'SessionToken': VALID_SESSION_TOKEN,
|
|
'ImageFile': 'abc',
|
|
'FileType': 'JPEG',
|
|
'ImageType': 'Selfie'
|
|
}
|
|
}
|
|
],
|
|
GetImage: [
|
|
{
|
|
name: '',
|
|
valid: true,
|
|
data: {
|
|
'DeviceToken': VALID_DEVICE_TOKEN,
|
|
'SessionToken': VALID_SESSION_TOKEN,
|
|
'ImageRef': VALID_IMAGE_REF
|
|
}
|
|
},
|
|
{
|
|
name: 'With defaultSelfie define.',
|
|
valid: true,
|
|
data: {
|
|
'DeviceToken': VALID_DEVICE_TOKEN,
|
|
'SessionToken': VALID_SESSION_TOKEN,
|
|
'ImageRef': 'defaultSelfie'
|
|
}
|
|
},
|
|
{
|
|
name: 'With defaultCompanyLogo0 define.',
|
|
valid: true,
|
|
data: {
|
|
'DeviceToken': VALID_DEVICE_TOKEN,
|
|
'SessionToken': VALID_SESSION_TOKEN,
|
|
'ImageRef': 'defaultCompanyLogo0'
|
|
}
|
|
},
|
|
{
|
|
name: 'no required params',
|
|
valid: false,
|
|
expect: {
|
|
missingRequiredCount: 3,
|
|
additionalPropsCount: 1
|
|
},
|
|
data: {invalidAdditionalProp: 'a'}
|
|
},
|
|
{
|
|
name: 'mistake in every param',
|
|
valid: false,
|
|
expect: {
|
|
errors: [
|
|
{
|
|
dataPath: '.ImageRef',
|
|
keyword: 'minLength'
|
|
},
|
|
{
|
|
dataPath: '.ImageRef',
|
|
keyword: 'pattern'
|
|
},
|
|
{
|
|
dataPath: '.ImageRef',
|
|
keyword: 'enum'
|
|
},
|
|
{
|
|
dataPath: '.ImageRef',
|
|
keyword: 'oneOf'
|
|
}
|
|
]
|
|
},
|
|
data: {
|
|
'DeviceToken': VALID_DEVICE_TOKEN,
|
|
'SessionToken': VALID_SESSION_TOKEN,
|
|
'ImageRef': 'A'
|
|
}
|
|
}
|
|
],
|
|
IconCache: [
|
|
{
|
|
name: '',
|
|
valid: true,
|
|
data: {}
|
|
},
|
|
{
|
|
name: 'additionalProperties',
|
|
valid: false,
|
|
expect: {
|
|
additionalPropsCount: 1
|
|
},
|
|
data: {invalidAdditionalProp: 'a'}
|
|
}
|
|
],
|
|
ImageCache: [
|
|
{
|
|
name: '',
|
|
valid: true,
|
|
data: {
|
|
'DeviceToken': VALID_DEVICE_TOKEN,
|
|
'SessionToken': VALID_SESSION_TOKEN
|
|
}
|
|
},
|
|
{
|
|
name: 'no required params',
|
|
valid: false,
|
|
expect: {
|
|
missingRequiredCount: 2,
|
|
additionalPropsCount: 1
|
|
},
|
|
data: {invalidAdditionalProp: 'a'}
|
|
}
|
|
],
|
|
ReportImage: [
|
|
{
|
|
name: '',
|
|
valid: true,
|
|
data: {
|
|
'DeviceToken': VALID_DEVICE_TOKEN,
|
|
'SessionToken': VALID_SESSION_TOKEN,
|
|
'ImageRef': VALID_IMAGE_REF
|
|
}
|
|
},
|
|
{
|
|
name: 'With defaultSelfie define.',
|
|
valid: true,
|
|
data: {
|
|
'DeviceToken': VALID_DEVICE_TOKEN,
|
|
'SessionToken': VALID_SESSION_TOKEN,
|
|
'ImageRef': 'defaultSelfie'
|
|
}
|
|
},
|
|
{
|
|
name: 'With defaultCompanyLogo0 define.',
|
|
valid: true,
|
|
data: {
|
|
'DeviceToken': VALID_DEVICE_TOKEN,
|
|
'SessionToken': VALID_SESSION_TOKEN,
|
|
'ImageRef': 'defaultCompanyLogo0'
|
|
}
|
|
},
|
|
{
|
|
name: 'no required params',
|
|
valid: false,
|
|
expect: {
|
|
missingRequiredCount: 3,
|
|
additionalPropsCount: 1
|
|
},
|
|
data: {invalidAdditionalProp: 'a'}
|
|
},
|
|
{
|
|
name: 'mistake in every param',
|
|
valid: false,
|
|
expect: {
|
|
errors: [
|
|
{
|
|
dataPath: '.ImageRef',
|
|
keyword: 'maxLength'
|
|
},
|
|
{
|
|
dataPath: '.ImageRef',
|
|
keyword: 'pattern'
|
|
},
|
|
{
|
|
dataPath: '.ImageRef',
|
|
keyword: 'enum'
|
|
},
|
|
{
|
|
dataPath: '.ImageRef',
|
|
keyword: 'oneOf'
|
|
}
|
|
]
|
|
},
|
|
data: {
|
|
'DeviceToken': VALID_DEVICE_TOKEN,
|
|
'SessionToken': VALID_SESSION_TOKEN,
|
|
'ImageRef': '0123456789abcdef01234567z'
|
|
}
|
|
}
|
|
]
|
|
};
|
|
|
|
/**
|
|
* Run the test suite through the test runner
|
|
*/
|
|
testHelper.runTestSuite('Schemas: Image Commands', TEST_SUITE);
|