forked from pnp/cli-microsoft365
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthServer.spec.ts
More file actions
161 lines (146 loc) · 5.61 KB
/
AuthServer.spec.ts
File metadata and controls
161 lines (146 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import * as assert from 'assert';
import Axios from 'axios';
import { AddressInfo } from 'net';
import { Auth } from './Auth';
import authServer from './AuthServer';
import { Logger } from './cli';
import sinon = require('sinon');
describe('AuthServer', () => {
let log: any[];
let callbackResolveStub: sinon.SinonStub;
let callbackRejectStub: sinon.SinonStub;
let openStub: sinon.SinonStub;
let serverUrl: string = "";
let auth: Auth;
const logger: Logger = {
log: (msg: any) => log.push(msg),
logRaw: (msg: any) => log.push(msg),
logToStderr: (msg: any) => log.push(msg)
};
beforeEach(() => {
log = [];
auth = new Auth();
auth.service.appId = '9bc3ab49-b65d-410a-85ad-de819febfddc';
auth.service.tenant = '9bc3ab49-b65d-410a-85ad-de819febfddd';
openStub = sinon.stub(authServer as any, 'open').callsFake(_ => Promise.resolve());
callbackResolveStub = sinon.stub().callsFake(() => { });
callbackRejectStub = sinon.stub().callsFake(() => { });
authServer.initializeServer(auth.service, auth.defaultResource, callbackResolveStub, callbackRejectStub, logger, true);
const address = authServer.server.address() as AddressInfo;
serverUrl = `http://localhost:${address?.port}`;
});
afterEach(() => {
if (authServer.server.listening) {
authServer.server.close();
}
openStub.restore();
});
it('successfully listens', (done) => {
const server = authServer.server;
try {
assert(server !== undefined && server !== null);
assert(server.listening);
assert(serverUrl.indexOf("http://localhost:") > -1);
done();
}
catch (err) {
done(err);
}
});
it('successfully returns an auth code', (done) => {
const server = authServer.server;
try {
assert(server !== undefined && server !== null);
assert(server.listening);
assert(serverUrl.indexOf("http://localhost:") > -1);
Axios.get(`${serverUrl}/?code=1111`).then((response) => {
assert(response.data.indexOf("You have logged into CLI for Microsoft 365!") > -1);
assert(callbackResolveStub.called);
assert(callbackResolveStub.args[0][0].code === "1111");
assert(callbackResolveStub.args[0][0].redirectUri === serverUrl);
assert(callbackRejectStub.notCalled);
assert(authServer.server.listening === false, "server is closed after a successful request");
done();
}).catch((reason) => {
done(reason);
});
}
catch (err) {
done(err);
}
});
it('successfully returns error message only', (done) => {
try {
Axios.get(`${serverUrl}/?error=an error has occurred`).then((response) => {
assert(response.data.indexOf("Oops! Azure Active Directory replied with an error message.") > -1);
assert(callbackResolveStub.notCalled);
assert(callbackRejectStub.called);
assert(callbackRejectStub.args[0][0].error === "an error has occurred");
assert(callbackRejectStub.args[0][0].errorDescription === undefined);
assert(authServer.server.listening === false, "server is closed after a successful request");
done();
}).catch((reason) => {
done(reason);
});
}
catch (err) {
done(err);
}
});
it('successfully returns error message and error description', (done) => {
try {
Axios.get(`${serverUrl}/?error=an error has occurred&error_description=error description`).then((response) => {
assert(response.data.indexOf("Oops! Azure Active Directory replied with an error message.") > -1);
assert(callbackResolveStub.notCalled);
assert(callbackRejectStub.called);
assert(callbackRejectStub.args[0][0].error === "an error has occurred");
assert(callbackRejectStub.args[0][0].errorDescription === "error description");
assert(authServer.server.listening === false, "server is closed after a successful request");
done();
}).catch((reason) => {
done(reason);
});
}
catch (err) {
done(err);
}
});
it('fails if there is an invalid request', (done) => {
try {
Axios.get(`${serverUrl}/?requestingSomthingElse=true`).then((response) => {
assert(response.data.indexOf("Oops! This is an invalid request.") > -1);
assert(callbackResolveStub.notCalled);
assert(callbackRejectStub.called);
assert(callbackRejectStub.args[0][0].error === "invalid request");
assert(callbackRejectStub.args[0][0].errorDescription === "An invalid request has been received by the HTTP server");
assert(authServer.server.listening === false, "server is closed after a successful request");
done();
}).catch((reason) => {
done(reason);
});
}
catch (err) {
done(err);
}
});
it('fails if open fails', (done) => {
try {
if (authServer.server.listening) {
authServer.server.close();
}
openStub.restore();
openStub = sinon.stub(authServer as any, 'open').callsFake(_ => Promise.reject());
authServer.initializeServer(auth.service, auth.defaultResource, callbackResolveStub, callbackRejectStub, logger, true);
setTimeout(() => {
assert(callbackRejectStub.called);
assert(callbackRejectStub.args[0][0].error === "Can't open the default browser");
assert(callbackRejectStub.args[0][0].errorDescription === "Was not able to open a browser instance. Try again later or use a different authentication method.");
assert(authServer.server.listening === false, "server is closed after a successful request");
done();
}, 10);
}
catch (err) {
done(err);
}
});
});