-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PM-4963] Migrate breach report components (#10045)
* WIP - migrate exposed passwords report components * lint fix * migrate components in reports * migrate breach and unsecured websites reports * undo change routing * revert changes to reports * revert changes * migrate breach report component * update form * revert back to text input * revert change to logic * layout fixes * add spec * fix typo * undo changes to exposed passowords report * fix test --------- Co-authored-by: jordan-bite <[email protected]>
- Loading branch information
1 parent
e22568f
commit b2d4d1b
Showing
3 changed files
with
162 additions
and
28 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
128 changes: 128 additions & 0 deletions
128
apps/web/src/app/tools/reports/pages/breach-report.component.spec.ts
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 |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// eslint-disable-next-line no-restricted-imports | ||
import { ComponentFixture, TestBed } from "@angular/core/testing"; | ||
import { ReactiveFormsModule } from "@angular/forms"; | ||
import { mock, MockProxy } from "jest-mock-extended"; | ||
import { BehaviorSubject } from "rxjs"; | ||
|
||
import { I18nPipe } from "@bitwarden/angular/platform/pipes/i18n.pipe"; | ||
import { AuditService } from "@bitwarden/common/abstractions/audit.service"; | ||
import { AccountInfo, AccountService } from "@bitwarden/common/auth/abstractions/account.service"; | ||
import { BreachAccountResponse } from "@bitwarden/common/models/response/breach-account.response"; | ||
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; | ||
import { UserId } from "@bitwarden/common/types/guid"; | ||
|
||
import { BreachReportComponent } from "./breach-report.component"; | ||
|
||
const breachedAccounts = [ | ||
new BreachAccountResponse({ | ||
addedDate: "2021-01-01", | ||
breachDate: "2021-01-01", | ||
dataClasses: ["test"], | ||
description: "test", | ||
domain: "test.com", | ||
isActive: true, | ||
isVerified: true, | ||
logoPath: "test", | ||
modifiedDate: "2021-01-01", | ||
name: "test", | ||
pwnCount: 1, | ||
title: "test", | ||
}), | ||
]; | ||
|
||
describe("BreachReportComponent", () => { | ||
let component: BreachReportComponent; | ||
let fixture: ComponentFixture<BreachReportComponent>; | ||
let auditService: MockProxy<AuditService>; | ||
let accountService: MockProxy<AccountService>; | ||
const activeAccountSubject = new BehaviorSubject<{ id: UserId } & AccountInfo>({ | ||
id: "testId" as UserId, | ||
email: "[email protected]", | ||
emailVerified: true, | ||
name: "Test User", | ||
}); | ||
|
||
beforeEach(async () => { | ||
auditService = mock<AuditService>(); | ||
accountService = mock<AccountService>(); | ||
accountService.activeAccount$ = activeAccountSubject; | ||
|
||
await TestBed.configureTestingModule({ | ||
declarations: [BreachReportComponent, I18nPipe], | ||
imports: [ReactiveFormsModule], | ||
providers: [ | ||
{ | ||
provide: AuditService, | ||
useValue: auditService, | ||
}, | ||
{ | ||
provide: AccountService, | ||
useValue: accountService, | ||
}, | ||
{ | ||
provide: I18nService, | ||
useValue: mock<I18nService>(), | ||
}, | ||
], | ||
}).compileComponents(); | ||
}); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(BreachReportComponent); | ||
component = fixture.componentInstance as BreachReportComponent; | ||
fixture.detectChanges(); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should initialize component", () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
|
||
it("should initialize form with account email", async () => { | ||
expect(component.formGroup.get("username").value).toEqual("[email protected]"); | ||
}); | ||
|
||
it("should mark form as touched and show validation error if form is invalid on submit", async () => { | ||
component.formGroup.get("username").setValue(""); | ||
await component.submit(); | ||
|
||
expect(component.formGroup.touched).toBe(true); | ||
expect(component.formGroup.invalid).toBe(true); | ||
}); | ||
|
||
it("should call auditService.breachedAccounts with lowercase username", async () => { | ||
auditService.breachedAccounts.mockResolvedValue(breachedAccounts); | ||
component.formGroup.get("username").setValue("validUsername"); | ||
|
||
await component.submit(); | ||
|
||
expect(auditService.breachedAccounts).toHaveBeenCalledWith("validusername"); | ||
}); | ||
|
||
it("should set breachedAccounts and checkedUsername after successful submit", async () => { | ||
auditService.breachedAccounts.mockResolvedValue(breachedAccounts); | ||
|
||
await component.submit(); | ||
|
||
expect(component.breachedAccounts).toEqual(breachedAccounts); | ||
expect(component.checkedUsername).toEqual("[email protected]"); | ||
}); | ||
|
||
it("should set error to true if auditService.breachedAccounts throws an error", async () => { | ||
auditService.breachedAccounts.mockRejectedValue(new Error("test error")); | ||
component.formGroup.get("username").setValue("validUsername"); | ||
|
||
await component.submit(); | ||
|
||
expect(component.error).toBe(true); | ||
}); | ||
|
||
it("should set loading to false after submit", async () => { | ||
auditService.breachedAccounts.mockResolvedValue([]); | ||
component.formGroup.get("username").setValue("validUsername"); | ||
|
||
await component.submit(); | ||
|
||
expect(component.loading).toBe(false); | ||
}); | ||
}); |
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