fix(repositories): throw proper errors when deleting snapshots

This commit is contained in:
Nicolas Meienberger 2026-02-25 18:15:36 +01:00 committed by Nico
parent 9bfbf71447
commit 2ce225f7d1
2 changed files with 71 additions and 0 deletions

View file

@ -240,4 +240,31 @@ describe("repositories updates", () => {
expect(res.status).toBe(400); expect(res.status).toBe(400);
}); });
describe("delete snapshot", () => {
test("should return 500 when restic deleteSnapshot throws ResticError", async () => {
const { token, organizationId } = await createTestSession();
const repository = await createRepositoryRecord(organizationId);
const { restic } = await import("~/server/utils/restic");
const { ResticError } = await import("~/server/utils/errors");
const originalDeleteSnapshot = restic.deleteSnapshot;
// Mock it to throw an error
restic.deleteSnapshot = async () => {
throw new ResticError(1, "Fatal: unexpected HTTP response (403): 403 Forbidden");
};
const res = await app.request(`/api/v1/repositories/${repository.shortId}/snapshots/snap123`, {
method: "DELETE",
headers: getAuthHeaders(token),
});
restic.deleteSnapshot = originalDeleteSnapshot;
expect(res.status).toBe(500);
const body = await res.json();
expect(body.message).toContain("Command failed");
});
});
}); });

View file

@ -12,8 +12,28 @@ import { restic } from "~/server/utils/restic";
import { createTestSession } from "~/test/helpers/auth"; import { createTestSession } from "~/test/helpers/auth";
import { createTestBackupSchedule } from "~/test/helpers/backup"; import { createTestBackupSchedule } from "~/test/helpers/backup";
import { cache, cacheKeys } from "~/server/utils/cache"; import { cache, cacheKeys } from "~/server/utils/cache";
import { ResticError } from "~/server/utils/errors";
import { repositoriesService } from "../repositories.service"; import { repositoriesService } from "../repositories.service";
const createTestRepository = async (organizationId: string) => {
const id = randomUUID();
const shortId = generateShortId();
const [repository] = await db
.insert(repositoriesTable)
.values({
id,
shortId,
name: `Test-${randomUUID()}`,
type: "local",
config: { backend: "local", path: "/tmp" },
compressionMode: "auto",
status: "healthy",
organizationId,
})
.returning();
return repository;
};
describe("repositoriesService.createRepository", () => { describe("repositoriesService.createRepository", () => {
const initMock = mock(() => Promise.resolve({ success: true, error: null })); const initMock = mock(() => Promise.resolve({ success: true, error: null }));
@ -371,3 +391,27 @@ describe("repositoriesService.getRetentionCategories", () => {
expect(forgetSpy).toHaveBeenCalledTimes(2); expect(forgetSpy).toHaveBeenCalledTimes(2);
}); });
}); });
describe("repositoriesService.deleteSnapshot", () => {
afterEach(() => {
mock.restore();
});
test("should throw original error when restic deleteSnapshot fails", async () => {
const { organizationId, user } = await createTestSession();
const repository = await createTestRepository(organizationId);
const originalDeleteSnapshot = restic.deleteSnapshot;
restic.deleteSnapshot = mock(async () => {
throw new ResticError(1, "Fatal: unexpected HTTP response (403): 403 Forbidden");
});
await expect(
withContext({ organizationId, userId: user.id }, () =>
repositoriesService.deleteSnapshot(repository.shortId, "snap123"),
),
).rejects.toThrow("Fatal: unexpected HTTP response");
restic.deleteSnapshot = originalDeleteSnapshot;
});
});