Clean up logout handling in tests.

This commit is contained in:
Ian Gulliver
2014-05-16 17:57:26 +03:00
parent 482feee804
commit fe7bab189d
2 changed files with 48 additions and 29 deletions

View File

@@ -402,6 +402,9 @@ Cosmopolite.prototype.onSocketMessage_ = function(msg) {
* @param {!Object} e Deserialized event object * @param {!Object} e Deserialized event object
*/ */
Cosmopolite.prototype.onServerEvent_ = function(e) { Cosmopolite.prototype.onServerEvent_ = function(e) {
if (this.shutdown_) {
return;
}
switch (e['event_type']) { switch (e['event_type']) {
case 'login': case 'login':
if ('onLogin' in this.callbacks_) { if ('onLogin' in this.callbacks_) {

View File

@@ -34,17 +34,26 @@ var randstring = function() {
return ret.join(''); return ret.join('');
}; };
var logout = function(callback) {
var innerCallback = function(e) {
window.removeEventListener('message', innerCallback);
if (e.origin != window.location.origin ||
e.data != 'logout_complete') {
return;
}
if (callback) {
callback();
}
};
window.addEventListener('message', innerCallback);
window.open('/cosmopolite/auth/logout');
};
QUnit.testStart(localStorage.clear.bind(localStorage)); QUnit.testStart(localStorage.clear.bind(localStorage));
QUnit.testDone(localStorage.clear.bind(localStorage)); QUnit.testDone(localStorage.clear.bind(localStorage));
QUnit.testStart(function() { module('All platforms');
// Log us out.
var req = new XMLHttpRequest();
req.open('GET', '/cosmopolite/auth/logout', false);
req.send();
});
module('General');
test('Construct/shutdown', function() { test('Construct/shutdown', function() {
expect(2); expect(2);
@@ -56,14 +65,17 @@ test('Construct/shutdown', function() {
asyncTest('onLogout fires', function() { asyncTest('onLogout fires', function() {
expect(1); expect(1);
var callbacks = {
'onLogout': function(login_url) { logout(function() {
ok(true, 'onLogout fired'); var callbacks = {
cosmo.shutdown(); 'onLogout': function(login_url) {
start(); ok(true, 'onLogout fired');
} cosmo.shutdown();
}; start();
var cosmo = new Cosmopolite(callbacks); }
};
var cosmo = new Cosmopolite(callbacks);
});
}); });
asyncTest('Message round trip', function() { asyncTest('Message round trip', function() {
@@ -188,17 +200,21 @@ module('dev_appserver only');
asyncTest('Login', function() { asyncTest('Login', function() {
expect(2); expect(2);
var callbacks = {
'onLogin': function(login_url) { logout(function() {
ok(true, 'onLogin fired'); var callbacks = {
cosmo.shutdown(); 'onLogin': function(login_url) {
start(); ok(true, 'onLogin fired');
}, cosmo.shutdown();
'onLogout': function(login_url) { logout();
ok(true, 'onLogout fired'); start();
// Entirely magic URL that sets the login cookie and redirects. },
window.open('/_ah/login?email=test%40example.com&action=Login&continue=/cosmopolite/static/login_complete.html'); 'onLogout': function(login_url) {
} ok(true, 'onLogout fired');
}; // Entirely magic URL that sets the login cookie and redirects.
var cosmo = new Cosmopolite(callbacks); window.open('/_ah/login?email=test%40example.com&action=Login&continue=/cosmopolite/static/login_complete.html');
}
};
var cosmo = new Cosmopolite(callbacks);
});
}); });