Skip to content

Per 10004 add delete share links #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions packages/api/src/share_link/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@ describe("GET /share-links", () => {
});

afterEach(async () => {
jest.restoreAllMocks();
await clearDatabase();
});

Expand Down Expand Up @@ -697,3 +698,94 @@ describe("GET /share-links", () => {
await agent.get("/api/v2/share-links?shareLinkIds[]=1000").expect(500);
});
});

describe("DELETE /share-links", () => {
const agent = request(app);

beforeEach(async () => {
(verifyUserAuthentication as jest.Mock).mockImplementation(
(req: Request, _, next: NextFunction) => {
(
req.body as {
emailFromAuthToken: string;
userSubjectFromAuthToken: string;
}
).emailFromAuthToken = "[email protected]";
(
req.body as {
emailFromAuthToken: string;
userSubjectFromAuthToken: string;
}
).userSubjectFromAuthToken = "ceca5477-3f9c-4d0a-a7b8-04d5e5adac32";
next();
}
);

await loadFixtures();
});

afterEach(async () => {
await clearDatabase();
});

test("should return 204 for a valid request", async () => {
await agent.delete("/api/v2/share-links/1000").expect(204);
});

test("should return 401 if the caller is unauthenticated", async () => {
(verifyUserAuthentication as jest.Mock).mockImplementation(
(__: Request, _, next: NextFunction) => {
next(new createError.Unauthorized("Invalid token"));
}
);
await agent.delete("/api/v2/share-links/1000").expect(401);
});

test("should return 400 if header values are missing", async () => {
(verifyUserAuthentication as jest.Mock).mockImplementation(
(__: Request, _, next: NextFunction) => {
next();
}
);
await agent.delete("/api/v2/share-links/1000").expect(400);
});

test("should delete the share link", async () => {
await agent.delete("/api/v2/share-links/1000").expect(204);
const shareLinks = await agent
.get("/api/v2/share-links?shareLinkIds[]=1000")
.expect(200);
expect((shareLinks.body as { items: ShareLink[] }).items.length).toEqual(0);
});

test("should return 404 if the share link doesn't exist", async () => {
await agent.delete("/api/v2/share-links/1000").expect(204);
await agent.delete("/api/v2/share-links/1000").expect(404);
});

test("should return 404 if the caller doesn't have access to the share link", async () => {
(verifyUserAuthentication as jest.Mock).mockImplementation(
(req: Request, _, next: NextFunction) => {
(
req.body as {
emailFromAuthToken: string;
userSubjectFromAuthToken: string;
}
).emailFromAuthToken = "[email protected]";
(
req.body as {
emailFromAuthToken: string;
userSubjectFromAuthToken: string;
}
).userSubjectFromAuthToken = "ceca5477-3f9c-4d0a-a7b8-04d5e5adac32";
next();
}
);
await agent.delete("/api/v2/share-links/1000").expect(404);
});

test("should return 500 if the database call fails", async () => {
jest.spyOn(db, "sql").mockRejectedValue(new Error("Test error"));
await agent.delete("/api/v2/share-links/1000").expect(500);
});
});
21 changes: 21 additions & 0 deletions packages/api/src/share_link/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,24 @@ shareLinkController.get(
}
}
);

shareLinkController.delete(
"/:shareLinkId",
verifyUserAuthentication,
async (req: Request, res: Response, next: NextFunction) => {
try {
validateBodyFromAuthentication(req.body);
await shareLinkService.deleteShareLink(
req.body.emailFromAuthToken,
req.params["shareLinkId"] ?? ""
);
res.sendStatus(204);
} catch (err) {
if (isValidationError(err)) {
res.status(400).json({ error: err });
return;
}
next(err);
}
}
);
9 changes: 9 additions & 0 deletions packages/api/src/share_link/queries/delete_share_link.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
DELETE FROM
shareby_url
USING
account
WHERE
account.accountid = shareby_url.byaccountid
AND account.primaryemail = :email
AND shareby_urlid = :shareLinkId
RETURNING shareby_urlid AS "id";
18 changes: 18 additions & 0 deletions packages/api/src/share_link/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,26 @@ const getShareLinks = async (
return shareLinks.rows;
};

const deleteShareLink = async (
email: string,
shareLinkId: string
): Promise<void> => {
const response = await db
.sql<{ id: string }>("share_link.queries.delete_share_link", {
email,
shareLinkId,
})
.catch((err) => {
logger.error(err);
throw new Error("Failed to delete share link");
});
if (response.rows.length === 0)
throw new createError.NotFound("Share link not found");
};

export const shareLinkService = {
createShareLink,
updateShareLink,
getShareLinks,
deleteShareLink,
};