mirror of
https://github.com/bitwarden/browser
synced 2025-12-06 00:13:28 +00:00
[PM-21041] Fix cipher view security tasks fetching (#14569)
* [PM-21041] Add taskEnabled$ dependency to tasks$ observable * [PM-21041] Rework cipher view component to only check tasks for organization Login type ciphers - Remove dependency on feature flag check (handled by tasks$ observable now) - Add try/catch in case of request failures to avoid breaking component initialization * [PM-21041] Remove now redundant taskEnabled$ chain * [PM-21041] Fix tests
This commit is contained in:
@@ -108,6 +108,34 @@ describe("Default task service", () => {
|
||||
});
|
||||
|
||||
describe("tasks$", () => {
|
||||
beforeEach(() => {
|
||||
mockGetFeatureFlag$.mockReturnValue(new BehaviorSubject(true));
|
||||
mockGetAllOrgs$.mockReturnValue(
|
||||
new BehaviorSubject([
|
||||
{
|
||||
useRiskInsights: true,
|
||||
},
|
||||
] as Organization[]),
|
||||
);
|
||||
});
|
||||
|
||||
it("should return an empty array if tasks are not enabled", async () => {
|
||||
mockGetAllOrgs$.mockReturnValue(
|
||||
new BehaviorSubject([
|
||||
{
|
||||
useRiskInsights: false,
|
||||
},
|
||||
] as Organization[]),
|
||||
);
|
||||
|
||||
const { tasks$ } = service;
|
||||
|
||||
const result = await firstValueFrom(tasks$("user-id" as UserId));
|
||||
|
||||
expect(result.length).toBe(0);
|
||||
expect(mockApiSend).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should fetch tasks from the API when the state is null", async () => {
|
||||
mockApiSend.mockResolvedValue({
|
||||
data: [
|
||||
@@ -153,6 +181,34 @@ describe("Default task service", () => {
|
||||
});
|
||||
|
||||
describe("pendingTasks$", () => {
|
||||
beforeEach(() => {
|
||||
mockGetFeatureFlag$.mockReturnValue(new BehaviorSubject(true));
|
||||
mockGetAllOrgs$.mockReturnValue(
|
||||
new BehaviorSubject([
|
||||
{
|
||||
useRiskInsights: true,
|
||||
},
|
||||
] as Organization[]),
|
||||
);
|
||||
});
|
||||
|
||||
it("should return an empty array if tasks are not enabled", async () => {
|
||||
mockGetAllOrgs$.mockReturnValue(
|
||||
new BehaviorSubject([
|
||||
{
|
||||
useRiskInsights: false,
|
||||
},
|
||||
] as Organization[]),
|
||||
);
|
||||
|
||||
const { pendingTasks$ } = service;
|
||||
|
||||
const result = await firstValueFrom(pendingTasks$("user-id" as UserId));
|
||||
|
||||
expect(result.length).toBe(0);
|
||||
expect(mockApiSend).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should filter tasks to only pending tasks", async () => {
|
||||
fakeStateProvider.singleUser.mockFor("user-id" as UserId, SECURITY_TASKS, [
|
||||
{
|
||||
|
||||
@@ -1,4 +1,14 @@
|
||||
import { combineLatest, filter, map, merge, Observable, of, Subscription, switchMap } from "rxjs";
|
||||
import {
|
||||
combineLatest,
|
||||
filter,
|
||||
map,
|
||||
merge,
|
||||
Observable,
|
||||
of,
|
||||
Subscription,
|
||||
switchMap,
|
||||
distinctUntilChanged,
|
||||
} from "rxjs";
|
||||
|
||||
import { ApiService } from "@bitwarden/common/abstractions/api.service";
|
||||
import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction";
|
||||
@@ -45,20 +55,30 @@ export class DefaultTaskService implements TaskService {
|
||||
.organizations$(userId)
|
||||
.pipe(map((orgs) => orgs.some((o) => o.useRiskInsights))),
|
||||
this.configService.getFeatureFlag$(FeatureFlag.SecurityTasks),
|
||||
]).pipe(map(([atLeastOneOrgEnabled, flagEnabled]) => atLeastOneOrgEnabled && flagEnabled));
|
||||
]).pipe(
|
||||
map(([atLeastOneOrgEnabled, flagEnabled]) => atLeastOneOrgEnabled && flagEnabled),
|
||||
distinctUntilChanged(),
|
||||
);
|
||||
});
|
||||
|
||||
tasks$ = perUserCache$((userId) => {
|
||||
return this.taskState(userId).state$.pipe(
|
||||
switchMap(async (tasks) => {
|
||||
if (tasks == null) {
|
||||
await this.fetchTasksFromApi(userId);
|
||||
return null;
|
||||
return this.tasksEnabled$(userId).pipe(
|
||||
switchMap((enabled) => {
|
||||
if (!enabled) {
|
||||
return of([]);
|
||||
}
|
||||
return tasks;
|
||||
return this.taskState(userId).state$.pipe(
|
||||
switchMap(async (tasks) => {
|
||||
if (tasks == null) {
|
||||
await this.fetchTasksFromApi(userId);
|
||||
return null;
|
||||
}
|
||||
return tasks;
|
||||
}),
|
||||
filterOutNullish(),
|
||||
map((tasks) => tasks.map((t) => new SecurityTask(t))),
|
||||
);
|
||||
}),
|
||||
filterOutNullish(),
|
||||
map((tasks) => tasks.map((t) => new SecurityTask(t))),
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user