-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4341a08
commit 3b38443
Showing
1 changed file
with
115 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -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 Error loading related location Loading The hard-coded value "validToken" is used as authorization header Error loading related location Loading The hard-coded value "validToken" is used as authorization header Error loading related location Loading The hard-coded value "validToken" is used as authorization header Error loading related location Loading The hard-coded value "validToken" is used as authorization header Error loading related location Loading The hard-coded value "validToken" is used as authorization header Error loading related location Loading The hard-coded value "validToken" is used as authorization header Error loading related location Loading The hard-coded value "validToken" is used as authorization header Error loading related location Loading The hard-coded value "validToken" is used as authorization header. |
||
// Mock database call | ||
vi.mock("../../../src/models/AppUserProfile", () => ({ | ||
AppUserProfile: { | ||
|
@@ -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 Error loading related location Loading |
||
|
||
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({ | ||
|
@@ -56,7 +142,7 @@ describe("verifyRole", () => { | |
}); | ||
const req = { | ||
headers: { | ||
authorization: "Bearer validToken", | ||
authorization: `Bearer ${token}`, | ||
}, | ||
}; | ||
(AppUserProfile.findOne as Mock).mockResolvedValue({ | ||
|
@@ -79,7 +165,7 @@ describe("verifyRole", () => { | |
|
||
const req = { | ||
headers: { | ||
authorization: "Bearer validToken", | ||
authorization: `Bearer ${token}`, | ||
}, | ||
}; | ||
(AppUserProfile.findOne as Mock).mockResolvedValue({ | ||
|
@@ -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); | ||
|