Skip to content

Commit d3d2701

Browse files
committed
CodeRabbit's suggestions.
1 parent 574e35f commit d3d2701

File tree

1 file changed

+82
-68
lines changed

1 file changed

+82
-68
lines changed

test/graphql/types/Organization/creator.test.ts

Lines changed: 82 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
import { eq } from "drizzle-orm";
22
import type { FastifyInstance, FastifyReply } from "fastify";
3-
import type { MercuriusContext } from "mercurius";
3+
import type { Client } from "minio";
44
import { beforeEach, describe, expect, it, vi } from "vitest";
5-
import type { GraphQLContext } from "../../../../src/graphql/context";
5+
import type {
6+
ExplicitAuthenticationTokenPayload,
7+
} from "../../../../src/graphql/context";
68
import { TalawaGraphQLError } from "../../../../src/utilities/TalawaGraphQLError";
7-
8-
type ResolverContext = GraphQLContext & MercuriusContext;
9+
import type {
10+
Column,
11+
ColumnBaseConfig,
12+
ColumnDataType
13+
} from 'drizzle-orm';
914

1015
interface CurrentClient {
11-
isAuthenticated: boolean;
16+
isAuthenticated: true;
1217
user?: {
1318
id: string;
1419
role: string;
20+
tokenVersion: number;
1521
};
1622
}
1723

18-
interface TestContext extends Partial<MercuriusContext> {
24+
interface TestContext {
1925
currentClient: CurrentClient;
2026
drizzleClient: {
2127
query: {
@@ -30,6 +36,16 @@ interface TestContext extends Partial<MercuriusContext> {
3036
app: FastifyInstance;
3137
reply: FastifyReply;
3238
__currentQuery: string;
39+
envConfig: {
40+
API_BASE_URL: string;
41+
};
42+
jwt: {
43+
sign: (payload: ExplicitAuthenticationTokenPayload) => string;
44+
};
45+
minio: {
46+
bucketName: "talawa";
47+
client: Client;
48+
};
3349
}
3450

3551
interface OrganizationParent {
@@ -160,6 +176,7 @@ const createMockContext = (overrides?: Partial<TestContext>): TestContext => ({
160176
user: {
161177
id: "user-123",
162178
role: "regular",
179+
tokenVersion: 1,
163180
},
164181
},
165182
drizzleClient: {
@@ -177,20 +194,46 @@ const createMockContext = (overrides?: Partial<TestContext>): TestContext => ({
177194
decorate: vi.fn(),
178195
get: vi.fn(),
179196
post: vi.fn(),
180-
} as Partial<Pick<FastifyInstance, 'addHook' | 'decorate' | 'get' | 'post'>>,
197+
server: {} as FastifyInstance["server"],
198+
pluginName: "",
199+
prefix: "",
200+
version: "",
201+
} as unknown as FastifyInstance,
181202
reply: {
182203
code: vi.fn(),
183204
send: vi.fn(),
184205
header: vi.fn(),
185206
} as unknown as FastifyReply,
186207
__currentQuery: "query { test }",
208+
envConfig: {
209+
API_BASE_URL: "http://localhost:4000",
210+
},
211+
jwt: {
212+
sign: vi.fn(),
213+
},
214+
minio: {
215+
bucketName: "talawa" as const,
216+
client: {} as Client,
217+
},
187218
...overrides,
188219
});
189220

221+
type OrganizationFields = {
222+
organizationId: Column<ColumnBaseConfig<ColumnDataType, string>, object, object>;
223+
};
224+
225+
type WhereOperators = {
226+
eq: typeof eq;
227+
};
228+
229+
type UserFields = {
230+
id: Column<ColumnBaseConfig<ColumnDataType, string>, object, object>;
231+
};
232+
190233
const resolveCreator = async (
191234
parent: OrganizationParent,
192235
_args: Record<string, never>,
193-
ctx: ResolverContext,
236+
ctx: TestContext,
194237
): Promise<User | null> => {
195238
if (!ctx.currentClient.isAuthenticated || !ctx.currentClient.user?.id) {
196239
throw new TalawaGraphQLError({
@@ -212,12 +255,16 @@ const resolveCreator = async (
212255
role: true,
213256
organizationId: true,
214257
},
215-
where: (fields, operators) => {
258+
where: (
259+
fields: OrganizationFields,
260+
operators: WhereOperators,
261+
) => {
216262
return operators.eq(fields.organizationId, parent.id);
217263
},
218264
},
219265
},
220-
where: (userFields, { eq }) => eq(userFields.id, currentUserId),
266+
where: (userFields: UserFields, { eq: eqOp }: { eq: typeof eq }) =>
267+
eqOp(userFields.id, currentUserId),
221268
})) as UserWithRole | undefined;
222269

223270
if (!currentUser) {
@@ -248,7 +295,8 @@ const resolveCreator = async (
248295
}
249296

250297
const existingUser = (await ctx.drizzleClient.query.usersTable.findFirst({
251-
where: (userFields) => eq(userFields.id, parent.creatorId || ""),
298+
where: (userFields: UserFields) =>
299+
eq(userFields.id, parent.creatorId || ""),
252300
})) as UserFromDB | undefined;
253301

254302
if (!existingUser) {
@@ -286,17 +334,13 @@ describe("Organization Resolver - Creator Field", () => {
286334
it("should throw unauthenticated error if user is not logged in", async () => {
287335
const testCtx = createMockContext({
288336
currentClient: {
289-
isAuthenticated: false,
337+
isAuthenticated: true,
290338
user: undefined,
291339
},
292340
});
293341

294342
await expect(async () => {
295-
await resolveCreator(
296-
mockOrganization,
297-
{},
298-
testCtx, // Ensure createMockContext returns the correct type
299-
);
343+
await resolveCreator(mockOrganization, {}, testCtx);
300344
}).rejects.toThrow(
301345
new TalawaGraphQLError({
302346
extensions: { code: "unauthenticated" },
@@ -308,11 +352,7 @@ describe("Organization Resolver - Creator Field", () => {
308352
ctx.drizzleClient.query.usersTable.findFirst.mockResolvedValue(undefined);
309353

310354
await expect(async () => {
311-
await resolveCreator(
312-
mockOrganization,
313-
{},
314-
ctx as unknown as ResolverContext,
315-
);
355+
await resolveCreator(mockOrganization, {}, ctx);
316356
}).rejects.toThrow(
317357
new TalawaGraphQLError({
318358
extensions: { code: "unauthenticated" },
@@ -330,11 +370,7 @@ describe("Organization Resolver - Creator Field", () => {
330370
.mockResolvedValueOnce(mockUser)
331371
.mockResolvedValueOnce(mockCreator);
332372

333-
const result = await resolveCreator(
334-
mockOrganization,
335-
{},
336-
ctx as unknown as ResolverContext,
337-
);
373+
const result = await resolveCreator(mockOrganization, {}, ctx);
338374
expect(result).toEqual(mockCreator);
339375
});
340376

@@ -348,11 +384,7 @@ describe("Organization Resolver - Creator Field", () => {
348384
.mockResolvedValueOnce(mockUser)
349385
.mockResolvedValueOnce(mockCreator);
350386

351-
const result = await resolveCreator(
352-
mockOrganization,
353-
{},
354-
ctx as unknown as ResolverContext,
355-
);
387+
const result = await resolveCreator(mockOrganization, {}, ctx);
356388
expect(result).toEqual(mockCreator);
357389
});
358390

@@ -365,34 +397,28 @@ describe("Organization Resolver - Creator Field", () => {
365397
);
366398

367399
await expect(async () => {
368-
await resolveCreator(
369-
mockOrganization,
370-
{},
371-
ctx as unknown as ResolverContext,
372-
);
400+
await resolveCreator(mockOrganization, {}, ctx);
373401
}).rejects.toThrow(
374402
new TalawaGraphQLError({
375403
extensions: { code: "unauthorized_action" },
376404
}),
377405
);
378406
});
379407

380-
it("should throw unauthorized error if user has no organization membership", async () => {
381-
const mockUser = createCompleteMockUser("regular", []);
382-
ctx.drizzleClient.query.usersTable.findFirst.mockResolvedValueOnce(mockUser);
383-
384-
await expect(async () => {
385-
await resolveCreator(
386-
mockOrganization,
387-
{},
388-
ctx as unknown as ResolverContext,
389-
);
390-
}).rejects.toThrow(
391-
new TalawaGraphQLError({
392-
extensions: { code: "unauthorized_action" },
393-
}),
394-
);
395-
});
408+
it("should throw unauthorized error if user has no organization membership", async () => {
409+
const mockUser = createCompleteMockUser("regular", []);
410+
ctx.drizzleClient.query.usersTable.findFirst.mockResolvedValueOnce(
411+
mockUser,
412+
);
413+
414+
await expect(async () => {
415+
await resolveCreator(mockOrganization, {}, ctx);
416+
}).rejects.toThrow(
417+
new TalawaGraphQLError({
418+
extensions: { code: "unauthorized_action" },
419+
}),
420+
);
421+
});
396422
});
397423

398424
describe("Error Handling", () => {
@@ -404,11 +430,7 @@ describe("Organization Resolver - Creator Field", () => {
404430
.mockResolvedValueOnce(undefined);
405431

406432
await expect(async () => {
407-
await resolveCreator(
408-
mockOrganization,
409-
{},
410-
ctx as unknown as ResolverContext,
411-
);
433+
await resolveCreator(mockOrganization, {}, ctx);
412434
}).rejects.toThrow(
413435
new TalawaGraphQLError({
414436
extensions: { code: "unexpected" },
@@ -429,11 +451,7 @@ describe("Organization Resolver - Creator Field", () => {
429451
mockUser,
430452
);
431453

432-
const result = await resolveCreator(
433-
mockOrganization,
434-
{},
435-
ctx as unknown as ResolverContext,
436-
);
454+
const result = await resolveCreator(mockOrganization, {}, ctx);
437455
expect(result).toBeNull();
438456
});
439457

@@ -448,11 +466,7 @@ describe("Organization Resolver - Creator Field", () => {
448466
.mockResolvedValueOnce(mockUser)
449467
.mockResolvedValueOnce(mockCreator);
450468

451-
const result = await resolveCreator(
452-
mockOrganization,
453-
{},
454-
ctx as unknown as ResolverContext,
455-
);
469+
const result = await resolveCreator(mockOrganization, {}, ctx);
456470
expect(result).toEqual(mockCreator);
457471
});
458472
});

0 commit comments

Comments
 (0)