feat(realdebrid): add multi-account settings model
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { defaultSettings } from "../src/main/constants";
|
||||
import { logger } from "../src/main/logger";
|
||||
import { collectAccountStatusRedactionValues, sanitizeAccountStatusText } from "../src/main/account-status-sanitizer";
|
||||
import {
|
||||
configureCredentialProtector,
|
||||
CredentialProtector,
|
||||
@@ -31,6 +32,7 @@ describe("credential protection", () => {
|
||||
...defaultSettings(),
|
||||
rememberToken: true,
|
||||
token: "value-to-protect",
|
||||
realDebridApiTokens: "first-real-debrid-token\nsecond-real-debrid-token",
|
||||
megaLogin: "account@example.invalid",
|
||||
megaPassword: "password-value"
|
||||
};
|
||||
@@ -38,10 +40,13 @@ describe("credential protection", () => {
|
||||
const persisted = protectPersistedSettings(input);
|
||||
|
||||
expect(persisted.token).not.toBe(input.token);
|
||||
expect(persisted.realDebridApiTokens).not.toBe(input.realDebridApiTokens);
|
||||
expect(persisted.megaLogin).not.toBe(input.megaLogin);
|
||||
expect(JSON.stringify(persisted)).not.toContain(input.token);
|
||||
expect(JSON.stringify(persisted)).not.toContain("first-real-debrid-token");
|
||||
expect(restorePersistedSettings(persisted)).toMatchObject({
|
||||
token: input.token,
|
||||
realDebridApiTokens: input.realDebridApiTokens,
|
||||
megaLogin: input.megaLogin,
|
||||
megaPassword: input.megaPassword
|
||||
});
|
||||
@@ -135,6 +140,7 @@ describe("credential protection", () => {
|
||||
...defaultSettings(),
|
||||
rememberToken: true,
|
||||
token: "value-to-protect",
|
||||
realDebridApiTokens: "pool-value-to-protect",
|
||||
megaDebridApiCredentials: "account@example.invalid:password-value",
|
||||
debridLinkApiKeys: "key-value"
|
||||
};
|
||||
@@ -143,11 +149,28 @@ describe("credential protection", () => {
|
||||
const serialized = JSON.stringify(projected);
|
||||
|
||||
expect(serialized).not.toContain(input.token);
|
||||
expect(serialized).not.toContain(input.realDebridApiTokens);
|
||||
expect(serialized).not.toContain("account@example.invalid");
|
||||
expect(serialized).not.toContain("password-value");
|
||||
expect(serialized).not.toContain("key-value");
|
||||
expect(projected.token).not.toBe("");
|
||||
expect(projected.realDebridApiTokens).not.toBe("");
|
||||
expect(projected.megaDebridApiCredentials).not.toBe("");
|
||||
expect(projected.debridLinkApiKeys).not.toBe("");
|
||||
});
|
||||
|
||||
it("redacts every Real-Debrid pool token from account status text", () => {
|
||||
const settings = {
|
||||
...defaultSettings(),
|
||||
realDebridApiTokens: "first-private-token\nsecond-private-token"
|
||||
};
|
||||
const redactions = collectAccountStatusRedactionValues(settings);
|
||||
const sanitized = sanitizeAccountStatusText(
|
||||
"Unrestrict first-private-token schlug fehl; Fallback second-private-token ebenfalls",
|
||||
redactions
|
||||
);
|
||||
|
||||
expect(sanitized).not.toContain("first-private-token");
|
||||
expect(sanitized).not.toContain("second-private-token");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
import crypto from "node:crypto";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
getRealDebridAccountIds,
|
||||
getRealDebridAccounts,
|
||||
getRealDebridApiAccountId,
|
||||
parseRealDebridApiAccounts,
|
||||
serializeRealDebridApiAccounts
|
||||
} from "../src/shared/real-debrid-accounts";
|
||||
|
||||
describe("Real-Debrid account pool", () => {
|
||||
it("parses distinct API tokens and removes exact duplicates", () => {
|
||||
const accounts = parseRealDebridApiAccounts("first-secret-token\r\nsecond-secret-token\nfirst-secret-token");
|
||||
|
||||
expect(accounts).toHaveLength(2);
|
||||
expect(accounts.map((entry) => entry.token)).toEqual(["first-secret-token", "second-secret-token"]);
|
||||
expect(accounts.map((entry) => entry.label)).toEqual(["API-Token 1", "API-Token 2"]);
|
||||
});
|
||||
|
||||
it("creates stable opaque IDs without secret material", () => {
|
||||
const token = "private-real-debrid-token";
|
||||
const first = getRealDebridApiAccountId(token);
|
||||
const second = getRealDebridApiAccountId(` ${token} `);
|
||||
|
||||
expect(first).toBe(second);
|
||||
expect(first).toMatch(/^rda_[a-z0-9]+$/);
|
||||
expect(first).not.toContain(token);
|
||||
expect(parseRealDebridApiAccounts(token)[0]).toMatchObject({ id: first, kind: "api" });
|
||||
expect(parseRealDebridApiAccounts(token)[0].label).not.toContain(token);
|
||||
expect(parseRealDebridApiAccounts(token)[0].maskedLogin).not.toContain(token);
|
||||
});
|
||||
|
||||
it.each([
|
||||
"cryptographic-real-debrid-token",
|
||||
"üñîçødé-real-debrid-token",
|
||||
"x".repeat(160)
|
||||
])("derives API account IDs from a cryptographic SHA-256 fingerprint", (token) => {
|
||||
const digest = crypto.createHash("sha256").update(token, "utf8").digest("hex").slice(0, 32);
|
||||
|
||||
expect(getRealDebridApiAccountId(token)).toBe(`rda_${digest}`);
|
||||
});
|
||||
|
||||
it("serializes normalized unique API tokens one per line", () => {
|
||||
expect(serializeRealDebridApiAccounts([
|
||||
" first-secret-token ",
|
||||
"",
|
||||
"second-secret-token",
|
||||
"first-secret-token"
|
||||
])).toBe("first-secret-token\nsecond-secret-token");
|
||||
});
|
||||
|
||||
it("combines API and opaque Web accounts and marks disabled entries", () => {
|
||||
const apiId = getRealDebridApiAccountId("api-secret");
|
||||
const settings = {
|
||||
realDebridApiTokens: "api-secret",
|
||||
realDebridWebAccountIds: ["rdw_legacy", "rdw_second"],
|
||||
realDebridDisabledAccountIds: [apiId, "rdw_second"]
|
||||
};
|
||||
|
||||
expect(getRealDebridAccounts(settings)).toEqual([
|
||||
expect.objectContaining({ id: apiId, kind: "api", enabled: false }),
|
||||
expect.objectContaining({ id: "rdw_legacy", kind: "web", enabled: true }),
|
||||
expect.objectContaining({ id: "rdw_second", kind: "web", enabled: false })
|
||||
]);
|
||||
expect(getRealDebridAccountIds(settings)).toEqual([apiId, "rdw_legacy", "rdw_second"]);
|
||||
});
|
||||
});
|
||||
+168
-5
@@ -4,7 +4,8 @@ import path from "node:path";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { parseDebridLinkApiKeys } from "../src/shared/debrid-link-keys";
|
||||
import { getMegaDebridAccountId } from "../src/shared/mega-debrid-accounts";
|
||||
import { getProviderUsageDayKey } from "../src/shared/provider-daily-limits";
|
||||
import { getProviderUsageDayKey } from "../src/shared/provider-daily-limits";
|
||||
import { getRealDebridApiAccountId } from "../src/shared/real-debrid-accounts";
|
||||
import { AppSettings } from "../src/shared/types";
|
||||
import { defaultSettings } from "../src/main/constants";
|
||||
import { configureCredentialProtector } from "../src/main/credential-protection";
|
||||
@@ -659,9 +660,168 @@ describe("settings storage", () => {
|
||||
expect(normalizedDisabled.realDebridUseWebLogin).toBe(false);
|
||||
});
|
||||
|
||||
it("keeps the Real-Debrid service status while web login remains configured", () => {
|
||||
it("migrates legacy Real-Debrid API and Web accounts with their existing status", () => {
|
||||
const checkedAt = Date.now();
|
||||
const apiToken = "legacy-real-debrid-token";
|
||||
const apiId = getRealDebridApiAccountId(apiToken);
|
||||
const legacyApi = {
|
||||
...defaultSettings(),
|
||||
token: apiToken,
|
||||
debridAccountStatuses: {
|
||||
"svc-realdebrid": {
|
||||
accountId: "svc-realdebrid",
|
||||
provider: "realdebrid",
|
||||
label: "Real-Debrid",
|
||||
maskedLogin: "API-Token",
|
||||
valid: true,
|
||||
isPremium: true,
|
||||
premiumUntilMs: checkedAt + 1000,
|
||||
username: "api-user",
|
||||
message: "Premium aktiv",
|
||||
checkedAt
|
||||
}
|
||||
}
|
||||
} as Partial<AppSettings>;
|
||||
delete legacyApi.realDebridApiTokens;
|
||||
delete legacyApi.realDebridWebAccountIds;
|
||||
const normalizedApi = normalizeSettings(legacyApi as AppSettings);
|
||||
|
||||
expect(normalizedApi.realDebridApiTokens).toBe(apiToken);
|
||||
expect(normalizedApi.realDebridWebAccountIds).toEqual([]);
|
||||
expect(normalizedApi.debridAccountStatuses[apiId]).toMatchObject({
|
||||
accountId: apiId,
|
||||
provider: "realdebrid",
|
||||
username: "api-user"
|
||||
});
|
||||
expect(normalizedApi.debridAccountStatuses["svc-realdebrid"]).toBeUndefined();
|
||||
|
||||
const legacyWeb = {
|
||||
...defaultSettings(),
|
||||
realDebridUseWebLogin: true,
|
||||
debridAccountStatuses: {
|
||||
"svc-realdebrid": {
|
||||
accountId: "svc-realdebrid",
|
||||
provider: "realdebrid",
|
||||
label: "Real-Debrid",
|
||||
maskedLogin: "Browser-Login",
|
||||
valid: true,
|
||||
isPremium: true,
|
||||
premiumUntilMs: checkedAt + 1000,
|
||||
username: "web-user",
|
||||
message: "Premium aktiv",
|
||||
checkedAt
|
||||
}
|
||||
}
|
||||
} as Partial<AppSettings>;
|
||||
delete legacyWeb.realDebridApiTokens;
|
||||
delete legacyWeb.realDebridWebAccountIds;
|
||||
const normalizedWeb = normalizeSettings(legacyWeb as AppSettings);
|
||||
|
||||
expect(normalizedWeb.realDebridApiTokens).toBe("");
|
||||
expect(normalizedWeb.realDebridWebAccountIds).toEqual(["rdw_legacy"]);
|
||||
expect(normalizedWeb.debridAccountStatuses.rdw_legacy).toMatchObject({
|
||||
accountId: "rdw_legacy",
|
||||
provider: "realdebrid",
|
||||
username: "web-user"
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps authoritative empty Real-Debrid pools empty instead of restoring legacy accounts", () => {
|
||||
const normalized = normalizeSettings({
|
||||
...defaultSettings(),
|
||||
token: "deleted-legacy-api-token",
|
||||
realDebridUseWebLogin: true,
|
||||
realDebridApiTokens: "",
|
||||
realDebridWebAccountIds: []
|
||||
});
|
||||
|
||||
expect(normalized.realDebridApiTokens).toBe("");
|
||||
expect(normalized.realDebridWebAccountIds).toEqual([]);
|
||||
});
|
||||
|
||||
it("prefers a concrete Real-Debrid status over the legacy status regardless of object order", () => {
|
||||
const token = "status-order-token";
|
||||
const accountId = getRealDebridApiAccountId(token);
|
||||
const checkedAt = Date.now();
|
||||
const legacyStatus = {
|
||||
accountId: "svc-realdebrid",
|
||||
provider: "realdebrid" as const,
|
||||
label: "Legacy",
|
||||
maskedLogin: "Legacy",
|
||||
valid: false,
|
||||
isPremium: false,
|
||||
premiumUntilMs: null,
|
||||
username: "legacy-user",
|
||||
message: "Legacy status",
|
||||
checkedAt: checkedAt - 1000
|
||||
};
|
||||
const concreteStatus = {
|
||||
...legacyStatus,
|
||||
accountId,
|
||||
label: "Concrete",
|
||||
valid: true,
|
||||
isPremium: true,
|
||||
username: "concrete-user",
|
||||
message: "Concrete status",
|
||||
checkedAt
|
||||
};
|
||||
const normalizeWithOrder = (entries: [string, typeof legacyStatus][]) => normalizeSettings({
|
||||
...defaultSettings(),
|
||||
realDebridApiTokens: token,
|
||||
debridAccountStatuses: Object.fromEntries(entries)
|
||||
}).debridAccountStatuses[accountId];
|
||||
|
||||
expect(normalizeWithOrder([["svc-realdebrid", legacyStatus], [accountId, concreteStatus]])).toMatchObject({
|
||||
valid: true,
|
||||
username: "concrete-user"
|
||||
});
|
||||
expect(normalizeWithOrder([[accountId, concreteStatus], ["svc-realdebrid", legacyStatus]])).toMatchObject({
|
||||
valid: true,
|
||||
username: "concrete-user"
|
||||
});
|
||||
});
|
||||
|
||||
it("normalizes the Real-Debrid pool idempotently and prunes stale account maps", () => {
|
||||
const apiId = getRealDebridApiAccountId("api-token");
|
||||
const today = getProviderUsageDayKey();
|
||||
const once = normalizeSettings({
|
||||
...defaultSettings(),
|
||||
realDebridApiTokens: "api-token\napi-token\nsecond-token",
|
||||
realDebridWebAccountIds: ["rdw_legacy", "rdw_second", "broken", "rdw_second"],
|
||||
realDebridDisabledAccountIds: [apiId, "rdw_second", "stale"],
|
||||
realDebridAccountDailyLimitBytes: { [apiId]: 1000, rdw_second: 2000, stale: 3000 },
|
||||
realDebridAccountDailyUsageBytes: { [apiId]: 4000, stale: 5000 },
|
||||
realDebridAccountTotalUsageBytes: { rdw_second: 6000, stale: 7000 },
|
||||
providerDailyUsageDay: today
|
||||
});
|
||||
const twice = normalizeSettings(once);
|
||||
|
||||
expect(once.realDebridApiTokens).toBe("api-token\nsecond-token");
|
||||
expect(once.realDebridWebAccountIds).toEqual(["rdw_legacy", "rdw_second"]);
|
||||
expect(once.realDebridDisabledAccountIds).toEqual([apiId, "rdw_second"]);
|
||||
expect(once.realDebridAccountDailyLimitBytes).toEqual({ [apiId]: 1000, rdw_second: 2000 });
|
||||
expect(once.realDebridAccountDailyUsageBytes).toEqual({ [apiId]: 4000 });
|
||||
expect(once.realDebridAccountTotalUsageBytes).toEqual({ rdw_second: 6000 });
|
||||
expect(twice).toEqual(once);
|
||||
});
|
||||
|
||||
it("resets stale per-account Real-Debrid daily usage", () => {
|
||||
const apiId = getRealDebridApiAccountId("api-token");
|
||||
const normalized = normalizeSettings({
|
||||
...defaultSettings(),
|
||||
realDebridApiTokens: "api-token",
|
||||
providerDailyUsageDay: "2001-01-01",
|
||||
realDebridAccountDailyUsageBytes: { [apiId]: 4000 },
|
||||
realDebridAccountTotalUsageBytes: { [apiId]: 9000 }
|
||||
});
|
||||
|
||||
expect(normalized.realDebridAccountDailyUsageBytes).toEqual({});
|
||||
expect(normalized.realDebridAccountTotalUsageBytes).toEqual({ [apiId]: 9000 });
|
||||
});
|
||||
|
||||
it("moves the Real-Debrid service status to the migrated Web account", () => {
|
||||
const checkedAt = Date.now();
|
||||
const legacy = {
|
||||
...defaultSettings(),
|
||||
realDebridUseWebLogin: true,
|
||||
debridAccountStatuses: {
|
||||
@@ -679,10 +839,13 @@ describe("settings storage", () => {
|
||||
checkedAt
|
||||
}
|
||||
}
|
||||
});
|
||||
} as Partial<AppSettings>;
|
||||
delete legacy.realDebridApiTokens;
|
||||
delete legacy.realDebridWebAccountIds;
|
||||
const normalized = normalizeSettings(legacy as AppSettings);
|
||||
|
||||
expect(normalized.debridAccountStatuses["svc-realdebrid"]).toMatchObject({
|
||||
accountId: "svc-realdebrid",
|
||||
expect(normalized.debridAccountStatuses.rdw_legacy).toMatchObject({
|
||||
accountId: "rdw_legacy",
|
||||
provider: "realdebrid",
|
||||
valid: true,
|
||||
username: "web-user",
|
||||
|
||||
@@ -55,8 +55,14 @@ function createSettings(): AppSettings {
|
||||
const megaAccountId = getMegaDebridAccountId(megaLogin);
|
||||
const debridLinkKeys = parseDebridLinkApiKeys(debridLinkApiKeys);
|
||||
return {
|
||||
token: "visual-real-debrid-token",
|
||||
realDebridUseWebLogin: false,
|
||||
token: "visual-real-debrid-token",
|
||||
realDebridUseWebLogin: false,
|
||||
realDebridApiTokens: "visual-real-debrid-token",
|
||||
realDebridWebAccountIds: [],
|
||||
realDebridDisabledAccountIds: [],
|
||||
realDebridAccountDailyLimitBytes: {},
|
||||
realDebridAccountDailyUsageBytes: {},
|
||||
realDebridAccountTotalUsageBytes: {},
|
||||
megaLogin,
|
||||
megaPassword: "visual-password",
|
||||
language: "de",
|
||||
|
||||
Reference in New Issue
Block a user