Skip to content

Organizations Access #3373

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
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
9 changes: 1 addition & 8 deletions src/graphql/types/Query/organizations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
ImplicitMercuriusContext,
} from "~/src/graphql/context";
import { Organization } from "~/src/graphql/types/Organization/Organization";
import { TalawaGraphQLError } from "~/src/utilities/TalawaGraphQLError";

// Define type for organization model
type OrganizationType = InferSelectModel<typeof organizationsTable>;

Expand All @@ -27,13 +27,6 @@ export const resolveOrganizations = async (
args: OrganizationsArgs,
ctx: ContextType,
): Promise<OrganizationType[]> => {
if (!ctx.currentClient.isAuthenticated) {
throw new TalawaGraphQLError({
extensions: {
code: "unauthenticated",
},
});
}
const { filter } = args;
try {
const organizations =
Expand Down
27 changes: 22 additions & 5 deletions test/graphql/types/Query/organizations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type {
ImplicitMercuriusContext,
} from "~/src/graphql/context";
import { resolveOrganizations } from "~/src/graphql/types/Query/organizations";
import { TalawaGraphQLError } from "~/src/utilities/TalawaGraphQLError";

type ContextType = ExplicitGraphQLContext & ImplicitMercuriusContext;

Expand All @@ -31,15 +30,33 @@ describe("resolveOrganizations", () => {
vi.clearAllMocks();
});

test("throws an unauthenticated error if user is not authenticated", async () => {
test("returns organizations array if user is not authenticated", async () => {
const unauthenticatedCtx = {
...baseMockCtx,
currentClient: { isAuthenticated: false },
} as unknown as ContextType;

await expect(
resolveOrganizations(null, { filter: null }, unauthenticatedCtx),
).rejects.toThrow(TalawaGraphQLError);
const orgs = [
{ id: "org1", name: "Organization 1" },
{ id: "org2", name: "Organization 2" },
];
mockDrizzleClient.query.organizationsTable.findMany.mockResolvedValue(orgs);

const result = await resolveOrganizations(
null,
{ filter: null },
unauthenticatedCtx,
);
expect(result).toEqual(orgs);

expect(
mockDrizzleClient.query.organizationsTable.findMany,
).toHaveBeenCalledWith(
expect.objectContaining({
limit: 20,
offset: 0,
}),
);
});

test("returns organizations array on successful query", async () => {
Expand Down
Loading