fix: Single share record per document (#1984)

This commit is contained in:
Tom Moor 2021-03-24 18:28:38 -07:00 committed by GitHub
parent 877c01f723
commit 46912f8ddb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 7 deletions

View File

@ -25,7 +25,7 @@ router.post("shares.info", auth(), async (ctx) => {
}
: {
documentId,
userId: user.id,
teamId: user.teamId,
revokedAt: { [Op.eq]: null },
},
});
@ -134,10 +134,12 @@ router.post("shares.create", auth(), async (ctx) => {
const [share, isCreated] = await Share.findOrCreate({
where: {
documentId,
userId: user.id,
teamId: user.teamId,
revokedAt: null,
},
defaults: {
userId: user.id,
},
});
if (isCreated) {

View File

@ -277,12 +277,12 @@ describe("#shares.info", () => {
expect(body.data.published).toBe(true);
});
it("should not find share for different user", async () => {
it("should find share created by another user", async () => {
const { admin, document } = await seed();
const user = await buildUser({
teamId: admin.teamId,
});
await buildShare({
const share = await buildShare({
documentId: document.id,
teamId: admin.teamId,
userId: admin.id,
@ -290,7 +290,11 @@ describe("#shares.info", () => {
const res = await server.post("/api/shares.info", {
body: { token: user.getJwtToken(), documentId: document.id },
});
expect(res.status).toEqual(204);
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.id).toBe(share.id);
expect(body.data.published).toBe(true);
});
it("should not find revoked share", async () => {
@ -353,6 +357,23 @@ describe("#shares.info", () => {
});
describe("#shares.update", () => {
it("should allow user to update a share", async () => {
const { user, document } = await seed();
const share = await buildShare({
documentId: document.id,
teamId: user.teamId,
});
const res = await server.post("/api/shares.update", {
body: { token: user.getJwtToken(), id: share.id, published: true },
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.id).toBe(share.id);
expect(body.data.published).toBe(true);
});
it("should allow author to update a share", async () => {
const { user, document } = await seed();
const share = await buildShare({

View File

@ -5,8 +5,14 @@ import policy from "./policy";
const { allow } = policy;
allow(User, ["read"], Share, (user, share) => user.teamId === share.teamId);
allow(User, ["update", "revoke"], Share, (user, share) => {
allow(
User,
["read", "update"],
Share,
(user, share) => user.teamId === share.teamId
);
allow(User, "revoke", Share, (user, share) => {
if (!share || user.teamId !== share.teamId) return false;
if (user.id === share.userId) return true;
if (user.isAdmin) return true;