|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { createMockSpinner, runCommand } from "./helpers.js"; |
| 3 | + |
| 4 | +vi.mock("yocto-spinner", () => ({ default: () => createMockSpinner() })); |
| 5 | +vi.mock("../../src/lib/program.js", () => ({ |
| 6 | + program: { opts: () => ({}) }, |
| 7 | +})); |
| 8 | + |
| 9 | +const graphqlRequest = vi.fn(); |
| 10 | +vi.mock("../../src/lib/graphql-client.js", () => ({ |
| 11 | + graphqlRequest: (...args: unknown[]) => graphqlRequest(...args), |
| 12 | +})); |
| 13 | + |
| 14 | +const { providersCommand } = await import("../../src/commands/providers.js"); |
| 15 | + |
| 16 | +const mockProvider = { |
| 17 | + id: "sso_1", |
| 18 | + providerType: "GOOGLE", |
| 19 | + name: "Google", |
| 20 | + provider: "google", |
| 21 | + enabled: true, |
| 22 | + clientId: "client_123", |
| 23 | +}; |
| 24 | + |
| 25 | +describe("providers", () => { |
| 26 | + it("list fetches providers", async () => { |
| 27 | + graphqlRequest.mockResolvedValueOnce({ |
| 28 | + getSSOClients: [mockProvider], |
| 29 | + }); |
| 30 | + |
| 31 | + await runCommand(providersCommand, ["list"]); |
| 32 | + |
| 33 | + expect(graphqlRequest).toHaveBeenCalledWith( |
| 34 | + expect.objectContaining({ |
| 35 | + query: expect.stringContaining("getSSOClients"), |
| 36 | + }) |
| 37 | + ); |
| 38 | + }); |
| 39 | + |
| 40 | + it("configure sends type and options", async () => { |
| 41 | + graphqlRequest.mockResolvedValueOnce({ |
| 42 | + updateSSOClient: mockProvider, |
| 43 | + }); |
| 44 | + |
| 45 | + await runCommand(providersCommand, [ |
| 46 | + "configure", |
| 47 | + "--type", |
| 48 | + "GOOGLE", |
| 49 | + "--client-id", |
| 50 | + "my_client_id", |
| 51 | + "--client-secret", |
| 52 | + "my_secret", |
| 53 | + "--status", |
| 54 | + "enabled", |
| 55 | + ]); |
| 56 | + |
| 57 | + const call = graphqlRequest.mock.calls[0][0]; |
| 58 | + expect(call.variables.input.provider).toBe("google"); |
| 59 | + expect(call.variables.input.clientId).toBe("my_client_id"); |
| 60 | + expect(call.variables.input.clientSecret).toBe("my_secret"); |
| 61 | + expect(call.variables.input.enabled).toBe(true); |
| 62 | + }); |
| 63 | + |
| 64 | + it("configure sends type only", async () => { |
| 65 | + graphqlRequest.mockResolvedValueOnce({ |
| 66 | + updateSSOClient: mockProvider, |
| 67 | + }); |
| 68 | + |
| 69 | + await runCommand(providersCommand, ["configure", "--type", "GITHUB"]); |
| 70 | + |
| 71 | + const call = graphqlRequest.mock.calls[0][0]; |
| 72 | + expect(call.variables.input.provider).toBe("github"); |
| 73 | + expect(call.variables.input.clientId).toBeUndefined(); |
| 74 | + }); |
| 75 | + |
| 76 | + it("configure can disable a provider", async () => { |
| 77 | + graphqlRequest.mockResolvedValueOnce({ |
| 78 | + updateSSOClient: { ...mockProvider, enabled: false }, |
| 79 | + }); |
| 80 | + |
| 81 | + await runCommand(providersCommand, [ |
| 82 | + "configure", |
| 83 | + "--type", |
| 84 | + "GOOGLE", |
| 85 | + "--status", |
| 86 | + "disabled", |
| 87 | + ]); |
| 88 | + |
| 89 | + const call = graphqlRequest.mock.calls[0][0]; |
| 90 | + expect(call.variables.input.enabled).toBe(false); |
| 91 | + }); |
| 92 | + |
| 93 | + it("configure rejects enabling without client id", async () => { |
| 94 | + const original = process.exitCode; |
| 95 | + const callCountBefore = graphqlRequest.mock.calls.length; |
| 96 | + |
| 97 | + await runCommand(providersCommand, [ |
| 98 | + "configure", |
| 99 | + "--type", |
| 100 | + "GOOGLE", |
| 101 | + "--status", |
| 102 | + "enabled", |
| 103 | + "--client-secret", |
| 104 | + "my_secret", |
| 105 | + ]); |
| 106 | + |
| 107 | + expect(process.exitCode).toBe(1); |
| 108 | + expect(graphqlRequest.mock.calls.length).toBe(callCountBefore); |
| 109 | + process.exitCode = original; |
| 110 | + }); |
| 111 | + |
| 112 | + it("configure rejects enabling without client secret", async () => { |
| 113 | + const original = process.exitCode; |
| 114 | + const callCountBefore = graphqlRequest.mock.calls.length; |
| 115 | + |
| 116 | + await runCommand(providersCommand, [ |
| 117 | + "configure", |
| 118 | + "--type", |
| 119 | + "GOOGLE", |
| 120 | + "--status", |
| 121 | + "enabled", |
| 122 | + "--client-id", |
| 123 | + "my_client_id", |
| 124 | + ]); |
| 125 | + |
| 126 | + expect(process.exitCode).toBe(1); |
| 127 | + expect(graphqlRequest.mock.calls.length).toBe(callCountBefore); |
| 128 | + process.exitCode = original; |
| 129 | + }); |
| 130 | + |
| 131 | + it("remove sends id", async () => { |
| 132 | + graphqlRequest.mockResolvedValueOnce({ removeSSOClient: "sso_1" }); |
| 133 | + |
| 134 | + await runCommand(providersCommand, ["remove", "sso_1"]); |
| 135 | + |
| 136 | + expect(graphqlRequest).toHaveBeenCalledWith( |
| 137 | + expect.objectContaining({ |
| 138 | + variables: { input: { id: "sso_1" } }, |
| 139 | + }) |
| 140 | + ); |
| 141 | + }); |
| 142 | + |
| 143 | + it("rejects invalid type", async () => { |
| 144 | + const original = process.exitCode; |
| 145 | + try { |
| 146 | + await runCommand(providersCommand, ["configure", "--type", "INVALID"]); |
| 147 | + } catch { |
| 148 | + // Commander throws on invalid choices |
| 149 | + } |
| 150 | + process.exitCode = original; |
| 151 | + }); |
| 152 | + |
| 153 | + it("handles errors gracefully", async () => { |
| 154 | + graphqlRequest.mockRejectedValueOnce(new Error("Unauthorized")); |
| 155 | + |
| 156 | + const original = process.exitCode; |
| 157 | + await runCommand(providersCommand, ["list"]); |
| 158 | + expect(process.exitCode).toBe(1); |
| 159 | + process.exitCode = original; |
| 160 | + }); |
| 161 | +}); |
0 commit comments