forked from solid-contrib/solid-auth-fetcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AuthFetcher.spec.ts
201 lines (187 loc) · 7.22 KB
/
AuthFetcher.spec.ts
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
* This project is a continuation of Inrupt's awesome solid-auth-fetcher project,
* see https://www.npmjs.com/package/@inrupt/solid-auth-fetcher.
* Copyright 2020 The Solid Project.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// Required by TSyringe:
import "reflect-metadata";
import {
LoginHandlerMock,
LoginHandlerResponse
} from "../src/login/__mocks__/LoginHandler";
import {
RedirectHandlerMock,
RedirectHandlerResponse
} from "../src/login/oidc/redirectHandler/__mocks__/RedirectHandler";
import { LogoutHandlerMock } from "../src/logout/__mocks__/LogoutHandler";
import {
SessionCreatorMock,
SessionCreatorCreateResponse
} from "../src/solidSession/__mocks__/SessionCreator";
import {
AuthenticatedFetcherMock,
AuthenticatedFetcherResponse
} from "../src/authenticatedFetch/__mocks__/AuthenticatedFetcher";
import { EnvironmentDetectorMock } from "../src/util/__mocks__/EnvironmentDetector";
import AuthFetcher from "../src/AuthFetcher";
import URL from "url-parse";
describe("AuthFetcher", () => {
const defaultMocks = {
loginHandler: LoginHandlerMock,
redirectHandler: RedirectHandlerMock,
logoutHandler: LogoutHandlerMock,
sessionCreator: SessionCreatorMock,
authenticatedFetcher: AuthenticatedFetcherMock,
environmentDetector: EnvironmentDetectorMock
};
function getAuthFetcher(
mocks: Partial<typeof defaultMocks> = defaultMocks
): AuthFetcher {
return new AuthFetcher(
mocks.loginHandler ?? defaultMocks.loginHandler,
mocks.redirectHandler ?? defaultMocks.redirectHandler,
mocks.logoutHandler ?? defaultMocks.logoutHandler,
mocks.sessionCreator ?? defaultMocks.sessionCreator,
mocks.authenticatedFetcher ?? defaultMocks.authenticatedFetcher,
mocks.environmentDetector ?? defaultMocks.environmentDetector
);
}
describe("login", () => {
it("calls login", async () => {
const authFetcher = getAuthFetcher();
const session = await authFetcher.login({
clientId: "coolApp",
redirect: "https://coolapp.com/redirect",
oidcIssuer: "https://idp.com"
});
expect(session).toBe(LoginHandlerResponse);
expect(defaultMocks.loginHandler.handle).toHaveBeenCalledWith({
localUserId: "global",
clientId: "coolApp",
redirect: new URL("https://coolapp.com/redirect"),
oidcIssuer: new URL("https://idp.com")
});
});
});
describe("fetch", () => {
it("calls fetch", async () => {
const authFetcher = getAuthFetcher();
const response = await authFetcher.fetch("https://zombo.com");
expect(response).toBe(AuthenticatedFetcherResponse);
expect(defaultMocks.authenticatedFetcher.handle).toHaveBeenCalledWith(
{
localUserId: "global",
type: "dpop"
},
"https://zombo.com",
undefined
);
});
});
describe("logout", () => {
it("calls logout", async () => {
const authFetcher = getAuthFetcher();
await authFetcher.logout();
expect(defaultMocks.logoutHandler.handle).toHaveBeenCalledWith("global");
});
});
describe("getSession", () => {
it("creates a session for the global user", async () => {
const authFetcher = getAuthFetcher();
const session = await authFetcher.getSession();
expect(session).toBe(SessionCreatorCreateResponse);
expect(defaultMocks.sessionCreator.getSession).toHaveBeenCalledWith(
"global"
);
});
});
describe("uniqueLogin", () => {
it("calls login without a local user", async () => {
const authFetcher = getAuthFetcher();
const session = await authFetcher.uniqueLogin({
clientId: "coolApp",
redirect: "https://coolapp.com/redirect",
oidcIssuer: "https://idp.com"
});
expect(session).toBe(LoginHandlerResponse);
expect(defaultMocks.loginHandler.handle).toHaveBeenCalledWith({
clientId: "coolApp",
redirect: new URL("https://coolapp.com/redirect"),
oidcIssuer: new URL("https://idp.com")
});
});
});
describe("onSession", () => {
it("Automatically calls the callback if the session is present", async () => {
const authFetcher = getAuthFetcher();
const callback = jest.fn();
await authFetcher.onSession(callback);
expect(callback).toHaveBeenCalledWith(SessionCreatorCreateResponse);
});
it("Does not automatically call the callback if the session is not present", async () => {
defaultMocks.sessionCreator.getSession.mockResolvedValueOnce(null);
const authFetcher = getAuthFetcher();
const callback = jest.fn();
await authFetcher.onSession(callback);
expect(callback).not.toHaveBeenCalled();
});
});
describe("onLogout", () => {
it("Temp throws an error", () => {
const authFetcher = getAuthFetcher();
expect(() =>
authFetcher.onLogout(() => {
/* do nothing */
})
).rejects.toThrowError("Not Implemented");
});
});
describe("handleRedirect", () => {
it("calls handle redirect", async () => {
const authFetcher = getAuthFetcher();
const url =
"https://coolapp.com/redirect?state=userId&id_token=idToken&access_token=accessToken";
const session = await authFetcher.handleRedirect(url);
expect(session).toBe(RedirectHandlerResponse);
expect(defaultMocks.redirectHandler.handle).toHaveBeenCalledWith(url);
});
});
describe("automaticallyHandleRedirect", () => {
it("handles redirect when in the browser", async () => {
const authFetcher = getAuthFetcher();
await authFetcher.automaticallyHandleRedirect();
expect(defaultMocks.redirectHandler.handle).toHaveBeenCalledWith(
"http://localhost/"
);
});
it("does not handle redirect when not in the browser", async () => {
defaultMocks.environmentDetector.detect.mockReturnValueOnce("server");
const authFetcher = getAuthFetcher();
await authFetcher.automaticallyHandleRedirect();
expect(defaultMocks.redirectHandler.handle).not.toHaveBeenCalled();
});
});
describe("customAuthFetcher", () => {
it("Temp throws an error", () => {
const authFetcher = getAuthFetcher();
expect(() => authFetcher.customAuthFetcher({})).toThrowError();
});
});
});