Skip to content

Commit d8f4df8

Browse files
author
Eric Koleda
committed
Additional changes.
1 parent 08d9d45 commit d8f4df8

File tree

8 files changed

+61
-52
lines changed

8 files changed

+61
-52
lines changed

.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = {
1111
},
1212
"rules": {
1313
"comma-dangle": "off",
14-
"no-var": "off"
14+
"no-var": "off",
15+
"generator-star-spacing": ["error", {"anonymous": "neither"}],
1516
}
1617
};

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
"eslint-config-google": "^0.9.1",
2121
"gas-local": "^1.3.0",
2222
"gulp": "^3.9.1",
23-
"gulp-bump": "^0.3.1",
2423
"gulp-clean": "^0.3.2",
2524
"gulp-concat": "^2.6.1",
2625
"gulp-eslint": "^4.0.2",

src/Service.gs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ var Service_ = function(serviceName) {
4646
Service_.EXPIRATION_BUFFER_SECONDS_ = 60;
4747

4848
/**
49-
* The number of seconds that a token should remain in the cache.
49+
* The number of milliseconds that a token should remain in the cache.
5050
* @type {number}
5151
* @private
5252
*/
@@ -181,6 +181,7 @@ Service_.prototype.setClientSecret = function(clientSecret) {
181181
* @param {PropertiesService.Properties} propertyStore The property store to use
182182
* when persisting credentials.
183183
* @return {Service_} This service, for chaining.
184+
* @see https://developers.google.com/apps-script/reference/properties/
184185
*/
185186
Service_.prototype.setPropertyStore = function(propertyStore) {
186187
this.propertyStore_ = propertyStore;
@@ -195,6 +196,7 @@ Service_.prototype.setPropertyStore = function(propertyStore) {
195196
* @param {CacheService.Cache} cache The cache to use when persisting
196197
* credentials.
197198
* @return {Service_} This service, for chaining.
199+
* @see https://developers.google.com/apps-script/reference/cache/
198200
*/
199201
Service_.prototype.setCache = function(cache) {
200202
this.cache_ = cache;
@@ -208,6 +210,7 @@ Service_.prototype.setCache = function(cache) {
208210
* when two executions attempt to refresh an expired token.
209211
* @param {LockService.Lock} lock The lock to use when accessing credentials.
210212
* @return {Service_} This service, for chaining.
213+
* @see https://developers.google.com/apps-script/reference/lock/
211214
*/
212215
Service_.prototype.setLock = function(lock) {
213216
this.lock_ = lock;
@@ -510,7 +513,7 @@ Service_.prototype.refresh = function() {
510513
throw new Error('Offline access is required.');
511514
}
512515
var headers = {
513-
'Accept': this.tokenFormat_
516+
Accept: this.tokenFormat_
514517
};
515518
if (this.tokenHeaders_) {
516519
headers = extend_(headers, this.tokenHeaders_);
@@ -661,7 +664,7 @@ Service_.prototype.createJwt_ = function() {
661664
};
662665

663666
/**
664-
* Locks access to a block of code, if a lock has been set on this service.
667+
* Locks access to a block of code if a lock has been set on this service.
665668
* @param {function} func The code to execute.
666669
* @return {*} The result of the code block.
667670
* @private

test/mocks/cache.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
var MockCache = function() {
2-
this.store = {};
1+
var MockCache = function(optCache) {
2+
this.store = optCache || {};
33
this.counter = 0;
44
};
55

test/mocks/lock.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,44 @@
1+
/**
2+
* @file Mocks out Apps Script's LockService.Lock, using the fibers library to
3+
* emulate concurrent executions. Like Apps Script's locks, only one execution
4+
* can hold the lock at a time.
5+
*/
6+
17
var Fiber = require('fibers');
28

39
var locked = false;
410
var waitingFibers = [];
511

612
var MockLock = function() {
7-
this.gotLock = false;
13+
this.hasLock_ = false;
814
this.id = Math.random();
915
};
1016

1117
MockLock.prototype.waitLock = function(timeoutInMillis) {
1218
var start = new Date();
1319
do {
14-
if (!locked || this.gotLock) {
20+
if (!locked || this.hasLock_) {
1521
locked = true;
16-
this.gotLock = true;
22+
this.hasLock_ = true;
1723
return;
1824
} else {
1925
waitingFibers.push(Fiber.current);
2026
Fiber.yield();
2127
}
22-
} while (timeDiffInMillis(new Date(), start) < timeoutInMillis);
28+
} while (new Date().getTime() - start.getTime() < timeoutInMillis);
2329
throw new Error('Unable to get lock');
2430
};
2531

2632
MockLock.prototype.releaseLock = function() {
2733
locked = false;
28-
this.gotLock = false;
34+
this.hasLock_ = false;
2935
if (waitingFibers.length) {
3036
waitingFibers.pop().run();
3137
}
3238
};
3339

3440
MockLock.prototype.hasLock = function() {
35-
return this.gotLock;
41+
return this.hasLock_;
3642
};
3743

38-
function timeDiffInMillis(a, b) {
39-
return a.getTime() - b.getTime();
40-
}
41-
4244
module.exports = MockLock;

test/mocks/properties.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
var MockProperties = function() {
2-
this.store = {};
1+
var MockProperties = function(optStore) {
2+
this.store = optStore || {};
33
this.counter = 0;
44
};
55

test/mocks/urlfetchapp.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* @file Mocks out Apps Script's UrlFetchApp, using the fibers library to
3+
* emulate concurrent executions.
4+
*/
5+
16
var Future = require('fibers/future');
27

38
var MockUrlFetchApp = function() {
@@ -18,7 +23,7 @@ MockUrlFetchApp.prototype.fetch = function(url, optOptions) {
1823
};
1924

2025
function sleep(ms) {
21-
var future = new Future;
26+
var future = new Future();
2227
setTimeout(function() {
2328
future.return();
2429
}, ms);

test/test.js

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -47,44 +47,45 @@ describe('Service', function() {
4747
});
4848

4949
it('should load from the cache', function() {
50-
var cache = new MockCache();
51-
var service = OAuth2.createService('test')
52-
.setPropertyStore(new MockProperties())
53-
.setCache(cache);
5450
var token = {
5551
access_token: 'foo'
5652
};
57-
cache.put('oauth2.test', JSON.stringify(token));
53+
var cache = new MockCache({
54+
'oauth2.test': JSON.stringify(token)
55+
});
56+
var service = OAuth2.createService('test')
57+
.setPropertyStore(new MockProperties())
58+
.setCache(cache);
5859
assert.deepEqual(service.getToken(), token);
5960
});
6061

6162
it('should load from the properties and set the cache', function() {
63+
var token = {
64+
access_token: 'foo'
65+
};
6266
var cache = new MockCache();
63-
var properties = new MockProperties();
67+
var properties = new MockProperties({
68+
'oauth2.test': JSON.stringify(token)
69+
});
6470
var service = OAuth2.createService('test')
6571
.setPropertyStore(properties)
6672
.setCache(cache);
67-
var key = 'oauth2.test';
68-
var token = {
69-
access_token: 'foo'
70-
};
71-
properties.setProperty(key, JSON.stringify(token));
73+
7274
assert.deepEqual(service.getToken(), token);
73-
assert.deepEqual(JSON.parse(cache.get(key)), token);
75+
assert.deepEqual(JSON.parse(cache.get('oauth2.test')), token);
7476
});
7577

7678
it('should not hit the cache or properties on subsequent calls',
7779
function() {
7880
var cache = new MockCache();
79-
var properties = new MockProperties();
81+
var properties = new MockProperties({
82+
'oauth2.test': JSON.stringify({
83+
access_token: 'foo'
84+
})
85+
});
8086
var service = OAuth2.createService('test')
8187
.setPropertyStore(properties)
8288
.setCache(cache);
83-
var key = 'oauth2.test';
84-
var token = {
85-
access_token: 'foo'
86-
};
87-
properties.setProperty(key, JSON.stringify(token));
8889

8990
service.getToken();
9091
var cacheStart = cache.counter;
@@ -144,14 +145,14 @@ describe('Service', function() {
144145
expires_in: 100,
145146
refresh_token: 'bar'
146147
};
147-
var properties = new MockProperties();
148-
properties.setProperty('oauth2.test', JSON.stringify(token));
148+
var properties = new MockProperties({
149+
'oauth2.test': JSON.stringify(token)
150+
});
149151

150152
mocks.UrlFetchApp.delayFunction = () => 100;
151-
mocks.UrlFetchApp.resultFunction = () =>
152-
JSON.stringify({
153-
access_token: Math.random().toString(36)
154-
});
153+
mocks.UrlFetchApp.resultFunction = () => JSON.stringify({
154+
access_token: Math.random().toString(36)
155+
});
155156

156157
var getAccessToken = function() {
157158
var service = OAuth2.createService('test')
@@ -197,8 +198,9 @@ describe('Service', function() {
197198
expires_in: 100,
198199
refresh_token: 'bar'
199200
};
200-
var properties = new MockProperties();
201-
properties.setProperty('oauth2.test', JSON.stringify(token));
201+
var properties = new MockProperties({
202+
'oauth2.test': JSON.stringify(token)
203+
});
202204

203205
var count = 0;
204206
mocks.UrlFetchApp.resultFunction = function() {
@@ -215,13 +217,13 @@ describe('Service', function() {
215217
};
216218

217219
var refreshToken = function() {
218-
var service = OAuth2.createService('test')
220+
OAuth2.createService('test')
219221
.setClientId('abc')
220222
.setClientSecret('def')
221223
.setTokenUrl('http://www.example.com')
222224
.setPropertyStore(properties)
223-
.setLock(new MockLock());
224-
service.refresh();
225+
.setLock(new MockLock())
226+
.refresh();
225227
}.future();
226228

227229
Future.task(function() {
@@ -239,11 +241,8 @@ describe('Service', function() {
239241
});
240242
});
241243
});
242-
243244
});
244245

245-
246-
247246
describe('Utilities', function() {
248247
describe('#extend_()', function() {
249248
var extend_ = OAuth2.extend_;

0 commit comments

Comments
 (0)