test(users): add unit tests for UsersService with mocked dependencies
This commit is contained in:
@@ -128,4 +128,112 @@ describe("UsersService", () => {
|
|||||||
expect(result[0].displayName).toBe("New");
|
expect(result[0].displayName).toBe("New");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("clearUserCache", () => {
|
||||||
|
it("should delete cache", async () => {
|
||||||
|
await service.clearUserCache("u1");
|
||||||
|
expect(mockCacheManager.del).toHaveBeenCalledWith("users/profile/u1");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("findByEmailHash", () => {
|
||||||
|
it("should call repository.findByEmailHash", async () => {
|
||||||
|
mockUsersRepository.findByEmailHash.mockResolvedValue({ uuid: "u1" });
|
||||||
|
const result = await service.findByEmailHash("hash");
|
||||||
|
expect(result.uuid).toBe("u1");
|
||||||
|
expect(mockUsersRepository.findByEmailHash).toHaveBeenCalledWith("hash");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("findOneWithPrivateData", () => {
|
||||||
|
it("should return user with roles", async () => {
|
||||||
|
mockUsersRepository.findOneWithPrivateData.mockResolvedValue({ uuid: "u1" });
|
||||||
|
mockRbacService.getUserRoles.mockResolvedValue(["admin"]);
|
||||||
|
const result = await service.findOneWithPrivateData("u1");
|
||||||
|
expect(result.roles).toEqual(["admin"]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("findAll", () => {
|
||||||
|
it("should return all users", async () => {
|
||||||
|
mockUsersRepository.findAll.mockResolvedValue([{ uuid: "u1" }]);
|
||||||
|
mockUsersRepository.countAll.mockResolvedValue(1);
|
||||||
|
|
||||||
|
const result = await service.findAll(10, 0);
|
||||||
|
|
||||||
|
expect(result.totalCount).toBe(1);
|
||||||
|
expect(result.data[0].uuid).toBe("u1");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("findPublicProfile", () => {
|
||||||
|
it("should return public profile", async () => {
|
||||||
|
mockUsersRepository.findByUsername.mockResolvedValue({
|
||||||
|
uuid: "u1",
|
||||||
|
username: "u1",
|
||||||
|
});
|
||||||
|
const result = await service.findPublicProfile("u1");
|
||||||
|
expect(result.username).toBe("u1");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("updateConsent", () => {
|
||||||
|
it("should update consent", async () => {
|
||||||
|
await service.updateConsent("u1", "v1", "v2");
|
||||||
|
expect(mockUsersRepository.update).toHaveBeenCalledWith("u1", {
|
||||||
|
termsVersion: "v1",
|
||||||
|
privacyVersion: "v2",
|
||||||
|
gdprAcceptedAt: expect.any(Date),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("setTwoFactorSecret", () => {
|
||||||
|
it("should set 2fa secret", async () => {
|
||||||
|
await service.setTwoFactorSecret("u1", "secret");
|
||||||
|
expect(mockUsersRepository.update).toHaveBeenCalledWith("u1", {
|
||||||
|
twoFactorSecret: "secret",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("toggleTwoFactor", () => {
|
||||||
|
it("should toggle 2fa", async () => {
|
||||||
|
await service.toggleTwoFactor("u1", true);
|
||||||
|
expect(mockUsersRepository.update).toHaveBeenCalledWith("u1", {
|
||||||
|
isTwoFactorEnabled: true,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("getTwoFactorSecret", () => {
|
||||||
|
it("should return 2fa secret", async () => {
|
||||||
|
mockUsersRepository.getTwoFactorSecret.mockResolvedValue("secret");
|
||||||
|
const result = await service.getTwoFactorSecret("u1");
|
||||||
|
expect(result).toBe("secret");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("exportUserData", () => {
|
||||||
|
it("should return all user data", async () => {
|
||||||
|
mockUsersRepository.findOneWithPrivateData.mockResolvedValue({ uuid: "u1" });
|
||||||
|
mockUsersRepository.getUserContents.mockResolvedValue([]);
|
||||||
|
mockUsersRepository.getUserFavorites.mockResolvedValue([]);
|
||||||
|
|
||||||
|
const result = await service.exportUserData("u1");
|
||||||
|
|
||||||
|
expect(result.profile).toBeDefined();
|
||||||
|
expect(result.contents).toBeDefined();
|
||||||
|
expect(result.favorites).toBeDefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("remove", () => {
|
||||||
|
it("should soft delete user", async () => {
|
||||||
|
await service.remove("u1");
|
||||||
|
expect(mockUsersRepository.softDeleteUserAndContents).toHaveBeenCalledWith(
|
||||||
|
"u1",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user