2019-07-11 18:00:24 +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 ScalarAuthClient from '../src/ScalarAuthClient';
|
2019-12-21 00:13:46 +03:00
|
|
|
import {MatrixClientPeg} from '../src/MatrixClientPeg';
|
2019-07-11 18:00:24 +03:00
|
|
|
import { stubClient } from './test-utils';
|
|
|
|
|
|
|
|
describe('ScalarAuthClient', function() {
|
|
|
|
beforeEach(function() {
|
2019-12-16 14:12:48 +03:00
|
|
|
window.localStorage.getItem = jest.fn((arg) => {
|
|
|
|
if (arg === "mx_scalar_token") return "brokentoken";
|
|
|
|
});
|
|
|
|
stubClient();
|
2019-07-11 18:00:24 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should request a new token if the old one fails', async function() {
|
|
|
|
const sac = new ScalarAuthClient();
|
|
|
|
|
2019-12-16 14:12:48 +03:00
|
|
|
sac._getAccountName = jest.fn((arg) => {
|
|
|
|
switch (arg) {
|
|
|
|
case "brokentoken":
|
|
|
|
return Promise.reject({
|
|
|
|
message: "Invalid token",
|
|
|
|
});
|
|
|
|
case "wokentoken":
|
|
|
|
return Promise.resolve(MatrixClientPeg.get().getUserId());
|
|
|
|
}
|
2019-07-11 18:00:24 +03:00
|
|
|
});
|
|
|
|
|
2019-12-16 14:12:48 +03:00
|
|
|
MatrixClientPeg.get().getOpenIdToken = jest.fn().mockResolvedValue('this is your openid token');
|
2019-07-11 18:00:24 +03:00
|
|
|
|
2019-12-16 14:12:48 +03:00
|
|
|
sac.exchangeForScalarToken = jest.fn((arg) => {
|
|
|
|
if (arg === "this is your openid token") return Promise.resolve("wokentoken");
|
|
|
|
});
|
2019-07-11 18:00:24 +03:00
|
|
|
|
|
|
|
await sac.connect();
|
|
|
|
|
2019-12-16 14:12:48 +03:00
|
|
|
expect(sac.exchangeForScalarToken).toBeCalledWith('this is your openid token');
|
2019-07-11 18:00:24 +03:00
|
|
|
expect(sac.scalarToken).toEqual('wokentoken');
|
|
|
|
});
|
|
|
|
});
|