UNPKG

5 kBJavaScriptView Raw
1/*
2Copyright 2016 OpenMarket Ltd
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16"use strict";
17
18import 'source-map-support/register';
19import Promise from 'bluebird';
20const sdk = require("../..");
21const utils = require("../test-utils");
22
23const InteractiveAuth = sdk.InteractiveAuth;
24const MatrixError = sdk.MatrixError;
25
26import expect from 'expect';
27import logger from '../../src/logger';
28
29// Trivial client object to test interactive auth
30// (we do not need TestClient here)
31class FakeClient {
32 generateClientSecret() {
33 return "testcl1Ent5EcreT";
34 }
35}
36
37describe("InteractiveAuth", function() {
38 beforeEach(function() {
39 utils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
40 });
41
42 it("should start an auth stage and complete it", function(done) {
43 const doRequest = expect.createSpy();
44 const stateUpdated = expect.createSpy();
45
46 const ia = new InteractiveAuth({
47 matrixClient: new FakeClient(),
48 doRequest: doRequest,
49 stateUpdated: stateUpdated,
50 authData: {
51 session: "sessionId",
52 flows: [
53 { stages: ["logintype"] },
54 ],
55 params: {
56 "logintype": { param: "aa" },
57 },
58 },
59 });
60
61 expect(ia.getSessionId()).toEqual("sessionId");
62 expect(ia.getStageParams("logintype")).toEqual({
63 param: "aa",
64 });
65
66 // first we expect a call here
67 stateUpdated.andCall(function(stage) {
68 logger.log('aaaa');
69 expect(stage).toEqual("logintype");
70 ia.submitAuthDict({
71 type: "logintype",
72 foo: "bar",
73 });
74 });
75
76 // .. which should trigger a call here
77 const requestRes = {"a": "b"};
78 doRequest.andCall(function(authData) {
79 logger.log('cccc');
80 expect(authData).toEqual({
81 session: "sessionId",
82 type: "logintype",
83 foo: "bar",
84 });
85 return Promise.resolve(requestRes);
86 });
87
88 ia.attemptAuth().then(function(res) {
89 expect(res).toBe(requestRes);
90 expect(doRequest.calls.length).toEqual(1);
91 expect(stateUpdated.calls.length).toEqual(1);
92 }).nodeify(done);
93 });
94
95 it("should make a request if no authdata is provided", function(done) {
96 const doRequest = expect.createSpy();
97 const stateUpdated = expect.createSpy();
98
99 const ia = new InteractiveAuth({
100 matrixClient: new FakeClient(),
101 stateUpdated: stateUpdated,
102 doRequest: doRequest,
103 });
104
105 expect(ia.getSessionId()).toBe(undefined);
106 expect(ia.getStageParams("logintype")).toBe(undefined);
107
108 // first we expect a call to doRequest
109 doRequest.andCall(function(authData) {
110 logger.log("request1", authData);
111 expect(authData).toEqual({});
112 const err = new MatrixError({
113 session: "sessionId",
114 flows: [
115 { stages: ["logintype"] },
116 ],
117 params: {
118 "logintype": { param: "aa" },
119 },
120 });
121 err.httpStatus = 401;
122 throw err;
123 });
124
125 // .. which should be followed by a call to stateUpdated
126 const requestRes = {"a": "b"};
127 stateUpdated.andCall(function(stage) {
128 expect(stage).toEqual("logintype");
129 expect(ia.getSessionId()).toEqual("sessionId");
130 expect(ia.getStageParams("logintype")).toEqual({
131 param: "aa",
132 });
133
134 // submitAuthDict should trigger another call to doRequest
135 doRequest.andCall(function(authData) {
136 logger.log("request2", authData);
137 expect(authData).toEqual({
138 session: "sessionId",
139 type: "logintype",
140 foo: "bar",
141 });
142 return Promise.resolve(requestRes);
143 });
144
145 ia.submitAuthDict({
146 type: "logintype",
147 foo: "bar",
148 });
149 });
150
151 ia.attemptAuth().then(function(res) {
152 expect(res).toBe(requestRes);
153 expect(doRequest.calls.length).toEqual(2);
154 expect(stateUpdated.calls.length).toEqual(1);
155 }).nodeify(done);
156 });
157});