API to fetch users who have read/write permission on a document collection (#5047)
This commit is contained in:
@@ -8,6 +8,8 @@ import {
|
||||
CollectionUser,
|
||||
SearchQuery,
|
||||
Event,
|
||||
User,
|
||||
CollectionGroup,
|
||||
} from "@server/models";
|
||||
import DocumentHelper from "@server/models/helpers/DocumentHelper";
|
||||
import {
|
||||
@@ -18,6 +20,7 @@ import {
|
||||
buildDraftDocument,
|
||||
buildViewer,
|
||||
buildTeam,
|
||||
buildGroup,
|
||||
} from "@server/test/factories";
|
||||
import { seed, getTestServer } from "@server/test/support";
|
||||
|
||||
@@ -3107,3 +3110,251 @@ describe("#documents.unpublish", () => {
|
||||
expect(res.status).toEqual(401);
|
||||
});
|
||||
});
|
||||
|
||||
describe("#documents.users", () => {
|
||||
it("should return document users", async () => {
|
||||
const user = await buildUser();
|
||||
const collection = await buildCollection({
|
||||
teamId: user.teamId,
|
||||
createdById: user.id,
|
||||
});
|
||||
const document = await buildDocument({
|
||||
collectionId: collection.id,
|
||||
createdById: user.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
const [alan, bret, ken] = await Promise.all([
|
||||
buildUser({
|
||||
name: "Alan Kay",
|
||||
teamId: user.teamId,
|
||||
}),
|
||||
buildUser({
|
||||
name: "Bret Victor",
|
||||
teamId: user.teamId,
|
||||
}),
|
||||
buildUser({
|
||||
name: "Ken Thompson",
|
||||
teamId: user.teamId,
|
||||
}),
|
||||
]);
|
||||
|
||||
// add people to collection
|
||||
await Promise.all([
|
||||
CollectionUser.create({
|
||||
collectionId: collection.id,
|
||||
userId: alan.id,
|
||||
permission: CollectionPermission.Read,
|
||||
createdById: user.id,
|
||||
}),
|
||||
CollectionUser.create({
|
||||
collectionId: collection.id,
|
||||
userId: bret.id,
|
||||
permission: CollectionPermission.Read,
|
||||
createdById: user.id,
|
||||
}),
|
||||
CollectionUser.create({
|
||||
collectionId: collection.id,
|
||||
userId: ken.id,
|
||||
permission: CollectionPermission.Read,
|
||||
createdById: user.id,
|
||||
}),
|
||||
]);
|
||||
|
||||
const res = await server.post("/api/documents.users", {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
id: document.id,
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(body.data.length).toBe(3);
|
||||
|
||||
const memberIds = body.data.map((u: User) => u.id);
|
||||
expect(memberIds).toContain(alan.id);
|
||||
expect(memberIds).toContain(bret.id);
|
||||
expect(memberIds).toContain(ken.id);
|
||||
});
|
||||
|
||||
it("should return document users with names matching the search query", async () => {
|
||||
const user = await buildUser();
|
||||
const collection = await buildCollection({
|
||||
teamId: user.teamId,
|
||||
createdById: user.id,
|
||||
});
|
||||
const document = await buildDocument({
|
||||
collectionId: collection.id,
|
||||
createdById: user.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
const [alan, bret, ken, jamie] = await Promise.all([
|
||||
buildUser({
|
||||
name: "Alan Kay",
|
||||
teamId: user.teamId,
|
||||
}),
|
||||
buildUser({
|
||||
name: "Bret Victor",
|
||||
teamId: user.teamId,
|
||||
}),
|
||||
buildUser({
|
||||
name: "Ken Thompson",
|
||||
teamId: user.teamId,
|
||||
}),
|
||||
buildUser({
|
||||
name: "Jamie Zawinsky",
|
||||
teamId: user.teamId,
|
||||
}),
|
||||
]);
|
||||
const group = await buildGroup({
|
||||
name: "Hackers",
|
||||
createdById: user.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
|
||||
// add people to group
|
||||
await Promise.all([
|
||||
group.$add("user", ken, {
|
||||
through: {
|
||||
createdById: user.id,
|
||||
},
|
||||
}),
|
||||
group.$add("user", jamie, {
|
||||
through: {
|
||||
createdById: user.id,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
// add people and groups to collection
|
||||
await Promise.all([
|
||||
CollectionUser.create({
|
||||
collectionId: collection.id,
|
||||
userId: alan.id,
|
||||
permission: CollectionPermission.Read,
|
||||
createdById: user.id,
|
||||
}),
|
||||
CollectionUser.create({
|
||||
collectionId: collection.id,
|
||||
userId: bret.id,
|
||||
permission: CollectionPermission.Read,
|
||||
createdById: user.id,
|
||||
}),
|
||||
CollectionUser.create({
|
||||
collectionId: collection.id,
|
||||
userId: ken.id,
|
||||
permission: CollectionPermission.Read,
|
||||
createdById: user.id,
|
||||
}),
|
||||
CollectionGroup.create({
|
||||
collectionId: collection.id,
|
||||
groupId: group.id,
|
||||
permission: CollectionPermission.ReadWrite,
|
||||
createdById: user.id,
|
||||
}),
|
||||
]);
|
||||
|
||||
const res = await server.post("/api/documents.users", {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
id: document.id,
|
||||
query: "Al",
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
|
||||
const anotherRes = await server.post("/api/documents.users", {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
id: document.id,
|
||||
query: "e",
|
||||
},
|
||||
});
|
||||
const anotherBody = await anotherRes.json();
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(body.data.length).toBe(1);
|
||||
expect(body.data[0].id).toContain(alan.id);
|
||||
expect(body.data[0].name).toBe(alan.name);
|
||||
|
||||
expect(anotherRes.status).toBe(200);
|
||||
expect(anotherBody.data.length).toBe(3);
|
||||
const memberIds = anotherBody.data.map((u: User) => u.id);
|
||||
const memberNames = anotherBody.data.map((u: User) => u.name);
|
||||
expect(memberIds).toContain(bret.id);
|
||||
expect(memberIds).toContain(ken.id);
|
||||
expect(memberIds).toContain(jamie.id);
|
||||
expect(memberNames).toContain(bret.name);
|
||||
expect(memberNames).toContain(ken.name);
|
||||
expect(memberNames).toContain(jamie.name);
|
||||
});
|
||||
|
||||
it("should not return suspended users", async () => {
|
||||
const user = await buildUser();
|
||||
const collection = await buildCollection({
|
||||
teamId: user.teamId,
|
||||
createdById: user.id,
|
||||
});
|
||||
const document = await buildDocument({
|
||||
collectionId: collection.id,
|
||||
createdById: user.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
const [alan, bret, ken] = await Promise.all([
|
||||
buildUser({
|
||||
name: "Alan Kay",
|
||||
teamId: user.teamId,
|
||||
}),
|
||||
buildUser({
|
||||
name: "Bret Victor",
|
||||
teamId: user.teamId,
|
||||
}),
|
||||
buildUser({
|
||||
name: "Ken Thompson",
|
||||
teamId: user.teamId,
|
||||
}),
|
||||
]);
|
||||
|
||||
// add people to collection
|
||||
await Promise.all([
|
||||
CollectionUser.create({
|
||||
collectionId: collection.id,
|
||||
userId: alan.id,
|
||||
permission: CollectionPermission.Read,
|
||||
createdById: user.id,
|
||||
}),
|
||||
CollectionUser.create({
|
||||
collectionId: collection.id,
|
||||
userId: bret.id,
|
||||
permission: CollectionPermission.Read,
|
||||
createdById: user.id,
|
||||
}),
|
||||
CollectionUser.create({
|
||||
collectionId: collection.id,
|
||||
userId: ken.id,
|
||||
permission: CollectionPermission.Read,
|
||||
createdById: user.id,
|
||||
}),
|
||||
]);
|
||||
|
||||
// suspend Alan
|
||||
alan.suspendedAt = new Date();
|
||||
await alan.save();
|
||||
|
||||
const res = await server.post("/api/documents.users", {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
id: document.id,
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(body.data.length).toBe(2);
|
||||
|
||||
const memberIds = body.data.map((u: User) => u.id);
|
||||
expect(memberIds).not.toContain(alan.id);
|
||||
expect(memberIds).toContain(bret.id);
|
||||
expect(memberIds).toContain(ken.id);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user