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,6 +65,8 @@ test('Construct/shutdown', function() {
asyncTest('onLogout fires', function() { asyncTest('onLogout fires', function() {
expect(1); expect(1);
logout(function() {
var callbacks = { var callbacks = {
'onLogout': function(login_url) { 'onLogout': function(login_url) {
ok(true, 'onLogout fired'); ok(true, 'onLogout fired');
@@ -64,6 +75,7 @@ asyncTest('onLogout fires', function() {
} }
}; };
var cosmo = new Cosmopolite(callbacks); var cosmo = new Cosmopolite(callbacks);
});
}); });
asyncTest('Message round trip', function() { asyncTest('Message round trip', function() {
@@ -188,10 +200,13 @@ module('dev_appserver only');
asyncTest('Login', function() { asyncTest('Login', function() {
expect(2); expect(2);
logout(function() {
var callbacks = { var callbacks = {
'onLogin': function(login_url) { 'onLogin': function(login_url) {
ok(true, 'onLogin fired'); ok(true, 'onLogin fired');
cosmo.shutdown(); cosmo.shutdown();
logout();
start(); start();
}, },
'onLogout': function(login_url) { 'onLogout': function(login_url) {
@@ -201,4 +216,5 @@ asyncTest('Login', function() {
} }
}; };
var cosmo = new Cosmopolite(callbacks); var cosmo = new Cosmopolite(callbacks);
});
}); });