Skip to content

Commit

Permalink
[refactor tests] First step in refactor of tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
stolsma committed Jan 4, 2012
1 parent 5a1e939 commit 298fd05
Show file tree
Hide file tree
Showing 8 changed files with 451 additions and 309 deletions.
473 changes: 317 additions & 156 deletions test/helpers.js

Large diffs are not rendered by default.

47 changes: 25 additions & 22 deletions test/oauth2/authentication-test.js
Expand Up @@ -14,49 +14,52 @@ vows.describe('OAuth2/authentication').addBatch({
"When using the authentication server": helpers.startTestServer({
"get login page": {
topic: function(credentials, confClient, publicClient, oauth2) {
helpers.performLogin(this.callback);
return helpers.TestClient().performLogin();
},
"check if login page is presented": function(err, loginPage, auth_key) {
"check if login page is presented": function(err, promise) {
assert.isNull(err);
assert.isTrue(loginPage);
assert.isString(auth_key);
assert.isTrue(promise.loginPage);
assert.isString(promise.authenticationKey);
},
"and login with correct credentials": {
topic: function(loginPage, auth_key, credentials) {
helpers.performLoginPost(credentials, auth_key, this.callback);
topic: function(promise, credentials) {
return promise.client.performLoginPost(credentials);
},
"and check if login is accepted": function(err, res, body) {
"and check if login is accepted": function(err, promise) {
assert.isNull(err);
assert.equal(res.statusCode, 200);
assert.equal(body, 'Logged in!');
assert.equal(promise.statusCode, 200);
assert.isTrue(promise.loggedIn);
},
"and logout again": {
topic: function() {
helpers.performLogout(this.callback);
topic: function(promise) {
return promise.client.performLogout();
},
"and check if logout is accepted": function(err, result) {
"and check if logout is accepted": function(err, promise) {
assert.isNull(err);
assert.isTrue(promise.loggedOut);
}
}
},

"and login with wrong password": {
topic: function(loginPage, auth_key, credentials) {
helpers.performLoginPost({username: credentials.username, password: 'jombo'}, auth_key, this.callback);
topic: function(promise, credentials) {
return promise.client.performLoginPost({username: credentials.username, password: 'jombo'});
},
"and check if login is rejected": function(err, res, body) {
"and check if login is rejected": function(err, promise) {
assert.isNull(err);
assert.equal(res.statusCode, 400);
assert.equal(body, 'The resource owner or authorization server denied the request.');
assert.equal(promise.statusCode, 400);
assert.equal(promise.body, 'The resource owner or authorization server denied the request.');
}
},

"and login with wrong username": {
topic: function(loginPage, auth_key, credentials) {
helpers.performLoginPost({username: 'dombo', password: credentials.password}, auth_key, this.callback);
topic: function(promise, credentials) {
return promise.client.performLoginPost({username: 'dombo', password: credentials.password});
},
"and check if login is rejected": function(err, res, body) {
"and check if login is rejected": function(err, promise) {
assert.isNull(err);
assert.equal(res.statusCode, 400);
assert.equal(body, 'The resource owner or authorization server denied the request.');
assert.equal(promise.statusCode, 400);
assert.equal(promise.body, 'The resource owner or authorization server denied the request.');
}
}
}
Expand Down
39 changes: 15 additions & 24 deletions test/oauth2/client-credentials-test.js
Expand Up @@ -15,20 +15,17 @@ vows.describe('OAuth2/client-credentials').addBatch({

"test 'basic authentication' of client (password grant)": {
topic: function(credentials, client, oauth2) {
var self = this;
var params = {
grant_type: 'password',
client_id: client.id,
client_secret: client.secret,
basic: true
};
helpers.performAccessTokenRequest(params, function(err, result, statusCode){
self.callback(err, result, statusCode);
});
return helpers.TestClient().performAccessTokenRequest(params);
},
"and client is authenticated": function(err, result, statusCode) {
"and client is authenticated": function(err, promise) {
assert.isNull(err);
assert.equal(statusCode, 200);
assert.equal(promise.statusCode, 200);
}
},

Expand All @@ -40,20 +37,17 @@ vows.describe('OAuth2/client-credentials').addBatch({

"test 'param authentication' of client (password grant)": {
topic: function(credentials, client, oauth2) {
var self = this;
var params = {
grant_type: 'password',
client_id: client.id,
client_secret: client.secret,
basic: false
};
helpers.performAccessTokenRequest(params, function(err, result, statusCode){
self.callback(err, result, statusCode);
});
return helpers.TestClient().performAccessTokenRequest(params);
},
"and client is authenticated": function(err, result, statusCode) {
"and client is authenticated": function(err, promise) {
assert.isNull(err);
assert.equal(statusCode, 200);
assert.equal(promise.statusCode, 200);
}
},

Expand All @@ -69,29 +63,26 @@ vows.describe('OAuth2/client-credentials').addBatch({
function accessTokenRequestErrorTest(client_id, client_secret, basic, extraContext) {
var context = {
topic: function(credentials, client, oauth2) {
var self = this;
var params = {
grant_type: 'password',
client_id: client_id || client.id,
client_secret: client_secret || client.secret,
basic: basic
};
helpers.performAccessTokenRequest(params, function(err, result, statusCode, headers){
self.callback(err, result, statusCode, headers);
});
return helpers.TestClient().performAccessTokenRequest(params);
},
"check if HTTP status code 401 is returned": function(err, result, statusCode) {
"check if HTTP status code 401 is returned": function(err, promise) {
assert.isNull(err);
assert.equal(statusCode, 401);
assert.equal(promise.statusCode, 401);
},
"check if 'WWW-Authenticate' header is presented": function(err, result, statusCode, headers) {
assert.equal(headers["www-authenticate"], 'Basic realm="authorization service"');
"check if 'WWW-Authenticate' header is presented": function(err, promise) {
assert.equal(promise.headers["www-authenticate"], 'Basic realm="authorization service"');
},
"check if correct 'error' type ('invalid_client') is presented": function(err, result, statusCode) {
assert.equal(result.error, 'invalid_client');
"check if correct 'error' type ('invalid_client') is presented": function(err, promise) {
assert.equal(promise.errorParams.error, 'invalid_client');
},
"check if 'error_description' is presented": function(err, result, statusCode) {
assert.isString(result.error_description);
"check if 'error_description' is presented": function(err, promise) {
assert.isString(promise.errorParams.error_description);
}
};

Expand Down
72 changes: 34 additions & 38 deletions test/oauth2/code-flow-test.js
Expand Up @@ -14,43 +14,41 @@ vows.describe('OAuth2/code-flow').addBatch({
"When using the authorization server": helpers.startTestServer({
"start authorization code flow": {
topic: function(credentials, client, oauth2) {
var self = this,
codeParameters = {
var codeParameters = {
response_type: 'code',
client_id: client.id,
client_secret: client.secret,
redirect_uri: client.redirect_uris[0],
scope: 'test',
state: 'statetest'
};
helpers.getLoginPage(codeParameters, '', 'GET', function(err, loginPage, auth_key) {
loginPage = (loginPage) ? null : 'No login page returned';
self.callback(err || loginPage, codeParameters, auth_key);
});
return helpers.TestClient().getLoginPage(codeParameters, 'GET');
},
"check if login page is presented": function(err, codeParameters) {
"check if login page is presented": function(err, promise) {
assert.isNull(err);
},
"do login and get authorization": {
topic: function(codeParameters, auth_key, credentials) {
helpers.getAuthorizationPage(codeParameters, '', auth_key, credentials, this.callback);
topic: function(promise, credentials) {
return promise.client.getAuthorizationPage(promise.flowOptions, promise.authenticationKey, credentials);
},
"check if authorization page is presented": function(err, auth_key) {
"check if authorization page is presented": function(err, promise) {
assert.isNull(err);
assert.isString(auth_key);
assert.isTrue(promise.authorizationPage);
assert.isString(promise.authorizationKey);
},
"give authorization and get code": {
topic: function(auth_key, codeParameters) {
helpers.performCodeFlowAuthorization(auth_key, codeParameters, this.callback);
topic: function(promise) {
return promise.client.performCodeFlowAuthorization(promise.authorizationKey, promise.flowOptions);
},
"request is handled correctly": function(err, params) {
"request is handled correctly": function(err, promise) {
assert.isNull(err);
assert.isTrue(promise.codeFlowBody);
},
"correct 'state' is returned": function(err, params) {
assert.equal(params.state, 'statetest');
"correct 'state' is returned": function(err, promise) {
assert.equal(promise.codeFlowResult.state, promise.flowOptions.state);
},
"'code' is returned": function(err, params) {
assert.isTrue(!!params.code);
"'code' is returned": function(err, promise) {
assert.isString(promise.codeFlowResult.code);
},
"do 'acces token' request": accessTokenRequestTest('code', {
"do 'refresh_token' request": accessTokenRequestTest('refresh', {
Expand All @@ -66,44 +64,42 @@ vows.describe('OAuth2/code-flow').addBatch({

function accessTokenRequestTest(type, extraContext) {
var context = {
topic: function(result, dummy, codeParameters) {
var self = this;
topic: function(promise) {

var params = {
client_id: codeParameters.client_id,
client_secret: codeParameters.client_secret
client_id: promise.flowOptions.client_id,
client_secret: promise.flowOptions.client_secret
};

if (type === 'code') {
params.grant_type = 'authorization_code';
params.code = result.code;
params.code = promise.codeFlowResult.code;
}

if (type === 'refresh') {
params.grant_type = 'refresh_token';
params.refresh_token = result.refresh_token;
params.refresh_token = promise.accessTokenResult.refresh_token;
}

helpers.performAccessTokenRequest(params, function(err, result){
self.callback(err, result, null, codeParameters);
});
return promise.client.performAccessTokenRequest(params);
},
"request is handled correctly": function(err, result) {
"request is handled correctly": function(err, promise) {
assert.isNull(err);
},
"'access_token' is returned": function(err, result) {
assert.isString(result.access_token);
"'access_token' is returned": function(err, promise) {
assert.isString(promise.accessTokenResult.access_token);
},
"'token_type' is `bearer`": function(err, result) {
assert.equal(result.token_type, 'bearer');
"'token_type' is `bearer`": function(err, promise) {
assert.equal(promise.accessTokenResult.token_type, 'bearer');
},
"'expires_in' = 3600": function(err, result) {
assert.equal(result.expires_in, 3600);
"'expires_in' = 3600": function(err, promise) {
assert.equal(promise.accessTokenResult.expires_in, 3600);
},
"'refresh_token' is returned": function(err, result) {
assert.isString(result.refresh_token);
"'refresh_token' is returned": function(err, promise) {
assert.isString(promise.accessTokenResult.refresh_token);
},
"correct 'scope' is returned": function(err, result) {
assert.equal(result.scope, '');
"correct 'scope' is returned": function(err, promise) {
assert.equal(promise.accessTokenResult.scope, '');
}
};

Expand Down
29 changes: 14 additions & 15 deletions test/oauth2/grant-type-error-test.js
Expand Up @@ -20,24 +20,23 @@ vows.describe('OAuth2/grant-type-error').addBatch({
function testGrantType(grant_type) {
return {
topic: function(credentials, client, oauth2) {
var self = this,
grantParameters = {
client_id: client.id,
client_secret: client.secret,
redirect_uri: client.redirect_uris[0],
scope: 'test'
};
var grantParameters = {
client_id: client.id,
client_secret: client.secret,
redirect_uri: client.redirect_uris[0],
scope: 'test'
};

if (grant_type) grantParameters.grant_type = grant_type;
helpers.performAccessTokenRequest(grantParameters, function(err, param) {
self.callback(err, param, grantParameters);
});

return helpers.TestClient().performAccessTokenRequest(grantParameters);
},
"check if correct 'error' type is presented": function(err, param, codeParameters) {
"check if correct 'error' type is presented": function(err, promise) {
assert.isNull(err);
assert.equal(param.error, 'unsupported_grant_type');
assert.equal(promise.errorParams.error, 'unsupported_grant_type');
},
"check if 'error_description' is presented": function(err, param, codeParameters) {
assert.isString(param.error_description);
"check if 'error_description' is presented": function(err, promise) {
assert.isString(promise.errorParams.error_description);
}
}
};
}
42 changes: 20 additions & 22 deletions test/oauth2/implicit-grant-flow-test.js
Expand Up @@ -22,43 +22,41 @@ vows.describe('OAuth2/implicit-grant-flow').addBatch({
scope: 'test',
state: 'statetest'
};
helpers.getLoginPage(codeParameters, '', 'GET', function(err, loginPage, auth_key) {
loginPage = (loginPage) ? null : 'No login page returned';
self.callback(err || loginPage, codeParameters, auth_key);
});
return helpers.TestClient().getLoginPage(codeParameters, 'GET');
},
"check if login page is presented": function(err, codeParameters) {
"check if login page is presented": function(err, promise) {
assert.isNull(err);
},
"do login and get authorization": {
topic: function(codeParameters, auth_key, credentials) {
helpers.getAuthorizationPage(codeParameters, '', auth_key, credentials, this.callback);
topic: function(promise, credentials) {
return promise.client.getAuthorizationPage(promise.flowOptions, promise.authenticationKey, credentials);
},
"check if authorization page is presented": function(err, userId) {
"check if authorization page is presented": function(err, promise) {
assert.isNull(err);
assert.isString(userId);
assert.isTrue(promise.authorizationPage);
assert.isString(promise.authorizationKey);
},
"give authorization and get code": {
topic: function(userId, codeParameters, credentials, oauth2) {
helpers.performImplicitGrantAuthorization(userId, codeParameters, this.callback);
topic: function(promise) {
return promise.client.performImplicitGrantAuthorization(promise.authorizationKey, promise.flowOptions);
},
"request is handled correctly": function(err, result) {
"request is handled correctly": function(err, promise) {
assert.isNull(err);
},
"'access_token' is returned": function(err, result) {
assert.isString(result.access_token);
"'access_token' is returned": function(err, promise) {
assert.isString(promise.implicitGrantResult.access_token);
},
"'token_type' is `bearer`": function(err, result) {
assert.equal(result.token_type, 'bearer');
"'token_type' is `bearer`": function(err, promise) {
assert.equal(promise.implicitGrantResult.token_type, 'bearer');
},
"'expires_in' = 3600": function(err, result) {
assert.equal(result.expires_in, 3600);
"'expires_in' = 3600": function(err, promise) {
assert.equal(promise.implicitGrantResult.expires_in, 3600);
},
"correct 'scope' is returned": function(err, result) {
assert.equal(result.scope, 'test');
"correct 'scope' is returned": function(err, promise) {
assert.equal(promise.implicitGrantResult.scope, 'test');
},
"correct 'state' is returned": function(err, result) {
assert.equal(result.state, 'statetest');
"correct 'state' is returned": function(err, promise) {
assert.equal(promise.implicitGrantResult.state, promise.flowOptions.state);
}
}
}
Expand Down

0 comments on commit 298fd05

Please sign in to comment.