Skip to content

Commit

Permalink
Merge pull request #80 from ably/auth-callback
Browse files Browse the repository at this point in the history
Add tests for authCallback and update its docstring
  • Loading branch information
paddybyers committed Jul 30, 2015
2 parents a592717 + 530732f commit 9a54c34
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 1 deletion.
3 changes: 2 additions & 1 deletion common/lib/client/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions spec/common/modules/client_module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
111 changes: 111 additions & 0 deletions spec/realtime/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 9a54c34

Please sign in to comment.