1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-21 18:53:29 +00:00

add back events for browser refresh extension (#11085)

- something went sideways in a merge
This commit is contained in:
Nick Krantz
2024-09-16 14:02:20 -05:00
committed by GitHub
parent 51a2ec393c
commit 26f3dcfc66
22 changed files with 300 additions and 16 deletions

View File

@@ -3,6 +3,8 @@ import { ActivatedRoute, Router } from "@angular/router";
import { mock, MockProxy } from "jest-mock-extended";
import { BehaviorSubject } from "rxjs";
import { EventCollectionService } from "@bitwarden/common/abstractions/event/event-collection.service";
import { EventType } from "@bitwarden/common/enums";
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
@@ -44,12 +46,14 @@ describe("AddEditV2Component", () => {
const disable = jest.fn();
const navigate = jest.fn();
const back = jest.fn().mockResolvedValue(null);
const collect = jest.fn().mockResolvedValue(null);
beforeEach(async () => {
buildConfig.mockClear();
disable.mockClear();
navigate.mockClear();
back.mockClear();
collect.mockClear();
addEditCipherInfo$ = new BehaviorSubject(null);
cipherServiceMock = mock<CipherService>();
@@ -66,6 +70,7 @@ describe("AddEditV2Component", () => {
{ provide: ActivatedRoute, useValue: { queryParams: queryParams$ } },
{ provide: I18nService, useValue: { t: (key: string) => key } },
{ provide: CipherService, useValue: cipherServiceMock },
{ provide: EventCollectionService, useValue: { collect } },
],
})
.overrideProvider(CipherFormConfigService, {
@@ -122,6 +127,57 @@ describe("AddEditV2Component", () => {
});
});
describe("analytics", () => {
it("does not log viewed event when mode is add", fakeAsync(() => {
queryParams$.next({});
tick();
expect(collect).not.toHaveBeenCalled();
}));
it("does not log viewed event whe mode is clone", fakeAsync(() => {
queryParams$.next({ cipherId: "222-333-444-5555", clone: "true" });
buildConfigResponse.originalCipher = {} as Cipher;
tick();
expect(collect).not.toHaveBeenCalled();
}));
it("logs viewed event when mode is edit", fakeAsync(() => {
buildConfigResponse.originalCipher = {
edit: true,
id: "222-333-444-5555",
organizationId: "444-555-666",
} as Cipher;
queryParams$.next({ cipherId: "222-333-444-5555" });
tick();
expect(collect).toHaveBeenCalledWith(
EventType.Cipher_ClientViewed,
"222-333-444-5555",
false,
"444-555-666",
);
}));
it("logs viewed event whe mode is partial-edit", fakeAsync(() => {
buildConfigResponse.originalCipher = { edit: false } as Cipher;
queryParams$.next({ cipherId: "222-333-444-5555", orgId: "444-555-666" });
tick();
expect(collect).toHaveBeenCalledWith(
EventType.Cipher_ClientViewed,
"222-333-444-5555",
false,
"444-555-666",
);
}));
});
describe("addEditCipherInfo initialization", () => {
it("populates config.initialValues with `addEditCipherInfo` values", fakeAsync(() => {
const addEditCipherInfo = {

View File

@@ -6,6 +6,8 @@ import { ActivatedRoute, Params, Router } from "@angular/router";
import { firstValueFrom, map, switchMap } from "rxjs";
import { JslibModule } from "@bitwarden/angular/jslib.module";
import { EventCollectionService } from "@bitwarden/common/abstractions/event/event-collection.service";
import { EventType } from "@bitwarden/common/enums";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { CipherId, CollectionId, OrganizationId } from "@bitwarden/common/types/guid";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
@@ -160,6 +162,7 @@ export class AddEditV2Component implements OnInit {
private popupRouterCacheService: PopupRouterCacheService,
private router: Router,
private cipherService: CipherService,
private eventCollectionService: EventCollectionService,
) {
this.subscribeToParams();
}
@@ -275,6 +278,15 @@ export class AddEditV2Component implements OnInit {
await this.cipherService.setAddEditCipherInfo(null);
}
if (["edit", "partial-edit"].includes(config.mode) && config.originalCipher?.id) {
await this.eventCollectionService.collect(
EventType.Cipher_ClientViewed,
config.originalCipher.id,
false,
config.originalCipher.organizationId,
);
}
return config;
}),
)

View File

@@ -3,7 +3,9 @@ import { ActivatedRoute, Router } from "@angular/router";
import { mock } from "jest-mock-extended";
import { Subject } from "rxjs";
import { EventCollectionService } from "@bitwarden/common/abstractions/event/event-collection.service";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { EventType } from "@bitwarden/common/enums";
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
@@ -29,10 +31,12 @@ describe("ViewV2Component", () => {
let fixture: ComponentFixture<ViewV2Component>;
const params$ = new Subject();
const mockNavigate = jest.fn();
const collect = jest.fn().mockResolvedValue(null);
const mockCipher = {
id: "122-333-444",
type: CipherType.Login,
orgId: "222-444-555",
};
const mockVaultPopupAutofillService = {
@@ -48,6 +52,7 @@ describe("ViewV2Component", () => {
beforeEach(async () => {
mockNavigate.mockClear();
collect.mockClear();
await TestBed.configureTestingModule({
imports: [ViewV2Component],
@@ -59,6 +64,7 @@ describe("ViewV2Component", () => {
{ provide: ConfigService, useValue: mock<ConfigService>() },
{ provide: PopupRouterCacheService, useValue: mock<PopupRouterCacheService>() },
{ provide: ActivatedRoute, useValue: { queryParams: params$ } },
{ provide: EventCollectionService, useValue: { collect } },
{
provide: I18nService,
useValue: {
@@ -122,5 +128,18 @@ describe("ViewV2Component", () => {
expect(component.headerText).toEqual("viewItemHeader note");
}));
it("sends viewed event", fakeAsync(() => {
params$.next({ cipherId: "122-333-444" });
flush(); // Resolve all promises
expect(collect).toHaveBeenCalledWith(
EventType.Cipher_ClientViewed,
mockCipher.id,
false,
undefined,
);
}));
});
});

View File

@@ -6,9 +6,11 @@ import { ActivatedRoute, Router } from "@angular/router";
import { firstValueFrom, map, Observable, switchMap } from "rxjs";
import { JslibModule } from "@bitwarden/angular/jslib.module";
import { EventCollectionService } from "@bitwarden/common/abstractions/event/event-collection.service";
import { Organization } from "@bitwarden/common/admin-console/models/domain/organization";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { AUTOFILL_ID, SHOW_AUTOFILL_BUTTON } from "@bitwarden/common/autofill/constants";
import { EventType } from "@bitwarden/common/enums";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
@@ -73,6 +75,7 @@ export class ViewV2Component {
private toastService: ToastService,
private vaultPopupAutofillService: VaultPopupAutofillService,
private accountService: AccountService,
private eventCollectionService: EventCollectionService,
) {
this.subscribeToParams();
}
@@ -90,6 +93,13 @@ export class ViewV2Component {
if (this.loadAction === AUTOFILL_ID || this.loadAction === SHOW_AUTOFILL_BUTTON) {
await this.vaultPopupAutofillService.doAutofill(this.cipher);
}
await this.eventCollectionService.collect(
EventType.Cipher_ClientViewed,
cipher.id,
false,
cipher.organizationId,
);
}),
takeUntilDestroyed(),
)