mirror of
https://github.com/bitwarden/browser
synced 2026-02-05 03:03:26 +00:00
[PM-23758] Api method to save and retrieve report summary (#15705)
This commit is contained in:
@@ -158,6 +158,13 @@ export interface PasswordHealthReportApplicationsRequest {
|
||||
url: string;
|
||||
}
|
||||
|
||||
export interface EncryptedDataModel {
|
||||
organizationId: OrganizationId;
|
||||
encryptedData: string;
|
||||
encryptionKey: string;
|
||||
date: Date;
|
||||
}
|
||||
|
||||
// FIXME: update to use a const object instead of a typescript enum
|
||||
// eslint-disable-next-line @bitwarden/platform/no-enums
|
||||
export enum DrawerType {
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
import { mock } from "jest-mock-extended";
|
||||
|
||||
import { ApiService } from "@bitwarden/common/abstractions/api.service";
|
||||
|
||||
import { EncryptedDataModel } from "../models/password-health";
|
||||
|
||||
import { RiskInsightsApiService } from "./risk-insights-api.service";
|
||||
|
||||
describe("RiskInsightsApiService", () => {
|
||||
let service: RiskInsightsApiService;
|
||||
const mockApiService = mock<ApiService>();
|
||||
|
||||
beforeEach(() => {
|
||||
service = new RiskInsightsApiService(mockApiService);
|
||||
});
|
||||
|
||||
it("should be created", () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
|
||||
describe("getRiskInsightsSummary", () => {
|
||||
it("should call apiService.send with correct parameters and return an Observable", (done) => {
|
||||
const orgId = "org123";
|
||||
const minDate = new Date("2024-01-01");
|
||||
const maxDate = new Date("2024-01-31");
|
||||
const mockResponse: EncryptedDataModel[] = [{ encryptedData: "abc" } as EncryptedDataModel];
|
||||
|
||||
mockApiService.send.mockResolvedValueOnce(mockResponse);
|
||||
|
||||
service.getRiskInsightsSummary(orgId, minDate, maxDate).subscribe((result) => {
|
||||
expect(mockApiService.send).toHaveBeenCalledWith(
|
||||
"GET",
|
||||
`organization-report-summary/org123?from=2024-01-01&to=2024-01-31`,
|
||||
null,
|
||||
true,
|
||||
true,
|
||||
);
|
||||
expect(result).toEqual(mockResponse);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("saveRiskInsightsSummary", () => {
|
||||
it("should call apiService.send with correct parameters and return an Observable", (done) => {
|
||||
const data: EncryptedDataModel = { encryptedData: "xyz" } as EncryptedDataModel;
|
||||
|
||||
mockApiService.send.mockResolvedValueOnce(undefined);
|
||||
|
||||
service.saveRiskInsightsSummary(data).subscribe((result) => {
|
||||
expect(mockApiService.send).toHaveBeenCalledWith(
|
||||
"POST",
|
||||
"organization-report-summary",
|
||||
data,
|
||||
true,
|
||||
true,
|
||||
);
|
||||
expect(result).toBeUndefined();
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateRiskInsightsSummary", () => {
|
||||
it("should call apiService.send with correct parameters and return an Observable", (done) => {
|
||||
const data: EncryptedDataModel = { encryptedData: "xyz" } as EncryptedDataModel;
|
||||
|
||||
mockApiService.send.mockResolvedValueOnce(undefined);
|
||||
|
||||
service.updateRiskInsightsSummary(data).subscribe((result) => {
|
||||
expect(mockApiService.send).toHaveBeenCalledWith(
|
||||
"PUT",
|
||||
"organization-report-summary",
|
||||
data,
|
||||
true,
|
||||
true,
|
||||
);
|
||||
expect(result).toBeUndefined();
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,45 @@
|
||||
import { from, Observable } from "rxjs";
|
||||
|
||||
import { ApiService } from "@bitwarden/common/abstractions/api.service";
|
||||
|
||||
import { EncryptedDataModel } from "../models/password-health";
|
||||
|
||||
export class RiskInsightsApiService {
|
||||
constructor(private apiService: ApiService) {}
|
||||
|
||||
getRiskInsightsSummary(
|
||||
orgId: string,
|
||||
minDate: Date,
|
||||
maxDate: Date,
|
||||
): Observable<EncryptedDataModel[]> {
|
||||
const minDateStr = minDate.toISOString().split("T")[0];
|
||||
const maxDateStr = maxDate.toISOString().split("T")[0];
|
||||
const dbResponse = this.apiService.send(
|
||||
"GET",
|
||||
`organization-report-summary/${orgId.toString()}?from=${minDateStr}&to=${maxDateStr}`,
|
||||
null,
|
||||
true,
|
||||
true,
|
||||
);
|
||||
|
||||
return from(dbResponse as Promise<EncryptedDataModel[]>);
|
||||
}
|
||||
|
||||
saveRiskInsightsSummary(data: EncryptedDataModel): Observable<void> {
|
||||
const dbResponse = this.apiService.send(
|
||||
"POST",
|
||||
"organization-report-summary",
|
||||
data,
|
||||
true,
|
||||
true,
|
||||
);
|
||||
|
||||
return from(dbResponse as Promise<void>);
|
||||
}
|
||||
|
||||
updateRiskInsightsSummary(data: EncryptedDataModel): Observable<void> {
|
||||
const dbResponse = this.apiService.send("PUT", "organization-report-summary", data, true, true);
|
||||
|
||||
return from(dbResponse as Promise<void>);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user