-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Test: src/graphql/types/Venue/creator.ts #3151
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
Merged
palisadoes
merged 19 commits into
PalisadoesFoundation:develop-postgres
from
NishantSinghhhhh:creator-vitest
Feb 6, 2025
Merged
Changes from 11 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
5976685
added test for creator.ts
NishantSinghhhhh a48d58b
Added test for creator.ts
NishantSinghhhhh a53023e
Added test for creator.ts
NishantSinghhhhh e8ac898
Added test for creator.ts
NishantSinghhhhh 2ff9f7d
changes
NishantSinghhhhh a8adc6d
changes
NishantSinghhhhh 395a130
Added tests for creator-Venue
NishantSinghhhhh 565e329
Added tests for creator-Venue
NishantSinghhhhh 0863fca
Merge branch 'develop-postgres' into creator-vitest
NishantSinghhhhh 38322df
errors
NishantSinghhhhh 7972fc2
Merge branch 'develop-postgres' into creator-vitest
palisadoes b7cbd4b
errors
NishantSinghhhhh 50474e9
merging
NishantSinghhhhh 220b8b0
Merge branch 'develop-postgres' into creator-vitest
NishantSinghhhhh 344dd61
Rabbits changes
NishantSinghhhhh 7be2cb5
Merge branch 'develop-postgres' into creator-vitest
NishantSinghhhhh 768b092
Rabbits changes
NishantSinghhhhh e8b0542
Rabbits changes
NishantSinghhhhh 607d7b3
Rabbits changes
NishantSinghhhhh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,378 @@ | ||
import type { MercuriusContext } from "mercurius"; | ||
import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
import type { GraphQLContext } from "~/src/graphql/context"; | ||
import type { User } from "~/src/graphql/types/User/User"; | ||
import { TalawaGraphQLError } from "~/src/utilities/TalawaGraphQLError"; | ||
|
||
type ResolverContext = GraphQLContext & MercuriusContext; | ||
|
||
interface CurrentClient { | ||
isAuthenticated: boolean; | ||
user?: { | ||
id: string; | ||
role: string; | ||
}; | ||
} | ||
|
||
interface TestContext extends Partial<MercuriusContext> { | ||
currentClient: CurrentClient; | ||
drizzleClient: { | ||
query: { | ||
usersTable: { | ||
findFirst: ReturnType<typeof vi.fn>; | ||
}; | ||
}; | ||
}; | ||
log: { | ||
error: ReturnType<typeof vi.fn>; | ||
}; | ||
app: any; // Add required MercuriusContext properties | ||
reply: any; | ||
__currentQuery: any; | ||
} | ||
|
||
interface VenueParent { | ||
id: string; | ||
name: string; | ||
description: string; | ||
creatorId: string | null; | ||
organizationId: string; | ||
} | ||
NishantSinghhhhh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const resolveCreator = async ( | ||
parent: VenueParent, | ||
_args: Record<string, never>, | ||
ctx: ResolverContext, | ||
): Promise<typeof User | null> => { | ||
if (!ctx.currentClient.isAuthenticated || !ctx.currentClient.user?.id) { | ||
throw new TalawaGraphQLError({ | ||
extensions: { | ||
code: "unauthenticated", | ||
}, | ||
}); | ||
NishantSinghhhhh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
const currentUserId = ctx.currentClient.user.id; | ||
|
||
const currentUser = await ctx.drizzleClient.query.usersTable.findFirst({ | ||
with: { | ||
organizationMembershipsWhereMember: { | ||
columns: { | ||
role: true, | ||
}, | ||
where: (fields, operators) => | ||
operators.eq(fields.organizationId, parent.organizationId), | ||
}, | ||
}, | ||
where: (fields, operators) => operators.eq(fields.id, currentUserId), | ||
}); | ||
|
||
if (!currentUser) { | ||
throw new TalawaGraphQLError({ | ||
extensions: { | ||
code: "unauthenticated", | ||
}, | ||
}); | ||
} | ||
|
||
const currentUserOrganizationMembership = | ||
currentUser.organizationMembershipsWhereMember[0]; | ||
|
||
if ( | ||
currentUser.role !== "administrator" && | ||
(!currentUserOrganizationMembership || | ||
currentUserOrganizationMembership.role !== "administrator") | ||
) { | ||
throw new TalawaGraphQLError({ | ||
extensions: { | ||
code: "unauthorized_action", | ||
}, | ||
}); | ||
} | ||
NishantSinghhhhh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (parent.creatorId === null) { | ||
return null; | ||
} | ||
|
||
if (parent.creatorId === currentUserId) { | ||
return currentUser as unknown as typeof User; | ||
NishantSinghhhhh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
if (typeof parent.creatorId !== "string") { | ||
throw new TalawaGraphQLError({ | ||
extensions: { | ||
code: "unexpected", | ||
}, | ||
}); | ||
} | ||
|
||
const existingUser = await ctx.drizzleClient.query.usersTable.findFirst({ | ||
where: (fields, operators) => | ||
operators.eq(fields.id, parent.creatorId as string), | ||
}); | ||
|
||
if (!existingUser) { | ||
ctx.log.error( | ||
"Postgres select operation returned an empty array for a venue's creator id that isn't null.", | ||
); | ||
|
||
throw new TalawaGraphQLError({ | ||
extensions: { | ||
code: "unexpected", | ||
}, | ||
}); | ||
} | ||
|
||
return existingUser as unknown as typeof User; | ||
}; | ||
|
||
// Tests section | ||
describe("Venue Resolver - Creator Field", () => { | ||
let ctx: TestContext; | ||
let mockVenue: VenueParent; | ||
|
||
beforeEach(() => { | ||
mockVenue = { | ||
id: "venue-123", | ||
name: "Test Venue", | ||
description: "Test Description", | ||
creatorId: "user-123", | ||
organizationId: "org-123", | ||
}; | ||
|
||
ctx = { | ||
currentClient: { | ||
isAuthenticated: true, | ||
user: { | ||
id: "user-123", | ||
role: "member", | ||
}, | ||
}, | ||
drizzleClient: { | ||
query: { | ||
usersTable: { | ||
findFirst: vi.fn(), | ||
}, | ||
}, | ||
}, | ||
log: { | ||
error: vi.fn(), | ||
}, | ||
// Add required MercuriusContext properties | ||
app: {}, | ||
reply: {}, | ||
__currentQuery: {}, | ||
}; | ||
}); | ||
|
||
it("should throw unauthenticated error if user is not logged in", async () => { | ||
const testCtx = { | ||
...ctx, | ||
currentClient: { | ||
isAuthenticated: false, | ||
user: undefined, | ||
}, | ||
} as unknown as ResolverContext; | ||
|
||
await expect(async () => { | ||
await resolveCreator(mockVenue, {}, testCtx); | ||
}).rejects.toThrow( | ||
new TalawaGraphQLError({ | ||
extensions: { code: "unauthenticated" }, | ||
}), | ||
); | ||
}); | ||
|
||
it("should throw unauthenticated error if user is not logged in", async () => { | ||
const testCtx = { | ||
...ctx, | ||
currentClient: { | ||
isAuthenticated: false, | ||
user: undefined, | ||
}, | ||
} as unknown as ResolverContext; | ||
|
||
await expect(async () => { | ||
await resolveCreator(mockVenue, {}, testCtx); | ||
}).rejects.toThrow( | ||
new TalawaGraphQLError({ | ||
extensions: { code: "unauthenticated" }, | ||
}), | ||
); | ||
}); | ||
NishantSinghhhhh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
it("should throw unauthenticated error if current user is not found", async () => { | ||
ctx.drizzleClient.query.usersTable.findFirst.mockResolvedValue(undefined); | ||
|
||
await expect(async () => { | ||
await resolveCreator(mockVenue, {}, ctx as unknown as ResolverContext); | ||
}).rejects.toThrow( | ||
new TalawaGraphQLError({ | ||
extensions: { code: "unauthenticated" }, | ||
}), | ||
); | ||
}); | ||
|
||
it("should allow access if user is system administrator", async () => { | ||
const mockUser = { | ||
id: "user-123", | ||
role: "administrator", | ||
organizationMembershipsWhereMember: [], | ||
}; | ||
ctx.drizzleClient.query.usersTable.findFirst.mockResolvedValueOnce( | ||
mockUser, | ||
); | ||
|
||
const result = await resolveCreator( | ||
mockVenue, | ||
{}, | ||
ctx as unknown as ResolverContext, | ||
); | ||
expect(result).toEqual(mockUser); | ||
}); | ||
|
||
it("should allow access if user is organization administrator", async () => { | ||
const mockUser = { | ||
id: "user-123", | ||
role: "member", | ||
organizationMembershipsWhereMember: [ | ||
{ | ||
role: "administrator", | ||
}, | ||
], | ||
}; | ||
ctx.drizzleClient.query.usersTable.findFirst.mockResolvedValueOnce( | ||
mockUser, | ||
); | ||
|
||
const result = await resolveCreator( | ||
mockVenue, | ||
{}, | ||
ctx as unknown as ResolverContext, | ||
); | ||
expect(result).toEqual(mockUser); | ||
}); | ||
|
||
it("should throw unauthorized error if user is not an administrator", async () => { | ||
const mockUser = { | ||
id: "user-123", | ||
role: "member", | ||
organizationMembershipsWhereMember: [ | ||
{ | ||
role: "member", | ||
}, | ||
], | ||
}; | ||
ctx.drizzleClient.query.usersTable.findFirst.mockResolvedValueOnce( | ||
mockUser, | ||
); | ||
|
||
await expect(async () => { | ||
await resolveCreator(mockVenue, {}, ctx as unknown as ResolverContext); | ||
}).rejects.toThrow( | ||
new TalawaGraphQLError({ | ||
extensions: { code: "unauthorized_action" }, | ||
}), | ||
); | ||
}); | ||
|
||
it("should return null if venue has no creator", async () => { | ||
mockVenue.creatorId = null; | ||
const mockUser = { | ||
id: "user-123", | ||
role: "administrator", | ||
organizationMembershipsWhereMember: [], | ||
}; | ||
ctx.drizzleClient.query.usersTable.findFirst.mockResolvedValueOnce( | ||
mockUser, | ||
); | ||
|
||
const result = await resolveCreator( | ||
mockVenue, | ||
{}, | ||
ctx as unknown as ResolverContext, | ||
); | ||
expect(result).toBeNull(); | ||
}); | ||
|
||
it("should return current user if they are the creator", async () => { | ||
const mockUser = { | ||
id: "user-123", | ||
role: "administrator", | ||
organizationMembershipsWhereMember: [], | ||
}; | ||
ctx.drizzleClient.query.usersTable.findFirst.mockResolvedValueOnce( | ||
mockUser, | ||
); | ||
|
||
const result = await resolveCreator( | ||
mockVenue, | ||
{}, | ||
ctx as unknown as ResolverContext, | ||
); | ||
expect(result).toEqual(mockUser); | ||
}); | ||
|
||
it("should fetch and return creator if different from current user", async () => { | ||
const currentUser = { | ||
id: "user-123", | ||
role: "administrator", | ||
organizationMembershipsWhereMember: [], | ||
}; | ||
const creatorUser = { | ||
id: "creator-456", | ||
role: "member", | ||
organizationMembershipsWhereMember: [], | ||
}; | ||
|
||
mockVenue.creatorId = "creator-456"; | ||
ctx.drizzleClient.query.usersTable.findFirst | ||
.mockResolvedValueOnce(currentUser) | ||
.mockResolvedValueOnce(creatorUser); | ||
|
||
const result = await resolveCreator( | ||
mockVenue, | ||
{}, | ||
ctx as unknown as ResolverContext, | ||
); | ||
expect(result).toEqual(creatorUser); | ||
}); | ||
|
||
it("should handle empty organization memberships array", async () => { | ||
const mockUser = { | ||
id: "user-123", | ||
role: "member", | ||
organizationMembershipsWhereMember: [], | ||
}; | ||
ctx.drizzleClient.query.usersTable.findFirst.mockResolvedValueOnce( | ||
mockUser, | ||
); | ||
|
||
await expect(async () => { | ||
await resolveCreator(mockVenue, {}, ctx as unknown as ResolverContext); | ||
}).rejects.toThrow( | ||
new TalawaGraphQLError({ | ||
extensions: { code: "unauthorized_action" }, | ||
}), | ||
); | ||
}); | ||
|
||
it("should handle undefined organization membership role", async () => { | ||
const mockUser = { | ||
id: "user-123", | ||
role: "member", | ||
organizationMembershipsWhereMember: [{ role: undefined }], | ||
}; | ||
ctx.drizzleClient.query.usersTable.findFirst.mockResolvedValueOnce( | ||
mockUser, | ||
); | ||
|
||
await expect(async () => { | ||
await resolveCreator(mockVenue, {}, ctx as unknown as ResolverContext); | ||
}).rejects.toThrow( | ||
new TalawaGraphQLError({ | ||
extensions: { code: "unauthorized_action" }, | ||
}), | ||
); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.