You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-08-05 00:42:10 +03:00
Fix tests
This commit is contained in:
@@ -25,6 +25,14 @@ const MatrixError = sdk.MatrixError;
|
|||||||
|
|
||||||
import expect from 'expect';
|
import expect from 'expect';
|
||||||
|
|
||||||
|
// Trivial client object to test interactive auth
|
||||||
|
// (we do not need TestClient here)
|
||||||
|
class FakeClient {
|
||||||
|
generateClientSecret() {
|
||||||
|
return "testcl1Ent5EcreT";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
describe("InteractiveAuth", function() {
|
describe("InteractiveAuth", function() {
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
utils.beforeEach(this); // eslint-disable-line no-invalid-this
|
utils.beforeEach(this); // eslint-disable-line no-invalid-this
|
||||||
@@ -32,11 +40,12 @@ describe("InteractiveAuth", function() {
|
|||||||
|
|
||||||
it("should start an auth stage and complete it", function(done) {
|
it("should start an auth stage and complete it", function(done) {
|
||||||
const doRequest = expect.createSpy();
|
const doRequest = expect.createSpy();
|
||||||
const startAuthStage = expect.createSpy();
|
const stateUpdated = expect.createSpy();
|
||||||
|
|
||||||
const ia = new InteractiveAuth({
|
const ia = new InteractiveAuth({
|
||||||
|
matrixClient: new FakeClient(),
|
||||||
doRequest: doRequest,
|
doRequest: doRequest,
|
||||||
startAuthStage: startAuthStage,
|
stateUpdated: stateUpdated,
|
||||||
authData: {
|
authData: {
|
||||||
session: "sessionId",
|
session: "sessionId",
|
||||||
flows: [
|
flows: [
|
||||||
@@ -54,7 +63,8 @@ describe("InteractiveAuth", function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// first we expect a call here
|
// first we expect a call here
|
||||||
startAuthStage.andCall(function(stage) {
|
stateUpdated.andCall(function(stage) {
|
||||||
|
console.log('aaaa');
|
||||||
expect(stage).toEqual("logintype");
|
expect(stage).toEqual("logintype");
|
||||||
ia.submitAuthDict({
|
ia.submitAuthDict({
|
||||||
type: "logintype",
|
type: "logintype",
|
||||||
@@ -65,6 +75,7 @@ describe("InteractiveAuth", function() {
|
|||||||
// .. which should trigger a call here
|
// .. which should trigger a call here
|
||||||
const requestRes = {"a": "b"};
|
const requestRes = {"a": "b"};
|
||||||
doRequest.andCall(function(authData) {
|
doRequest.andCall(function(authData) {
|
||||||
|
console.log('cccc');
|
||||||
expect(authData).toEqual({
|
expect(authData).toEqual({
|
||||||
session: "sessionId",
|
session: "sessionId",
|
||||||
type: "logintype",
|
type: "logintype",
|
||||||
@@ -76,17 +87,18 @@ describe("InteractiveAuth", function() {
|
|||||||
ia.attemptAuth().then(function(res) {
|
ia.attemptAuth().then(function(res) {
|
||||||
expect(res).toBe(requestRes);
|
expect(res).toBe(requestRes);
|
||||||
expect(doRequest.calls.length).toEqual(1);
|
expect(doRequest.calls.length).toEqual(1);
|
||||||
expect(startAuthStage.calls.length).toEqual(1);
|
expect(stateUpdated.calls.length).toEqual(1);
|
||||||
}).catch(utils.failTest).done(done);
|
}).catch(utils.failTest).done(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should make a request if no authdata is provided", function(done) {
|
it("should make a request if no authdata is provided", function(done) {
|
||||||
const doRequest = expect.createSpy();
|
const doRequest = expect.createSpy();
|
||||||
const startAuthStage = expect.createSpy();
|
const stateUpdated = expect.createSpy();
|
||||||
|
|
||||||
const ia = new InteractiveAuth({
|
const ia = new InteractiveAuth({
|
||||||
|
matrixClient: new FakeClient(),
|
||||||
|
stateUpdated: stateUpdated,
|
||||||
doRequest: doRequest,
|
doRequest: doRequest,
|
||||||
startAuthStage: startAuthStage,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(ia.getSessionId()).toBe(undefined);
|
expect(ia.getSessionId()).toBe(undefined);
|
||||||
@@ -95,7 +107,7 @@ describe("InteractiveAuth", function() {
|
|||||||
// first we expect a call to doRequest
|
// first we expect a call to doRequest
|
||||||
doRequest.andCall(function(authData) {
|
doRequest.andCall(function(authData) {
|
||||||
console.log("request1", authData);
|
console.log("request1", authData);
|
||||||
expect(authData).toBe(null);
|
expect(authData).toEqual({});
|
||||||
const err = new MatrixError({
|
const err = new MatrixError({
|
||||||
session: "sessionId",
|
session: "sessionId",
|
||||||
flows: [
|
flows: [
|
||||||
@@ -109,9 +121,9 @@ describe("InteractiveAuth", function() {
|
|||||||
throw err;
|
throw err;
|
||||||
});
|
});
|
||||||
|
|
||||||
// .. which should be followed by a call to startAuthStage
|
// .. which should be followed by a call to stateUpdated
|
||||||
const requestRes = {"a": "b"};
|
const requestRes = {"a": "b"};
|
||||||
startAuthStage.andCall(function(stage) {
|
stateUpdated.andCall(function(stage) {
|
||||||
expect(stage).toEqual("logintype");
|
expect(stage).toEqual("logintype");
|
||||||
expect(ia.getSessionId()).toEqual("sessionId");
|
expect(ia.getSessionId()).toEqual("sessionId");
|
||||||
expect(ia.getStageParams("logintype")).toEqual({
|
expect(ia.getStageParams("logintype")).toEqual({
|
||||||
@@ -138,7 +150,7 @@ describe("InteractiveAuth", function() {
|
|||||||
ia.attemptAuth().then(function(res) {
|
ia.attemptAuth().then(function(res) {
|
||||||
expect(res).toBe(requestRes);
|
expect(res).toBe(requestRes);
|
||||||
expect(doRequest.calls.length).toEqual(2);
|
expect(doRequest.calls.length).toEqual(2);
|
||||||
expect(startAuthStage.calls.length).toEqual(1);
|
expect(stateUpdated.calls.length).toEqual(1);
|
||||||
}).catch(utils.failTest).done(done);
|
}).catch(utils.failTest).done(done);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@@ -222,12 +222,13 @@ InteractiveAuth.prototype = {
|
|||||||
self._completionDeferred.resolve(result);
|
self._completionDeferred.resolve(result);
|
||||||
}, function(error) {
|
}, function(error) {
|
||||||
// sometimes UI auth errors don't come with flows
|
// sometimes UI auth errors don't come with flows
|
||||||
const haveFlows = Boolean(self._data.flows) || Boolean(error.data.flows);
|
const errorFlows = error.data ? error.data.flows : null;
|
||||||
|
const haveFlows = Boolean(self._data.flows) || Boolean(errorFlows);
|
||||||
if (error.httpStatus !== 401 || !error.data || !haveFlows) {
|
if (error.httpStatus !== 401 || !error.data || !haveFlows) {
|
||||||
// doesn't look like an interactive-auth failure. fail the whole lot.
|
// doesn't look like an interactive-auth failure. fail the whole lot.
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
if (error.data.flows) self._data = error.data;
|
if (errorFlows) self._data = error.data;
|
||||||
self._startNextAuthStage();
|
self._startNextAuthStage();
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
Reference in New Issue
Block a user