Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Fraggle committed Jan 16, 2025
1 parent ec15631 commit e1ba876
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 11 deletions.
2 changes: 1 addition & 1 deletion front/lib/resources/space_resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ export class SpaceResource extends BaseResource<SpaceModel> {
workspaceId: this.workspaceId,
roles: [
{ role: "admin", permissions: ["admin", "read", "write"] },
//{ role: "builder", permissions: ["read", "write"] },
{ role: "builder", permissions: ["read", "write"] },
],
groups: this.groups.map((group) => ({
id: group.id,
Expand Down
2 changes: 1 addition & 1 deletion front/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 31 additions & 9 deletions front/pages/api/v1/w/[wId]/spaces/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { createMocks } from "node-mocks-http";
import { describe, expect, expectTypeOf, it } from "vitest";
import { describe, expect, it } from "vitest";

import { groupFactory } from "@app/tests/utils/GroupFactory";
import { groupSpaceFactory } from "@app/tests/utils/GroupSpaceFactory";
import { keyFactory } from "@app/tests/utils/KeyFactory";
import { spaceFactory } from "@app/tests/utils/SpaceFactory";
import { withinTransaction } from "@app/tests/utils/utils";
import {
expectArrayOfObjectsWithSpecificLength,
withinTransaction,
} from "@app/tests/utils/utils";
import { workspaceFactory } from "@app/tests/utils/WorkspaceFactory";

import handler from "./index";
Expand Down Expand Up @@ -47,19 +51,37 @@ describe(
},
});

await spaceFactory().global(workspace).create();
const globalSpace = await spaceFactory().global(workspace).create();
await spaceFactory().system(workspace).create();
await spaceFactory().regular(workspace).create();
await spaceFactory().regular(workspace).create();
const regularSpace1 = await spaceFactory().regular(workspace).create();
const regularSpace2 = await spaceFactory().regular(workspace).create();
await spaceFactory().regular(workspace).create();

await groupSpaceFactory().associate(regularSpace1, globalGroup);
await groupSpaceFactory().associate(regularSpace2, globalGroup);

await handler(req, res);

expect(res._getStatusCode()).toBe(200);
console.log(res._getData());
expect(JSON.parse(res._getData())).toEqual({
spaces: expect([expect.any(Object)]).toHaveLength(5),
});

const response = JSON.parse(res._getData());

expectArrayOfObjectsWithSpecificLength(response["spaces"], 3);

expect(response["spaces"]).toEqual([
expect.objectContaining({
name: globalSpace.name,
kind: "global",
}),
expect.objectContaining({
name: regularSpace1.name,
kind: "regular",
}),
expect.objectContaining({
name: regularSpace2.name,
kind: "regular",
}),
]);
});
})
);
27 changes: 27 additions & 0 deletions front/tests/utils/GroupSpaceFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { InferCreationAttributes } from "sequelize";

import { GroupSpaceModel } from "@app/lib/resources/storage/models/group_spaces";
import type { GroupModel } from "@app/lib/resources/storage/models/groups";
import type { SpaceModel } from "@app/lib/resources/storage/models/spaces";

import { Factory } from "./factories";

class GroupSpaceFactory extends Factory<
GroupSpaceModel,
InferCreationAttributes<GroupSpaceModel>
> {
async make(params: InferCreationAttributes<GroupSpaceModel>) {
return GroupSpaceModel.create(params);
}

associate(space: SpaceModel, group: GroupModel) {
return this.params({
groupId: group.id,
vaultId: space.id,
}).create();
}
}

export const groupSpaceFactory = () => {
return new GroupSpaceFactory();
};
11 changes: 11 additions & 0 deletions front/tests/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ type NextHandler = (
res: NextApiResponse
) => Promise<void> | void;

export const expectArrayOfObjectsWithSpecificLength = (
value: any,
length: number
) => {
expect(Array.isArray(value)).toBe(true);
expect(value).toHaveLength(length);
expect(
value.every((item: unknown) => typeof item === "object" && item !== null)
).toBe(true);
};

// Wrapper to make sure that each test suite has a clean database
export const withinTransaction = (testSuite) => {
return async () => {
Expand Down

0 comments on commit e1ba876

Please sign in to comment.