Skip to content
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

Pub authorization tests #857

Merged
merged 3 commits into from
Dec 19, 2024
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
150 changes: 150 additions & 0 deletions core/lib/server/pub-capabilities.db.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import { describe, expect, it } from "vitest";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pulled this into its own file because pub.db.test.ts has an issue of its transactions not working properly, I think because we aren't dynamically importing seedCommunity. this meant that I'd query for pubs only to get pubs created by other tests in the file. so this separates it out, but we should fix pub.db.test.ts so we can run its tests without the tests bumping into each other

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

separating it seems good for organization anyways!


import { CoreSchemaType, MemberRole } from "db/public";

import type { Seed } from "~/prisma/seed/seedCommunity";
import { mockServerCode } from "../__tests__/utils";

await mockServerCode();

const seed = {
community: {
name: "test-pub-capabilities",
slug: "test-pub-capabilities",
},
users: {
admin: {
role: MemberRole.admin,
},
editor: {
role: MemberRole.editor,
},
stage1Editor: {
role: MemberRole.contributor,
},
stage2Editor: {
role: MemberRole.contributor,
},
contributor: {
role: MemberRole.contributor,
},
},
pubFields: {
Title: { schemaName: CoreSchemaType.String },
Description: { schemaName: CoreSchemaType.String },
"Some relation": { schemaName: CoreSchemaType.String, relation: true },
},
pubTypes: {
"Basic Pub": {
Title: { isTitle: true },
"Some relation": { isTitle: false },
},
"Minimal Pub": {
Title: { isTitle: true },
},
},
stages: {
"Stage 1": {
members: {
stage1Editor: MemberRole.editor,
},
},
"Stage 2": {
members: {
stage2Editor: MemberRole.editor,
},
},
},
pubs: [
{
pubType: "Basic Pub",
values: {
Title: "Some title",
},
stage: "Stage 1",
},
{
pubType: "Basic Pub",
values: {
Title: "Another title",
},
stage: "Stage 2",
relatedPubs: {
"Some relation": [
{
value: "test relation value",
pub: {
pubType: "Basic Pub",
values: {
Title: "A pub related to another Pub",
},
},
},
],
},
},
{
stage: "Stage 1",
pubType: "Minimal Pub",
values: {
Title: "Minimal pub",
},
},
],
} as Seed;

describe("getPubsWithRelatedValuesAndChildren capabilities", () => {
it("should restrict pubs by visibility", async () => {
const { seedCommunity } = await import("~/prisma/seed/seedCommunity");
const { community, pubFields, pubTypes, stages, pubs, users } = await seedCommunity(seed);
const { getPubsWithRelatedValuesAndChildren } = await import("./pub");

// Admins and editors of the community should see all pubs
const pubsVisibleToAdmin = await getPubsWithRelatedValuesAndChildren({
communityId: community.id,
userId: users.admin.id,
});
// Includes +1 related pub
expect(pubsVisibleToAdmin.length).toEqual(4);
// Do the same check for editors
const pubsVisibleToEditor = await getPubsWithRelatedValuesAndChildren({
communityId: community.id,
userId: users.editor.id,
});
expect(pubsVisibleToEditor.length).toEqual(4);

// Stage member should only see stages they were added to
const pubsVisibleToStage1Editor = await getPubsWithRelatedValuesAndChildren({
communityId: community.id,
userId: users.stage1Editor.id,
});
const stage1 = stages["Stage 1"];
expect(
pubsVisibleToStage1Editor.sort((a, b) =>
a.title ? a.title.localeCompare(b.title || "") : -1
)
).toMatchObject([
{ title: "Minimal pub", stageId: stage1.id },
{ title: "Some title", stageId: stage1.id },
]);

// Check a stage that has a related pub not in the same stage. Should not get the related pub
const pubsVisibleToStage2Editor = await getPubsWithRelatedValuesAndChildren({
communityId: community.id,
userId: users.stage2Editor.id,
});
const stage2 = stages["Stage 2"];
expect(
pubsVisibleToStage2Editor.sort((a, b) =>
a.title ? a.title.localeCompare(b.title || "") : -1
)
).toMatchObject([{ title: "Another title", stageId: stage2.id }]);

// Contributor should not see any pubs
const pubsVisibleToContributor = await getPubsWithRelatedValuesAndChildren({
communityId: community.id,
userId: users.contributor.id,
});
expect(pubsVisibleToContributor.length).toEqual(0);
});
});
2 changes: 2 additions & 0 deletions core/prisma/seed/seedCommunity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1196,3 +1196,5 @@ export const createSeed = <
pubs?: PI;
forms?: F;
}) => props;

export type Seed = Parameters<typeof createSeed>[0];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can import types without problems and our transactions will work. importing createSeed the function, without doing a dynamic import in the test itself, seems to cause the data to persist though

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

smart!

Loading