Test typescriptification - Terms/ScalarAuthClient (#8480)
* test/Terms-test.js -> test/Terms-test.tsx Signed-off-by: Kerry Archibald <kerrya@element.io> * fix ts issues in Terms-test Signed-off-by: Kerry Archibald <kerrya@element.io> * test/ScalarAuthClient-test.js -> test/ScalarAuthClient-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * ts fixes in ScalarAuthClient-test Signed-off-by: Kerry Archibald <kerrya@element.io> * comment Signed-off-by: Kerry Archibald <kerrya@element.io>
This commit is contained in:
parent
4d122db7cf
commit
3d0045dab5
2 changed files with 61 additions and 33 deletions
|
@ -19,6 +19,8 @@ import { MatrixClientPeg } from '../src/MatrixClientPeg';
|
||||||
import { stubClient } from './test-utils';
|
import { stubClient } from './test-utils';
|
||||||
|
|
||||||
describe('ScalarAuthClient', function() {
|
describe('ScalarAuthClient', function() {
|
||||||
|
const apiUrl = 'test.com/api';
|
||||||
|
const uiUrl = 'test.com/app';
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
window.localStorage.getItem = jest.fn((arg) => {
|
window.localStorage.getItem = jest.fn((arg) => {
|
||||||
if (arg === "mx_scalar_token") return "brokentoken";
|
if (arg === "mx_scalar_token") return "brokentoken";
|
||||||
|
@ -27,15 +29,17 @@ describe('ScalarAuthClient', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should request a new token if the old one fails', async function() {
|
it('should request a new token if the old one fails', async function() {
|
||||||
const sac = new ScalarAuthClient();
|
const sac = new ScalarAuthClient(apiUrl, uiUrl);
|
||||||
|
|
||||||
sac.getAccountName = jest.fn((arg) => {
|
// @ts-ignore unhappy with Promise calls
|
||||||
|
jest.spyOn(sac, 'getAccountName').mockImplementation((arg: string) => {
|
||||||
switch (arg) {
|
switch (arg) {
|
||||||
case "brokentoken":
|
case "brokentoken":
|
||||||
return Promise.reject({
|
return Promise.reject({
|
||||||
message: "Invalid token",
|
message: "Invalid token",
|
||||||
});
|
});
|
||||||
case "wokentoken":
|
case "wokentoken":
|
||||||
|
default:
|
||||||
return Promise.resolve(MatrixClientPeg.get().getUserId());
|
return Promise.resolve(MatrixClientPeg.get().getUserId());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -49,6 +53,8 @@ describe('ScalarAuthClient', function() {
|
||||||
await sac.connect();
|
await sac.connect();
|
||||||
|
|
||||||
expect(sac.exchangeForScalarToken).toBeCalledWith('this is your openid token');
|
expect(sac.exchangeForScalarToken).toBeCalledWith('this is your openid token');
|
||||||
|
expect(sac.hasCredentials).toBeTruthy();
|
||||||
|
// @ts-ignore private property
|
||||||
expect(sac.scalarToken).toEqual('wokentoken');
|
expect(sac.scalarToken).toEqual('wokentoken');
|
||||||
});
|
});
|
||||||
});
|
});
|
|
@ -14,10 +14,14 @@ See the License for the specific language governing permissions and
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import * as Matrix from 'matrix-js-sdk/src/matrix';
|
import {
|
||||||
|
MatrixEvent,
|
||||||
|
EventType,
|
||||||
|
SERVICE_TYPES,
|
||||||
|
} from 'matrix-js-sdk/src/matrix';
|
||||||
|
|
||||||
import { startTermsFlow, Service } from '../src/Terms';
|
import { startTermsFlow, Service } from '../src/Terms';
|
||||||
import { stubClient } from './test-utils';
|
import { getMockClientWithEventEmitter } from './test-utils';
|
||||||
import { MatrixClientPeg } from '../src/MatrixClientPeg';
|
import { MatrixClientPeg } from '../src/MatrixClientPeg';
|
||||||
|
|
||||||
const POLICY_ONE = {
|
const POLICY_ONE = {
|
||||||
|
@ -36,17 +40,31 @@ const POLICY_TWO = {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const IM_SERVICE_ONE = new Service(Matrix.SERVICE_TYPES.IM, 'https://imone.test', 'a token token');
|
const IM_SERVICE_ONE = new Service(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');
|
const IM_SERVICE_TWO = new Service(SERVICE_TYPES.IM, 'https://imtwo.test', 'a token token');
|
||||||
|
|
||||||
describe('Terms', function() {
|
describe('Terms', function() {
|
||||||
|
const mockClient = getMockClientWithEventEmitter({
|
||||||
|
getAccountData: jest.fn(),
|
||||||
|
getTerms: jest.fn(),
|
||||||
|
agreeToTerms: jest.fn(),
|
||||||
|
setAccountData: jest.fn(),
|
||||||
|
});
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
stubClient();
|
jest.clearAllMocks();
|
||||||
|
mockClient.getAccountData.mockReturnValue(null);
|
||||||
|
mockClient.getTerms.mockResolvedValue(null);
|
||||||
|
mockClient.setAccountData.mockResolvedValue({});
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(() => {
|
||||||
|
jest.spyOn(MatrixClientPeg, 'get').mockRestore();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should prompt for all terms & services if no account data', async function() {
|
it('should prompt for all terms & services if no account data', async function() {
|
||||||
MatrixClientPeg.get().getAccountData = jest.fn().mockReturnValue(null);
|
mockClient.getAccountData.mockReturnValue(null);
|
||||||
MatrixClientPeg.get().getTerms = jest.fn().mockReturnValue({
|
mockClient.getTerms.mockResolvedValue({
|
||||||
policies: {
|
policies: {
|
||||||
"policy_the_first": POLICY_ONE,
|
"policy_the_first": POLICY_ONE,
|
||||||
},
|
},
|
||||||
|
@ -65,24 +83,26 @@ describe('Terms', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not prompt if all policies are signed in account data', async function() {
|
it('should not prompt if all policies are signed in account data', async function() {
|
||||||
MatrixClientPeg.get().getAccountData = jest.fn().mockReturnValue({
|
const directEvent = new MatrixEvent({
|
||||||
getContent: jest.fn().mockReturnValue({
|
type: EventType.Direct,
|
||||||
|
content: {
|
||||||
accepted: ["http://example.com/one"],
|
accepted: ["http://example.com/one"],
|
||||||
}),
|
},
|
||||||
});
|
});
|
||||||
MatrixClientPeg.get().getTerms = jest.fn().mockReturnValue({
|
mockClient.getAccountData.mockReturnValue(directEvent);
|
||||||
|
mockClient.getTerms.mockResolvedValue({
|
||||||
policies: {
|
policies: {
|
||||||
"policy_the_first": POLICY_ONE,
|
"policy_the_first": POLICY_ONE,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
MatrixClientPeg.get().agreeToTerms = jest.fn();
|
mockClient.agreeToTerms;
|
||||||
|
|
||||||
const interactionCallback = jest.fn();
|
const interactionCallback = jest.fn();
|
||||||
await startTermsFlow([IM_SERVICE_ONE], interactionCallback);
|
await startTermsFlow([IM_SERVICE_ONE], interactionCallback);
|
||||||
|
|
||||||
expect(interactionCallback).not.toHaveBeenCalled();
|
expect(interactionCallback).not.toHaveBeenCalled();
|
||||||
expect(MatrixClientPeg.get().agreeToTerms).toBeCalledWith(
|
expect(mockClient.agreeToTerms).toBeCalledWith(
|
||||||
Matrix.SERVICE_TYPES.IM,
|
SERVICE_TYPES.IM,
|
||||||
'https://imone.test',
|
'https://imone.test',
|
||||||
'a token token',
|
'a token token',
|
||||||
["http://example.com/one"],
|
["http://example.com/one"],
|
||||||
|
@ -90,18 +110,20 @@ describe('Terms', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should prompt for only terms that aren't already signed", async function() {
|
it("should prompt for only terms that aren't already signed", async function() {
|
||||||
MatrixClientPeg.get().getAccountData = jest.fn().mockReturnValue({
|
const directEvent = new MatrixEvent({
|
||||||
getContent: jest.fn().mockReturnValue({
|
type: EventType.Direct,
|
||||||
|
content: {
|
||||||
accepted: ["http://example.com/one"],
|
accepted: ["http://example.com/one"],
|
||||||
}),
|
},
|
||||||
});
|
});
|
||||||
MatrixClientPeg.get().getTerms = jest.fn().mockReturnValue({
|
mockClient.getAccountData.mockReturnValue(directEvent);
|
||||||
|
|
||||||
|
mockClient.getTerms.mockResolvedValue({
|
||||||
policies: {
|
policies: {
|
||||||
"policy_the_first": POLICY_ONE,
|
"policy_the_first": POLICY_ONE,
|
||||||
"policy_the_second": POLICY_TWO,
|
"policy_the_second": POLICY_TWO,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
MatrixClientPeg.get().agreeToTerms = jest.fn();
|
|
||||||
|
|
||||||
const interactionCallback = jest.fn().mockResolvedValue(["http://example.com/one", "http://example.com/two"]);
|
const interactionCallback = jest.fn().mockResolvedValue(["http://example.com/one", "http://example.com/two"]);
|
||||||
await startTermsFlow([IM_SERVICE_ONE], interactionCallback);
|
await startTermsFlow([IM_SERVICE_ONE], interactionCallback);
|
||||||
|
@ -114,8 +136,8 @@ describe('Terms', function() {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
], ["http://example.com/one"]);
|
], ["http://example.com/one"]);
|
||||||
expect(MatrixClientPeg.get().agreeToTerms).toBeCalledWith(
|
expect(mockClient.agreeToTerms).toBeCalledWith(
|
||||||
Matrix.SERVICE_TYPES.IM,
|
SERVICE_TYPES.IM,
|
||||||
'https://imone.test',
|
'https://imone.test',
|
||||||
'a token token',
|
'a token token',
|
||||||
["http://example.com/one", "http://example.com/two"],
|
["http://example.com/one", "http://example.com/two"],
|
||||||
|
@ -123,13 +145,15 @@ describe('Terms', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should prompt for only services with un-agreed policies", async function() {
|
it("should prompt for only services with un-agreed policies", async function() {
|
||||||
MatrixClientPeg.get().getAccountData = jest.fn().mockReturnValue({
|
const directEvent = new MatrixEvent({
|
||||||
getContent: jest.fn().mockReturnValue({
|
type: EventType.Direct,
|
||||||
|
content: {
|
||||||
accepted: ["http://example.com/one"],
|
accepted: ["http://example.com/one"],
|
||||||
}),
|
},
|
||||||
});
|
});
|
||||||
|
mockClient.getAccountData.mockReturnValue(directEvent);
|
||||||
|
|
||||||
MatrixClientPeg.get().getTerms = jest.fn((serviceType, baseUrl, accessToken) => {
|
mockClient.getTerms.mockImplementation(async (_serviceTypes: SERVICE_TYPES, baseUrl: string) => {
|
||||||
switch (baseUrl) {
|
switch (baseUrl) {
|
||||||
case 'https://imone.test':
|
case 'https://imone.test':
|
||||||
return {
|
return {
|
||||||
|
@ -146,8 +170,6 @@ describe('Terms', function() {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
MatrixClientPeg.get().agreeToTerms = jest.fn();
|
|
||||||
|
|
||||||
const interactionCallback = jest.fn().mockResolvedValue(["http://example.com/one", "http://example.com/two"]);
|
const interactionCallback = jest.fn().mockResolvedValue(["http://example.com/one", "http://example.com/two"]);
|
||||||
await startTermsFlow([IM_SERVICE_ONE, IM_SERVICE_TWO], interactionCallback);
|
await startTermsFlow([IM_SERVICE_ONE, IM_SERVICE_TWO], interactionCallback);
|
||||||
|
|
||||||
|
@ -159,14 +181,14 @@ describe('Terms', function() {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
], ["http://example.com/one"]);
|
], ["http://example.com/one"]);
|
||||||
expect(MatrixClientPeg.get().agreeToTerms).toBeCalledWith(
|
expect(mockClient.agreeToTerms).toBeCalledWith(
|
||||||
Matrix.SERVICE_TYPES.IM,
|
SERVICE_TYPES.IM,
|
||||||
'https://imone.test',
|
'https://imone.test',
|
||||||
'a token token',
|
'a token token',
|
||||||
["http://example.com/one"],
|
["http://example.com/one"],
|
||||||
);
|
);
|
||||||
expect(MatrixClientPeg.get().agreeToTerms).toBeCalledWith(
|
expect(mockClient.agreeToTerms).toBeCalledWith(
|
||||||
Matrix.SERVICE_TYPES.IM,
|
SERVICE_TYPES.IM,
|
||||||
'https://imtwo.test',
|
'https://imtwo.test',
|
||||||
'a token token',
|
'a token token',
|
||||||
["http://example.com/two"],
|
["http://example.com/two"],
|
Loading…
Reference in a new issue