17
17
'use strict' ;
18
18
19
19
var assert = require ( 'assert' ) ;
20
+ var extend = require ( 'extend' ) ;
21
+ var mockery = require ( 'mockery' ) ;
20
22
var util = require ( '../../lib/common/util.js' ) ;
21
23
24
+ var makeAuthenticatedRequestFactoryCache = util . makeAuthenticatedRequestFactory ;
25
+ var makeAuthenticatedRequestFactoryOverride ;
26
+ util . makeAuthenticatedRequestFactory = function ( ) {
27
+ if ( makeAuthenticatedRequestFactoryOverride ) {
28
+ return makeAuthenticatedRequestFactoryOverride . apply ( this , arguments ) ;
29
+ } else {
30
+ return makeAuthenticatedRequestFactoryCache . apply ( this , arguments ) ;
31
+ }
32
+ } ;
33
+
22
34
describe ( 'Dataset' , function ( ) {
23
35
var Dataset ;
36
+ var dataset ;
37
+
38
+ var OPTIONS = {
39
+ projectId : 'project-id' ,
40
+ apiEndpoint : 'endpoint' ,
41
+ credentials : { } ,
42
+ keyFilename : 'key/file' ,
43
+ email : 'email' ,
44
+ namespace : 'namespace'
45
+ } ;
46
+
47
+ before ( function ( ) {
48
+ mockery . registerMock ( '../common/util.js' , util ) ;
49
+
50
+ mockery . enable ( {
51
+ useCleanCache : true ,
52
+ warnOnUnregistered : false
53
+ } ) ;
24
54
25
- beforeEach ( function ( ) {
26
- delete require . cache [ require . resolve ( '../../lib/datastore/dataset' ) ] ;
27
55
Dataset = require ( '../../lib/datastore/dataset' ) ;
28
56
} ) ;
29
57
58
+ after ( function ( ) {
59
+ mockery . deregisterAll ( ) ;
60
+ mockery . disable ( ) ;
61
+ } ) ;
62
+
63
+ beforeEach ( function ( ) {
64
+ makeAuthenticatedRequestFactoryOverride = null ;
65
+ dataset = new Dataset ( OPTIONS ) ;
66
+ } ) ;
67
+
30
68
describe ( 'instantiation' , function ( ) {
31
69
it ( 'should throw if a projectId is not specified' , function ( ) {
32
70
assert . throws ( function ( ) {
33
71
new Dataset ( ) ;
34
72
} , / S o r r y , w e c a n n o t c o n n e c t / ) ;
35
73
} ) ;
36
74
37
- it ( 'should set default API connection details' , function ( ) {
38
- var options = { a : 'b' , c : 'd' , projectId : 'project-id' } ;
39
- var mockApiEndpoint = 'http://localhost:8080' ;
75
+ it ( 'should set default API connection details' , function ( done ) {
76
+ var determineApiEndpoint_ = Dataset . prototype . determineApiEndpoint_ ;
77
+
78
+ Dataset . prototype . determineApiEndpoint_ = function ( customApiEndpoint ) {
79
+ Dataset . prototype . determineApiEndpoint_ = determineApiEndpoint_ ;
80
+
81
+ assert . strictEqual ( customApiEndpoint , OPTIONS . apiEndpoint ) ;
82
+ done ( ) ;
83
+ } ;
84
+
85
+ new Dataset ( OPTIONS ) ;
86
+ } ) ;
87
+
88
+ it ( 'should create an authenticated request factory' , function ( ) {
89
+ var authenticatedRequest = { } ;
90
+ var customEndpoint = 'custom-endpoint' ;
91
+
92
+ var determineApiEndpoint_ = Dataset . prototype . determineApiEndpoint_ ;
93
+ Dataset . prototype . determineApiEndpoint_ = function ( ) {
94
+ Dataset . prototype . determineApiEndpoint_ = determineApiEndpoint_ ;
95
+ this . customEndpoint = customEndpoint ;
96
+ } ;
97
+
98
+ makeAuthenticatedRequestFactoryOverride = function ( config ) {
99
+ assert . strictEqual ( config . customEndpoint , customEndpoint ) ;
100
+ assert . strictEqual ( config . credentials , OPTIONS . credentials ) ;
101
+ assert . strictEqual ( config . keyFile , OPTIONS . keyFilename ) ;
102
+ assert . strictEqual ( config . email , OPTIONS . email ) ;
40
103
41
- Dataset . determineApiEndpoint_ = function ( opts ) {
42
- assert . deepEqual ( opts , options ) ;
43
- return mockApiEndpoint ;
104
+ assert . deepEqual ( config . scopes , [
105
+ 'https://www.googleapis.com/auth/datastore' ,
106
+ 'https://www.googleapis.com/auth/userinfo.email'
107
+ ] ) ;
108
+
109
+ return authenticatedRequest ;
44
110
} ;
45
111
46
- var ds = new Dataset ( options ) ;
47
- assert . equal ( ds . apiEndpoint , mockApiEndpoint ) ;
112
+ var ds = new Dataset ( OPTIONS ) ;
113
+ assert . strictEqual ( ds . makeAuthenticatedRequest_ , authenticatedRequest ) ;
114
+ } ) ;
115
+
116
+ it ( 'should localize the project id' , function ( ) {
117
+ assert . strictEqual ( dataset . projectId , OPTIONS . projectId ) ;
118
+ } ) ;
119
+
120
+ it ( 'should localize the namespace' , function ( ) {
121
+ assert . strictEqual ( dataset . namespace , OPTIONS . namespace ) ;
48
122
} ) ;
49
123
} ) ;
50
124
@@ -196,35 +270,47 @@ describe('Dataset', function() {
196
270
describe ( 'determineApiEndpoint_' , function ( ) {
197
271
it ( 'should default to googleapis.com' , function ( ) {
198
272
delete process . env . DATASTORE_HOST ;
273
+
274
+ dataset . determineApiEndpoint_ ( ) ;
275
+
199
276
var expectedApiEndpoint = 'https://www.googleapis.com' ;
200
- assert . equal ( Dataset . determineApiEndpoint_ ( { } ) , expectedApiEndpoint ) ;
277
+ assert . strictEqual ( dataset . apiEndpoint , expectedApiEndpoint ) ;
201
278
} ) ;
202
279
203
280
it ( 'should remove slashes from the apiEndpoint' , function ( ) {
204
281
var expectedApiEndpoint = 'http://localhost:8080' ;
205
282
206
- assert . equal ( Dataset . determineApiEndpoint_ ( {
207
- apiEndpoint : expectedApiEndpoint
208
- } ) , expectedApiEndpoint ) ;
283
+ dataset . determineApiEndpoint_ ( expectedApiEndpoint ) ;
284
+ assert . strictEqual ( dataset . apiEndpoint , expectedApiEndpoint ) ;
209
285
210
- assert . equal ( Dataset . determineApiEndpoint_ ( {
211
- apiEndpoint : 'http://localhost:8080/'
212
- } ) , expectedApiEndpoint ) ;
286
+ dataset . determineApiEndpoint_ ( 'http://localhost:8080/' ) ;
287
+ assert . strictEqual ( dataset . apiEndpoint , expectedApiEndpoint ) ;
213
288
214
- assert . equal ( Dataset . determineApiEndpoint_ ( {
215
- apiEndpoint : 'http://localhost:8080//'
216
- } ) , expectedApiEndpoint ) ;
289
+ dataset . determineApiEndpoint_ ( 'http://localhost:8080//' ) ;
290
+ assert . strictEqual ( dataset . apiEndpoint , expectedApiEndpoint ) ;
217
291
} ) ;
218
292
219
293
it ( 'should default to http if protocol is unspecified' , function ( ) {
220
- var apiEndpoint = Dataset . determineApiEndpoint_ ( {
221
- apiEndpoint : 'localhost:8080'
222
- } ) ;
223
- assert . equal ( apiEndpoint , 'http://localhost:8080' ) ;
294
+ dataset . determineApiEndpoint_ ( 'localhost:8080' ) ;
295
+ assert . strictEqual ( dataset . apiEndpoint , 'http://localhost:8080' ) ;
296
+ } ) ;
297
+
298
+ it ( 'should set customEndpoint when using explicit endpoint' , function ( ) {
299
+ dataset . determineApiEndpoint_ ( 'http://localhost:8080' ) ;
300
+ assert . strictEqual ( dataset . customEndpoint , true ) ;
301
+ } ) ;
302
+
303
+ it ( 'should not set customEndpoint when using default endpoint' , function ( ) {
304
+ var options = extend ( { } , OPTIONS ) ;
305
+ delete options . apiEndpoint ;
306
+
307
+ var dataset = new Dataset ( options ) ;
308
+ dataset . determineApiEndpoint_ ( ) ;
309
+ assert . strictEqual ( dataset . customEndpoint , undefined ) ;
224
310
} ) ;
225
311
226
312
describe ( 'with DATASTORE_HOST environment variable' , function ( ) {
227
- var DATASTORE_HOST = 'http://localhost:8080 ' ;
313
+ var DATASTORE_HOST = 'http://localhost:9090 ' ;
228
314
229
315
before ( function ( ) {
230
316
process . env . DATASTORE_HOST = DATASTORE_HOST ;
@@ -235,15 +321,19 @@ describe('Dataset', function() {
235
321
} ) ;
236
322
237
323
it ( 'should use the DATASTORE_HOST env var' , function ( ) {
238
- assert . equal ( Dataset . determineApiEndpoint_ ( { } ) , DATASTORE_HOST ) ;
324
+ dataset . determineApiEndpoint_ ( ) ;
325
+ assert . strictEqual ( dataset . apiEndpoint , DATASTORE_HOST ) ;
239
326
} ) ;
240
327
241
328
it ( 'should favor an explicit apiEndpoint option' , function ( ) {
242
- var expectedApiEndpoint = 'http://apiendpointoverride' ;
329
+ var explicitApiEndpoint = 'http://apiendpointoverride' ;
330
+ dataset . determineApiEndpoint_ ( explicitApiEndpoint ) ;
331
+ assert . strictEqual ( dataset . apiEndpoint , explicitApiEndpoint ) ;
332
+ } ) ;
243
333
244
- assert . equal ( Dataset . determineApiEndpoint_ ( {
245
- apiEndpoint : expectedApiEndpoint
246
- } ) , expectedApiEndpoint ) ;
334
+ it ( 'should set customEndpoint' , function ( ) {
335
+ dataset . determineApiEndpoint_ ( ) ;
336
+ assert . strictEqual ( dataset . customEndpoint , true ) ;
247
337
} ) ;
248
338
} ) ;
249
339
} ) ;
0 commit comments