small refactoring
This commit is contained in:
@@ -32,7 +32,7 @@ describe("UserService", () => {
|
||||
|
||||
describe("create (singleton)", () => {
|
||||
it("should create a singleton instance", async () => {
|
||||
const instance1 = userService; // Bereits im beforeEach erstellt
|
||||
const instance1 = userService;
|
||||
const instance2 = await UserService.create();
|
||||
|
||||
expect(instance1).toBe(instance2);
|
||||
@@ -60,7 +60,7 @@ describe("UserService", () => {
|
||||
expect(mockedUserModel.findByIdAndUpdate).toHaveBeenCalledWith(
|
||||
userId,
|
||||
updateData,
|
||||
{ new: true, projection: { password: 0 } }
|
||||
{ new: true }
|
||||
);
|
||||
expect(result).toEqual(updatedUser);
|
||||
});
|
||||
@@ -74,42 +74,6 @@ describe("UserService", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateUser", () => {
|
||||
it("should update user using id field", async () => {
|
||||
const user = { id: "507f1f77bcf86cd799439011", name: "Test User" };
|
||||
mockedUserModel.findByIdAndUpdate.mockReturnValue(createMockMongooseQuery(user) as any);
|
||||
|
||||
const result = await userService.updateUser(user as any);
|
||||
|
||||
expect(mockedUserModel.findByIdAndUpdate).toHaveBeenCalledWith(
|
||||
user.id,
|
||||
{ name: "Test User" },
|
||||
expect.any(Object)
|
||||
);
|
||||
expect(result).toEqual(user);
|
||||
});
|
||||
|
||||
it("should update user using _id field", async () => {
|
||||
const user = { _id: "507f1f77bcf86cd799439011", name: "Test User" };
|
||||
mockedUserModel.findByIdAndUpdate.mockReturnValue(createMockMongooseQuery(user) as any);
|
||||
|
||||
const result = await userService.updateUser(user as any);
|
||||
|
||||
expect(mockedUserModel.findByIdAndUpdate).toHaveBeenCalledWith(
|
||||
user._id,
|
||||
{ name: "Test User" },
|
||||
expect.any(Object)
|
||||
);
|
||||
expect(result).toEqual(user);
|
||||
});
|
||||
|
||||
it("should throw error if user has no id or _id", async () => {
|
||||
await expect(userService.updateUser({ name: "Test User" } as any)).rejects.toThrow(
|
||||
"updateUser requires user.id or user._id"
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getAllUsers", () => {
|
||||
it("should return all users without sensitive fields", async () => {
|
||||
const users = [{ name: "User1" }, { name: "User2" }];
|
||||
@@ -225,16 +189,18 @@ describe("UserService", () => {
|
||||
|
||||
describe("existsUserByName", () => {
|
||||
it("should return true if user exists", async () => {
|
||||
mockedUserModel.findOne.mockReturnValue(createMockMongooseQuery({ _id: "some-id" }) as any);
|
||||
// @ts-ignore
|
||||
mockedUserModel.countDocuments.mockReturnValue(1);
|
||||
|
||||
const result = await userService.existsUserByName("ExistingUser");
|
||||
|
||||
expect(mockedUserModel.findOne).toHaveBeenCalledWith({ name: "ExistingUser" });
|
||||
expect(mockedUserModel.countDocuments).toHaveBeenCalledWith({ name: "ExistingUser" });
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it("should return false if user does not exist", async () => {
|
||||
mockedUserModel.findOne.mockReturnValue(createMockMongooseQuery(null) as any);
|
||||
// @ts-ignore
|
||||
mockedUserModel.countDocuments.mockReturnValue(0);
|
||||
|
||||
const result = await userService.existsUserByName("NonExistentUser");
|
||||
|
||||
@@ -252,7 +218,7 @@ describe("UserService", () => {
|
||||
expect(mockedUserModel.findOneAndUpdate).toHaveBeenCalledWith(
|
||||
{ uuid: "uuid-123" },
|
||||
{ $unset: { spotifyConfig: 1 } },
|
||||
{ new: true, projection: { password: 0 } }
|
||||
{ new: true }
|
||||
);
|
||||
expect(result).toEqual(updatedUser);
|
||||
});
|
||||
|
||||
@@ -49,7 +49,6 @@ export const createMockUserService = () => ({
|
||||
getAllUsers: vi.fn(),
|
||||
getUserByUUID: vi.fn(),
|
||||
getUserById: vi.fn(),
|
||||
updateUser: vi.fn(),
|
||||
updateUserById: vi.fn(),
|
||||
getUserByName: vi.fn(),
|
||||
getSpotifyConfigByUUID: vi.fn(),
|
||||
|
||||
@@ -202,23 +202,6 @@ describe("RestAuth", () => {
|
||||
expect(authTokenCookie).toContain("Expires=Thu, 01 Jan 1970 00:00:00 GMT");
|
||||
});
|
||||
|
||||
it("should handle user with _id instead of id", async () => {
|
||||
const mockUser = {name: "testuser", password: "hashed", uuid: "uuid-123", _id: "user-id-123"};
|
||||
const mockToken = "jwt-token-123";
|
||||
|
||||
mockUserService.getUserAuthByName.mockResolvedValue(mockUser);
|
||||
mockPasswordUtils.comparePassword.mockResolvedValue(true);
|
||||
mockJwtAuthenticator.generateToken.mockReturnValue(mockToken);
|
||||
|
||||
await request(app).post("/auth/login").send(validLoginData).expect(200);
|
||||
|
||||
expect(mockJwtAuthenticator.generateToken).toHaveBeenCalledWith({
|
||||
username: "testuser",
|
||||
id: "user-id-123",
|
||||
uuid: "uuid-123",
|
||||
});
|
||||
});
|
||||
|
||||
it("should return not found when user does not exist", async () => {
|
||||
mockUserService.getUserAuthByName.mockResolvedValue(null);
|
||||
const response = await request(app).post("/auth/login").send(validLoginData).expect(404);
|
||||
|
||||
@@ -73,7 +73,7 @@ describe("RestUser", () => {
|
||||
};
|
||||
|
||||
mockedUserService.getUserByUUID.mockResolvedValue(mockUser);
|
||||
mockedUserService.updateUser.mockResolvedValue(mockUser);
|
||||
mockedUserService.updateUserById.mockResolvedValue(mockUser);
|
||||
|
||||
const response = await request(testEnv.app)
|
||||
.put("/user/me/spotify")
|
||||
@@ -82,8 +82,8 @@ describe("RestUser", () => {
|
||||
|
||||
expect(response.body.data.message).toBe("Spotify Config erfolgreich geändert");
|
||||
expect(mockedUserService.getUserByUUID).toHaveBeenCalledWith("test-user-uuid");
|
||||
expect(mockedUserService.updateUser).toHaveBeenCalledWith({
|
||||
...mockUser,
|
||||
expect(mockedUserService.updateUserById).toHaveBeenCalledWith(
|
||||
mockUser.id, {
|
||||
spotifyConfig: {
|
||||
accessToken: "access-token-123",
|
||||
refreshToken: "refresh-token-123",
|
||||
@@ -219,7 +219,7 @@ describe("RestUser", () => {
|
||||
mockedUserService.getUserByUUID.mockResolvedValue(mockUser);
|
||||
vi.mocked(PasswordUtils.validatePassword).mockReturnValue({valid: true});
|
||||
vi.mocked(PasswordUtils.hashPassword).mockResolvedValue("new-hashed-password");
|
||||
mockedUserService.updateUser.mockResolvedValue(mockUser);
|
||||
mockedUserService.updateUserById.mockResolvedValue(mockUser);
|
||||
|
||||
const response = await request(testEnv.app)
|
||||
.put("/user/me/password")
|
||||
@@ -229,8 +229,8 @@ describe("RestUser", () => {
|
||||
expect(response.body.data.message).toBe("Passwort erfolgreich geändert");
|
||||
expect(PasswordUtils.validatePassword).toHaveBeenCalledWith("newpassword123");
|
||||
expect(PasswordUtils.hashPassword).toHaveBeenCalledWith("newpassword123");
|
||||
expect(mockedUserService.updateUser).toHaveBeenCalledWith({
|
||||
...mockUser,
|
||||
expect(mockedUserService.updateUserById).toHaveBeenCalledWith(
|
||||
mockUser.id, {
|
||||
password: "new-hashed-password"
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user