Browse Source

[PM-23758] Api method to save and retrieve report summary (#15705)

pull/15711/head
Vijay Oommen 5 months ago committed by GitHub
parent
commit
b33bdd60ae
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 7
      bitwarden_license/bit-common/src/dirt/reports/risk-insights/models/password-health.ts
  2. 83
      bitwarden_license/bit-common/src/dirt/reports/risk-insights/services/risk-insights-api.service.spec.ts
  3. 45
      bitwarden_license/bit-common/src/dirt/reports/risk-insights/services/risk-insights-api.service.ts

7
bitwarden_license/bit-common/src/dirt/reports/risk-insights/models/password-health.ts

@ -158,6 +158,13 @@ export interface PasswordHealthReportApplicationsRequest { @@ -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 {

83
bitwarden_license/bit-common/src/dirt/reports/risk-insights/services/risk-insights-api.service.spec.ts

@ -0,0 +1,83 @@ @@ -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();
});
});
});
});

45
bitwarden_license/bit-common/src/dirt/reports/risk-insights/services/risk-insights-api.service.ts

@ -0,0 +1,45 @@ @@ -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>);
}
}
Loading…
Cancel
Save