Skip to content
This repository has been archived by the owner on Nov 23, 2019. It is now read-only.

Commit

Permalink
Test framework, testcases, and update README on testing
Browse files Browse the repository at this point in the history
  • Loading branch information
timdream committed Oct 18, 2012
1 parent a29bfab commit b7bdf43
Show file tree
Hide file tree
Showing 10 changed files with 614 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitmodules
@@ -0,0 +1,6 @@
[submodule "test/qunit"]
path = test/qunit
url = git://github.com/jquery/qunit.git
[submodule "test/sinon"]
path = test/sinon
url = git://github.com/cjohansen/Sinon.JS.git
15 changes: 15 additions & 0 deletions README.md
Expand Up @@ -32,3 +32,18 @@ You can

- Use the token to request data from Google's server directly from the client-side web app in the browser (with JSON-P or CORS), for example, [this is how HTML Word Cloud does it](https://github.com/timdream/wordcloud/blob/master/jquery.getcontent.js#L124).
- Send the token to your own server, verify it with Google to associate a Google account with a user session on your site. [Documentation here](https://developers.google.com/accounts/docs/OAuth2Login#validatingtoken).

## Testing

To run tests, first you would need to pull the required QUnit and Sinon.JS library by running

git submodule init
git submodule update

Then, start a localhost HTTP server, for example,

python -m SimpleHTTPServer

Point your browser to [http://127-0-0-1.org.uk:8009/test/](http://127-0-0-1.org.uk:8009/test/) to start testing. You will need to disable popup blocker to properly run the interactive testcases.

You will find all the information you need to write testcases on the [QUnit](http://qunitjs.com) and [Sinon.JS](http://sinonjs.org) website. All code submission are expected to accompany with testcases.
28 changes: 28 additions & 0 deletions test/index.html
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>google-oauth2-web-client tests</title>
<link rel="stylesheet" href="qunit/qunit/qunit.css">
</head>
<body>
<div id="qunit"></div>
<script src="qunit/qunit/qunit.js"></script>

<!-- sinon -->
<script src="sinon/lib/sinon.js"></script>
<script src="sinon/lib/sinon/spy.js"></script>

<!-- script to test -->
<script src="../src/google-oauth2.js"></script>

<!-- testinit -->
<script src="testinit.js"></script>

<!-- test scripts -->
<script src="unit/basics.js"></script>
<script src="unit/initiation.js"></script>
<script src="integration/interactive.js"></script>
<script src="integration/immediate.js"></script>
</body>
</html>
47 changes: 47 additions & 0 deletions test/integration/immediate.js
@@ -0,0 +1,47 @@
'use strict';

module('Immediate login integration');

test('successful login should trigger onlogin callback', function () {
var client_id = localhost_client_id;

var returnValue = window.GO2.init({
client_id: client_id,
scope: localhost_scope,
redirect_uri: localhost_redirect_uri
});
equal(returnValue, true, 'init() returns true.');

window.GO2.login(false, true);

stop();

window.GO2.onlogin = function (token) {
window.GO2.onlogin = null;
ok(!!token, 'Got token from onlogin: ' + token);

start();
};
});

test('when logged in, GO2.getAccessToken() should return the token', function () {
var token = window.GO2.getAccessToken();
ok(!!token, 'Pass with token:' + token);
});

test('GO2.logout() should trigger onlogout callback', function () {
stop();

window.GO2.onlogout = function () {
window.GO2.onlogout = null;
ok(true, 'Passed!');

start();
};
window.GO2.logout();
});

test('when logged out, GO2.getAccessToken() should not return the token', function () {
var token = window.GO2.getAccessToken();
ok(!token, 'Not getting token.');
});
46 changes: 46 additions & 0 deletions test/integration/interactive.js
@@ -0,0 +1,46 @@
'use strict';

module('Interactive login integration');

test('successful login should trigger onlogin callback', function () {
var client_id = localhost_client_id;

var returnValue = window.GO2.init({
client_id: client_id,
scope: localhost_scope,
redirect_uri: localhost_redirect_uri
});
equal(returnValue, true, 'init() returns true.');

window.GO2.login();

stop();
window.GO2.onlogin = function (token) {
window.GO2.onlogin = null;
ok(!!token, 'Got token from onlogin: ' + token);

start();
};
});

test('when logged in, GO2.getAccessToken() should return the token', function () {
var token = window.GO2.getAccessToken();
ok(!!token, 'Pass with token:' + token);
});

test('GO2.logout() should trigger onlogout callback', function () {
stop();

window.GO2.onlogout = function () {
window.GO2.onlogout = null;
ok(true, 'Passed!');

start();
};
window.GO2.logout();
});

test('when logged out, GO2.getAccessToken() should not return the token', function () {
var token = window.GO2.getAccessToken();
ok(!token, 'Not getting token.');
});
1 change: 1 addition & 0 deletions test/qunit
Submodule qunit added at 900f72
1 change: 1 addition & 0 deletions test/sinon
Submodule sinon added at 4fa7a7
38 changes: 38 additions & 0 deletions test/testinit.js
@@ -0,0 +1,38 @@
'use strict';

var localhost_client_id = '519733320959.apps.googleusercontent.com';
var localhost_scope = ['https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/userinfo.email'];
var localhost_redirect_uri = 'http://127-0-0-1.org.uk:8009/test/';

// The fake one allows Google to show an error page without log us in automatically
var fake_client_id = 'not-valid.apps.googleusercontent.com';

var apiUrl = 'https://accounts.google.com/o/oauth2/auth';
var defaultScope = 'https://www.googleapis.com/auth/plus.me';

function splitUrlArgs(urlArgs) {
var urlArgKeys = [];
var urlArgValues = [];

urlArgs.split('&').forEach(function (keyValue) {
var kv = keyValue.split('=');
urlArgKeys.push(kv.shift());
urlArgValues.push(decodeURIComponent(kv.join('')));
});

return {
keys: urlArgKeys,
values: urlArgValues
};
}

test('Testing environment test', function () {
var win = window.open('');
equal(typeof win, 'object', 'Popup blocker is not in-effect');
if (win && win.close)
win.close();

equal(window.location.href.substr(0, localhost_redirect_uri.length), localhost_redirect_uri,
'Integration will work; test is running on presumed URL.');
});
23 changes: 23 additions & 0 deletions test/unit/basics.js
@@ -0,0 +1,23 @@
'use strict';

module('Basics');

test('GO2 object exists', function () {
equal(typeof window.GO2, 'object', 'Passed!');
});

test('GO2.init function exists', function () {
equal(typeof window.GO2.init, 'function', 'Passed!');
});

test('GO2.login function exists', function () {
equal(typeof window.GO2.login, 'function', 'Passed!');
});

test('GO2.logout function exists', function () {
equal(typeof window.GO2.logout, 'function', 'Passed!');
});

test('GO2.getAccessToken function exists', function () {
equal(typeof window.GO2.getAccessToken, 'function', 'Passed!');
});

0 comments on commit b7bdf43

Please sign in to comment.