-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathutil.js
More file actions
352 lines (313 loc) · 11.8 KB
/
util.js
File metadata and controls
352 lines (313 loc) · 11.8 KB
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
const _ = require( "lodash" );
const { expect } = require( "chai" );
const crypto = require( "crypto" );
const util = require( "../lib/util" );
function expectError( e, done ) {
const res = {
status: ( ) => { },
jsonp: j => {
expect( j ).to.deep.eq( e );
done( );
}
};
return res;
}
describe( "util", ( ) => {
describe( "renderError", ( ) => {
it( "renders a generic exception", done => {
const res = expectError( { error: "Error", status: 500 }, done );
util.renderError( " testing renderError", res );
} );
it( "renders a custom exception", done => {
const e = { error: "testing", status: 501 };
const res = expectError( e, done );
util.renderError( e, res );
} );
} );
describe( "debug", ( ) => {
it( "writes to console.log", ( ) => {
expect( util.debug( " testing debug" ) ).to.be.undefined;
} );
} );
describe( "pp", ( ) => {
it( "writes to compclidated objects", ( ) => {
expect( util.pp( { just: { a: { test: { } } } } ) ).to.be.undefined;
} );
} );
describe( "capitalize", ( ) => {
it( "capitalizes only the first letter", ( ) => {
expect( util.capitalize( "helloWorld" ) ).to.eq( "Helloworld" );
} );
} );
describe( "localeOpts", ( ) => {
it( "defaults to en", ( ) => {
const opts = util.localeOpts( { query: { } } );
expect( opts.locale ).to.eq( "en" );
} );
it( "uses the specified locale", ( ) => {
const opts = util.localeOpts( { query: { locale: "de" } } );
expect( opts.locale ).to.eq( "de" );
} );
it( "sets places", ( ) => {
const req = { query: { }, inat: { place: "PL", preferredPlace: "PPL" } };
const opts = util.localeOpts( req );
expect( opts.place ).to.eq( "PL" );
expect( opts.preferredPlace ).to.eq( "PPL" );
} );
it( "sets locale based on user session", ( ) => {
const req = { query: { }, inat: { }, userSession: { locale: "de" } };
const opts = util.localeOpts( req );
expect( opts.locale ).to.eq( "de" );
} );
it( "sets preferredPlace based on user session", ( ) => {
const req = { query: { }, inat: { }, userSession: { preferredPlace: { id: 111 } } };
const opts = util.localeOpts( req );
expect( opts.preferredPlace.id ).to.eq( 111 );
} );
it( "overrides user session locale with params", ( ) => {
const req = { query: { locale: "es" }, inat: { }, userSession: { locale: "de" } };
const opts = util.localeOpts( req );
expect( opts.locale ).to.eq( "es" );
} );
it( "overrides user session place with params", ( ) => {
const req = {
query: { },
inat: { place: "PL", preferredPlace: "PPL" },
userSession: { preferredPlace: { id: 111 } }
};
const opts = util.localeOpts( req );
expect( opts.place ).to.eq( "PL" );
expect( opts.preferredPlace ).to.eq( "PPL" );
} );
it( "sets iw to he", ( ) => {
const opts = util.localeOpts( { query: { locale: "iw" } } );
expect( opts.locale ).to.eq( "he" );
} );
it( "sets iw-IL to he-IL", ( ) => {
const opts = util.localeOpts( { query: { locale: "iw-IL" } } );
expect( opts.locale ).to.eq( "he-il" ); // not sure why it's lowercase...
} );
} );
describe( "userAgentClient", ( ) => {
it( "returns nil when request user agent is empty", ( ) => {
expect( util.userAgentClient( { headers: { } } ) ).to.be.null;
expect( util.userAgentClient( { headers: { "user-agent": null } } ) ).to.be.null;
expect( util.userAgentClient( { headers: { "user-agent": "" } } ) ).to.be.null;
} );
it( "returns nil when request user agent is unrecognized", ( ) => {
expect( util.userAgentClient( { headers: { "user-agent": "nonsense" } } ) ).to.be.null;
} );
it( "recognizes inatrn client user agents", ( ) => {
expect( util.userAgentClient( {
headers: {
"user-agent": "iNaturalistReactNative/60 CFNetwork/1474 Darwin/23.0.0"
}
} ) ).to.eq( "inatrn" );
expect( util.userAgentClient( {
headers: {
"user-agent": "iNaturalistRN/0.14.0 Handset (Build 60) iOS/17.0.3"
}
} ) ).to.eq( "inatrn" );
} );
it( "recognizes seek client user agents", ( ) => {
expect( util.userAgentClient( {
headers: {
"user-agent": "Seek/2.15.3 Handset (Build 316) iOS/17.0.3"
}
} ) ).to.eq( "seek" );
expect( util.userAgentClient( {
headers: {
"user-agent": "Seek/2.15.3 Handset (Build 316) Android/13"
}
} ) ).to.eq( "seek" );
} );
it( "recognizes inat-ios client user agents", ( ) => {
expect( util.userAgentClient( {
headers: {
"user-agent": "iNaturalist/708 CFNetwork/1410.0.3 Darwin/22.6.0"
}
} ) ).to.eq( "inat-ios" );
} );
it( "recognizes inat-android client user agents", ( ) => {
expect( util.userAgentClient( {
headers: {
"user-agent": "iNaturalist/1.29.18 (Build 592; Android 5.10.157-android13-4-00001-g5c7ff5dc7aac-ab10381520 10754064; SDK 34; bluejay Pixel 6a bluejay; OS Version 14)"
}
} ) ).to.eq( "inat-android" );
} );
it( "returns nil for unrecognized user agents", ( ) => {
expect( util.userAgentClient( {
headers: {
"user-agent": "nonsense"
}
} ) ).to.be.null;
} );
} );
describe( "observationSearchRequestCacheKey", ( ) => {
it( "returns a cache key for cacheable queries", ( ) => {
const req = {
query: {
page: "1",
return_bounds: "true"
}
};
expect( util.observationSearchRequestCacheKey( req, "ObservationsController.search", {
enableInTestEnv: true
} ) ).to.eq( "ObservationsController.search-returnBounds-true" );
} );
function expectParamInCacheKey( paramKey, paramVal, paramCacheKey ) {
const req = {
query: {
[paramKey]: paramVal
}
};
expect( util.observationSearchRequestCacheKey( req, "ObservationsController.search", {
enableInTestEnv: true
} ) ).to.eq( `ObservationsController.search-${paramCacheKey}-${paramVal}` );
}
it( "allows queries with place_id to be cached for obs search", ( ) => {
expectParamInCacheKey( "place_id", 1, "placeID" );
} );
it( "allows queries with numeric per_page value of 0 to be cached for obs search", ( ) => {
expectParamInCacheKey( "per_page", 0, "perPage" );
} );
it( "does not allow queries with place_id to be cached for obs search when logged in", ( ) => {
const req = {
query: {
place_id: 1
},
userSession: {
user_id: 1
}
};
expect( util.observationSearchRequestCacheKey( req, "ObservationsController.search", {
enableInTestEnv: true
} ) ).to.be.null;
} );
function expectParamNotToGenerateCacheKey( paramKey, paramValue ) {
const req = { query: { [paramKey]: paramValue } };
expect( util.observationSearchRequestCacheKey( req, "ObservationsController.search", {
enableInTestEnv: true
} ) ).to.be.null;
}
it( "should not generate a key if lat in params", ( ) => expectParamNotToGenerateCacheKey( "lat", 1 ) );
it( "should not generate a key if lng in params", ( ) => expectParamNotToGenerateCacheKey( "lng", 1 ) );
it( "should not generate a key if swlat in params", ( ) => expectParamNotToGenerateCacheKey( "swlat", 1 ) );
it( "should not generate a key if swlng in params", ( ) => expectParamNotToGenerateCacheKey( "swlng", 1 ) );
it( "should not generate a key if nelat in params", ( ) => expectParamNotToGenerateCacheKey( "nelat", 1 ) );
it( "should not generate a key if nelng in params", ( ) => expectParamNotToGenerateCacheKey( "nelng", 1 ) );
it( "generates a key if radius is included without lat or lng", ( ) => {
const req = {
query: {
radius: 1
}
};
expect( util.observationSearchRequestCacheKey( req, "ObservationsController.search", {
enableInTestEnv: true
} ) ).to.eq( "ObservationsController.search" );
} );
it( "ignores known invalid parameters", ( ) => {
const req = {
query: {
queryKey: "something"
}
};
expect( util.observationSearchRequestCacheKey( req, "ObservationsController.search", {
enableInTestEnv: true
} ) ).to.eq( "ObservationsController.search" );
} );
it( "does not generate a key if lat and lng are present", ( ) => {
const req = {
query: {
lat: 1,
lng: 1
}
};
expect( util.observationSearchRequestCacheKey( req, "ObservationsController.search", {
enableInTestEnv: true
} ) ).to.be.null;
} );
it( "does not generate a key if lat, lng, and radius are present", ( ) => {
const req = {
query: {
lat: 1,
lng: 1,
radius: 1
}
};
expect( util.observationSearchRequestCacheKey( req, "ObservationsController.search", {
enableInTestEnv: true
} ) ).to.be.null;
} );
it( "includes locale in cache key for obs search by default", ( ) => {
const req = {
query: {
locale: "en"
}
};
expect( util.observationSearchRequestCacheKey( req, "ObservationsController.search", {
enableInTestEnv: true
} ) ).to.eq( "ObservationsController.search-locale-en" );
} );
it( "excludes locale from cache key for obs search when requested", ( ) => {
const req = {
query: {
locale: "en"
}
};
expect( util.observationSearchRequestCacheKey( req, "ObservationsController.search", {
enableInTestEnv: true,
ignoreLocalization: true
} ) ).to.eq( "ObservationsController.search" );
} );
it( "returns a cache key for v2-like cacheable queries", ( ) => {
const fields = {
taxon: true,
user: true
};
const fieldsHash = crypto
.createHash( "md5" )
.update( JSON.stringify( fields ), "utf8" )
.digest( "hex" );
const req = {
query: {
locale: "en",
fields
},
inat: {
isV2: true,
fieldRequested: ( ) => { },
fields
}
};
expect( util.observationSearchRequestCacheKey( req, "ObservationsController.search", {
enableInTestEnv: true,
ignoreLocalization: true
} ) ).to.eq( `ObservationsController.search-fields-${fieldsHash}` );
} );
} );
describe( "inRandomGrouping", ( ) => {
it( "returns true when percentage is at or above 100", ( ) => {
let samples = _.times( 10000, util.inRandomGrouping( 100 ) );
expect( _.every( samples, sample => sample === true ) );
samples = _.times( 10000, util.inRandomGrouping( 110 ) );
expect( _.every( samples, sample => sample === true ) );
} );
it( "returns false when percentage is at or below 0", ( ) => {
let samples = _.times( 10000, util.inRandomGrouping( 0 ) );
expect( _.every( samples, sample => sample === false ) );
samples = _.times( 10000, util.inRandomGrouping( -1 ) );
expect( _.every( samples, sample => sample === false ) );
} );
it( "returns true roughly `percentage` percent of the time", ( ) => {
const testPercentages = [1, 5, 10, 20, 30, 40, 50, 60, 70, 80, 99];
_.each( testPercentages, testPercentage => {
const samples = _.times( 100000, ( ) => util.inRandomGrouping( testPercentage ) );
const countTrue = _.filter( samples, sample => sample === true ).length;
const truePercentage = ( countTrue / samples.length ) * 100;
expect( truePercentage ).to.be.below( testPercentage + 2 );
expect( truePercentage ).to.be.above( testPercentage - 2 );
} );
} );
} );
} );