Skip to content

Commit

Permalink
added test cases for falling line
Browse files Browse the repository at this point in the history
  • Loading branch information
PurnenduMIshra129th committed Feb 1, 2025
1 parent 4341a08 commit 3b38443
Showing 1 changed file with 115 additions and 5 deletions.
120 changes: 115 additions & 5 deletions tests/resolvers/Query/verifyRole.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { AppUserProfile } from "../../../src/models/AppUserProfile";
process.env.ACCESS_TOKEN_SECRET = "test_secret";
process.env.DEFAULT_LANGUAGE_CODE = "en";
process.env.TOKEN_VERSION = "0";
const token = "validToken";

Check failure

Code scanning / CodeQL

Hard-coded credentials Critical test

The hard-coded value "validToken" is used as
authorization header
.
The hard-coded value "validToken" is used as
authorization header
.
The hard-coded value "validToken" is used as
authorization header
.
The hard-coded value "validToken" is used as
authorization header
.
The hard-coded value "validToken" is used as
authorization header
.
The hard-coded value "validToken" is used as
authorization header
.
The hard-coded value "validToken" is used as
authorization header
.
The hard-coded value "validToken" is used as
authorization header
.
The hard-coded value "validToken" is used as authorization header.
// Mock database call
vi.mock("../../../src/models/AppUserProfile", () => ({
AppUserProfile: {
Expand All @@ -21,19 +22,104 @@ describe("verifyRole", () => {
beforeEach(() => {
req = {
headers: {
authorization: "Bearer validToken",
authorization: `Bearer ${token}`,
},
};
vi.restoreAllMocks(); // Reset all mocks before each test
});

test("should return unauthorized when Authorization header is missing", async () => {
const req = { headers: {} }; // No authorization header

if (verifyRole !== undefined) {
const result = await verifyRole({}, {}, { req });
expect(result).toEqual({ role: "", isAuthorized: false });
} else {
throw new Error("verifyRole is undefined");
}
});
test("should handle token without 'Bearer' prefix correctly", async () => {
const req = { headers: { authorization: "validToken" } };

Check failure

Code scanning / CodeQL

Hard-coded credentials Critical test

The hard-coded value "validToken" is used as
authorization header
.

if (verifyRole !== undefined) {
vi.spyOn(jwt, "verify").mockImplementationOnce(() => {
return { userId: "user123" };
});

(AppUserProfile.findOne as Mock).mockResolvedValue({
userId: "user123",
isSuperAdmin: false,
adminFor: [],
});

const result = await verifyRole({}, {}, { req });
expect(result).toEqual({ role: "user", isAuthorized: true });
} else {
throw new Error("verifyRole is undefined");
}
});

test("should extract token correctly when it starts with 'Bearer '", async () => {
const req = { headers: { authorization: `Bearer ${token}` } };

if (verifyRole !== undefined) {
vi.spyOn(jwt, "verify").mockImplementationOnce(() => {
return { userId: "user123" };
});

(AppUserProfile.findOne as Mock).mockResolvedValue({
userId: "user123",
isSuperAdmin: false,
adminFor: [],
});

const result = await verifyRole({}, {}, { req });
expect(result).toEqual({ role: "user", isAuthorized: true });
} else {
throw new Error("verifyRole is undefined");
}
});

test("should return unauthorized when token is missing", async () => {
const req = { headers: { authorization: "Bearer " } }; // Empty token after 'Bearer'

if (verifyRole !== undefined) {
const result = await verifyRole({}, {}, { req });
expect(result).toEqual({ role: "", isAuthorized: false });
} else {
throw new Error("verifyRole is undefined");
}
});

test("should throw an error when userId is missing in the decoded token", async () => {
const req = { headers: { authorization: `Bearer ${token}` } };

if (verifyRole !== undefined) {
// Mock jwt.verify to return a decoded object without userId
vi.spyOn(jwt, "verify").mockImplementationOnce(() => {
return { someOtherKey: "someValue" }; // No userId in the decoded token
});

const result = await verifyRole({}, {}, { req });

// We expect the result to contain an error about missing userId
expect(result).toEqual({
role: "",
isAuthorized: false,
error: "Authentication failed",
});
} else {
throw new Error("verifyRole is undefined");
}
});

test("should return role 'user' for a valid user token", async () => {
vi.spyOn(jwt, "verify").mockImplementationOnce(() => {
return { userId: "user123" };
});
const req = {
headers: {
authorization: "Bearer validToken",
authorization: `Bearer ${token}`,
},
};
(AppUserProfile.findOne as Mock).mockResolvedValue({
Expand All @@ -56,7 +142,7 @@ describe("verifyRole", () => {
});
const req = {
headers: {
authorization: "Bearer validToken",
authorization: `Bearer ${token}`,
},
};
(AppUserProfile.findOne as Mock).mockResolvedValue({
Expand All @@ -79,7 +165,7 @@ describe("verifyRole", () => {

const req = {
headers: {
authorization: "Bearer validToken",
authorization: `Bearer ${token}`,
},
};
(AppUserProfile.findOne as Mock).mockResolvedValue({
Expand All @@ -94,14 +180,38 @@ describe("verifyRole", () => {
throw new Error("verifyRole is undefined");
}
});
test("should return role 'user' when a valid user profile is found", async () => {
const req = { headers: { authorization: `Bearer ${token}` } };

if (verifyRole !== undefined) {
// Mock jwt.verify to return a decoded token with userId
vi.spyOn(jwt, "verify").mockImplementationOnce(() => {
return { userId: "user123" }; // userId is present
});

// Mock the database call to return a valid user profile
(AppUserProfile.findOne as Mock).mockResolvedValue({
userId: "user123",
isSuperAdmin: false,
adminFor: [],
});

const result = await verifyRole({}, {}, { req });

// We expect to get the role and authorization success
expect(result).toEqual({ role: "user", isAuthorized: true });
} else {
throw new Error("verifyRole is undefined");
}
});

test("should return unauthorized when user is not found in DB", async () => {
vi.spyOn(jwt, "verify").mockImplementationOnce(() => {
return { userId: "unknownUser" };
});
const req = {
headers: {
authorization: "Bearer validToken",
authorization: `Bearer ${token}`,
},
};
(AppUserProfile.findOne as Mock).mockResolvedValue(null);
Expand Down

0 comments on commit 3b38443

Please sign in to comment.