|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 2 | +import { Command } from "commander"; |
| 3 | +import { registerLoginCommand } from "./login"; |
| 4 | +import { AccountService } from "../api/client/services/AccountService"; |
| 5 | + |
| 6 | +vi.mock("../api/client/services/AccountService"); |
| 7 | +vi.mock("../utils/credentials", () => ({ |
| 8 | + saveCredentials: vi.fn(), |
| 9 | + getCredentialsPath: vi.fn(() => "/home/test/.codacy/credentials"), |
| 10 | + promptForToken: vi.fn(), |
| 11 | +})); |
| 12 | + |
| 13 | +vi.spyOn(console, "log").mockImplementation(() => {}); |
| 14 | +vi.spyOn(console, "error").mockImplementation(() => {}); |
| 15 | + |
| 16 | +import { saveCredentials, promptForToken } from "../utils/credentials"; |
| 17 | + |
| 18 | +function createProgram(): Command { |
| 19 | + const program = new Command(); |
| 20 | + program.exitOverride(); |
| 21 | + registerLoginCommand(program); |
| 22 | + return program; |
| 23 | +} |
| 24 | + |
| 25 | +const mockUser = { |
| 26 | + name: "Test User", |
| 27 | + mainEmail: "test@example.com", |
| 28 | + otherEmails: [], |
| 29 | + isAdmin: false, |
| 30 | + isActive: true, |
| 31 | +}; |
| 32 | + |
| 33 | +describe("login command", () => { |
| 34 | + beforeEach(() => { |
| 35 | + vi.clearAllMocks(); |
| 36 | + }); |
| 37 | + |
| 38 | + it("should validate and store token via --token flag", async () => { |
| 39 | + vi.mocked(AccountService.getUser).mockResolvedValue({ data: mockUser }); |
| 40 | + |
| 41 | + const program = createProgram(); |
| 42 | + await program.parseAsync(["node", "test", "login", "--token", "my-token"]); |
| 43 | + |
| 44 | + expect(AccountService.getUser).toHaveBeenCalledOnce(); |
| 45 | + expect(saveCredentials).toHaveBeenCalledWith("my-token"); |
| 46 | + expect(console.log).toHaveBeenCalledWith( |
| 47 | + expect.stringContaining("Token stored at"), |
| 48 | + ); |
| 49 | + }); |
| 50 | + |
| 51 | + it("should show error on invalid token (401)", async () => { |
| 52 | + const apiError = new Error("Unauthorized"); |
| 53 | + (apiError as any).status = 401; |
| 54 | + vi.mocked(AccountService.getUser).mockRejectedValue(apiError); |
| 55 | + |
| 56 | + const mockExit = vi.spyOn(process, "exit").mockImplementation(() => { |
| 57 | + throw new Error("process.exit called"); |
| 58 | + }); |
| 59 | + |
| 60 | + const program = createProgram(); |
| 61 | + await expect( |
| 62 | + program.parseAsync(["node", "test", "login", "--token", "bad-token"]), |
| 63 | + ).rejects.toThrow("process.exit called"); |
| 64 | + |
| 65 | + expect(saveCredentials).not.toHaveBeenCalled(); |
| 66 | + mockExit.mockRestore(); |
| 67 | + }); |
| 68 | + |
| 69 | + it("should show network error when API is unreachable", async () => { |
| 70 | + vi.mocked(AccountService.getUser).mockRejectedValue( |
| 71 | + new Error("fetch failed"), |
| 72 | + ); |
| 73 | + |
| 74 | + const mockExit = vi.spyOn(process, "exit").mockImplementation(() => { |
| 75 | + throw new Error("process.exit called"); |
| 76 | + }); |
| 77 | + |
| 78 | + const program = createProgram(); |
| 79 | + await expect( |
| 80 | + program.parseAsync(["node", "test", "login", "--token", "some-token"]), |
| 81 | + ).rejects.toThrow("process.exit called"); |
| 82 | + |
| 83 | + expect(saveCredentials).not.toHaveBeenCalled(); |
| 84 | + mockExit.mockRestore(); |
| 85 | + }); |
| 86 | + |
| 87 | + it("should use interactive prompt when no --token flag", async () => { |
| 88 | + vi.mocked(promptForToken).mockResolvedValue("prompted-token"); |
| 89 | + vi.mocked(AccountService.getUser).mockResolvedValue({ data: mockUser }); |
| 90 | + |
| 91 | + const program = createProgram(); |
| 92 | + await program.parseAsync(["node", "test", "login"]); |
| 93 | + |
| 94 | + expect(promptForToken).toHaveBeenCalledWith("API Token: "); |
| 95 | + expect(saveCredentials).toHaveBeenCalledWith("prompted-token"); |
| 96 | + }); |
| 97 | + |
| 98 | + it("should reject empty token from interactive prompt", async () => { |
| 99 | + vi.mocked(promptForToken).mockResolvedValue(" "); |
| 100 | + |
| 101 | + const mockExit = vi.spyOn(process, "exit").mockImplementation(() => { |
| 102 | + throw new Error("process.exit called"); |
| 103 | + }); |
| 104 | + |
| 105 | + const program = createProgram(); |
| 106 | + await expect( |
| 107 | + program.parseAsync(["node", "test", "login"]), |
| 108 | + ).rejects.toThrow("process.exit called"); |
| 109 | + |
| 110 | + expect(saveCredentials).not.toHaveBeenCalled(); |
| 111 | + mockExit.mockRestore(); |
| 112 | + }); |
| 113 | +}); |
0 commit comments