element-web/test/Terms-test.js

185 lines
6.7 KiB
JavaScript
Raw Normal View History

2019-07-11 16:32:04 +03:00
/*
Copyright 2019 The Matrix.org Foundation C.I.C.
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 * as Matrix from 'matrix-js-sdk';
import { startTermsFlow, Service } from '../src/Terms';
import { stubClient } from './test-utils';
2019-12-21 00:13:46 +03:00
import {MatrixClientPeg} from '../src/MatrixClientPeg';
2019-07-11 16:32:04 +03:00
const POLICY_ONE = {
version: "six",
en: {
name: "The first policy",
url: "http://example.com/one",
},
};
const POLICY_TWO = {
version: "IX",
en: {
name: "The second policy",
url: "http://example.com/two",
},
};
const IM_SERVICE_ONE = new Service(Matrix.SERVICE_TYPES.IM, 'https://imone.test', 'a token token');
const IM_SERVICE_TWO = new Service(Matrix.SERVICE_TYPES.IM, 'https://imtwo.test', 'a token token');
describe('Terms', function() {
beforeEach(function() {
2019-12-16 14:12:48 +03:00
stubClient();
2019-07-11 16:32:04 +03:00
});
it('should prompt for all terms & services if no account data', async function() {
2019-12-16 14:12:48 +03:00
MatrixClientPeg.get().getAccountData = jest.fn().mockReturnValue(null);
MatrixClientPeg.get().getTerms = jest.fn().mockReturnValue({
2019-07-11 16:32:04 +03:00
policies: {
"policy_the_first": POLICY_ONE,
},
});
2019-12-16 14:12:48 +03:00
const interactionCallback = jest.fn().mockResolvedValue([]);
2019-07-11 16:32:04 +03:00
await startTermsFlow([IM_SERVICE_ONE], interactionCallback);
2019-12-16 14:12:48 +03:00
console.log("interaction callback calls", interactionCallback.mock.calls[0]);
2019-07-11 16:32:04 +03:00
2019-12-16 14:12:48 +03:00
expect(interactionCallback).toBeCalledWith([
2019-07-11 16:32:04 +03:00
{
service: IM_SERVICE_ONE,
policies: {
policy_the_first: POLICY_ONE,
},
},
2019-12-16 14:12:48 +03:00
], []);
2019-07-11 16:32:04 +03:00
});
it('should not prompt if all policies are signed in account data', async function() {
2019-12-16 14:12:48 +03:00
MatrixClientPeg.get().getAccountData = jest.fn().mockReturnValue({
getContent: jest.fn().mockReturnValue({
2019-07-11 16:32:04 +03:00
accepted: ["http://example.com/one"],
}),
});
2019-12-16 14:12:48 +03:00
MatrixClientPeg.get().getTerms = jest.fn().mockReturnValue({
2019-07-11 16:32:04 +03:00
policies: {
"policy_the_first": POLICY_ONE,
},
});
2019-12-16 14:12:48 +03:00
MatrixClientPeg.get().agreeToTerms = jest.fn();
2019-07-11 16:32:04 +03:00
2019-12-16 14:12:48 +03:00
const interactionCallback = jest.fn();
2019-07-11 16:32:04 +03:00
await startTermsFlow([IM_SERVICE_ONE], interactionCallback);
2019-12-16 14:12:48 +03:00
console.log("agreeToTerms call", MatrixClientPeg.get().agreeToTerms.mock.calls[0]);
2019-07-11 16:32:04 +03:00
2019-12-16 14:12:48 +03:00
expect(interactionCallback).not.toHaveBeenCalled();
expect(MatrixClientPeg.get().agreeToTerms).toBeCalledWith(
2019-07-11 16:32:04 +03:00
Matrix.SERVICE_TYPES.IM,
'https://imone.test',
'a token token',
["http://example.com/one"],
2019-12-16 14:12:48 +03:00
);
2019-07-11 16:32:04 +03:00
});
it("should prompt for only terms that aren't already signed", async function() {
2019-12-16 14:12:48 +03:00
MatrixClientPeg.get().getAccountData = jest.fn().mockReturnValue({
getContent: jest.fn().mockReturnValue({
2019-07-11 16:32:04 +03:00
accepted: ["http://example.com/one"],
}),
});
2019-12-16 14:12:48 +03:00
MatrixClientPeg.get().getTerms = jest.fn().mockReturnValue({
2019-07-11 16:32:04 +03:00
policies: {
"policy_the_first": POLICY_ONE,
"policy_the_second": POLICY_TWO,
},
});
2019-12-16 14:12:48 +03:00
MatrixClientPeg.get().agreeToTerms = jest.fn();
2019-07-11 16:32:04 +03:00
2019-12-16 14:12:48 +03:00
const interactionCallback = jest.fn().mockResolvedValue(["http://example.com/one", "http://example.com/two"]);
2019-07-11 16:32:04 +03:00
await startTermsFlow([IM_SERVICE_ONE], interactionCallback);
2019-12-16 14:12:48 +03:00
console.log("interactionCallback call", interactionCallback.mock.calls[0]);
console.log("agreeToTerms call", MatrixClientPeg.get().agreeToTerms.mock.calls[0]);
2019-07-11 16:32:04 +03:00
2019-12-16 14:12:48 +03:00
expect(interactionCallback).toBeCalledWith([
2019-07-11 16:32:04 +03:00
{
service: IM_SERVICE_ONE,
policies: {
policy_the_second: POLICY_TWO,
},
},
2019-12-16 14:12:48 +03:00
], ["http://example.com/one"]);
expect(MatrixClientPeg.get().agreeToTerms).toBeCalledWith(
2019-07-11 16:32:04 +03:00
Matrix.SERVICE_TYPES.IM,
'https://imone.test',
'a token token',
["http://example.com/one", "http://example.com/two"],
2019-12-16 14:12:48 +03:00
);
2019-07-11 16:32:04 +03:00
});
it("should prompt for only services with un-agreed policies", async function() {
2019-12-16 14:12:48 +03:00
MatrixClientPeg.get().getAccountData = jest.fn().mockReturnValue({
getContent: jest.fn().mockReturnValue({
2019-07-11 16:32:04 +03:00
accepted: ["http://example.com/one"],
}),
});
2019-12-16 14:12:48 +03:00
MatrixClientPeg.get().getTerms = jest.fn((serviceType, baseUrl, accessToken) => {
2019-07-11 16:32:04 +03:00
switch (baseUrl) {
case 'https://imone.test':
return {
policies: {
"policy_the_first": POLICY_ONE,
},
};
case 'https://imtwo.test':
return {
policies: {
"policy_the_second": POLICY_TWO,
},
};
}
});
2019-12-16 14:12:48 +03:00
MatrixClientPeg.get().agreeToTerms = jest.fn();
2019-07-11 16:32:04 +03:00
2019-12-16 14:12:48 +03:00
const interactionCallback = jest.fn().mockResolvedValue(["http://example.com/one", "http://example.com/two"]);
2019-07-11 16:32:04 +03:00
await startTermsFlow([IM_SERVICE_ONE, IM_SERVICE_TWO], interactionCallback);
2019-12-17 14:24:37 +03:00
console.log("getTerms call 0", MatrixClientPeg.get().getTerms.mock.calls[0]);
console.log("getTerms call 1", MatrixClientPeg.get().getTerms.mock.calls[1]);
console.log("interactionCallback call", interactionCallback.mock.calls[0]);
console.log("agreeToTerms call", MatrixClientPeg.get().agreeToTerms.mock.calls[0]);
2019-07-11 16:32:04 +03:00
2019-12-16 14:12:48 +03:00
expect(interactionCallback).toBeCalledWith([
2019-07-11 16:32:04 +03:00
{
service: IM_SERVICE_TWO,
policies: {
policy_the_second: POLICY_TWO,
},
},
2019-12-16 14:12:48 +03:00
], ["http://example.com/one"]);
expect(MatrixClientPeg.get().agreeToTerms).toBeCalledWith(
2019-07-11 16:32:04 +03:00
Matrix.SERVICE_TYPES.IM,
'https://imone.test',
'a token token',
["http://example.com/one"],
2019-12-16 14:12:48 +03:00
);
expect(MatrixClientPeg.get().agreeToTerms).toBeCalledWith(
2019-07-11 16:32:04 +03:00
Matrix.SERVICE_TYPES.IM,
'https://imtwo.test',
'a token token',
["http://example.com/two"],
2019-12-16 14:12:48 +03:00
);
2019-07-11 16:32:04 +03:00
});
});