Files
outline/server/routes/api/fileOperations/fileOperations.test.ts
T
Tom Moor 6ac407de84 chore: Run server tests with a shared module registry (#13799)
Adds a server-shared Vitest project with isolate disabled and moves every
server test into it by default. Files that mock or reset modules opt out
with a header marker and stay in the isolated project.

Restores env values that leaked between files: the test URL is reset after
each file, and tests that mutate env restore the previous value. The test
server no longer closes the shared sequelize pool.
2026-09-18 17:45:39 -04:00

596 lines
17 KiB
TypeScript

// @vitest-isolate true
import {
CollectionPermission,
FileOperationState,
FileOperationType,
} from "@shared/types";
import {
Collection,
User,
Event,
FileOperation,
UserMembership,
} from "@server/models";
import {
buildAdmin,
buildCollection,
buildDocument,
buildFileOperation,
buildTeam,
buildUser,
} from "@server/test/factories";
import { getTestServer } from "@server/test/support";
const server = getTestServer();
vi.mock("@server/storage/files");
describe("#fileOperations.info", () => {
it("should return fileOperation", async () => {
const team = await buildTeam();
const admin = await buildAdmin({
teamId: team.id,
});
const exportData = await buildFileOperation({
type: FileOperationType.Export,
teamId: team.id,
userId: admin.id,
});
const res = await server.post("/api/fileOperations.info", admin, {
body: {
id: exportData.id,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.id).toBe(exportData.id);
expect(body.data.state).toBe(exportData.state);
});
it("should allow user to read their own export", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const collection = await buildCollection({ teamId: team.id });
const exportData = await buildFileOperation({
type: FileOperationType.Export,
teamId: team.id,
userId: user.id,
collectionId: collection.id,
});
const res = await server.post("/api/fileOperations.info", user, {
body: {
id: exportData.id,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.id).toBe(exportData.id);
});
it("should not allow user to read their own export of a collection they lost access to", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const collection = await buildCollection({
teamId: team.id,
permission: null,
});
const exportData = await buildFileOperation({
type: FileOperationType.Export,
teamId: team.id,
userId: user.id,
collectionId: collection.id,
});
const res = await server.post("/api/fileOperations.info", user, {
body: {
id: exportData.id,
},
});
expect(res.status).toEqual(403);
});
it("should not allow demoted admin to read their own workspace export", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const exportData = await buildFileOperation({
type: FileOperationType.Export,
teamId: team.id,
userId: user.id,
});
const res = await server.post("/api/fileOperations.info", user, {
body: {
id: exportData.id,
},
});
expect(res.status).toEqual(403);
});
it("should not allow user to read another user's export", async () => {
const team = await buildTeam();
const admin = await buildAdmin({ teamId: team.id });
const user = await buildUser({ teamId: team.id });
const exportData = await buildFileOperation({
type: FileOperationType.Export,
teamId: team.id,
userId: admin.id,
});
const res = await server.post("/api/fileOperations.info", user, {
body: {
id: exportData.id,
},
});
expect(res.status).toEqual(403);
});
it("should allow admin to read another admin's export", async () => {
const team = await buildTeam();
const admin1 = await buildAdmin({ teamId: team.id });
const admin2 = await buildAdmin({ teamId: team.id });
const exportData = await buildFileOperation({
type: FileOperationType.Export,
teamId: team.id,
userId: admin1.id,
});
const res = await server.post("/api/fileOperations.info", admin2, {
body: {
id: exportData.id,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.id).toBe(exportData.id);
});
it("should allow admin to read an import file operation", async () => {
const team = await buildTeam();
const admin1 = await buildAdmin({ teamId: team.id });
const admin2 = await buildAdmin({ teamId: team.id });
const importOp = await buildFileOperation({
type: FileOperationType.Import,
teamId: team.id,
userId: admin1.id,
});
const res = await server.post("/api/fileOperations.info", admin2, {
body: {
id: importOp.id,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.id).toBe(importOp.id);
});
it("should require authorization", async () => {
const team = await buildTeam();
const admin = await buildAdmin();
await buildUser({
teamId: team.id,
});
const exportData = await buildFileOperation({
type: FileOperationType.Export,
teamId: team.id,
userId: admin.id,
});
const res = await server.post("/api/fileOperations.info", admin, {
body: {
id: exportData.id,
},
});
expect(res.status).toEqual(403);
});
});
describe("#fileOperations.list", () => {
it("should return fileOperations list", async () => {
const team = await buildTeam();
const admin = await buildAdmin({
teamId: team.id,
});
const exportData = await buildFileOperation({
type: FileOperationType.Export,
teamId: team.id,
userId: admin.id,
});
const res = await server.post("/api/fileOperations.list", admin, {
body: {
type: FileOperationType.Export,
},
});
const body = await res.json();
const data = body.data[0];
expect(res.status).toEqual(200);
expect(body.data.length).toBe(1);
expect(data.id).toBe(exportData.id);
expect(data.key).toBe(undefined);
expect(data.state).toBe(exportData.state);
});
it("should return exports with collection data", async () => {
const team = await buildTeam();
const admin = await buildAdmin({
teamId: team.id,
});
const collection = await buildCollection({
userId: admin.id,
teamId: team.id,
});
const exportData = await buildFileOperation({
type: FileOperationType.Export,
teamId: team.id,
userId: admin.id,
collectionId: collection.id,
});
const res = await server.post("/api/fileOperations.list", admin, {
body: {
type: FileOperationType.Export,
},
});
const body = await res.json();
const data = body.data[0];
expect(res.status).toEqual(200);
expect(body.data.length).toBe(1);
expect(data.id).toBe(exportData.id);
expect(data.key).toBe(undefined);
expect(data.state).toBe(exportData.state);
expect(data.collectionId).toBe(collection.id);
});
it("should return exports with collection data even if collection is deleted", async () => {
const team = await buildTeam();
const admin = await buildAdmin({
teamId: team.id,
});
const collection = await buildCollection({
userId: admin.id,
teamId: team.id,
});
const exportData = await buildFileOperation({
type: FileOperationType.Export,
teamId: team.id,
userId: admin.id,
collectionId: collection.id,
});
await collection.destroy({ hooks: false });
const isCollectionPresent = await Collection.findByPk(collection.id);
expect(isCollectionPresent).toBe(null);
const res = await server.post("/api/fileOperations.list", admin, {
body: {
type: FileOperationType.Export,
},
});
const body = await res.json();
const data = body.data[0];
expect(res.status).toEqual(200);
expect(body.data.length).toBe(1);
expect(data.id).toBe(exportData.id);
expect(data.key).toBe(undefined);
expect(data.state).toBe(exportData.state);
expect(data.collectionId).toBe(collection.id);
});
it("should return exports with user data even if user is deleted", async () => {
const team = await buildTeam();
const admin2 = await buildAdmin({
teamId: team.id,
});
const admin = await buildAdmin({
teamId: team.id,
});
const collection = await buildCollection({
userId: admin.id,
teamId: team.id,
});
const exportData = await buildFileOperation({
type: FileOperationType.Export,
teamId: team.id,
userId: admin.id,
collectionId: collection.id,
});
await admin.destroy();
const isAdminPresent = await User.findByPk(admin.id);
expect(isAdminPresent).toBe(null);
const res = await server.post("/api/fileOperations.list", admin2, {
body: {
type: FileOperationType.Export,
},
});
const body = await res.json();
const data = body.data[0];
expect(res.status).toEqual(200);
expect(body.data.length).toBe(1);
expect(data.id).toBe(exportData.id);
expect(data.key).toBe(undefined);
expect(data.state).toBe(exportData.state);
expect(data.user.id).toBe(admin.id);
});
it("should require admin", async () => {
const user = await buildUser();
const res = await server.post("/api/fileOperations.list", user, {
body: {
type: FileOperationType.Export,
},
});
expect(res.status).toEqual(403);
});
});
describe("#fileOperations.redirect", () => {
it("should not redirect when file operation is not complete", async () => {
const team = await buildTeam();
const admin = await buildAdmin({
teamId: team.id,
});
const exportData = await buildFileOperation({
type: FileOperationType.Export,
teamId: team.id,
userId: admin.id,
});
const res = await server.post("/api/fileOperations.redirect", admin, {
body: {
id: exportData.id,
},
});
const body = await res.json();
expect(res.status).toEqual(400);
expect(body.message).toEqual("export is not complete yet");
});
it("should allow admin to redirect another admin's export", async () => {
const team = await buildTeam();
const admin1 = await buildAdmin({ teamId: team.id });
const admin2 = await buildAdmin({ teamId: team.id });
const exportData = await buildFileOperation({
state: FileOperationState.Complete,
type: FileOperationType.Export,
teamId: team.id,
userId: admin1.id,
});
const res = await server.post("/api/fileOperations.redirect", admin2, {
body: {
id: exportData.id,
},
redirect: "manual",
});
expect(res.status).toEqual(302);
});
it("should allow user to redirect their own export", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const collection = await buildCollection({ teamId: team.id });
const exportData = await buildFileOperation({
state: FileOperationState.Complete,
type: FileOperationType.Export,
teamId: team.id,
userId: user.id,
collectionId: collection.id,
});
const res = await server.post("/api/fileOperations.redirect", user, {
body: {
id: exportData.id,
},
redirect: "manual",
});
expect(res.status).toEqual(302);
});
it("should not allow user to redirect their own export of a collection they lost access to", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const collection = await buildCollection({
teamId: team.id,
permission: null,
});
const exportData = await buildFileOperation({
state: FileOperationState.Complete,
type: FileOperationType.Export,
teamId: team.id,
userId: user.id,
collectionId: collection.id,
});
const res = await server.post("/api/fileOperations.redirect", user, {
body: {
id: exportData.id,
},
redirect: "manual",
});
expect(res.status).toEqual(403);
});
it("should allow user to redirect their own export of a private collection they are a member of", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const collection = await buildCollection({
teamId: team.id,
permission: null,
});
await UserMembership.create({
createdById: user.id,
collectionId: collection.id,
userId: user.id,
permission: CollectionPermission.Admin,
});
const exportData = await buildFileOperation({
state: FileOperationState.Complete,
type: FileOperationType.Export,
teamId: team.id,
userId: user.id,
collectionId: collection.id,
});
const res = await server.post("/api/fileOperations.redirect", user, {
body: {
id: exportData.id,
},
redirect: "manual",
});
expect(res.status).toEqual(302);
});
it("should allow user to redirect their own document export", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const collection = await buildCollection({ teamId: team.id });
const document = await buildDocument({
teamId: team.id,
userId: user.id,
collectionId: collection.id,
});
const exportData = await buildFileOperation({
state: FileOperationState.Complete,
type: FileOperationType.Export,
teamId: team.id,
userId: user.id,
documentId: document.id,
});
const res = await server.post("/api/fileOperations.redirect", user, {
body: {
id: exportData.id,
},
redirect: "manual",
});
expect(res.status).toEqual(302);
});
it("should not allow user to redirect their own document export after losing access", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const collection = await buildCollection({
teamId: team.id,
permission: null,
});
const document = await buildDocument({
teamId: team.id,
collectionId: collection.id,
});
const exportData = await buildFileOperation({
state: FileOperationState.Complete,
type: FileOperationType.Export,
teamId: team.id,
userId: user.id,
documentId: document.id,
});
const res = await server.post("/api/fileOperations.redirect", user, {
body: {
id: exportData.id,
},
redirect: "manual",
});
expect(res.status).toEqual(403);
});
it("should not allow user to redirect another user's export", async () => {
const team = await buildTeam();
const admin = await buildAdmin({ teamId: team.id });
const user = await buildUser({ teamId: team.id });
const exportData = await buildFileOperation({
state: FileOperationState.Complete,
type: FileOperationType.Export,
teamId: team.id,
userId: admin.id,
});
const res = await server.post("/api/fileOperations.redirect", user, {
body: {
id: exportData.id,
},
});
expect(res.status).toEqual(403);
});
it("should require authorization", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const admin = await buildAdmin();
const exportData = await buildFileOperation({
state: FileOperationState.Complete,
type: FileOperationType.Export,
teamId: team.id,
userId: user.id,
});
const res = await server.post("/api/fileOperations.redirect", admin, {
body: {
id: exportData.id,
},
});
expect(res.status).toEqual(403);
});
});
describe("#fileOperations.delete", () => {
it.each([
FileOperationState.Complete,
FileOperationState.Error,
FileOperationState.Expired,
])("should delete %s export file operation", async (state) => {
const team = await buildTeam();
const admin = await buildAdmin({
teamId: team.id,
});
const exportData = await buildFileOperation({
type: FileOperationType.Export,
teamId: team.id,
userId: admin.id,
state,
});
const deleteResponse = await server.post(
"/api/fileOperations.delete",
admin,
{
body: {
id: exportData.id,
},
}
);
expect(deleteResponse.status).toBe(200);
expect(
await Event.count({
where: {
name: "fileOperations.delete",
teamId: team.id,
},
})
).toBe(1);
expect(
await FileOperation.count({
where: {
teamId: team.id,
},
})
).toBe(0);
});
it("should not delete an export file operation in a non-terminal state", async () => {
const team = await buildTeam();
const admin = await buildAdmin({ teamId: team.id });
const exportData = await buildFileOperation({
type: FileOperationType.Export,
teamId: team.id,
userId: admin.id,
state: FileOperationState.Creating,
});
const res = await server.post("/api/fileOperations.delete", admin, {
body: {
id: exportData.id,
},
});
expect(res.status).toEqual(403);
});
it("should require authorization", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const admin = await buildAdmin();
const exportData = await buildFileOperation({
type: FileOperationType.Export,
teamId: team.id,
userId: user.id,
});
const res = await server.post("/api/fileOperations.delete", admin, {
body: {
id: exportData.id,
},
});
expect(res.status).toEqual(403);
});
});