CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | /**
* Block I10 — Enterprise SSO (OIDC) tests.
*
* Covers pure helpers (URL building, domain gating, username normalization)
* and route authorization smokes. The full OIDC dance against a real IdP is
* exercised by live integration — we don't mock fetch here.
*/
import { describe, it, expect } from "bun:test";
import app from "../app";
import {
buildAuthorizeUrl,
emailDomainAllowed,
randomToken,
ssoRedirectUri,
__internal,
} from "../lib/sso";
const { emptyToNull, normalizeUsername } = __internal;
describe("sso — buildAuthorizeUrl", () => {
const cfg = {
authorizationEndpoint: "https://idp.example.com/authorize",
clientId: "abc123",
scopes: "openid profile email",
};
it("includes all OIDC required params", () => {
const url = buildAuthorizeUrl(
cfg,
"state-xyz",
"nonce-abc",
"https://app.example.com/login/sso/callback"
);
const u = new URL(url);
expect(u.origin + u.pathname).toBe("https://idp.example.com/authorize");
expect(u.searchParams.get("client_id")).toBe("abc123");
expect(u.searchParams.get("response_type")).toBe("code");
expect(u.searchParams.get("scope")).toBe("openid profile email");
expect(u.searchParams.get("state")).toBe("state-xyz");
expect(u.searchParams.get("nonce")).toBe("nonce-abc");
expect(u.searchParams.get("redirect_uri")).toBe(
"https://app.example.com/login/sso/callback"
);
});
it("preserves existing query params on the endpoint", () => {
const url = buildAuthorizeUrl(
{
authorizationEndpoint: "https://idp.example.com/authorize?ext=1",
clientId: "abc",
scopes: "openid",
},
"s",
"n",
"https://app/cb"
);
const u = new URL(url);
expect(u.searchParams.get("ext")).toBe("1");
expect(u.searchParams.get("client_id")).toBe("abc");
});
it("throws when endpoint or client_id is missing", () => {
expect(() =>
buildAuthorizeUrl(
{ authorizationEndpoint: null, clientId: "x", scopes: "openid" } as any,
"s",
"n",
"https://app/cb"
)
).toThrow();
expect(() =>
buildAuthorizeUrl(
{
authorizationEndpoint: "https://i/a",
clientId: null,
scopes: "openid",
} as any,
"s",
"n",
"https://app/cb"
)
).toThrow();
});
it("falls back to default scopes when empty", () => {
const url = buildAuthorizeUrl(
{
authorizationEndpoint: "https://idp/a",
clientId: "c",
scopes: "" as any,
},
"s",
"n",
"https://app/cb"
);
expect(new URL(url).searchParams.get("scope")).toBe(
"openid profile email"
);
});
});
describe("sso — emailDomainAllowed", () => {
it("allows any when domains list is null", () => {
expect(emailDomainAllowed("a@example.com", null)).toBe(true);
});
it("allows any when list is empty", () => {
expect(emailDomainAllowed("a@example.com", "")).toBe(true);
expect(emailDomainAllowed("a@example.com", " ")).toBe(true);
});
it("accepts matching domain (case-insensitive)", () => {
expect(emailDomainAllowed("a@EXAMPLE.COM", "example.com")).toBe(true);
expect(emailDomainAllowed("a@example.com", "acme.io, example.com")).toBe(
true
);
});
it("rejects unmatched domain", () => {
expect(emailDomainAllowed("a@evil.com", "example.com")).toBe(false);
});
it("rejects missing email when list is set", () => {
expect(emailDomainAllowed(null, "example.com")).toBe(false);
expect(emailDomainAllowed("", "example.com")).toBe(false);
});
it("rejects malformed email", () => {
expect(emailDomainAllowed("not-an-email", "example.com")).toBe(false);
});
});
describe("sso — normalizeUsername", () => {
it("lowercases and slugifies", () => {
expect(normalizeUsername("Alice Smith")).toBe("alice-smith");
expect(normalizeUsername("BOB@acme.IO")).toBe("bob-acme-io");
});
it("strips leading/trailing dashes", () => {
expect(normalizeUsername("---foo---")).toBe("foo");
});
it("falls back to 'user' for empty input", () => {
expect(normalizeUsername("")).toBe("user");
expect(normalizeUsername("@@@")).toBe("user");
});
it("caps at 32 chars", () => {
const out = normalizeUsername("a".repeat(80));
expect(out.length).toBeLessThanOrEqual(32);
});
});
describe("sso — emptyToNull", () => {
it("returns null for empty or whitespace-only", () => {
expect(emptyToNull("")).toBeNull();
expect(emptyToNull(" ")).toBeNull();
expect(emptyToNull(null)).toBeNull();
expect(emptyToNull(undefined)).toBeNull();
});
it("trims and returns non-empty strings", () => {
expect(emptyToNull(" hello ")).toBe("hello");
expect(emptyToNull("x")).toBe("x");
});
});
describe("sso — randomToken", () => {
it("returns hex of expected length", () => {
expect(randomToken(8)).toMatch(/^[0-9a-f]{16}$/);
expect(randomToken(16)).toMatch(/^[0-9a-f]{32}$/);
});
it("returns distinct values across calls", () => {
expect(randomToken(16)).not.toBe(randomToken(16));
});
});
describe("sso — ssoRedirectUri", () => {
it("ends with /login/sso/callback", () => {
expect(ssoRedirectUri().endsWith("/login/sso/callback")).toBe(true);
});
});
describe("sso — route auth", () => {
it("GET /admin/sso without auth → 302 /login", async () => {
const res = await app.request("/admin/sso");
expect(res.status).toBe(302);
expect(res.headers.get("location") || "").toContain("/login");
});
it("POST /admin/sso without auth → 302 /login", async () => {
const res = await app.request("/admin/sso", {
method: "POST",
body: new URLSearchParams({ provider_name: "x" }),
headers: { "content-type": "application/x-www-form-urlencoded" },
});
expect(res.status).toBe(302);
expect(res.headers.get("location") || "").toContain("/login");
});
it("POST /settings/sso/unlink without auth → 302 /login", async () => {
const res = await app.request("/settings/sso/unlink", { method: "POST" });
expect(res.status).toBe(302);
expect(res.headers.get("location") || "").toContain("/login");
});
it("GET /login/sso when SSO not configured → 302 /login with error", async () => {
const res = await app.request("/login/sso");
expect(res.status).toBe(302);
const loc = res.headers.get("location") || "";
expect(loc).toContain("/login");
// Either "not enabled" or "not fully configured" — either proves the
// route is mounted and SSO guard fires.
expect(loc).toContain("error=");
});
it("GET /login/sso/callback without state cookie → 302 /login", async () => {
const res = await app.request(
"/login/sso/callback?code=abc&state=xyz"
);
expect(res.status).toBe(302);
expect(res.headers.get("location") || "").toContain("/login");
});
});
|