-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsso.test.ts
More file actions
186 lines (154 loc) · 5.1 KB
/
sso.test.ts
File metadata and controls
186 lines (154 loc) · 5.1 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
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
import { describe, expect, it, vi } from "vitest";
import { createMockSpinner, runCommand } from "./helpers.js";
vi.mock("yocto-spinner", () => ({ default: () => createMockSpinner() }));
vi.mock("../../src/lib/program.js", () => ({
program: { opts: () => ({}) },
}));
const graphqlRequest = vi.fn();
vi.mock("../../src/lib/graphql-client.js", () => ({
graphqlRequest: (...args: unknown[]) => graphqlRequest(...args),
}));
const { ssoCommand } = await import("../../src/commands/sso.js");
const mockSSOApp = {
id: "sso_app_1",
name: "My App",
clientId: "client_123",
clientSecret: "secret_456",
redirectUris: ["https://example.com/callback"],
};
describe("sso", () => {
it("list fetches SSO apps", async () => {
graphqlRequest.mockResolvedValueOnce({
getSSOApps: [mockSSOApp],
});
await runCommand(ssoCommand, ["list"]);
expect(graphqlRequest).toHaveBeenCalledWith(
expect.objectContaining({
query: expect.stringContaining("getSSOApps"),
})
);
});
it("create sends name and redirect URIs", async () => {
graphqlRequest.mockResolvedValueOnce({
createSSOApp: mockSSOApp,
});
await runCommand(ssoCommand, [
"create",
"--name",
"My App",
"--redirect-uri",
"https://example.com/callback",
]);
const call = graphqlRequest.mock.calls[0][0];
expect(call.variables.input).toEqual({
name: "My App",
redirectUris: ["https://example.com/callback"],
});
});
it("create supports multiple redirect URIs", async () => {
graphqlRequest.mockResolvedValueOnce({
createSSOApp: {
...mockSSOApp,
redirectUris: [
"https://example.com/callback",
"https://example.com/auth",
],
},
});
await runCommand(ssoCommand, [
"create",
"--name",
"My App",
"--redirect-uri",
"https://example.com/callback",
"--redirect-uri",
"https://example.com/auth",
]);
const call = graphqlRequest.mock.calls[0][0];
expect(call.variables.input.redirectUris).toEqual([
"https://example.com/callback",
"https://example.com/auth",
]);
});
it("update sends id and name", async () => {
graphqlRequest.mockResolvedValueOnce({
updateSSOApp: { ...mockSSOApp, name: "Renamed" },
});
await runCommand(ssoCommand, ["update", "sso_app_1", "--name", "Renamed"]);
const call = graphqlRequest.mock.calls[0][0];
expect(call.variables.input.id).toBe("sso_app_1");
expect(call.variables.input.name).toBe("Renamed");
});
it("update sends id and redirect URIs", async () => {
graphqlRequest.mockResolvedValueOnce({
updateSSOApp: mockSSOApp,
});
await runCommand(ssoCommand, [
"update",
"sso_app_1",
"--redirect-uri",
"https://new.example.com/callback",
]);
const call = graphqlRequest.mock.calls[0][0];
expect(call.variables.input.id).toBe("sso_app_1");
expect(call.variables.input.redirectUris).toEqual([
"https://new.example.com/callback",
]);
});
it("update rejects with no options", async () => {
const original = process.exitCode;
await runCommand(ssoCommand, ["update", "sso_app_1"]);
expect(process.exitCode).toBe(1);
process.exitCode = original;
});
it("create rejects with no redirect URIs", async () => {
const original = process.exitCode;
await runCommand(ssoCommand, ["create", "--name", "No URIs"]);
expect(process.exitCode).toBe(1);
expect(graphqlRequest).not.toHaveBeenCalled();
process.exitCode = original;
});
it("delete sends id", async () => {
graphqlRequest.mockResolvedValueOnce({ deleteSSOApp: "sso_app_1" });
await runCommand(ssoCommand, ["delete", "sso_app_1"]);
expect(graphqlRequest).toHaveBeenCalledWith(
expect.objectContaining({
variables: { input: { id: "sso_app_1" } },
})
);
});
it("list handles errors gracefully", async () => {
graphqlRequest.mockRejectedValueOnce(new Error("Unauthorized"));
const original = process.exitCode;
await runCommand(ssoCommand, ["list"]);
expect(process.exitCode).toBe(1);
process.exitCode = original;
});
it("create handles errors gracefully", async () => {
graphqlRequest.mockRejectedValueOnce(new Error("Duplicate name"));
const original = process.exitCode;
await runCommand(ssoCommand, [
"create",
"--name",
"Bad",
"--redirect-uri",
"https://example.com",
]);
expect(process.exitCode).toBe(1);
process.exitCode = original;
});
it("update handles errors gracefully", async () => {
graphqlRequest.mockRejectedValueOnce(new Error("Not found"));
const original = process.exitCode;
await runCommand(ssoCommand, ["update", "sso_bad", "--name", "test"]);
expect(process.exitCode).toBe(1);
process.exitCode = original;
});
it("delete handles errors gracefully", async () => {
graphqlRequest.mockRejectedValueOnce(new Error("Not found"));
const original = process.exitCode;
await runCommand(ssoCommand, ["delete", "sso_bad"]);
expect(process.exitCode).toBe(1);
process.exitCode = original;
});
});