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