Skip to content

Commit

Permalink
✅ Fix e2e trash tests to check for personal trash folders (#433)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericlinagora committed May 28, 2024
1 parent 4fc1fe8 commit 139e95a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 48 deletions.
9 changes: 4 additions & 5 deletions tdrive/backend/node/test/e2e/common/user-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,13 @@ export default class UserApi {
return files;
};

async createDirectory(parent = "root") {
async createDirectory(parent = "root", overrides?: Partial<DriveFile>) {
const directory = await this.createDocument({
company_id: this.platform.workspace.company_id,
name: "Test Folder Name",
parent_id: parent,
is_directory: true
is_directory: true,
...overrides
}, {});
expect(directory).toBeDefined();
expect(directory).not.toBeNull();
Expand All @@ -230,9 +231,7 @@ export default class UserApi {
return directory;
}

/** Run the provided callback using the specified bearer JWT token.
* //TODO: Warning: does not override calls using `this.api` have to discuss
*/
/** Run the provided callback using the specified bearer JWT token */
async impersonateWithJWT<T>(jwt: string, cb: () => Promise<T>): Promise<T> {
const previous = this.jwt;
this.jwt = jwt;
Expand Down
96 changes: 53 additions & 43 deletions tdrive/backend/node/test/e2e/documents/trash.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
describe("the Drive's documents' trash feature", () => {
let platform: TestPlatform | null;
let currentUser: UserApi;
let currentUserRoot: string | undefined;
let currentUserTrash: string | undefined;

beforeAll(async () => {
platform = await init({
Expand All @@ -36,6 +38,8 @@ describe("the Drive's documents' trash feature", () => {
],
});
currentUser = await UserApi.getInstance(platform);
currentUserRoot = `user_${currentUser.user.id}`;
currentUserTrash = `trash_${currentUser.user.id}`;
});

afterAll(async () => {
Expand All @@ -53,56 +57,76 @@ describe("the Drive's documents' trash feature", () => {
expect(result.item.name).toEqual("Trash");
});

it("did move an item to trash", async () => {

async function getTrashContentIds(scope: "shared" | "personal") {
const id = scope === "shared" ? "trash" : currentUserTrash!;
const listTrashResponse = await currentUser.getDocument(id);
expect(listTrashResponse.statusCode).toBe(200);
const listTrashResult = deserialize<DriveItemDetailsMockClass>(
DriveItemDetailsMockClass,
listTrashResponse.body,
);
expect(listTrashResult.item.id).toEqual(id);
if (scope === "shared")
expect(listTrashResult.item.name).toEqual("Trash");
return listTrashResult.children.map(({id}) => id);
}

it("did move a shared item to shared trash", async () => {
const createItemResult = await currentUser.createDefaultDocument();

expect(createItemResult).toBeDefined();
expect(createItemResult.id).toBeDefined();
expect(createItemResult.scope).toEqual("shared");

expect(await getTrashContentIds("shared")).not.toContain(createItemResult.id);

const moveToTrashResponse = await currentUser.delete(createItemResult.id);
expect(moveToTrashResponse.statusCode).toEqual(200);

const listTrashResponse = await currentUser.getDocument("trash");
const listTrashResult = deserialize<DriveItemDetailsMockClass>(
DriveItemDetailsMockClass,
listTrashResponse.body,
);
expect(listTrashResult.item.name).toEqual("Trash");
expect(await getTrashContentIds("shared")).toContain(createItemResult.id);
expect(await getTrashContentIds("personal")).not.toContain(createItemResult.id);
});

it("did move a user item to user trash", async () => {
const createItemResult = await currentUser.createDefaultDocument({
parent_id: currentUserRoot,
scope: "personal",
});

expect(createItemResult).toBeDefined();
expect(createItemResult.scope).toEqual("shared");
expect(listTrashResult.children.some(({ id }) => id === createItemResult.id)).toBeTruthy();
expect(createItemResult.id).toBeDefined();
expect(createItemResult.scope).toEqual("personal");

expect(await getTrashContentIds("personal")).not.toContain(createItemResult.id);
const moveToTrashResponse = await currentUser.delete(createItemResult.id);
expect(moveToTrashResponse.statusCode).toEqual(200);
expect(await getTrashContentIds("personal")).toContain(createItemResult.id);
expect(await getTrashContentIds("shared")).not.toContain(createItemResult.id);
});

describe("deleting a file uploaded by an anonymous user should go to the sharers trash", () => {
async function getCurrentUsersTrashContentIds() {
const listTrashResponse = await currentUser.getDocument("trash");
expect(listTrashResponse.statusCode).toBe(200);
const listTrashResult = deserialize<DriveItemDetailsMockClass>(
DriveItemDetailsMockClass,
listTrashResponse.body,
);
return listTrashResult.children.map(({id}) => id);
}

it("finds the owner from the immediate parent folder", async () => {
const publiclyWriteableFolder = await currentUser.createDirectory();
const publiclyWriteableFolder = await currentUser.createDirectory(currentUserRoot, { scope: "personal" });
const setPublicWriteableResponse = await currentUser.shareWithPublicLink(publiclyWriteableFolder, "write");
expect(setPublicWriteableResponse.statusCode).toBe(200);

const anonymouslyUploadedDoc = await currentUser.impersonatePublicLinkAccessOf(publiclyWriteableFolder, () =>
currentUser.createDefaultDocument({
parent_id: publiclyWriteableFolder.id,
scope: "personal",
}));
expect(publiclyWriteableFolder.creator).toEqual(currentUser.user.id);
expect(anonymouslyUploadedDoc.creator).not.toEqual(currentUser.user.id);

const deletionToTrashResponse = await currentUser.delete(anonymouslyUploadedDoc.id);
expect(deletionToTrashResponse.statusCode).toBe(200);

expect((await getCurrentUsersTrashContentIds())).toContain(anonymouslyUploadedDoc.id);
expect((await getTrashContentIds("personal"))).toContain(anonymouslyUploadedDoc.id);
});

it("finds the owner from the indirect parent folder", async () => {
const publiclyWriteableFolder = await currentUser.createDirectory();
const publiclyWriteableFolder = await currentUser.createDirectory(currentUserRoot, { scope: "personal" });
const setPublicWriteableResponse = await currentUser.shareWithPublicLink(publiclyWriteableFolder, "write");
expect(setPublicWriteableResponse.statusCode).toBe(200);

Expand All @@ -111,49 +135,35 @@ describe("the Drive's documents' trash feature", () => {
expect(anonymouslyCreatedFolder.creator).not.toEqual(currentUser.user.id);
return currentUser.createDefaultDocument({
parent_id: anonymouslyCreatedFolder.id,
})
scope: "personal",
});
});
expect(publiclyWriteableFolder.creator).toEqual(currentUser.user.id);
expect(anonymouslyUploadedDoc.creator).not.toEqual(currentUser.user.id);

const deletionToTrashResponse = await currentUser.delete(anonymouslyUploadedDoc.id);
expect(deletionToTrashResponse.statusCode).toBe(200);

expect((await getCurrentUsersTrashContentIds())).toContain(anonymouslyUploadedDoc.id);
expect((await getTrashContentIds("personal"))).toContain(anonymouslyUploadedDoc.id);
});

it.only("goes into the sharers trash even if another user deletes the file", async () => {
const publiclyWriteableFolder = await currentUser.createDirectory();
it("goes into the sharers trash even if another user deletes the file", async () => {
const publiclyWriteableFolder = await currentUser.createDirectory(currentUserRoot, { scope: "personal" });
const setPublicWriteableResponse = await currentUser.shareWithPublicLink(publiclyWriteableFolder, "write");
expect(setPublicWriteableResponse.statusCode).toBe(200);

const anonymouslyUploadedDoc = await currentUser.impersonatePublicLinkAccessOf(publiclyWriteableFolder, () =>
currentUser.createDefaultDocument({
parent_id: publiclyWriteableFolder.id,
scope: "personal",
}));

const secondaryUser = await UserApi.getInstance(platform!);

const deletionToTrashResponse = await secondaryUser.delete(anonymouslyUploadedDoc.id);
expect(deletionToTrashResponse.statusCode).toBe(200);

expect((await getCurrentUsersTrashContentIds())).toContain(anonymouslyUploadedDoc.id);
});

it("If anonymous user deletes the files in should be in the users trash", async () => {
const publiclyWriteableFolder = await currentUser.createDirectory();
const anonymousUser = await UserApi.getInstance(platform);

const setPublicWriteableResponse = await currentUser.shareWithPublicLink(publiclyWriteableFolder, "manage");
expect(setPublicWriteableResponse.statusCode).toBe(200);

anonymousUser.jwt = (await anonymousUser.getPublicLinkAccessToken(publiclyWriteableFolder)).value;
const anonymouslyUploadedDoc = await anonymousUser.uploadRandomFileAndCreateDocument(publiclyWriteableFolder.id);

const deletionToTrashResponse = await anonymousUser.delete(anonymouslyUploadedDoc.id);
expect(deletionToTrashResponse.statusCode).toBe(200);

expect((await getCurrentUsersTrashContentIds())).toContain(anonymouslyUploadedDoc.id);
expect((await getTrashContentIds("personal"))).toContain(anonymouslyUploadedDoc.id);
});
});
});

0 comments on commit 139e95a

Please sign in to comment.