Skip to content

Commit ab93e96

Browse files
authored
Merge pull request element-hq#1947 from vector-im/rav/token_login_tests
More tests for the loading process:
2 parents 5a17d8b + da7a556 commit ab93e96

File tree

1 file changed

+128
-22
lines changed

1 file changed

+128
-22
lines changed

test/app-tests/loading.js

Lines changed: 128 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import test_utils from '../test-utils';
3131
import MockHttpBackend from '../mock-request';
3232
import {parseQs, parseQsFromFragment} from '../../src/vector/url_utils';
3333

34+
var DEFAULT_HS_URL='http://my_server';
35+
var DEFAULT_IS_URL='http://my_is';
3436

3537
describe('loading:', function () {
3638
let parentDiv;
@@ -39,6 +41,12 @@ describe('loading:', function () {
3941
// an Object simulating the window.location
4042
let windowLocation;
4143

44+
// the mounted MatrixChat
45+
let matrixChat;
46+
47+
// a promise which resolves when the MatrixChat calls onLoadCompleted
48+
let loadCompletePromise;
49+
4250
beforeEach(function() {
4351
test_utils.beforeEach(this);
4452
httpBackend = new MockHttpBackend();
@@ -50,6 +58,7 @@ describe('loading:', function () {
5058
// document.body.appendChild(parentDiv);
5159

5260
windowLocation = null;
61+
matrixChat = null;
5362

5463
window.localStorage.clear();
5564
});
@@ -67,15 +76,23 @@ describe('loading:', function () {
6776
* TODO: it would be nice to factor some of this stuff out of index.js so
6877
* that we can test it rather than our own implementation of it.
6978
*/
70-
function loadApp(uriFragment) {
79+
function loadApp(opts) {
80+
opts = opts || {};
81+
const queryString = opts.queryString || "";
82+
const uriFragment = opts.uriFragment || "";
83+
7184
windowLocation = {
72-
search: "",
85+
search: queryString,
7386
hash: uriFragment,
7487
toString: function() { return this.search + this.hash; },
7588
};
7689

7790
let lastLoadedScreen = null;
7891
let appLoaded = false;
92+
93+
let loadCompleteDefer = q.defer();
94+
loadCompletePromise = loadCompleteDefer.promise;
95+
7996
function onNewScreen(screen) {
8097
console.log("newscreen "+screen);
8198
if (!appLoaded) {
@@ -89,24 +106,23 @@ describe('loading:', function () {
89106

90107
const MatrixChat = sdk.getComponent('structures.MatrixChat');
91108
const fragParts = parseQsFromFragment(windowLocation);
92-
const matrixChat = ReactDOM.render(
109+
var params = parseQs(windowLocation);
110+
matrixChat = ReactDOM.render(
93111
<MatrixChat
94112
onNewScreen={onNewScreen}
95-
config={{}}
96-
startingQueryParams={fragParts.params}
113+
config={{
114+
default_hs_url: DEFAULT_HS_URL,
115+
default_is_url: DEFAULT_IS_URL,
116+
}}
117+
realQueryParams={params}
118+
startingFragmentQueryParams={fragParts.params}
97119
enableGuest={true}
120+
onLoadCompleted={loadCompleteDefer.resolve}
98121
/>, parentDiv
99122
);
100123

101124
function routeUrl(location, matrixChat) {
102125
console.log("Routing URL "+location);
103-
var params = parseQs(location);
104-
var loginToken = params.loginToken;
105-
if (loginToken) {
106-
matrixChat.showScreen('token_login', params);
107-
return;
108-
}
109-
110126
var fragparts = parseQsFromFragment(location);
111127
matrixChat.showScreen(fragparts.location.substring(1),
112128
fragparts.params);
@@ -122,13 +138,11 @@ describe('loading:', function () {
122138
lastLoadedScreen = null;
123139
}
124140
}).done();
125-
126-
return matrixChat;
127141
}
128142

129143
describe("Clean load with no stored credentials:", function() {
130144
it('gives a login panel by default', function (done) {
131-
let matrixChat = loadApp("");
145+
loadApp();
132146

133147
q.delay(1).then(() => {
134148
// at this point, we're trying to do a guest registration;
@@ -149,7 +163,9 @@ describe('loading:', function () {
149163
});
150164

151165
it('should follow the original link after successful login', function(done) {
152-
let matrixChat = loadApp("#/room/!room:id");
166+
loadApp({
167+
uriFragment: "#/room/!room:id",
168+
});
153169

154170
q.delay(1).then(() => {
155171
// at this point, we're trying to do a guest registration;
@@ -169,7 +185,10 @@ describe('loading:', function () {
169185
expect(req.data.type).toEqual('m.login.password');
170186
expect(req.data.user).toEqual('user');
171187
expect(req.data.password).toEqual('pass');
172-
}).respond(200, { user_id: 'user_id' });
188+
}).respond(200, {
189+
user_id: '@user:id',
190+
access_token: 'access_token',
191+
});
173192
login.onPasswordLogin("user", "pass")
174193
return httpBackend.flush();
175194
}).then(() => {
@@ -187,6 +206,12 @@ describe('loading:', function () {
187206
ReactTestUtils.findRenderedComponentWithType(
188207
matrixChat, sdk.getComponent('structures.RoomView'));
189208
expect(windowLocation.hash).toEqual("#/room/!room:id");
209+
210+
// and the localstorage should have been updated
211+
expect(localStorage.getItem('mx_user_id')).toEqual('@user:id');
212+
expect(localStorage.getItem('mx_access_token')).toEqual('access_token');
213+
expect(localStorage.getItem('mx_hs_url')).toEqual(DEFAULT_HS_URL);
214+
expect(localStorage.getItem('mx_is_url')).toEqual(DEFAULT_IS_URL);
190215
}).done(done, done);
191216
});
192217
});
@@ -204,7 +229,7 @@ describe('loading:', function () {
204229
httpBackend.when('POST', '/filter').respond(200, { filter_id: 'fid' });
205230
httpBackend.when('GET', '/sync').respond(200, {});
206231

207-
let matrixChat = loadApp("");
232+
loadApp();
208233

209234
q.delay(1).then(() => {
210235
// we expect a spinner
@@ -225,7 +250,9 @@ describe('loading:', function () {
225250
httpBackend.when('POST', '/filter').respond(200, { filter_id: 'fid' });
226251
httpBackend.when('GET', '/sync').respond(200, {});
227252

228-
let matrixChat = loadApp("#/room/!room:id");
253+
loadApp({
254+
uriFragment: "#/room/!room:id",
255+
});
229256

230257
q.delay(1).then(() => {
231258
// we expect a spinner
@@ -245,7 +272,7 @@ describe('loading:', function () {
245272

246273
describe('Guest auto-registration:', function() {
247274
it('shows a directory by default', function (done) {
248-
let matrixChat = loadApp("");
275+
loadApp();
249276

250277
q.delay(1).then(() => {
251278
// at this point, we're trying to do a guest registration;
@@ -275,9 +302,50 @@ describe('loading:', function () {
275302
}).done(done, done);
276303
});
277304

305+
it('uses the last known homeserver to register with', function (done) {
306+
localStorage.setItem("mx_hs_url", "https://homeserver" );
307+
localStorage.setItem("mx_is_url", "https://idserver" );
308+
309+
loadApp();
310+
311+
q.delay(1).then(() => {
312+
// at this point, we're trying to do a guest registration;
313+
// we expect a spinner
314+
assertAtLoadingSpinner(matrixChat);
315+
316+
httpBackend.when('POST', '/register').check(function(req) {
317+
expect(req.path).toMatch(new RegExp("^https://homeserver/"));
318+
expect(req.queryParams.kind).toEqual('guest');
319+
}).respond(200, {
320+
user_id: "@guest:localhost",
321+
access_token: "secret_token",
322+
});
323+
324+
return httpBackend.flush();
325+
}).then(() => {
326+
// now we should have a spinner with a logout link
327+
assertAtSyncingSpinner(matrixChat);
328+
329+
httpBackend.when('GET', '/sync').check(function(req) {
330+
expect(req.path).toMatch(new RegExp("^https://homeserver/"));
331+
}).respond(200, {});
332+
return httpBackend.flush();
333+
}).then(() => {
334+
// once the sync completes, we should have a directory
335+
httpBackend.verifyNoOutstandingExpectation();
336+
ReactTestUtils.findRenderedComponentWithType(
337+
matrixChat, sdk.getComponent('structures.RoomDirectory'));
338+
expect(windowLocation.hash).toEqual("#/directory");
339+
expect(MatrixClientPeg.get().baseUrl).toEqual("https://homeserver");
340+
expect(MatrixClientPeg.get().idBaseUrl).toEqual("https://idserver");
341+
}).done(done, done);
342+
});
343+
278344
it('shows a room view if we followed a room link', function(done) {
279-
let matrixChat = loadApp("#/room/!room:id");
280-
q.delay(10).then(() => {
345+
loadApp({
346+
uriFragment: "#/room/!room:id"
347+
});
348+
q.delay(1).then(() => {
281349
// at this point, we're trying to do a guest registration;
282350
// we expect a spinner
283351
assertAtLoadingSpinner(matrixChat);
@@ -305,6 +373,44 @@ describe('loading:', function () {
305373
}).done(done, done);
306374
});
307375
});
376+
377+
describe('Token login:', function() {
378+
it('logs in successfully', function (done) {
379+
loadApp({
380+
queryString: "?loginToken=secretToken&homeserver=https%3A%2F%2Fhomeserver&identityServer=https%3A%2F%2Fidserver",
381+
});
382+
383+
q.delay(1).then(() => {
384+
// we expect a spinner while we're logging in
385+
assertAtLoadingSpinner(matrixChat);
386+
387+
httpBackend.when('POST', '/login').check(function(req) {
388+
expect(req.path).toMatch(new RegExp("^https://homeserver/"));
389+
expect(req.data.type).toEqual("m.login.token");
390+
expect(req.data.token).toEqual("secretToken");
391+
}).respond(200, {
392+
user_id: "@user:localhost",
393+
access_token: "access_token",
394+
});
395+
396+
return httpBackend.flush();
397+
}).then(() => {
398+
// at this point, MatrixChat should fire onLoadCompleted, which
399+
// makes index.js reload the app. We're not going to attempt to
400+
// simulate the reload - just check that things are left in the
401+
// right state for the reloaded app.
402+
403+
return loadCompletePromise;
404+
}).then(() => {
405+
// check that the localstorage has been set up in such a way that
406+
// the reloaded app can pick up where we leave off.
407+
expect(localStorage.getItem('mx_user_id')).toEqual('@user:localhost');
408+
expect(localStorage.getItem('mx_access_token')).toEqual('access_token');
409+
expect(localStorage.getItem('mx_hs_url')).toEqual('https://homeserver');
410+
expect(localStorage.getItem('mx_is_url')).toEqual('https://idserver');
411+
}).done(done, done);
412+
});
413+
});
308414
});
309415

310416
// assert that we are on the loading page

0 commit comments

Comments
 (0)