127 lines
4.2 KiB
TypeScript
127 lines
4.2 KiB
TypeScript
import { afterEach, describe, expect, test, vi } from "vitest";
|
|
import { parseRedisUrl, resolveRedisConnectionOptions, resolveRedisConnectionOptionsFromProcessEnv } from "./connection.js";
|
|
|
|
const PASSWORD_REDIS_URL = "redis://user:super-secret-password@redis.example.com:6380/2";
|
|
|
|
function expectRedacted(error: unknown) {
|
|
const serialized = JSON.stringify(error, Object.getOwnPropertyNames(error));
|
|
|
|
expect(serialized).not.toContain(PASSWORD_REDIS_URL);
|
|
expect(serialized).not.toContain("super-secret-password");
|
|
}
|
|
|
|
describe("resolveRedisConnectionOptions", () => {
|
|
afterEach(() => {
|
|
vi.unstubAllEnvs();
|
|
});
|
|
|
|
test("prefers BULLMQ_REDIS_URL over REDIS_URL", () => {
|
|
expect(
|
|
resolveRedisConnectionOptions({
|
|
env: {
|
|
BULLMQ_REDIS_URL: "rediss://bullmq.example.com:6380/1",
|
|
REDIS_URL: "redis://redis.example.com:6379/0",
|
|
},
|
|
})
|
|
).toMatchObject({
|
|
host: "bullmq.example.com",
|
|
port: 6380,
|
|
db: 1,
|
|
tls: {},
|
|
maxRetriesPerRequest: null,
|
|
});
|
|
});
|
|
|
|
test("falls back to REDIS_URL when BULLMQ_REDIS_URL is absent", () => {
|
|
expect(resolveRedisConnectionOptions({ env: { REDIS_URL: "redis://redis.example.com:6381/3" } })).toMatchObject({
|
|
host: "redis.example.com",
|
|
port: 6381,
|
|
db: 3,
|
|
maxRetriesPerRequest: null,
|
|
});
|
|
});
|
|
|
|
test("returns undefined for missing optional Redis configuration", () => {
|
|
expect(resolveRedisConnectionOptions({ env: {} })).toBeUndefined();
|
|
});
|
|
|
|
test("throws a key-only error when required Redis configuration is missing", () => {
|
|
expect(() => resolveRedisConnectionOptions({ env: {}, required: true })).toThrowError(
|
|
"Set BULLMQ_REDIS_URL or REDIS_URL before starting queue workers."
|
|
);
|
|
});
|
|
|
|
test("rejects invalid Redis URLs and protocols without leaking URL values", () => {
|
|
let invalidUrlError: unknown;
|
|
try {
|
|
resolveRedisConnectionOptions({ env: { BULLMQ_REDIS_URL: "redis://:super-secret-password@" }, required: true });
|
|
} catch (error) {
|
|
invalidUrlError = error;
|
|
}
|
|
|
|
expect(invalidUrlError).toMatchObject({ message: "BULLMQ_REDIS_URL must be a valid Redis URL." });
|
|
expectRedacted(invalidUrlError);
|
|
|
|
let invalidProtocolError: unknown;
|
|
try {
|
|
resolveRedisConnectionOptions({ env: { BULLMQ_REDIS_URL: PASSWORD_REDIS_URL.replace("redis://", "https://") }, required: true });
|
|
} catch (error) {
|
|
invalidProtocolError = error;
|
|
}
|
|
|
|
expect(invalidProtocolError).toMatchObject({ message: "BULLMQ_REDIS_URL must use redis:// or rediss://." });
|
|
expectRedacted(invalidProtocolError);
|
|
});
|
|
|
|
test("does not implicitly fall back to process.env", () => {
|
|
vi.stubEnv("BULLMQ_REDIS_URL", "redis://process.example.com:6379/0");
|
|
|
|
expect(resolveRedisConnectionOptions()).toBeUndefined();
|
|
expect(resolveRedisConnectionOptions({ env: {} })).toBeUndefined();
|
|
});
|
|
|
|
test("keeps process.env access behind an explicit entrypoint helper", () => {
|
|
vi.stubEnv("BULLMQ_REDIS_URL", "redis://process.example.com:6382/4");
|
|
|
|
expect(resolveRedisConnectionOptionsFromProcessEnv()).toMatchObject({
|
|
host: "process.example.com",
|
|
port: 6382,
|
|
db: 4,
|
|
});
|
|
});
|
|
|
|
test("parses credentials without leaking password-bearing URLs in database errors", () => {
|
|
expect(parseRedisUrl(PASSWORD_REDIS_URL)).toMatchObject({
|
|
host: "redis.example.com",
|
|
username: "user",
|
|
password: "super-secret-password",
|
|
port: 6380,
|
|
db: 2,
|
|
});
|
|
|
|
let invalidDatabaseError: unknown;
|
|
try {
|
|
parseRedisUrl("BULLMQ_REDIS_URL", PASSWORD_REDIS_URL.replace("/2", "/nope"));
|
|
} catch (error) {
|
|
invalidDatabaseError = error;
|
|
}
|
|
|
|
expect(invalidDatabaseError).toMatchObject({ message: "BULLMQ_REDIS_URL database index must be a non-negative integer." });
|
|
expectRedacted(invalidDatabaseError);
|
|
});
|
|
|
|
test("defaults the port to 6379 and leaves db/tls unset when the URL omits them", () => {
|
|
const parsed = parseRedisUrl("redis://redis.internal");
|
|
|
|
expect(parsed).toMatchObject({
|
|
host: "redis.internal",
|
|
port: 6379,
|
|
maxRetriesPerRequest: null,
|
|
});
|
|
expect(parsed.db).toBeUndefined();
|
|
expect(parsed.tls).toBeUndefined();
|
|
expect(parsed.password).toBeUndefined();
|
|
expect(parsed.username).toBeUndefined();
|
|
});
|
|
});
|