-
Notifications
You must be signed in to change notification settings - Fork 320
/
Copy pathspechelper.js
321 lines (285 loc) · 9.62 KB
/
spechelper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
const isFunction = require('lodash/isFunction');
const querystring = require('querystring');
const sinon = require('sinon');
const ClientRequest = require('_http_client').ClientRequest;
const Q = require('q');
const http = require('http');
const https = require('https');
// Load all our custom assertions
const cloudinary = require("../cloudinary");
const { utils, config, Cache } = cloudinary;
const libPath = 'lib';
const FileKeyValueStorage = require(`../${libPath}/cache/FileKeyValueStorage`);
const KeyValueCacheAdapter = require(`../${libPath}/cache/KeyValueCacheAdapter`);
exports.libPath = libPath;
const api_http = String(config().upload_prefix).startsWith('http:') ? http : https;
exports.IMAGE_FILE = "test/.resources/logo.png";
exports.LARGE_IMAGE_FILE = "test/.resources/big-image.jpg";
exports.LARGE_RAW_FILE = "test/.resources/TheCompleteWorksOfShakespeare.mobi";
exports.LARGE_VIDEO = "test/.resources/CloudBookStudy-HD.mp4";
exports.EMPTY_IMAGE = "test/.resources/empty.gif";
exports.RAW_FILE = "test/.resources/docx.docx";
exports.ICON_FILE = "test/.resources/favicon.ico";
exports.VIDEO_URL = "https://res.cloudinary.com/demo/video/upload/dog.mp4";
exports.IMAGE_URL = "https://res.cloudinary.com/demo/image/upload/sample";
const ADDON_ALL = 'all'; // Test all addons.
exports.ADDON_ASPOSE = 'aspose'; // Aspose document conversion.
exports.ADDON_AZURE = 'azure'; // Microsoft azure video indexer.
exports.ADDON_BG_REMOVAL = 'bgremoval'; // Cloudinary AI background removal.
exports.ADDON_FACIAL_ATTRIBUTES_DETECTION = 'facialattributesdetection'; // Advanced facial attributes detection.
exports.ADDON_GOOGLE = 'google'; /* Google AI video moderation, google AI
video transcription, google auto tagging,
google automatic video tagging,
google translation.
*/
exports.ADDON_IMAGGA = 'imagga'; // Imagga auto tagging, crop and scale.
exports.ADDON_JPEGMINI = 'jpegmini'; // JPEGmini image optimization.
exports.ADDON_LIGHTROOM = 'lightroom'; // Adobe photoshop lightroom (BETA).
exports.ADDON_METADEFENDER = 'metadefender'; // MetaDefender anti-malware protection.
exports.ADDON_NEURAL_ARTWORK = 'neuralartwork'; // Neural artwork style transfer.
exports.ADDON_OBJECT_AWARE_CROPPING = 'objectawarecropping'; // Cloudinary object-aware cropping.
exports.ADDON_OCR = 'ocr'; // OCR text detection and extraction.
exports.ADDON_PIXELZ = 'pixelz'; // Remove the background.
exports.ADDON_REKOGNITION = 'rekognition'; /* Amazon rekognition AI moderation,
amazon rekognition auto tagging,
amazon rekognition celebrity detection.
*/
exports.ADDON_URL2PNG = 'url2png'; // URL2PNG website screenshots.
exports.ADDON_VIESUS = 'viesus'; // VIESUS automatic image enhancement.
exports.ADDON_WEBPURIFY = 'webpurify'; // WebPurify image moderation.
exports.DYNAMIC_FOLDERS = 'dynamic_folders'
const ALL = 'all';
const { TEST_TAG } = require('./testUtils/testConstants').TAGS;
exports.SAMPLE_VIDEO_SOURCES = [
{
type: 'mp4',
codecs: 'hev1',
transformations: { video_codec: 'h265' }
},
{
type: 'webm',
codecs: 'vp9',
transformations: { video_codec: 'vp9' }
},
{
type: 'mp4',
transformations: { video_codec: 'auto' }
},
{
type: 'webm',
transformations: { video_codec: 'auto' }
}
];
exports.test_cloudinary_url = function(public_id, options, expected_url, expected_options) {
var url;
url = utils.url(public_id, options);
expect(url).to.eql(expected_url);
expect(options).to.eql(expected_options);
return url;
};
const allExamples = {};
/**
* @Deprecated, please use testUtils/reusableTests/reusableTests.js
* @param name
* @param examples
* @return {(function(...[*]=))|*}
*/
function sharedExamples(name, examples) {
switch (true) {
case isFunction(examples):
allExamples[name] = examples;
return examples;
case allExamples.hasOwnProperty(name):
return allExamples[name];
default:
return function () {
console.log(`Shared example ${name} was not found!`);
};
}
}
exports.sharedExamples = sharedExamples;
exports.itBehavesLike = function (name, ...args) {
return it(`behaves like ${name}`, function () {
return sharedExamples(name).apply(this, args);
});
};
/**
Create a matcher method for upload parameters
@private
@function helper.paramMatcher
@param {string} name the parameter name
@param {*} value the parameter value
@return {function} the matcher function with the signature (arg)->Boolean
*/
exports.uploadParamMatcher = function (name, value) {
return function (arg) {
var return_part;
return_part = 'Content-Disposition: form-data; name="' + name + '"\r\n\r\n';
return_part += String(value);
return arg.indexOf(return_part) + 1;
};
};
/**
Create a matcher method for api parameters
@private
@function helper.apiParamMatcher
@param {string} name the parameter name
@param {*} value the parameter value
@return {function} the matcher function as (arg)->Boolean
*/
exports.apiParamMatcher = function (name, value) {
var expected, params;
params = {};
params[name] = value;
expected = querystring.stringify(params);
expected = exports.escapeRegexp(expected);
return function (arg) {
return new RegExp(expected).test(arg);
};
};
/**
Create a matcher method for api JSON parameters
@private
@function helper.apiJsonParamMatcher
@param {string} name the parameter name
@param {*} value the parameter value
@return {function} the matcher function as (arg)->Boolean
*/
exports.apiJsonParamMatcher = function (name, value) {
return function (arg) {
var expected, jsonArg;
jsonArg = JSON.parse(arg);
expected = JSON.stringify(value);
return jsonArg[name] && JSON.stringify(jsonArg[name]) === expected;
};
};
/**
Escape RegExp characters
@private
@param {string} s the string to escape
@return a new escaped string
*/
exports.escapeRegexp = function (s) {
return s.replace(/[{\[\].*+()}]/g, c => '\\' + c);
};
/**
@function mockTest
@nodoc
Provides a wrapper for mocked tests. Must be called in a `describe` context.
@example
<pre>
const mockTest = require('./spechelper').mockTest
describe("some topic", function() {
mocked = mockTest()
it("should do something" function() {
options.access_control = [acl];
cloudinary.v2.api.update("id", options);
sinon.assert.calledWith(mocked.writeSpy, sinon.match(function(arg) {
return helper.apiParamMatcher('access_control', "[" + acl_string + "]")(arg);
})
);
</pre>
@return {object} the mocked objects: `xhr`, `write`, `request`
*/
exports.mockTest = function () {
var mocked;
mocked = {};
before(function () {
mocked.xhr = sinon.useFakeXMLHttpRequest();
mocked.write = sinon.spy(ClientRequest.prototype, 'write');
mocked.request = sinon.spy(api_http, 'request');
});
after(function () {
mocked.request.restore();
mocked.write.restore();
mocked.xhr.restore();
});
return mocked;
};
/**
@callback mockBlock
A test block
@param xhr
@param writeSpy
@param requestSpy
@return {*} a promise or a value
*/
/**
@function provideMockObjects
Wraps the function to be mocked using a promise.
@param {function} providedFunction test function, accepting (mockXHR, writeSpy, requestSpy)
@return {Promise}
*/
exports.provideMockObjects = function (providedFunction) {
let requestSpy, writeSpy, mockXHR;
return Q.Promise(function (resolve, reject, notify) {
var result;
mockXHR = sinon.useFakeXMLHttpRequest();
writeSpy = sinon.spy(ClientRequest.prototype, 'write');
requestSpy = sinon.spy(api_http, 'request');
result = providedFunction(mockXHR, writeSpy, requestSpy);
if (result && isFunction(result.then)) {
return result.then(resolve);
} else {
return resolve(result);
}
}).finally(function () {
requestSpy.restore();
writeSpy.restore();
mockXHR.restore();
}).done();
};
exports.setupCache = function () {
if (!Cache.getAdapter()) {
Cache.setAdapter(new KeyValueCacheAdapter(new FileKeyValueStorage()));
}
};
/**
* Upload an image to be tested on.
*
* @param {object} options Optional options to use when uploading the test image
* @returns {object} A response object returned from the upload API
*/
exports.uploadImage = function (options = {}) {
// Ensure that options at the very least contains the TEST_TAG
options.tags = options.tags || [];
if (!options.tags.includes(TEST_TAG)) options.tags.push(TEST_TAG);
return cloudinary.v2.uploader.upload(exports.IMAGE_FILE, options);
};
/**
* Convert a timestamp to the date part of an ISO8601 string
*
* @param {string} timestamp The timestamp to convert
* @returns {string} The date part of a ISO8601 date time
*/
exports.toISO8601DateOnly = function (timestamp) {
const date = new Date(timestamp);
return date.toISOString().split('T')[0];
};
/**
* Checks if tests for an addon should be executed.
*
* @param {string} addOn
* @returns {boolean}
*/
exports.shouldTestAddOn = function (addOn) {
const cldTestAddons = (process.env.CLD_TEST_ADDONS || '').toLowerCase();
if (cldTestAddons === ADDON_ALL) {
return true;
}
return cldTestAddons.trim().split(',').includes(addOn.toLowerCase())
}
/**
* Should a certain feature be tested?
*
* @param {string} feature The feature to test.
*
* @return boolean
*/
exports.shouldTestFeature = function(feature){
const cldTestFeatures = (process.env.CLD_TEST_FEATURES || '').toLowerCase();
if (cldTestFeatures === ALL) {
return true;
}
return cldTestFeatures.trim().split(',').includes(feature.toLowerCase())
}