3 changed files with 135 additions and 0 deletions
@ -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(); |
||||
}); |
||||
}); |
||||
}); |
||||
}); |
||||
@ -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…
Reference in new issue