Skip to content

Commit 2afb28c

Browse files
author
Eric Koleda
committed
Add a test for passing additional parameters.
1 parent f9e29ef commit 2afb28c

File tree

3 files changed

+61
-6
lines changed

3 files changed

+61
-6
lines changed

src/Utilities.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function validate_(params) {
4242
Object.keys(params).forEach(function(name) {
4343
var value = params[name];
4444
if (!value) {
45-
throw Utilities.formatString('%s is required.', name);
45+
throw new Error(name + ' is required.');
4646
}
4747
});
4848
}

test/mocks/script.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var MockScriptApp = function() {};
2+
3+
MockScriptApp.prototype.getScriptId = function() {
4+
return Math.random().toString(36).substring(2);
5+
};
6+
7+
MockScriptApp.prototype.newStateToken = function() {
8+
return new MockStateTokenBuilder();
9+
};
10+
11+
var MockStateTokenBuilder = function() {
12+
this.arguments = {};
13+
};
14+
15+
MockStateTokenBuilder.prototype.withMethod = function(method) {
16+
this.method = method;
17+
return this;
18+
};
19+
20+
MockStateTokenBuilder.prototype.withArgument = function(key, value) {
21+
this.arguments[key] = value;
22+
return this;
23+
};
24+
25+
MockStateTokenBuilder.prototype.withTimeout = function(timeout) {
26+
this.timeout = timeout;
27+
return this;
28+
};
29+
30+
MockStateTokenBuilder.prototype.createToken = function() {
31+
return JSON.stringify(this);
32+
};
33+
34+
module.exports = MockScriptApp;

test/test.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@ var MockUrlFetchApp = require('./mocks/urlfetchapp');
44
var MockProperties = require('./mocks/properties');
55
var MockCache = require('./mocks/cache');
66
var MockLock = require('./mocks/lock');
7+
var MockScriptApp = require('./mocks/script');
78
var Future = require('fibers/future');
89

910
var mocks = {
10-
ScriptApp: {
11-
getScriptId: function() {
12-
return '12345';
13-
}
14-
},
11+
ScriptApp: new MockScriptApp(),
1512
UrlFetchApp: new MockUrlFetchApp(),
1613
Utilities: {
1714
base64Encode: function(data) {
@@ -405,6 +402,30 @@ describe('Service', function() {
405402
service.exchangeGrant_();
406403
});
407404
});
405+
406+
describe('#getAuthorizationUrl()', function() {
407+
it('should add additional parameters to the state token', function() {
408+
var service = OAuth2.createService('test')
409+
.setAuthorizationBaseUrl('http://www.example.com')
410+
.setClientId('abc')
411+
.setClientSecret('def')
412+
.setCallbackFunction('authCallback');
413+
var authorizationUrl = service.getAuthorizationUrl({
414+
foo: 'bar'
415+
});
416+
417+
// Extract the state token from the URL.
418+
var querystring = authorizationUrl.split('?')[1];
419+
var params = querystring.split('&').reduce(function(result, pair) {
420+
var parts = pair.split('=').map(decodeURIComponent);
421+
result[parts[0]] = parts[1];
422+
return result;
423+
}, {});
424+
var state = JSON.parse(params.state);
425+
426+
assert.equal(state.arguments.foo, 'bar');
427+
});
428+
});
408429
});
409430

410431
describe('Utilities', function() {

0 commit comments

Comments
 (0)