@@ -31,6 +31,8 @@ import test_utils from '../test-utils';
31
31
import MockHttpBackend from '../mock-request' ;
32
32
import { parseQs , parseQsFromFragment } from '../../src/vector/url_utils' ;
33
33
34
+ var DEFAULT_HS_URL = 'http://my_server' ;
35
+ var DEFAULT_IS_URL = 'http://my_is' ;
34
36
35
37
describe ( 'loading:' , function ( ) {
36
38
let parentDiv ;
@@ -39,6 +41,12 @@ describe('loading:', function () {
39
41
// an Object simulating the window.location
40
42
let windowLocation ;
41
43
44
+ // the mounted MatrixChat
45
+ let matrixChat ;
46
+
47
+ // a promise which resolves when the MatrixChat calls onLoadCompleted
48
+ let loadCompletePromise ;
49
+
42
50
beforeEach ( function ( ) {
43
51
test_utils . beforeEach ( this ) ;
44
52
httpBackend = new MockHttpBackend ( ) ;
@@ -50,6 +58,7 @@ describe('loading:', function () {
50
58
// document.body.appendChild(parentDiv);
51
59
52
60
windowLocation = null ;
61
+ matrixChat = null ;
53
62
54
63
window . localStorage . clear ( ) ;
55
64
} ) ;
@@ -67,15 +76,23 @@ describe('loading:', function () {
67
76
* TODO: it would be nice to factor some of this stuff out of index.js so
68
77
* that we can test it rather than our own implementation of it.
69
78
*/
70
- function loadApp ( uriFragment ) {
79
+ function loadApp ( opts ) {
80
+ opts = opts || { } ;
81
+ const queryString = opts . queryString || "" ;
82
+ const uriFragment = opts . uriFragment || "" ;
83
+
71
84
windowLocation = {
72
- search : "" ,
85
+ search : queryString ,
73
86
hash : uriFragment ,
74
87
toString : function ( ) { return this . search + this . hash ; } ,
75
88
} ;
76
89
77
90
let lastLoadedScreen = null ;
78
91
let appLoaded = false ;
92
+
93
+ let loadCompleteDefer = q . defer ( ) ;
94
+ loadCompletePromise = loadCompleteDefer . promise ;
95
+
79
96
function onNewScreen ( screen ) {
80
97
console . log ( "newscreen " + screen ) ;
81
98
if ( ! appLoaded ) {
@@ -89,24 +106,23 @@ describe('loading:', function () {
89
106
90
107
const MatrixChat = sdk . getComponent ( 'structures.MatrixChat' ) ;
91
108
const fragParts = parseQsFromFragment ( windowLocation ) ;
92
- const matrixChat = ReactDOM . render (
109
+ var params = parseQs ( windowLocation ) ;
110
+ matrixChat = ReactDOM . render (
93
111
< MatrixChat
94
112
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 }
97
119
enableGuest = { true }
120
+ onLoadCompleted = { loadCompleteDefer . resolve }
98
121
/> , parentDiv
99
122
) ;
100
123
101
124
function routeUrl ( location , matrixChat ) {
102
125
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
-
110
126
var fragparts = parseQsFromFragment ( location ) ;
111
127
matrixChat . showScreen ( fragparts . location . substring ( 1 ) ,
112
128
fragparts . params ) ;
@@ -122,13 +138,11 @@ describe('loading:', function () {
122
138
lastLoadedScreen = null ;
123
139
}
124
140
} ) . done ( ) ;
125
-
126
- return matrixChat ;
127
141
}
128
142
129
143
describe ( "Clean load with no stored credentials:" , function ( ) {
130
144
it ( 'gives a login panel by default' , function ( done ) {
131
- let matrixChat = loadApp ( "" ) ;
145
+ loadApp ( ) ;
132
146
133
147
q . delay ( 1 ) . then ( ( ) => {
134
148
// at this point, we're trying to do a guest registration;
@@ -149,7 +163,9 @@ describe('loading:', function () {
149
163
} ) ;
150
164
151
165
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
+ } ) ;
153
169
154
170
q . delay ( 1 ) . then ( ( ) => {
155
171
// at this point, we're trying to do a guest registration;
@@ -169,7 +185,10 @@ describe('loading:', function () {
169
185
expect ( req . data . type ) . toEqual ( 'm.login.password' ) ;
170
186
expect ( req . data . user ) . toEqual ( 'user' ) ;
171
187
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
+ } ) ;
173
192
login . onPasswordLogin ( "user" , "pass" )
174
193
return httpBackend . flush ( ) ;
175
194
} ) . then ( ( ) => {
@@ -187,6 +206,12 @@ describe('loading:', function () {
187
206
ReactTestUtils . findRenderedComponentWithType (
188
207
matrixChat , sdk . getComponent ( 'structures.RoomView' ) ) ;
189
208
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 ) ;
190
215
} ) . done ( done , done ) ;
191
216
} ) ;
192
217
} ) ;
@@ -204,7 +229,7 @@ describe('loading:', function () {
204
229
httpBackend . when ( 'POST' , '/filter' ) . respond ( 200 , { filter_id : 'fid' } ) ;
205
230
httpBackend . when ( 'GET' , '/sync' ) . respond ( 200 , { } ) ;
206
231
207
- let matrixChat = loadApp ( "" ) ;
232
+ loadApp ( ) ;
208
233
209
234
q . delay ( 1 ) . then ( ( ) => {
210
235
// we expect a spinner
@@ -225,7 +250,9 @@ describe('loading:', function () {
225
250
httpBackend . when ( 'POST' , '/filter' ) . respond ( 200 , { filter_id : 'fid' } ) ;
226
251
httpBackend . when ( 'GET' , '/sync' ) . respond ( 200 , { } ) ;
227
252
228
- let matrixChat = loadApp ( "#/room/!room:id" ) ;
253
+ loadApp ( {
254
+ uriFragment : "#/room/!room:id" ,
255
+ } ) ;
229
256
230
257
q . delay ( 1 ) . then ( ( ) => {
231
258
// we expect a spinner
@@ -245,7 +272,7 @@ describe('loading:', function () {
245
272
246
273
describe ( 'Guest auto-registration:' , function ( ) {
247
274
it ( 'shows a directory by default' , function ( done ) {
248
- let matrixChat = loadApp ( "" ) ;
275
+ loadApp ( ) ;
249
276
250
277
q . delay ( 1 ) . then ( ( ) => {
251
278
// at this point, we're trying to do a guest registration;
@@ -275,9 +302,50 @@ describe('loading:', function () {
275
302
} ) . done ( done , done ) ;
276
303
} ) ;
277
304
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
+
278
344
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 ( ( ) => {
281
349
// at this point, we're trying to do a guest registration;
282
350
// we expect a spinner
283
351
assertAtLoadingSpinner ( matrixChat ) ;
@@ -305,6 +373,44 @@ describe('loading:', function () {
305
373
} ) . done ( done , done ) ;
306
374
} ) ;
307
375
} ) ;
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
+ } ) ;
308
414
} ) ;
309
415
310
416
// assert that we are on the loading page
0 commit comments