File size: 2,585 Bytes
783fcb6
7f2cbc9
783fcb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f2cbc9
 
 
 
 
 
 
 
 
 
 
 
 
8bcc42c
 
 
 
 
 
7f2cbc9
 
 
 
b424a2e
 
 
 
 
 
 
 
 
 
 
783fcb6
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
import { describe, expect, it } from "vitest";
import { getAuthErrorMessage, getCallbackErrorFromUrl, toUserFacingAuthError } from "./errorUtils";

describe("errorUtils", () => {
  it("normalizes invalid state messages for users", () => {
    expect(toUserFacingAuthError("invalid state")).toContain("Retry login");
  });

  it("returns null when callback URL has no auth error params", () => {
    expect(getCallbackErrorFromUrl("https://toolkit.masterstelecom.com/")).toBeNull();
  });

  it("decodes callback error descriptions", () => {
    const out = getCallbackErrorFromUrl(
      "https://toolkit.masterstelecom.com/?error=invalid_request&error_description=Callback%20URL%20mismatch"
    );
    expect(out).toBe("Callback URL mismatch");
  });

  it("handles malformed encoded callback messages safely", () => {
    const out = getCallbackErrorFromUrl(
      "https://toolkit.masterstelecom.com/?error=invalid_request&error_description=%E0%A4%A"
    );
    expect(out).toBeTruthy();
  });

  it("supports auth errors returned in URL hash fragments", () => {
    const out = getCallbackErrorFromUrl(
      "https://toolkit.masterstelecom.com/#error=access_denied&error_description=Client+blocked"
    );
    expect(out).toContain("Client blocked");
  });

  it("normalizes raw access_denied and invalid_request tokens", () => {
    expect(toUserFacingAuthError("access_denied")).toContain("Access denied by Auth0 policy");
    expect(toUserFacingAuthError("invalid_request")).toContain("Authentication request is invalid");
  });

  it("surfaces explicit guidance for the removed masters-toolkit-api audience", () => {
    expect(toUserFacingAuthError("Service not found: https://masters-toolkit-api/")).toContain(
      "Remove `VITE_AUTH0_AUDIENCE`/`AUTH0_AUDIENCE`"
    );
  });

  it("extracts useful message fields from Auth0 error objects", () => {
    expect(getAuthErrorMessage({ error: "access_denied", error_description: "domain blocked" })).toBe("domain blocked");
    expect(getAuthErrorMessage({ cause: { message: "callback mismatch" } })).toBe("callback mismatch");
  });

  it("redacts sensitive query params and token-like strings", () => {
    const msg = getAuthErrorMessage({
      error_description:
        "invalid_request code=abc123 state=xyz789 Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.aaa.bbb sk-proj-1234567890abcdefghijklmnop",
    });
    expect(msg).toContain("code=[redacted]");
    expect(msg).toContain("state=[redacted]");
    expect(msg).toMatch(/Bearer \[(redacted|redacted-jwt)\]/);
    expect(msg).toContain("[redacted-key]");
  });
});