|
| 1 | +/* |
| 2 | +Copyright 2016 OpenMarket Ltd |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +/* loading.js: test the myriad paths we have for loading the application */ |
| 18 | + |
| 19 | +import React from 'react'; |
| 20 | +import ReactDOM from 'react-dom'; |
| 21 | +import ReactTestUtils from 'react-addons-test-utils'; |
| 22 | +import expect from 'expect'; |
| 23 | +import q from 'q'; |
| 24 | + |
| 25 | +import jssdk from 'matrix-js-sdk'; |
| 26 | + |
| 27 | +import sdk from 'matrix-react-sdk'; |
| 28 | +import MatrixClientPeg from 'matrix-react-sdk/lib/MatrixClientPeg'; |
| 29 | + |
| 30 | +import test_utils from '../test-utils'; |
| 31 | +import MockHttpBackend from '../mock-request'; |
| 32 | +import {parseQs, parseQsFromFragment} from '../../src/vector/url_utils'; |
| 33 | + |
| 34 | + |
| 35 | +describe('loading:', function () { |
| 36 | + let parentDiv; |
| 37 | + let httpBackend; |
| 38 | + |
| 39 | + // an Object simulating the window.location |
| 40 | + let windowLocation; |
| 41 | + |
| 42 | + beforeEach(function() { |
| 43 | + test_utils.beforeEach(this); |
| 44 | + httpBackend = new MockHttpBackend(); |
| 45 | + jssdk.request(httpBackend.requestFn); |
| 46 | + parentDiv = document.createElement('div'); |
| 47 | + |
| 48 | + // uncomment this to actually add the div to the UI, to help with |
| 49 | + // debugging (but slow things down) |
| 50 | + // document.body.appendChild(parentDiv); |
| 51 | + |
| 52 | + windowLocation = null; |
| 53 | + }); |
| 54 | + |
| 55 | + afterEach(function() { |
| 56 | + if (parentDiv) { |
| 57 | + ReactDOM.unmountComponentAtNode(parentDiv); |
| 58 | + parentDiv.remove(); |
| 59 | + parentDiv = null; |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + /* simulate the load process done by index.js |
| 64 | + * |
| 65 | + * TODO: it would be nice to factor some of this stuff out of index.js so |
| 66 | + * that we can test it rather than our own implementation of it. |
| 67 | + */ |
| 68 | + function loadApp(uriFragment) { |
| 69 | + windowLocation = { |
| 70 | + search: "", |
| 71 | + hash: uriFragment, |
| 72 | + toString: function() { return this.search + this.hash; }, |
| 73 | + }; |
| 74 | + |
| 75 | + let lastLoadedScreen = null; |
| 76 | + let appLoaded = false; |
| 77 | + function onNewScreen(screen) { |
| 78 | + console.log("newscreen "+screen); |
| 79 | + if (!appLoaded) { |
| 80 | + lastLoadedScreen = screen; |
| 81 | + } else { |
| 82 | + var hash = '#/' + screen; |
| 83 | + windowLocation.hash = hash; |
| 84 | + console.log("browser URI now "+ windowLocation); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + const MatrixChat = sdk.getComponent('structures.MatrixChat'); |
| 89 | + const fragParts = parseQsFromFragment(windowLocation); |
| 90 | + const matrixChat = ReactDOM.render( |
| 91 | + <MatrixChat |
| 92 | + onNewScreen={onNewScreen} |
| 93 | + config={{}} |
| 94 | + startingQueryParams={fragParts.params} |
| 95 | + enableGuest={true} |
| 96 | + />, parentDiv |
| 97 | + ); |
| 98 | + |
| 99 | + function routeUrl(location, matrixChat) { |
| 100 | + console.log("Routing URL "+location); |
| 101 | + var params = parseQs(location); |
| 102 | + var loginToken = params.loginToken; |
| 103 | + if (loginToken) { |
| 104 | + matrixChat.showScreen('token_login', params); |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + var fragparts = parseQsFromFragment(location); |
| 109 | + matrixChat.showScreen(fragparts.location.substring(1), |
| 110 | + fragparts.params); |
| 111 | + } |
| 112 | + |
| 113 | + // pause for a cycle, then simulate the window.onload handler |
| 114 | + q.delay(0).then(() => { |
| 115 | + console.log("simulating window.onload"); |
| 116 | + routeUrl(windowLocation, matrixChat); |
| 117 | + appLoaded = true; |
| 118 | + if (lastLoadedScreen) { |
| 119 | + onNewScreen(lastLoadedScreen); |
| 120 | + lastLoadedScreen = null; |
| 121 | + } |
| 122 | + }).done(); |
| 123 | + |
| 124 | + return matrixChat; |
| 125 | + } |
| 126 | + |
| 127 | + describe("Clean load with no stored credentials:", function() { |
| 128 | + it('gives a login panel by default', function (done) { |
| 129 | + let matrixChat = loadApp(""); |
| 130 | + |
| 131 | + q.delay(1).then(() => { |
| 132 | + // at this point, we're trying to do a guest registration; |
| 133 | + // we expect a spinner |
| 134 | + ReactTestUtils.findRenderedComponentWithType( |
| 135 | + matrixChat, sdk.getComponent('elements.Spinner')); |
| 136 | + |
| 137 | + httpBackend.when('POST', '/register').check(function(req) { |
| 138 | + expect(req.queryParams.kind).toEqual('guest'); |
| 139 | + }).respond(403, "Guest access is disabled"); |
| 140 | + |
| 141 | + return httpBackend.flush(); |
| 142 | + }).then(() => { |
| 143 | + // we expect a single <Login> component |
| 144 | + ReactTestUtils.findRenderedComponentWithType( |
| 145 | + matrixChat, sdk.getComponent('structures.login.Login')); |
| 146 | + expect(windowLocation.hash).toEqual(""); |
| 147 | + }).done(done, done); |
| 148 | + }); |
| 149 | + |
| 150 | + it('should follow the original link after successful login', function(done) { |
| 151 | + let matrixChat = loadApp("#/room/!room:id"); |
| 152 | + |
| 153 | + q.delay(1).then(() => { |
| 154 | + // at this point, we're trying to do a guest registration; |
| 155 | + // we expect a spinner |
| 156 | + ReactTestUtils.findRenderedComponentWithType( |
| 157 | + matrixChat, sdk.getComponent('elements.Spinner')); |
| 158 | + |
| 159 | + httpBackend.when('POST', '/register').check(function(req) { |
| 160 | + expect(req.queryParams.kind).toEqual('guest'); |
| 161 | + }).respond(403, "Guest access is disabled"); |
| 162 | + |
| 163 | + return httpBackend.flush(); |
| 164 | + }).then(() => { |
| 165 | + // we expect a single <Login> component |
| 166 | + let login = ReactTestUtils.findRenderedComponentWithType( |
| 167 | + matrixChat, sdk.getComponent('structures.login.Login')); |
| 168 | + httpBackend.when('POST', '/login').check(function(req) { |
| 169 | + expect(req.data.type).toEqual('m.login.password'); |
| 170 | + expect(req.data.user).toEqual('user'); |
| 171 | + expect(req.data.password).toEqual('pass'); |
| 172 | + }).respond(200, { user_id: 'user_id' }); |
| 173 | + login.onPasswordLogin("user", "pass") |
| 174 | + return httpBackend.flush(); |
| 175 | + }).then(() => { |
| 176 | + // we expect a spinner |
| 177 | + ReactTestUtils.findRenderedComponentWithType( |
| 178 | + matrixChat, sdk.getComponent('elements.Spinner')); |
| 179 | + |
| 180 | + httpBackend.when('GET', '/pushrules').respond(200, {}); |
| 181 | + httpBackend.when('POST', '/filter').respond(200, { filter_id: 'fid' }); |
| 182 | + httpBackend.when('GET', '/sync').respond(200, {}); |
| 183 | + return httpBackend.flush(); |
| 184 | + }).then(() => { |
| 185 | + // once the sync completes, we should have a room view |
| 186 | + httpBackend.verifyNoOutstandingExpectation(); |
| 187 | + ReactTestUtils.findRenderedComponentWithType( |
| 188 | + matrixChat, sdk.getComponent('structures.RoomView')); |
| 189 | + expect(windowLocation.hash).toEqual("#/room/!room:id"); |
| 190 | + }).done(done, done); |
| 191 | + }); |
| 192 | + }); |
| 193 | + |
| 194 | + describe("MatrixClient rehydrated from stored credentials:", function() { |
| 195 | + beforeEach(function() { |
| 196 | + // start with a logged-in client |
| 197 | + MatrixClientPeg.replaceUsingCreds({ |
| 198 | + homeserverUrl: 'http://localhost', |
| 199 | + identityServerUrl: 'http://localhost', |
| 200 | + userId: '@me:localhost', |
| 201 | + accessToken: 'access_token', |
| 202 | + guest: false, |
| 203 | + }); |
| 204 | + }); |
| 205 | + |
| 206 | + it('shows a directory by default if we have no joined rooms', function(done) { |
| 207 | + httpBackend.when('GET', '/pushrules').respond(200, {}); |
| 208 | + httpBackend.when('POST', '/filter').respond(200, { filter_id: 'fid' }); |
| 209 | + httpBackend.when('GET', '/sync').respond(200, {}); |
| 210 | + |
| 211 | + let matrixChat = loadApp(""); |
| 212 | + |
| 213 | + q.delay(1).then(() => { |
| 214 | + // we expect a spinner |
| 215 | + ReactTestUtils.findRenderedComponentWithType( |
| 216 | + matrixChat, sdk.getComponent('elements.Spinner')); |
| 217 | + return httpBackend.flush(); |
| 218 | + }).then(() => { |
| 219 | + // once the sync completes, we should have a directory |
| 220 | + httpBackend.verifyNoOutstandingExpectation(); |
| 221 | + ReactTestUtils.findRenderedComponentWithType( |
| 222 | + matrixChat, sdk.getComponent('structures.RoomDirectory')); |
| 223 | + expect(windowLocation.hash).toEqual("#/directory"); |
| 224 | + }).done(done, done); |
| 225 | + }); |
| 226 | + |
| 227 | + it('shows a room view if we followed a room link', function(done) { |
| 228 | + httpBackend.when('GET', '/pushrules').respond(200, {}); |
| 229 | + httpBackend.when('POST', '/filter').respond(200, { filter_id: 'fid' }); |
| 230 | + httpBackend.when('GET', '/sync').respond(200, {}); |
| 231 | + |
| 232 | + let matrixChat = loadApp("#/room/!room:id"); |
| 233 | + |
| 234 | + q.delay(1).then(() => { |
| 235 | + // we expect a spinner |
| 236 | + ReactTestUtils.findRenderedComponentWithType( |
| 237 | + matrixChat, sdk.getComponent('elements.Spinner')); |
| 238 | + return httpBackend.flush(); |
| 239 | + }).then(() => { |
| 240 | + // once the sync completes, we should have a room view |
| 241 | + httpBackend.verifyNoOutstandingExpectation(); |
| 242 | + ReactTestUtils.findRenderedComponentWithType( |
| 243 | + matrixChat, sdk.getComponent('structures.RoomView')); |
| 244 | + expect(windowLocation.hash).toEqual("#/room/!room:id"); |
| 245 | + }).done(done, done); |
| 246 | + |
| 247 | + }); |
| 248 | + }); |
| 249 | +}); |
0 commit comments