1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | "use strict";
|
17 |
|
18 | import 'source-map-support/register';
|
19 | import Promise from 'bluebird';
|
20 | const sdk = require("../..");
|
21 | const utils = require("../test-utils");
|
22 |
|
23 | const InteractiveAuth = sdk.InteractiveAuth;
|
24 | const MatrixError = sdk.MatrixError;
|
25 |
|
26 | import expect from 'expect';
|
27 | import logger from '../../src/logger';
|
28 |
|
29 |
|
30 |
|
31 | class FakeClient {
|
32 | generateClientSecret() {
|
33 | return "testcl1Ent5EcreT";
|
34 | }
|
35 | }
|
36 |
|
37 | describe("InteractiveAuth", function() {
|
38 | beforeEach(function() {
|
39 | utils.beforeEach(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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 | });
|