2016-10-11 20:04:55 +03: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 React from 'react';
|
|
|
|
import ReactDOM from 'react-dom';
|
2019-08-24 13:47:07 +03:00
|
|
|
import ReactTestUtils from 'react-dom/test-utils';
|
2017-07-12 01:14:56 +03:00
|
|
|
import MatrixReactTestUtils from 'matrix-react-test-utils';
|
2021-07-02 00:55:27 +03:00
|
|
|
import { sleep } from "matrix-js-sdk/src/utils";
|
2016-10-11 20:04:55 +03:00
|
|
|
|
2019-12-16 14:12:48 +03:00
|
|
|
import sdk from '../../../skinned-sdk';
|
2021-07-01 22:43:05 +03:00
|
|
|
import { MatrixClientPeg } from '../../../../src/MatrixClientPeg';
|
2021-07-01 22:54:04 +03:00
|
|
|
import * as TestUtilsMatrix from '../../../test-utils';
|
2016-10-11 20:04:55 +03:00
|
|
|
|
|
|
|
const InteractiveAuthDialog = sdk.getComponent(
|
2017-10-11 19:56:17 +03:00
|
|
|
'views.dialogs.InteractiveAuthDialog',
|
2016-10-11 20:04:55 +03:00
|
|
|
);
|
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
describe('InteractiveAuthDialog', function() {
|
|
|
|
let parentDiv;
|
2016-10-11 20:04:55 +03:00
|
|
|
|
|
|
|
beforeEach(function() {
|
2021-07-01 22:54:04 +03:00
|
|
|
TestUtilsMatrix.stubClient();
|
2016-10-11 20:04:55 +03:00
|
|
|
parentDiv = document.createElement('div');
|
|
|
|
document.body.appendChild(parentDiv);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
ReactDOM.unmountComponentAtNode(parentDiv);
|
|
|
|
parentDiv.remove();
|
|
|
|
});
|
|
|
|
|
2017-07-12 01:14:56 +03:00
|
|
|
it('Should successfully complete a password flow', function() {
|
2019-12-16 14:12:48 +03:00
|
|
|
const onFinished = jest.fn();
|
2021-07-01 22:43:05 +03:00
|
|
|
const doRequest = jest.fn().mockResolvedValue({ a: 1 });
|
2016-10-11 20:04:55 +03:00
|
|
|
|
|
|
|
// tell the stub matrixclient to return a real userid
|
2017-10-11 19:56:17 +03:00
|
|
|
const client = MatrixClientPeg.get();
|
2021-07-01 22:43:05 +03:00
|
|
|
client.credentials = { userId: "@user:id" };
|
2016-10-11 20:04:55 +03:00
|
|
|
|
|
|
|
const dlg = ReactDOM.render(
|
|
|
|
<InteractiveAuthDialog
|
2017-02-24 14:41:23 +03:00
|
|
|
matrixClient={client}
|
2016-10-11 20:04:55 +03:00
|
|
|
authData={{
|
|
|
|
session: "sess",
|
|
|
|
flows: [
|
2021-07-01 22:43:05 +03:00
|
|
|
{ "stages": ["m.login.password"] },
|
2017-10-11 19:56:17 +03:00
|
|
|
],
|
2016-10-11 20:04:55 +03:00
|
|
|
}}
|
|
|
|
makeRequest={doRequest}
|
|
|
|
onFinished={onFinished}
|
|
|
|
/>, parentDiv);
|
|
|
|
|
2017-03-16 20:26:42 +03:00
|
|
|
// wait for a password box and a submit button
|
2017-07-12 02:24:00 +03:00
|
|
|
return MatrixReactTestUtils.waitForRenderedDOMComponentWithTag(dlg, "form", 2).then((formNode) => {
|
2017-03-16 20:26:42 +03:00
|
|
|
const inputNodes = ReactTestUtils.scryRenderedDOMComponentsWithTag(
|
2017-10-11 19:56:17 +03:00
|
|
|
dlg, "input",
|
2017-03-16 20:26:42 +03: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 21:52:33 +03:00
|
|
|
}
|
2018-12-25 05:13:09 +03:00
|
|
|
expect(passwordNode).toBeTruthy();
|
|
|
|
expect(submitNode).toBeTruthy();
|
2017-03-16 20:26:42 +03: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, {});
|
|
|
|
|
2019-12-17 14:24:37 +03:00
|
|
|
expect(doRequest).toHaveBeenCalledTimes(1);
|
|
|
|
expect(doRequest).toBeCalledWith(expect.objectContaining({
|
2017-03-16 20:26:42 +03:00
|
|
|
session: "sess",
|
|
|
|
type: "m.login.password",
|
|
|
|
password: "s3kr3t",
|
2019-07-11 23:38:28 +03:00
|
|
|
identifier: {
|
|
|
|
type: "m.id.user",
|
|
|
|
user: "@user:id",
|
|
|
|
},
|
2019-12-17 14:24:37 +03:00
|
|
|
}));
|
2017-03-16 20:26:42 +03:00
|
|
|
// let the request complete
|
2019-11-14 17:25:54 +03:00
|
|
|
return sleep(1);
|
2019-12-17 16:11:30 +03:00
|
|
|
}).then(sleep(1)).then(() => {
|
2019-12-16 14:12:48 +03:00
|
|
|
expect(onFinished).toBeCalledTimes(1);
|
2021-07-01 22:43:05 +03:00
|
|
|
expect(onFinished).toBeCalledWith(true, { a: 1 });
|
2017-07-12 01:14:56 +03:00
|
|
|
});
|
2016-10-11 20:04:55 +03:00
|
|
|
});
|
|
|
|
});
|