2016-10-11 17:04:55 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import expect from 'expect';
|
2017-07-12 12:58:14 +00:00
|
|
|
import Promise from 'bluebird';
|
2016-10-11 17:04:55 +00:00
|
|
|
import React from 'react';
|
|
|
|
import ReactDOM from 'react-dom';
|
|
|
|
import ReactTestUtils from 'react-addons-test-utils';
|
|
|
|
import sinon from 'sinon';
|
2017-07-11 22:14:56 +00:00
|
|
|
import MatrixReactTestUtils from 'matrix-react-test-utils';
|
2016-10-11 17:04:55 +00:00
|
|
|
|
|
|
|
import sdk from 'matrix-react-sdk';
|
2017-05-25 22:13:49 +00:00
|
|
|
import MatrixClientPeg from '../../../../src/MatrixClientPeg';
|
2016-10-11 17:04:55 +00:00
|
|
|
|
|
|
|
import * as test_utils from '../../../test-utils';
|
|
|
|
|
|
|
|
const InteractiveAuthDialog = sdk.getComponent(
|
2017-10-11 16:56:17 +00:00
|
|
|
'views.dialogs.InteractiveAuthDialog',
|
2016-10-11 17:04:55 +00:00
|
|
|
);
|
|
|
|
|
2017-10-11 16:56:17 +00:00
|
|
|
describe('InteractiveAuthDialog', function() {
|
|
|
|
let parentDiv;
|
|
|
|
let sandbox;
|
2016-10-11 17:04:55 +00:00
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
test_utils.beforeEach(this);
|
|
|
|
sandbox = test_utils.stubClient(sandbox);
|
|
|
|
parentDiv = document.createElement('div');
|
|
|
|
document.body.appendChild(parentDiv);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
ReactDOM.unmountComponentAtNode(parentDiv);
|
|
|
|
parentDiv.remove();
|
|
|
|
sandbox.restore();
|
|
|
|
});
|
|
|
|
|
2017-07-11 22:14:56 +00:00
|
|
|
it('Should successfully complete a password flow', function() {
|
2016-10-11 17:04:55 +00:00
|
|
|
const onFinished = sinon.spy();
|
2017-10-11 16:56:17 +00:00
|
|
|
const doRequest = sinon.stub().returns(Promise.resolve({a: 1}));
|
2016-10-11 17:04:55 +00:00
|
|
|
|
|
|
|
// tell the stub matrixclient to return a real userid
|
2017-10-11 16:56:17 +00:00
|
|
|
const client = MatrixClientPeg.get();
|
2016-10-11 17:04:55 +00:00
|
|
|
client.credentials = {userId: "@user:id"};
|
|
|
|
|
|
|
|
const dlg = ReactDOM.render(
|
|
|
|
<InteractiveAuthDialog
|
2017-02-24 11:41:23 +00:00
|
|
|
matrixClient={client}
|
2016-10-11 17:04:55 +00:00
|
|
|
authData={{
|
|
|
|
session: "sess",
|
|
|
|
flows: [
|
2017-10-11 16:56:17 +00:00
|
|
|
{"stages": ["m.login.password"]},
|
|
|
|
],
|
2016-10-11 17:04:55 +00:00
|
|
|
}}
|
|
|
|
makeRequest={doRequest}
|
|
|
|
onFinished={onFinished}
|
|
|
|
/>, parentDiv);
|
|
|
|
|
2017-03-16 17:26:42 +00:00
|
|
|
// wait for a password box and a submit button
|
2017-07-11 23:24:00 +00:00
|
|
|
return MatrixReactTestUtils.waitForRenderedDOMComponentWithTag(dlg, "form", 2).then((formNode) => {
|
2017-03-16 17:26:42 +00:00
|
|
|
const inputNodes = ReactTestUtils.scryRenderedDOMComponentsWithTag(
|
2017-10-11 16:56:17 +00:00
|
|
|
dlg, "input",
|
2017-03-16 17:26:42 +00:00
|
|
|
);
|
|
|
|
let passwordNode;
|
|
|
|
let submitNode;
|
|
|
|
for (const node of inputNodes) {
|
|
|
|
if (node.type == 'password') {
|
|
|
|
passwordNode = node;
|
|
|
|
} else if (node.type == 'submit') {
|
|
|
|
submitNode = node;
|
|
|
|
}
|
2017-02-13 18:52:33 +00:00
|
|
|
}
|
2018-12-25 02:13:09 +00:00
|
|
|
expect(passwordNode).toBeTruthy();
|
|
|
|
expect(submitNode).toBeTruthy();
|
2017-03-16 17:26:42 +00:00
|
|
|
|
|
|
|
// submit should be disabled
|
|
|
|
expect(submitNode.disabled).toBe(true);
|
|
|
|
|
|
|
|
// put something in the password box, and hit enter; that should
|
|
|
|
// trigger a request
|
|
|
|
passwordNode.value = "s3kr3t";
|
|
|
|
ReactTestUtils.Simulate.change(passwordNode);
|
|
|
|
expect(submitNode.disabled).toBe(false);
|
|
|
|
ReactTestUtils.Simulate.submit(formNode, {});
|
|
|
|
|
|
|
|
expect(doRequest.callCount).toEqual(1);
|
|
|
|
expect(doRequest.calledWithExactly({
|
|
|
|
session: "sess",
|
|
|
|
type: "m.login.password",
|
|
|
|
password: "s3kr3t",
|
|
|
|
user: "@user:id",
|
|
|
|
})).toBe(true);
|
|
|
|
|
|
|
|
// there should now be a spinner
|
|
|
|
ReactTestUtils.findRenderedComponentWithType(
|
|
|
|
dlg, sdk.getComponent('elements.Spinner'),
|
|
|
|
);
|
|
|
|
|
|
|
|
// let the request complete
|
2017-07-12 13:04:20 +00:00
|
|
|
return Promise.delay(1);
|
2017-03-16 17:26:42 +00:00
|
|
|
}).then(() => {
|
2016-10-11 17:04:55 +00:00
|
|
|
expect(onFinished.callCount).toEqual(1);
|
2017-10-11 16:56:17 +00:00
|
|
|
expect(onFinished.calledWithExactly(true, {a: 1})).toBe(true);
|
2017-07-11 22:14:56 +00:00
|
|
|
});
|
2016-10-11 17:04:55 +00:00
|
|
|
});
|
|
|
|
});
|