1
0
mirror of https://github.com/bitwarden/browser synced 2026-02-17 18:09:17 +00:00

Added tests.

This commit is contained in:
Jared McCannon
2026-01-20 11:40:53 -06:00
parent 4253f05de7
commit ee0a0b5313
2 changed files with 148 additions and 14 deletions

View File

@@ -1,6 +1,6 @@
import { TestBed } from "@angular/core/testing";
import { MockProxy, mock } from "jest-mock-extended";
import { of } from "rxjs";
import { of, throwError } from "rxjs";
import {
OrganizationUserApiService,
@@ -178,25 +178,64 @@ describe("MemberActionsService", () => {
});
describe("restoreUser", () => {
it("should successfully restore a user", async () => {
organizationUserApiService.restoreOrganizationUser.mockResolvedValue(undefined);
describe("when feature flag is enabled", () => {
beforeEach(() => {
configService.getFeatureFlag$.mockReturnValue(of(true));
});
const result = await service.restoreUser(mockOrganization, userIdToManage);
it("should call organizationUserService.restoreUser", async () => {
organizationUserService.restoreUser.mockReturnValue(of(undefined));
expect(result).toEqual({ success: true });
expect(organizationUserApiService.restoreOrganizationUser).toHaveBeenCalledWith(
organizationId,
userIdToManage,
);
const result = await service.restoreUser(mockOrganization, userIdToManage);
expect(result).toEqual({ success: true });
expect(organizationUserService.restoreUser).toHaveBeenCalledWith(
mockOrganization,
userIdToManage,
);
expect(organizationUserApiService.restoreOrganizationUser).not.toHaveBeenCalled();
});
it("should handle errors from organizationUserService.restoreUser", async () => {
const errorMessage = "Restore failed";
organizationUserService.restoreUser.mockReturnValue(
throwError(() => new Error(errorMessage)),
);
const result = await service.restoreUser(mockOrganization, userIdToManage);
expect(result).toEqual({ success: false, error: errorMessage });
});
});
it("should handle restore errors", async () => {
const errorMessage = "Restore failed";
organizationUserApiService.restoreOrganizationUser.mockRejectedValue(new Error(errorMessage));
describe("when feature flag is disabled", () => {
beforeEach(() => {
configService.getFeatureFlag$.mockReturnValue(of(false));
});
const result = await service.restoreUser(mockOrganization, userIdToManage);
it("should call organizationUserApiService.restoreOrganizationUser", async () => {
organizationUserApiService.restoreOrganizationUser.mockResolvedValue(undefined);
expect(result).toEqual({ success: false, error: errorMessage });
const result = await service.restoreUser(mockOrganization, userIdToManage);
expect(result).toEqual({ success: true });
expect(organizationUserApiService.restoreOrganizationUser).toHaveBeenCalledWith(
organizationId,
userIdToManage,
);
expect(organizationUserService.restoreUser).not.toHaveBeenCalled();
});
it("should handle errors", async () => {
const errorMessage = "Restore failed";
organizationUserApiService.restoreOrganizationUser.mockRejectedValue(
new Error(errorMessage),
);
const result = await service.restoreUser(mockOrganization, userIdToManage);
expect(result).toEqual({ success: false, error: errorMessage });
});
});
});

View File

@@ -61,6 +61,8 @@ describe("DefaultOrganizationUserService", () => {
organizationUserApiService = {
postOrganizationUserConfirm: jest.fn(),
postOrganizationUserBulkConfirm: jest.fn(),
restoreOrganizationUser_vNext: jest.fn(),
restoreManyOrganizationUsers_vNext: jest.fn(),
} as any;
accountService = {
@@ -174,4 +176,97 @@ describe("DefaultOrganizationUserService", () => {
});
});
});
describe("buildRestoreUserRequest", () => {
beforeEach(() => {
setupCommonMocks();
});
it("should build a restore request with encrypted collection name", (done) => {
service.buildRestoreUserRequest(mockOrganization).subscribe({
next: (request) => {
expect(i18nService.t).toHaveBeenCalledWith("myItems");
expect(encryptService.encryptString).toHaveBeenCalledWith(
mockDefaultCollectionName,
mockOrgKey,
);
expect(request).toEqual({
defaultUserCollectionName: mockEncryptedCollectionName.encryptedString,
});
done();
},
error: done,
});
});
});
describe("restoreUser", () => {
beforeEach(() => {
setupCommonMocks();
organizationUserApiService.restoreOrganizationUser_vNext.mockReturnValue(Promise.resolve());
});
it("should restore a user successfully", (done) => {
service.restoreUser(mockOrganization, mockUserId).subscribe({
next: () => {
expect(i18nService.t).toHaveBeenCalledWith("myItems");
expect(encryptService.encryptString).toHaveBeenCalledWith(
mockDefaultCollectionName,
mockOrgKey,
);
expect(organizationUserApiService.restoreOrganizationUser_vNext).toHaveBeenCalledWith(
mockOrganization.id,
mockUserId,
{
defaultUserCollectionName: mockEncryptedCollectionName.encryptedString,
},
);
done();
},
error: done,
});
});
});
describe("bulkRestoreUsers", () => {
const mockUserIds = ["user-1", "user-2"];
const mockBulkResponse = {
data: [
{ id: "user-1", error: null } as OrganizationUserBulkResponse,
{ id: "user-2", error: null } as OrganizationUserBulkResponse,
],
} as ListResponse<OrganizationUserBulkResponse>;
beforeEach(() => {
setupCommonMocks();
organizationUserApiService.restoreManyOrganizationUsers_vNext.mockReturnValue(
Promise.resolve(mockBulkResponse),
);
});
it("should bulk restore users successfully", (done) => {
service.bulkRestoreUsers(mockOrganization, mockUserIds).subscribe({
next: (response) => {
expect(i18nService.t).toHaveBeenCalledWith("myItems");
expect(encryptService.encryptString).toHaveBeenCalledWith(
mockDefaultCollectionName,
mockOrgKey,
);
expect(
organizationUserApiService.restoreManyOrganizationUsers_vNext,
).toHaveBeenCalledWith(
mockOrganization.id,
expect.objectContaining({
userIds: mockUserIds,
defaultUserCollectionName: mockEncryptedCollectionName.encryptedString,
}),
);
expect(response).toEqual(mockBulkResponse);
done();
},
error: done,
});
});
});
});