forked from authts/oidc-client-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserInfoService.test.ts
More file actions
100 lines (83 loc) · 3.38 KB
/
Copy pathUserInfoService.test.ts
File metadata and controls
100 lines (83 loc) · 3.38 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
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
import { UserInfoService } from "./UserInfoService";
import { MetadataService } from "./MetadataService";
import type { JsonService } from "./JsonService";
import { OidcClientSettingsStore } from "./OidcClientSettings";
describe("UserInfoService", () => {
let subject: UserInfoService;
let metadataService: MetadataService;
let jsonService: JsonService;
beforeEach(() => {
const settings = new OidcClientSettingsStore({
authority: "authority",
client_id: "client",
redirect_uri: "redirect",
});
metadataService = new MetadataService(settings);
subject = new UserInfoService(metadataService);
// access private members
jsonService = subject["_jsonService"];
});
describe("getClaims", () => {
it("should return a promise", async () => {
// act
const p = subject.getClaims("");
// assert
expect(p).toBeInstanceOf(Promise);
// eslint-disable-next-line no-empty
try { await p; } catch {}
});
it("should require a token", async () => {
// act
try {
await subject.getClaims("");
fail("should not come here");
}
catch (err) {
expect(err).toBeInstanceOf(Error);
expect((err as Error).message).toContain("token");
}
});
it("should call userinfo endpoint and pass token", async () => {
// arrange
jest.spyOn(metadataService, "getUserInfoEndpoint").mockImplementation(() => Promise.resolve("http://sts/userinfo"));
const getJsonMock = jest.spyOn(jsonService, "getJson")
.mockResolvedValue({ foo: "bar" });
// act
await subject.getClaims("token");
// assert
expect(getJsonMock).toBeCalledWith("http://sts/userinfo", "token");
});
it("should fail when dependencies fail", async () => {
// arrange
jest.spyOn(metadataService, "getUserInfoEndpoint").mockRejectedValue(new Error("test"));
// act
try {
await subject.getClaims("token");
fail("should not come here");
}
catch (err) {
expect(err).toBeInstanceOf(Error);
expect((err as Error).message).toContain("test");
}
});
it("should return claims", async () => {
// arrange
jest.spyOn(metadataService, "getUserInfoEndpoint").mockImplementation(() => Promise.resolve("http://sts/userinfo"));
const expectedClaims = {
foo: 1, bar: "test",
aud:"some_aud", iss:"issuer",
sub:"123", email:"foo@gmail.com",
role:["admin", "dev"],
nonce:"nonce", at_hash:"athash",
iat:5, nbf:10, exp:20,
};
jest.spyOn(jsonService, "getJson").mockImplementation(() => Promise.resolve(expectedClaims));
// act
const claims = await subject.getClaims("token");
// assert
expect(claims).toEqual(expectedClaims);
});
});
});