Skip to content

Commit

Permalink
improve user query test
Browse files Browse the repository at this point in the history
  • Loading branch information
nitintwt committed Oct 30, 2024
1 parent 1277e3b commit 56bcb6f
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions tests/resolvers/Query/userAccess.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { USER_NOT_FOUND_ERROR } from "../../../src/constants";
import { USER_NOT_FOUND_ERROR, BASE_URL } from "../../../src/constants";
import type mongoose from "mongoose";
import { user } from "../../../src/resolvers/Query/user";
import { user as userResolver } from "../../../src/resolvers/Query/user";
import { User } from "../../../src/models";
import { connect, disconnect } from "../../helpers/db";
import type { TestUserType } from "../../helpers/userAndOrg";
import { createTestUser } from "../../helpers/userAndOrg";
Expand All @@ -23,6 +24,7 @@ afterAll(async () => {
describe("user Query", () => {
// Test case 1: Invalid user ID scenario
it("throws error if user doesn't exist", async () => {
expect.assertions(1);
const args = {
id: "invalidUserId",
};
Expand All @@ -32,14 +34,15 @@ describe("user Query", () => {
};

try {
await user?.({}, args, context);
await userResolver?.({}, args, context);
} catch (error: unknown) {
expect((error as Error).message).toEqual(USER_NOT_FOUND_ERROR.DESC);
}
});

// Test case 2: Unauthorized access scenario
it("throws unauthorized error when trying to access another user's data", async () => {
expect.assertions(1);
const args = {
id: anotherTestUser?.id,
};
Expand All @@ -49,7 +52,7 @@ describe("user Query", () => {
};

try {
await user?.({}, args, context);
await userResolver?.({}, args, context);
} catch (error: unknown) {
expect((error as Error).message).toEqual(
"Access denied. You can only view your own profile.",
Expand All @@ -65,10 +68,17 @@ describe("user Query", () => {

const context = {
userId: testUser?.id,
apiRootURL: BASE_URL,
};

const result = await user?.({}, args, context);
const result = await userResolver?.({}, args, context);

expect(result).toBeDefined();
const user = await User.findById(testUser?._id).lean();

expect(result?.user).toEqual({
...user,
organizationsBlockedBy: [],
image: user?.image ? `${BASE_URL}${user.image}` : null,
});
});
});

0 comments on commit 56bcb6f

Please sign in to comment.