Commit d2ee62da authored by JC Brand's avatar JC Brand

Make sure that there is a valid connection when login API is called

In the process I had to refactor some methods which required the mock
connection of tests to be changed as well.
parent a969d010
......@@ -136,6 +136,7 @@
describe("The live filter", $.proxy(function () {
beforeEach(function () {
test_utils.openControlBox();
test_utils.openContactsPanel();
});
it("will only appear when roster contacts flow over the visible area", function () {
......
......@@ -16,47 +16,40 @@
describe("Authentication", function () {
it("needs either a bosh_service_url a websocket_url or both", function () {
converse.connection.connected = false;
var url = converse.bosh_service_url;
var connection = converse.connection;
delete converse.bosh_service_url;
delete converse.connection;
expect(converse.initConnection.bind({})).toThrow(
new Error("initConnection: you must supply a value for either the bosh_service_url or websocket_url or both."));
converse.connection.connected = true;
converse.bosh_service_url = url;
converse.connection = connection;
});
describe("with prebind", function () {
it("needs a jid when also using keepalive", function () {
var authentication = converse.authentication;
var connection = converse.connection;
var jid = converse.jid;
converse.bosh_service_url = "localhost";
converse.connection = undefined;
converse.jid = undefined;
delete converse.jid;
converse.keepalive = true;
converse.authentication = "prebind";
expect(converse.initConnection.bind(converse)).toThrow(
new Error("initConnection: when using 'keepalive' with 'prebind, you must supply the JID of the current user."));
expect(converse.logIn.bind(converse)).toThrow(
new Error("attemptPreboundSession: when using 'keepalive' with 'prebind, you must supply the JID of the current user."));
converse.authentication= authentication;
converse.bosh_service_url = undefined;
converse.connection = connection;
converse.jid = jid;
converse.keepalive = undefined;
converse.keepalive = false;
});
it("needs jid, rid and sid values when not using keepalive", function () {
var authentication = converse.authentication;
var connection = converse.connection;
var jid = converse.jid;
converse.bosh_service_url = "localhost";
converse.connection = undefined;
converse.jid = undefined;
converse.keepalive = false;
delete converse.jid;
converse.authentication = "prebind";
expect(converse.initConnection.bind(converse)).toThrow(
new Error("initConnection: If you use prebind and not keepalive, then you MUST supply JID, RID and SID values"));
expect(converse.logIn.bind(converse)).toThrow(
new Error("attemptPreboundSession: If you use prebind and not keepalive, then you MUST supply JID, RID and SID values"));
converse.authentication= authentication;
converse.bosh_service_url = undefined;
converse.connection = connection;
converse.jid = jid;
converse.keepalive = undefined;
});
});
});
......
......@@ -13,6 +13,11 @@
describe("A headlines box", function () {
beforeEach(function () {
test_utils.openControlBox();
test_utils.openContactsPanel();
});
it("will not open nor display non-headline messages", function () {
/* XMPP spam message:
*
......
......@@ -11,34 +11,27 @@
} (this, function ($, mock, test_utils) {
var b64_sha1 = converse_api.env.b64_sha1;
return describe("The OTR module", $.proxy(function(mock, test_utils) {
return describe("The OTR module", function() {
beforeEach($.proxy(function () {
window.localStorage.clear();
window.sessionStorage.clear();
}, converse));
it("can store a session passphrase in session storage", $.proxy(function () {
var pp;
it("can store a session passphrase in session storage", function () {
// With no prebind, the user's XMPP password is used and nothing is
// stored in session storage.
this.authentication = "manual";
this.connection.pass = 's3cr3t!';
expect(this.otr.getSessionPassphrase()).toBe(this.connection.pass);
expect(window.sessionStorage.length).toBe(0);
expect(window.localStorage.length).toBe(0);
var auth = converse.authentication;
var pass = converse.connection.pass;
converse.authentication = "manual";
converse.connection.pass = 's3cr3t!';
expect(converse.otr.getSessionPassphrase()).toBe(converse.connection.pass);
// With prebind, a random passphrase is generated and stored in
// session storage.
this.authentication = "prebind";
pp = this.otr.getSessionPassphrase();
expect(pp).not.toBe(this.connection.pass);
expect(window.sessionStorage.length).toBe(1);
expect(window.localStorage.length).toBe(0);
converse.authentication = "prebind";
var pp = converse.otr.getSessionPassphrase();
expect(pp).not.toBe(converse.connection.pass);
expect(pp).toBe(window.sessionStorage[b64_sha1(converse.connection.jid)]);
// Clean up
this.authentication = "manual";
}, converse));
}, converse, mock, test_utils));
converse.authentication = auth;
converse.connection.pass = pass;
});
});
}));
This diff is collapsed.
......@@ -35,6 +35,7 @@
},
'user': {
'login': function (credentials) {
converse.initConnection();
converse.logIn(credentials);
},
'logout': function () {
......
......@@ -1620,7 +1620,8 @@
return this.connection.attach(this.jid, this.sid, this.rid, this.onConnectStatusChanged);
} else if (this.keepalive) {
if (!this.jid) {
throw new Error("initConnection: when using 'keepalive' with 'prebind, you must supply the JID of the current user.");
throw new Error("attemptPreboundSession: when using 'keepalive' with 'prebind, "+
"you must supply the JID of the current user.");
}
try {
return this.connection.restore(this.jid, this.onConnectStatusChanged);
......@@ -1629,7 +1630,7 @@
this.clearSession(); // If there's a roster, we want to clear it (see #555)
}
} else {
throw new Error("initConnection: If you use prebind and not keepalive, "+
throw new Error("attemptPreboundSession: If you use prebind and not keepalive, "+
"then you MUST supply JID, RID and SID values");
}
// We haven't been able to attach yet. Let's see if there
......@@ -1700,6 +1701,8 @@
this.logIn = function (credentials) {
if (credentials) {
// When credentials are passed in, they override prebinding
// or credentials fetching via HTTP
this.autoLogin(credentials);
} else {
// We now try to resume or automatically set up a new session.
......@@ -1713,22 +1716,18 @@
};
this.initConnection = function () {
if (this.connection && this.connection.connected) {
this.setUpXMLLogging();
this.onConnected();
if (this.connection) {
return;
}
if (!this.bosh_service_url && ! this.websocket_url) {
throw new Error("initConnection: you must supply a value for either the bosh_service_url or websocket_url or both.");
}
if (('WebSocket' in window || 'MozWebSocket' in window) && this.websocket_url) {
this.connection = new Strophe.Connection(this.websocket_url);
} else if (this.bosh_service_url) {
this.connection = new Strophe.Connection(this.bosh_service_url, {'keepalive': this.keepalive});
} else {
if (!this.bosh_service_url && ! this.websocket_url) {
throw new Error("initConnection: you must supply a value for either the bosh_service_url or websocket_url or both.");
}
if (('WebSocket' in window || 'MozWebSocket' in window) && this.websocket_url) {
this.connection = new Strophe.Connection(this.websocket_url);
} else if (this.bosh_service_url) {
this.connection = new Strophe.Connection(this.bosh_service_url, {'keepalive': this.keepalive});
} else {
throw new Error("initConnection: this browser does not support websockets and bosh_service_url wasn't specified.");
}
this.setUpXMLLogging();
this.logIn();
throw new Error("initConnection: this browser does not support websockets and bosh_service_url wasn't specified.");
}
};
......@@ -1753,6 +1752,8 @@
this.chatboxviews = new this.ChatBoxViews({model: this.chatboxes});
this.initSession();
this.initConnection();
this.setUpXMLLogging();
this.logIn();
return this;
};
......
......@@ -47,9 +47,13 @@ require([
converse.initialize({
i18n: window.locales.en,
auto_subscribe: false,
animate: false,
bosh_service_url: 'localhost',
connection: mock.mock_connection,
animate: false,
no_trimming: true,
auto_login: true,
jid: 'dummy@localhost',
password: 'secret',
debug: false
}, function (converse) {
window.converse = converse;
......@@ -64,6 +68,7 @@ require([
require([
"console-runner",
"spec/converse",
"spec/headline",
"spec/disco",
"spec/protocol",
"spec/mam",
......@@ -76,9 +81,8 @@ require([
"spec/notification",
"spec/profiling",
"spec/ping",
"spec/headline",
"spec/register",
"spec/xmppstatus"
"spec/xmppstatus",
], function () {
// Make sure this callback is only called once.
delete converse.callback;
......
......@@ -50,10 +50,6 @@
mock.mock_connection = function () {
Strophe.Bosh.prototype._processRequest = function () {}; // Don't attempt to send out stanzas
var c = new Strophe.Connection('jasmine tests');
c.authenticated = true;
c.connected = true;
c.mock = true;
c.jid = 'dummy@localhost/resource';
c.vcard = {
'get': function (callback, jid) {
var fullname;
......@@ -71,8 +67,13 @@
callback(vcard.tree());
}
};
c._changeConnectStatus(Strophe.Status.CONNECTED);
c.attach(c.jid);
c._proto._connect = function () {
c.authenticated = true;
c.connected = true;
c.mock = true;
c.jid = 'dummy@localhost/resource';
c._changeConnectStatus(Strophe.Status.CONNECTED);
};
return c;
}();
return mock;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment