From c8cdb1746579e8c7125f9142c4176938b63903ce Mon Sep 17 00:00:00 2001 From: Simon Woolf Date: Thu, 30 Jul 2015 18:01:47 +0100 Subject: [PATCH 1/2] =?UTF-8?q?Realtime=20spec=20helper:=20don=E2=80=99t?= =?UTF-8?q?=20add=20key=20if=20authCallback=20specified?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spec/common/modules/client_module.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spec/common/modules/client_module.js b/spec/common/modules/client_module.js index 5e4691bf33..24db79678c 100644 --- a/spec/common/modules/client_module.js +++ b/spec/common/modules/client_module.js @@ -26,6 +26,8 @@ define(['ably', 'globals', 'spec/common/modules/testapp_module'], function(Ably, Else an API key will be configured by default for all clients */ if (options.authUrl) { clientOptions.authUrl = options.authUrl; + } else if (options.authCallback) { + clientOptions.authCallback = options.authCallback; } else if (options.authToken) { clientOptions.authToken = options.authToken; } else { From 530732fa24dea5b0ff4d71e418cff0cd2e11ba01 Mon Sep 17 00:00:00 2001 From: Simon Woolf Date: Thu, 30 Jul 2015 18:03:07 +0100 Subject: [PATCH 2/2] Add tests for authCallback and update its docstring --- common/lib/client/auth.js | 3 +- spec/realtime/auth.test.js | 111 +++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 1 deletion(-) diff --git a/common/lib/client/auth.js b/common/lib/client/auth.js index f6310ebc6c..89cc265303 100644 --- a/common/lib/client/auth.js +++ b/common/lib/client/auth.js @@ -160,7 +160,8 @@ var Auth = (function() { * - key: the key to use. * * - authCallback: (optional) a javascript callback to be used, passing a set of token - * request params, to get a signed token request. + * request params, to get a signed token request, a token details object, + * or a token string. * * - authUrl: (optional) a URL to be used to GET or POST a set of token request * params, to obtain a signed token request. diff --git a/spec/realtime/auth.test.js b/spec/realtime/auth.test.js index fbfa4a96b5..2a5a661da9 100644 --- a/spec/realtime/auth.test.js +++ b/spec/realtime/auth.test.js @@ -117,6 +117,117 @@ define(['ably', 'shared_helper'], function(Ably, helper) { }); }; + /* + * Use authCallback for authentication with tokenRequest response + */ + exports.auth_useAuthCallback_tokenRequestResponse = function(test) { + test.expect(3); + + var rest = helper.AblyRest(); + var authCallback = function(tokenParams, callback) { + rest.auth.createTokenRequest(null, tokenParams, function(err, tokenRequest) { + if(err) { + test.ok(false, helper.displayError(err)); + test.done(); + return; + } + test.ok("nonce" in tokenRequest); + callback(null, tokenRequest); + }); + }; + + realtime = helper.AblyRealtime({ authCallback: authCallback }); + + realtime.connection.on('connected', function() { + realtime.connection.close(); + test.equal(realtime.auth.method, 'token'); + test.ok(true, 'Connected to Ably using authCallback returning a TokenRequest'); + test.done(); + return; + }); + + realtime.connection.on('failed', function(err) { + realtime.close(); + test.ok(false, "Failed: " + err); + test.done(); + return; + }); + }; + + /* + * Use authCallback for authentication with tokenDetails response + */ + exports.auth_useAuthCallback_tokenDetailsResponse = function(test) { + test.expect(3); + + var rest = helper.AblyRest(); + var authCallback = function(tokenParams, callback) { + rest.auth.requestToken(null, tokenParams, function(err, tokenDetails) { + if(err) { + test.ok(false, helper.displayError(err)); + test.done(); + return; + } + test.ok("token" in tokenDetails); + callback(null, tokenDetails); + }); + }; + + realtime = helper.AblyRealtime({ authCallback: authCallback }); + + realtime.connection.on('connected', function() { + realtime.connection.close(); + test.equal(realtime.auth.method, 'token'); + test.ok(true, 'Connected to Ably using authCallback returning a TokenRequest'); + test.done(); + return; + }); + + realtime.connection.on('failed', function(err) { + realtime.close(); + test.ok(false, "Failed: " + err); + test.done(); + return; + }); + }; + + /* + * Use authCallback for authentication with token string response + */ + exports.auth_useAuthCallback_tokenStringResponse = function(test) { + test.expect(3); + + var rest = helper.AblyRest(); + var authCallback = function(tokenParams, callback) { + rest.auth.requestToken(null, tokenParams, function(err, tokenDetails) { + if(err) { + test.ok(false, helper.displayError(err)); + test.done(); + return; + } + test.ok("token" in tokenDetails); + callback(null, tokenDetails.token); + }); + }; + + realtime = helper.AblyRealtime({ authCallback: authCallback }); + + realtime.connection.on('connected', function() { + realtime.connection.close(); + test.equal(realtime.auth.method, 'token'); + test.ok(true, 'Connected to Ably using authCallback returning a TokenRequest'); + test.done(); + return; + }); + + realtime.connection.on('failed', function(err) { + realtime.close(); + test.ok(false, "Failed: " + err); + test.done(); + return; + }); + }; + exports.teardown = function(test) { realtime.close(); test.done();