Skip to content

Commit 110aa99

Browse files
Joe CarpenitoJoe Carpenito
authored andcommitted
add node env tests in addition to DOM
1 parent 28fe0ea commit 110aa99

3 files changed

Lines changed: 156 additions & 16 deletions

File tree

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
1+
/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */
22

33
export default {
44
preset: "ts-jest",
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* ENV set to "jsdom" in order to gain the fetch API (Headers in particular)
2+
* ENV set to "jsdom" to assert functionality executing in the browser
33
* @jest-environment jsdom
44
*/
55

@@ -20,7 +20,7 @@ describe("Authorization Service", () => {
2020
global.fetch = jest.fn(() =>
2121
Promise.resolve({
2222
json: () => Promise.resolve({ roles: ["contact_center_associate"] }),
23-
})
23+
}),
2424
);
2525

2626
const aToken = "a_sample_token";
@@ -29,7 +29,7 @@ describe("Authorization Service", () => {
2929
const profile = await AuthorizationService.profileAuthorize(
3030
aProfileURL,
3131
aTokenHeader,
32-
aToken
32+
aToken,
3333
);
3434

3535
expect(global.fetch).toHaveBeenCalledWith(aProfileURL, {
@@ -81,7 +81,7 @@ describe("Authorization Service", () => {
8181

8282
it("does not add a new policy if the role has already been defined", () => {
8383
expect(
84-
AuthorizationService.defineRole("store_associate", ["ingredientLocking"])
84+
AuthorizationService.defineRole("store_associate", ["ingredientLocking"]),
8585
).toBe(false);
8686

8787
expect(AuthorizationService.getPolicyMap()).toEqual([
@@ -104,35 +104,35 @@ describe("Authorization Service", () => {
104104
expect(
105105
AuthorizationService.userCan(
106106
["contact_center_associate"],
107-
"manualPayments"
108-
)
107+
"manualPayments",
108+
),
109109
).toBe(true);
110110
expect(
111-
AuthorizationService.userCan(["store_associate"], "ingredientLocking")
111+
AuthorizationService.userCan(["store_associate"], "ingredientLocking"),
112112
).toBe(true);
113113
expect(
114114
AuthorizationService.userCan(
115115
["contact_center_associate"],
116-
"ingredientLocking"
117-
)
116+
"ingredientLocking",
117+
),
118118
).toBe(false);
119119
expect(
120-
AuthorizationService.userCan(["store_associate"], "manualPayments")
120+
AuthorizationService.userCan(["store_associate"], "manualPayments"),
121121
).toBe(false);
122122
expect(
123-
AuthorizationService.userCan(["store_associate"], "common_auth")
123+
AuthorizationService.userCan(["store_associate"], "common_auth"),
124124
).toBe(true);
125125
expect(
126126
AuthorizationService.userCan(
127127
["store_associate", "contact_center_associate"],
128-
"unregistered_policy"
129-
)
128+
"unregistered_policy",
129+
),
130130
).toBe(false);
131131
expect(
132132
AuthorizationService.userCan(
133133
["store_associate", "contact_center_associate"],
134-
"common_auth"
135-
)
134+
"common_auth",
135+
),
136136
).toBe(true);
137137
});
138138
});

src/index.node.unit.spec.ts

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/**
2+
* ENV set to "node" to assert functionality executing on the server.
3+
* This setting is redundant to the default jest config in `jest.config.js`,
4+
* but is helpful to have here for clarity.
5+
* @jest-environment node
6+
*/
7+
8+
import AuthorizationService from "./index";
9+
10+
describe("Authorization Service :: Node", () => {
11+
it("initializes with empty policy map", () => {
12+
expect(AuthorizationService.getPolicyMap()).toEqual([]);
13+
});
14+
15+
it("retrieves profile information asychronously with profileAuthorize() invocation", async () => {
16+
// mocking fetch for this test, but if it becomes relevant to other tests in this suite, it
17+
// should be elevated to the whole suite (describe)
18+
19+
// ignoring this type error for now since it is for a mock in test
20+
// need to revisit this
21+
// @ts-ignore
22+
global.fetch = jest.fn(() =>
23+
Promise.resolve({
24+
json: () => Promise.resolve({ roles: ["contact_center_associate"] }),
25+
}),
26+
);
27+
28+
const aToken = "a_sample_token";
29+
const aTokenHeader = "token-header";
30+
const aProfileURL = "sample/profile/url";
31+
const profile = await AuthorizationService.profileAuthorize(
32+
aProfileURL,
33+
aTokenHeader,
34+
aToken,
35+
);
36+
37+
expect(global.fetch).toHaveBeenCalledWith(aProfileURL, {
38+
headers: new Headers({
39+
cookie: `token-header=a_sample_token`,
40+
}),
41+
});
42+
expect(profile).toEqual({ roles: ["contact_center_associate"] });
43+
});
44+
45+
it("adds a policy when defineUser() called with new role(s)", () => {
46+
AuthorizationService.defineRole("contact_center_associate", [
47+
"manualPayments",
48+
"existingOrderCancellation",
49+
"common_auth",
50+
]);
51+
52+
expect(AuthorizationService.getPolicyMap()).toEqual([
53+
{
54+
role: "contact_center_associate",
55+
policies: [
56+
"manualPayments",
57+
"existingOrderCancellation",
58+
"common_auth",
59+
],
60+
},
61+
]);
62+
63+
AuthorizationService.defineRole("store_associate", [
64+
"ingredientLocking",
65+
"common_auth",
66+
]);
67+
68+
expect(AuthorizationService.getPolicyMap()).toEqual([
69+
{
70+
role: "contact_center_associate",
71+
policies: [
72+
"manualPayments",
73+
"existingOrderCancellation",
74+
"common_auth",
75+
],
76+
},
77+
{
78+
role: "store_associate",
79+
policies: ["ingredientLocking", "common_auth"],
80+
},
81+
]);
82+
});
83+
84+
it("does not add a new policy if the role has already been defined", () => {
85+
expect(
86+
AuthorizationService.defineRole("store_associate", ["ingredientLocking"]),
87+
).toBe(false);
88+
89+
expect(AuthorizationService.getPolicyMap()).toEqual([
90+
{
91+
role: "contact_center_associate",
92+
policies: [
93+
"manualPayments",
94+
"existingOrderCancellation",
95+
"common_auth",
96+
],
97+
},
98+
{
99+
role: "store_associate",
100+
policies: ["ingredientLocking", "common_auth"],
101+
},
102+
]);
103+
});
104+
105+
it("returns access confirmation or denial based on provided role information", () => {
106+
expect(
107+
AuthorizationService.userCan(
108+
["contact_center_associate"],
109+
"manualPayments",
110+
),
111+
).toBe(true);
112+
expect(
113+
AuthorizationService.userCan(["store_associate"], "ingredientLocking"),
114+
).toBe(true);
115+
expect(
116+
AuthorizationService.userCan(
117+
["contact_center_associate"],
118+
"ingredientLocking",
119+
),
120+
).toBe(false);
121+
expect(
122+
AuthorizationService.userCan(["store_associate"], "manualPayments"),
123+
).toBe(false);
124+
expect(
125+
AuthorizationService.userCan(["store_associate"], "common_auth"),
126+
).toBe(true);
127+
expect(
128+
AuthorizationService.userCan(
129+
["store_associate", "contact_center_associate"],
130+
"unregistered_policy",
131+
),
132+
).toBe(false);
133+
expect(
134+
AuthorizationService.userCan(
135+
["store_associate", "contact_center_associate"],
136+
"common_auth",
137+
),
138+
).toBe(true);
139+
});
140+
});

0 commit comments

Comments
 (0)