-
Notifications
You must be signed in to change notification settings - Fork 1
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
LightQv
authored and
LightQv
committed
Feb 7, 2025
1 parent
cc5c814
commit b0cce1a
Showing
1 changed file
with
51 additions
and
41 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 |
---|---|---|
|
@@ -4,10 +4,11 @@ import argon2 from "argon2"; | |
import jwt from "jsonwebtoken"; | ||
|
||
describe("UserResolver", () => { | ||
let userResolver: UserResolver | ||
let mockContext: any | ||
let mockUser: User | ||
let passwordTest: string | ||
let userResolver: UserResolver; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
let mockContext: any; | ||
let mockUser: User; | ||
let passwordTest: string; | ||
|
||
beforeEach(() => { | ||
userResolver = new UserResolver(); | ||
|
@@ -16,63 +17,72 @@ describe("UserResolver", () => { | |
setHeader: jest.fn(), | ||
}, | ||
payload: { id: "e5fb990e-f9d9-4858-82d1-1fd1755485a5" }, | ||
} | ||
}; | ||
mockUser = { | ||
id: "e5fb990e-f9d9-4858-82d1-1fd1755485a5", | ||
username: "Pierre", | ||
email: "[email protected]" | ||
} as User | ||
passwordTest = "password123" | ||
process.env.JWT_SECRET_KEY = "TEST_KEY" | ||
}) | ||
email: "[email protected]", | ||
} as User; | ||
passwordTest = "password123"; | ||
process.env.JWT_SECRET_KEY = "TEST_KEY"; | ||
}); | ||
|
||
it("createUser should hash password, save user, generate token, and return user info", async () => { | ||
jest.spyOn(argon2, "hash").mockResolvedValue("hashedPassword") | ||
jest.spyOn(User, "save").mockResolvedValue(mockUser) | ||
jest.spyOn(jwt, "sign").mockImplementation(() => "mockToken") | ||
jest.spyOn(argon2, "hash").mockResolvedValue("hashedPassword"); | ||
jest.spyOn(User, "save").mockResolvedValue(mockUser); | ||
jest.spyOn(jwt, "sign").mockImplementation(() => "mockToken"); | ||
|
||
const result = await userResolver.createUser( | ||
mockUser.username, | ||
mockUser.email, | ||
passwordTest, | ||
mockContext | ||
) | ||
mockUser.username, | ||
mockUser.email, | ||
passwordTest, | ||
mockContext, | ||
); | ||
|
||
expect(User.save).toHaveBeenCalled() | ||
expect(User.save).toHaveBeenCalled(); | ||
expect(mockContext.res.setHeader).toHaveBeenCalledWith( | ||
"Set-Cookie", | ||
expect.stringContaining("token=mockToken") | ||
) | ||
"Set-Cookie", | ||
expect.stringContaining("token=mockToken"), | ||
); | ||
expect(JSON.parse(result)).toEqual({ | ||
id: mockUser.id, | ||
username: mockUser.username, | ||
email: mockUser.email | ||
}) | ||
}) | ||
email: mockUser.email, | ||
}); | ||
}); | ||
|
||
it("should log in a user and set a cookie", async () => { | ||
jest.spyOn(User, "findOneByOrFail").mockResolvedValue(mockUser) | ||
jest.spyOn(argon2, "verify").mockResolvedValue(true) | ||
jest.spyOn(jwt, "sign").mockImplementation(() => "mockToken") | ||
jest.spyOn(User, "findOneByOrFail").mockResolvedValue(mockUser); | ||
jest.spyOn(argon2, "verify").mockResolvedValue(true); | ||
jest.spyOn(jwt, "sign").mockImplementation(() => "mockToken"); | ||
|
||
const result = await userResolver.login(mockUser.username, passwordTest, mockContext); | ||
const result = await userResolver.login( | ||
mockUser.username, | ||
passwordTest, | ||
mockContext, | ||
); | ||
|
||
expect(User.findOneByOrFail).toHaveBeenCalledWith({ email: mockUser.username }) | ||
expect(User.findOneByOrFail).toHaveBeenCalledWith({ | ||
email: mockUser.username, | ||
}); | ||
expect(mockContext.res.setHeader).toHaveBeenCalledWith( | ||
"Set-Cookie", | ||
expect.stringContaining("token=mockToken") | ||
) | ||
"Set-Cookie", | ||
expect.stringContaining("token=mockToken"), | ||
); | ||
expect(JSON.parse(result)).toEqual({ | ||
id: mockUser.id, | ||
username: mockUser.username, | ||
email: mockUser.email | ||
}) | ||
}) | ||
email: mockUser.email, | ||
}); | ||
}); | ||
|
||
it("should log out a user", async () => { | ||
const result = await userResolver.logout(mockContext) | ||
const result = await userResolver.logout(mockContext); | ||
|
||
expect(mockContext.res.setHeader).toHaveBeenCalledWith("Set-Cookie", "token=;Max-Age=0") | ||
expect(result).toBe("Logged out") | ||
}) | ||
}) | ||
expect(mockContext.res.setHeader).toHaveBeenCalledWith( | ||
"Set-Cookie", | ||
"token=;Max-Age=0", | ||
); | ||
expect(result).toBe("Logged out"); | ||
}); | ||
}); |