Skip to content

Commit 7374122

Browse files
committed
Merge pull request #940 from stephenplusplus/spp--gcd-no-auth
datastore: don't require auth with local datastore server
2 parents b7fed35 + 04f8418 commit 7374122

File tree

2 files changed

+135
-42
lines changed

2 files changed

+135
-42
lines changed

lib/datastore/dataset.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,17 @@ function Dataset(options) {
110110
throw util.missingProjectIdError;
111111
}
112112

113+
this.determineApiEndpoint_(options.apiEndpoint);
114+
this.namespace = options.namespace;
115+
this.projectId = options.projectId;
116+
113117
this.makeAuthenticatedRequest_ = util.makeAuthenticatedRequestFactory({
114-
customEndpoint: typeof options.apiEndpoint !== 'undefined',
118+
customEndpoint: this.customEndpoint,
115119
credentials: options.credentials,
116120
keyFile: options.keyFilename,
117121
scopes: SCOPES,
118122
email: options.email
119123
});
120-
121-
this.apiEndpoint = Dataset.determineApiEndpoint_(options);
122-
this.namespace = options.namespace;
123-
this.projectId = options.projectId;
124124
}
125125

126126
nodeutil.inherits(Dataset, DatastoreRequest);
@@ -132,24 +132,27 @@ nodeutil.inherits(Dataset, DatastoreRequest);
132132
*
133133
* @private
134134
*
135-
* @param {object} options - Configuration object.
136-
* @param {string=} options.apiEndpoint - Custom API endpoint.
135+
* @param {string} customApiEndpoint - Custom API endpoint.
137136
*/
138-
Dataset.determineApiEndpoint_ = function(options) {
139-
var apiEndpoint = 'https://www.googleapis.com';
137+
Dataset.prototype.determineApiEndpoint_ = function(customApiEndpoint) {
138+
var apiEndpoint;
140139
var trailingSlashes = new RegExp('/*$');
141140

142-
if (options.apiEndpoint) {
143-
apiEndpoint = options.apiEndpoint;
141+
if (customApiEndpoint) {
142+
apiEndpoint = customApiEndpoint;
143+
this.customEndpoint = true;
144144
} else if (process.env.DATASTORE_HOST) {
145145
apiEndpoint = process.env.DATASTORE_HOST;
146+
this.customEndpoint = true;
147+
} else {
148+
apiEndpoint = 'https://www.googleapis.com';
146149
}
147150

148151
if (apiEndpoint.indexOf('http') !== 0) {
149152
apiEndpoint = 'http://' + apiEndpoint;
150153
}
151154

152-
return apiEndpoint.replace(trailingSlashes, '');
155+
this.apiEndpoint = apiEndpoint.replace(trailingSlashes, '');
153156
};
154157

155158
/**

test/datastore/dataset.js

Lines changed: 120 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,108 @@
1717
'use strict';
1818

1919
var assert = require('assert');
20+
var extend = require('extend');
21+
var mockery = require('mockery');
2022
var util = require('../../lib/common/util.js');
2123

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+
2234
describe('Dataset', function() {
2335
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+
});
2454

25-
beforeEach(function() {
26-
delete require.cache[require.resolve('../../lib/datastore/dataset')];
2755
Dataset = require('../../lib/datastore/dataset');
2856
});
2957

58+
after(function() {
59+
mockery.deregisterAll();
60+
mockery.disable();
61+
});
62+
63+
beforeEach(function() {
64+
makeAuthenticatedRequestFactoryOverride = null;
65+
dataset = new Dataset(OPTIONS);
66+
});
67+
3068
describe('instantiation', function() {
3169
it('should throw if a projectId is not specified', function() {
3270
assert.throws(function() {
3371
new Dataset();
3472
}, /Sorry, we cannot connect/);
3573
});
3674

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);
40103

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;
44110
};
45111

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);
48122
});
49123
});
50124

@@ -196,35 +270,47 @@ describe('Dataset', function() {
196270
describe('determineApiEndpoint_', function() {
197271
it('should default to googleapis.com', function() {
198272
delete process.env.DATASTORE_HOST;
273+
274+
dataset.determineApiEndpoint_();
275+
199276
var expectedApiEndpoint = 'https://www.googleapis.com';
200-
assert.equal(Dataset.determineApiEndpoint_({}), expectedApiEndpoint);
277+
assert.strictEqual(dataset.apiEndpoint, expectedApiEndpoint);
201278
});
202279

203280
it('should remove slashes from the apiEndpoint', function() {
204281
var expectedApiEndpoint = 'http://localhost:8080';
205282

206-
assert.equal(Dataset.determineApiEndpoint_({
207-
apiEndpoint: expectedApiEndpoint
208-
}), expectedApiEndpoint);
283+
dataset.determineApiEndpoint_(expectedApiEndpoint);
284+
assert.strictEqual(dataset.apiEndpoint, expectedApiEndpoint);
209285

210-
assert.equal(Dataset.determineApiEndpoint_({
211-
apiEndpoint: 'http://localhost:8080/'
212-
}), expectedApiEndpoint);
286+
dataset.determineApiEndpoint_('http://localhost:8080/');
287+
assert.strictEqual(dataset.apiEndpoint, expectedApiEndpoint);
213288

214-
assert.equal(Dataset.determineApiEndpoint_({
215-
apiEndpoint: 'http://localhost:8080//'
216-
}), expectedApiEndpoint);
289+
dataset.determineApiEndpoint_('http://localhost:8080//');
290+
assert.strictEqual(dataset.apiEndpoint, expectedApiEndpoint);
217291
});
218292

219293
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);
224310
});
225311

226312
describe('with DATASTORE_HOST environment variable', function() {
227-
var DATASTORE_HOST = 'http://localhost:8080';
313+
var DATASTORE_HOST = 'http://localhost:9090';
228314

229315
before(function() {
230316
process.env.DATASTORE_HOST = DATASTORE_HOST;
@@ -235,15 +321,19 @@ describe('Dataset', function() {
235321
});
236322

237323
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);
239326
});
240327

241328
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+
});
243333

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);
247337
});
248338
});
249339
});

0 commit comments

Comments
 (0)