-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[PM-23995] Updated change kdf component for Forced update KDF settings #16516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
1fb0f81
move change-kdf into KM ownership
mzieniukbw b92cd27
Change kdf component update for Forced KDF update
mzieniukbw 0691579
correct validators load on init
mzieniukbw a20f052
incorrect feature flag observable check
mzieniukbw 5f8443c
unit test coverage
mzieniukbw 7b06cd6
unit test coverage
mzieniukbw f246c3b
Merge branch 'main' into km/pm-23995-new-change-kdf-component
mzieniukbw 97c3eed
remove Close button, wrong icon
mzieniukbw 02b69a4
Merge branch 'main' into km/pm-23995-new-change-kdf-component
mzieniukbw 5a64c34
change to `pm-23995-no-logout-on-kdf-change` feature flag
mzieniukbw 11e716c
updated unit tests
mzieniukbw 2b1d0d3
revert bad merge
mzieniukbw 2692ee9
Merge branch 'main' into km/pm-23995-new-change-kdf-component
mzieniukbw 40708ea
updated wording, TS strict enabled, use form controls, updated tests
mzieniukbw c03fa92
use localisation for button label
mzieniukbw 6f7ca6a
small margin in confirmation dialog
mzieniukbw 06985a4
Merge branch 'main' into km/pm-23995-new-change-kdf-component
mzieniukbw 03718a8
simpler I18nService mock
mzieniukbw 3c6bca4
Merge branch 'main' into km/pm-23995-new-change-kdf-component
mzieniukbw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
253 changes: 253 additions & 0 deletions
253
apps/web/src/app/key-management/change-kdf/change-kdf-confirmation.component.spec.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,253 @@ | ||
| import { ComponentFixture, TestBed } from "@angular/core/testing"; | ||
| import { FormControl } from "@angular/forms"; | ||
| import { mock, MockProxy } from "jest-mock-extended"; | ||
| import { of } from "rxjs"; | ||
|
|
||
| import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; | ||
| import { ChangeKdfService } from "@bitwarden/common/key-management/kdf/change-kdf-service.abstraction"; | ||
| import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service"; | ||
| import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; | ||
| import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service"; | ||
| import { FakeAccountService, mockAccountServiceWith } from "@bitwarden/common/spec"; | ||
| import { UserId } from "@bitwarden/common/types/guid"; | ||
| import { DIALOG_DATA, DialogRef, ToastService } from "@bitwarden/components"; | ||
| import { KdfType, PBKDF2KdfConfig, Argon2KdfConfig } from "@bitwarden/key-management"; | ||
|
|
||
| import { SharedModule } from "../../shared"; | ||
|
|
||
| import { ChangeKdfConfirmationComponent } from "./change-kdf-confirmation.component"; | ||
|
|
||
| describe("ChangeKdfConfirmationComponent", () => { | ||
| let component: ChangeKdfConfirmationComponent; | ||
| let fixture: ComponentFixture<ChangeKdfConfirmationComponent>; | ||
|
|
||
| // Mock Services | ||
| let mockI18nService: MockProxy<I18nService>; | ||
| let mockMessagingService: MockProxy<MessagingService>; | ||
| let mockToastService: MockProxy<ToastService>; | ||
| let mockDialogRef: MockProxy<DialogRef<ChangeKdfConfirmationComponent>>; | ||
| let mockConfigService: MockProxy<ConfigService>; | ||
| let accountService: FakeAccountService; | ||
| let mockChangeKdfService: MockProxy<ChangeKdfService>; | ||
|
|
||
| const mockUserId = "user-id" as UserId; | ||
| const mockEmail = "email"; | ||
| const mockMasterPassword = "master-password"; | ||
| const mockDialogData = jest.fn(); | ||
| const kdfConfig = new PBKDF2KdfConfig(600_001); | ||
|
|
||
| beforeEach(() => { | ||
| mockI18nService = mock<I18nService>(); | ||
| mockMessagingService = mock<MessagingService>(); | ||
| mockToastService = mock<ToastService>(); | ||
| mockDialogRef = mock<DialogRef<ChangeKdfConfirmationComponent>>(); | ||
| mockConfigService = mock<ConfigService>(); | ||
| accountService = mockAccountServiceWith(mockUserId, { email: mockEmail }); | ||
| mockChangeKdfService = mock<ChangeKdfService>(); | ||
|
|
||
| // Mock i18n service | ||
| mockI18nService.t.mockImplementation((key: string) => { | ||
| switch (key) { | ||
| case "encKeySettingsChanged": | ||
| return "Encryption key settings changed"; | ||
| case "logBackIn": | ||
| return "Please log back in"; | ||
| default: | ||
| return key; | ||
| } | ||
| }); | ||
|
|
||
| // Mock config service feature flag | ||
| mockConfigService.getFeatureFlag$.mockReturnValue(of(false)); | ||
|
|
||
| mockDialogData.mockReturnValue({ | ||
| kdf: KdfType.PBKDF2_SHA256, | ||
| kdfConfig, | ||
| }); | ||
|
|
||
| TestBed.configureTestingModule({ | ||
| declarations: [ChangeKdfConfirmationComponent], | ||
| imports: [SharedModule], | ||
| providers: [ | ||
| { provide: I18nService, useValue: mockI18nService }, | ||
| { provide: MessagingService, useValue: mockMessagingService }, | ||
| { provide: AccountService, useValue: accountService }, | ||
| { provide: ToastService, useValue: mockToastService }, | ||
| { provide: DialogRef, useValue: mockDialogRef }, | ||
| { provide: ConfigService, useValue: mockConfigService }, | ||
| { provide: ChangeKdfService, useValue: mockChangeKdfService }, | ||
| { | ||
| provide: DIALOG_DATA, | ||
| useFactory: mockDialogData, | ||
| }, | ||
| ], | ||
| }); | ||
| }); | ||
|
|
||
| describe("Component Initialization", () => { | ||
| it("should create component with PBKDF2 config", () => { | ||
| const fixture = TestBed.createComponent(ChangeKdfConfirmationComponent); | ||
| const component = fixture.componentInstance; | ||
|
|
||
| expect(component).toBeTruthy(); | ||
| expect(component.kdfConfig).toBeInstanceOf(PBKDF2KdfConfig); | ||
| expect(component.kdfConfig.iterations).toBe(600_001); | ||
| }); | ||
|
|
||
| it("should create component with Argon2id config", () => { | ||
| mockDialogData.mockReturnValue({ | ||
| kdf: KdfType.Argon2id, | ||
| kdfConfig: new Argon2KdfConfig(4, 65, 5), | ||
| }); | ||
|
|
||
| const fixture = TestBed.createComponent(ChangeKdfConfirmationComponent); | ||
| const component = fixture.componentInstance; | ||
|
|
||
| expect(component).toBeTruthy(); | ||
| expect(component.kdfConfig).toBeInstanceOf(Argon2KdfConfig); | ||
| const kdfConfig = component.kdfConfig as Argon2KdfConfig; | ||
| expect(kdfConfig.iterations).toBe(4); | ||
| expect(kdfConfig.memory).toBe(65); | ||
| expect(kdfConfig.parallelism).toBe(5); | ||
| }); | ||
|
|
||
| it("should initialize form with required master password field", () => { | ||
| const fixture = TestBed.createComponent(ChangeKdfConfirmationComponent); | ||
| const component = fixture.componentInstance; | ||
|
|
||
| expect(component.form.get("masterPassword")?.value).toEqual(null); | ||
| expect(component.form.get("masterPassword")).toBeInstanceOf(FormControl); | ||
| expect(component.form.get("masterPassword")?.hasError("required")).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Form Validation", () => { | ||
| beforeEach(() => { | ||
| fixture = TestBed.createComponent(ChangeKdfConfirmationComponent); | ||
| component = fixture.componentInstance; | ||
| }); | ||
|
|
||
| it("should be invalid when master password is empty", () => { | ||
| component.form.get("masterPassword")?.setValue(""); | ||
| expect(component.form.invalid).toBe(true); | ||
| }); | ||
|
|
||
| it("should be valid when master password is provided", () => { | ||
| component.form.get("masterPassword")?.setValue(mockMasterPassword); | ||
| expect(component.form.valid).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe("submit method", () => { | ||
| describe("should not update kdf and not show success toast", () => { | ||
| beforeEach(() => { | ||
| fixture = TestBed.createComponent(ChangeKdfConfirmationComponent); | ||
| component = fixture.componentInstance; | ||
|
|
||
| component.form.get("masterPassword")?.setValue(mockMasterPassword); | ||
| }); | ||
|
|
||
| it("when form is invalid", async () => { | ||
| // Arrange | ||
| component.form.get("masterPassword")?.setValue(""); | ||
| expect(component.form.invalid).toBe(true); | ||
|
|
||
| // Act | ||
| await component.submit(); | ||
|
|
||
| // Assert | ||
| expect(mockChangeKdfService.updateUserKdfParams).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("when no active account", async () => { | ||
| accountService.activeAccount$ = of(null); | ||
|
|
||
| await expect(component.submit()).rejects.toThrow("Null or undefined account"); | ||
|
|
||
| expect(mockChangeKdfService.updateUserKdfParams).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("when kdf is invalid", async () => { | ||
| // Arrange | ||
| component.kdfConfig = new PBKDF2KdfConfig(1); | ||
|
|
||
| // Act | ||
| await expect(component.submit).rejects.toThrow(); | ||
|
|
||
| expect(mockChangeKdfService.updateUserKdfParams).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("should update kdf and show success toast", () => { | ||
| it("should set loading to true during submission", async () => { | ||
| // Arrange | ||
| let loadingDuringExecution = false; | ||
| mockChangeKdfService.updateUserKdfParams.mockImplementation(async () => { | ||
| loadingDuringExecution = component.loading; | ||
| }); | ||
|
|
||
| const fixture = TestBed.createComponent(ChangeKdfConfirmationComponent); | ||
| const component = fixture.componentInstance; | ||
|
|
||
| component.form.get("masterPassword")?.setValue(mockMasterPassword); | ||
|
|
||
| // Act | ||
| await component.submit(); | ||
|
|
||
| expect(loadingDuringExecution).toBe(true); | ||
| expect(component.loading).toBe(false); | ||
| }); | ||
|
|
||
| it("not logout and close dialog when feature flag is enabled", async () => { | ||
mzieniukbw marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Arrange | ||
| mockConfigService.getFeatureFlag$.mockReturnValue(of(true)); | ||
|
|
||
| const fixture = TestBed.createComponent(ChangeKdfConfirmationComponent); | ||
| const component = fixture.componentInstance; | ||
|
|
||
| component.form.get("masterPassword")?.setValue(mockMasterPassword); | ||
|
|
||
| // Act | ||
| await component.submit(); | ||
|
|
||
| // Assert | ||
| expect(mockChangeKdfService.updateUserKdfParams).toHaveBeenCalledWith( | ||
| mockMasterPassword, | ||
| kdfConfig, | ||
| mockUserId, | ||
| ); | ||
| expect(mockToastService.showToast).toHaveBeenCalledWith({ | ||
| variant: "success", | ||
| message: "Encryption key settings changed", | ||
| }); | ||
| expect(mockDialogRef.close).toHaveBeenCalled(); | ||
| expect(mockMessagingService.send).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("with logout message and send logout when feature flag is enabled", async () => { | ||
mzieniukbw marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Arrange | ||
| const fixture = TestBed.createComponent(ChangeKdfConfirmationComponent); | ||
| const component = fixture.componentInstance; | ||
|
|
||
| component.form.get("masterPassword")?.setValue(mockMasterPassword); | ||
|
|
||
| // Act | ||
| await component.submit(); | ||
|
|
||
| // Assert | ||
| expect(mockChangeKdfService.updateUserKdfParams).toHaveBeenCalledWith( | ||
| mockMasterPassword, | ||
| kdfConfig, | ||
| mockUserId, | ||
| ); | ||
| expect(mockToastService.showToast).toHaveBeenCalledWith({ | ||
| variant: "success", | ||
| title: "Encryption key settings changed", | ||
| message: "Please log back in", | ||
| }); | ||
| expect(mockMessagingService.send).toHaveBeenCalledWith("logout"); | ||
| expect(mockDialogRef.close).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.