mirror of
https://github.com/bitwarden/browser.git
synced 2024-10-24 08:09:59 +02:00
Update upsert method
This commit is contained in:
parent
8264d5ad77
commit
45210eb32f
@ -9,7 +9,7 @@ import { CollectionData, Collection, CollectionView } from "../models";
|
|||||||
export abstract class CollectionvNextService {
|
export abstract class CollectionvNextService {
|
||||||
encryptedCollections$: (userId$: Observable<UserId>) => Observable<Collection[]>;
|
encryptedCollections$: (userId$: Observable<UserId>) => Observable<Collection[]>;
|
||||||
decryptedCollections$: (userId$: Observable<UserId>) => Observable<CollectionView[]>;
|
decryptedCollections$: (userId$: Observable<UserId>) => Observable<CollectionView[]>;
|
||||||
upsert: (collection: CollectionData | CollectionData[]) => Promise<any>;
|
upsert: (collection: CollectionData | CollectionData[], userId: UserId) => Promise<any>;
|
||||||
replace: (collections: { [id: string]: CollectionData }, userId: UserId) => Promise<any>;
|
replace: (collections: { [id: string]: CollectionData }, userId: UserId) => Promise<any>;
|
||||||
/**
|
/**
|
||||||
* Clear decrypted state without affecting encrypted state.
|
* Clear decrypted state without affecting encrypted state.
|
||||||
|
@ -167,14 +167,74 @@ describe("DefaultCollectionService", () => {
|
|||||||
|
|
||||||
it("handles null collection state", async () => {
|
it("handles null collection state", async () => {
|
||||||
await stateProvider.setUserState(ENCRYPTED_COLLECTION_DATA_KEY, null, userId);
|
await stateProvider.setUserState(ENCRYPTED_COLLECTION_DATA_KEY, null, userId);
|
||||||
cryptoKeys.next({});
|
|
||||||
|
|
||||||
const decryptedCollections = await firstValueFrom(
|
const decryptedCollections = await firstValueFrom(
|
||||||
collectionService.decryptedCollections$(of(userId)),
|
collectionService.encryptedCollections$(of(userId)),
|
||||||
);
|
);
|
||||||
expect(decryptedCollections.length).toBe(0);
|
expect(decryptedCollections.length).toBe(0);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("upsert", () => {
|
||||||
|
it("upserts to existing collections", async () => {
|
||||||
|
const org1 = Utils.newGuid() as OrganizationId;
|
||||||
|
const collection1 = collectionDataFactory(org1);
|
||||||
|
|
||||||
|
const org2 = Utils.newGuid() as OrganizationId;
|
||||||
|
const collection2 = collectionDataFactory(org2);
|
||||||
|
|
||||||
|
await stateProvider.setUserState(
|
||||||
|
ENCRYPTED_COLLECTION_DATA_KEY,
|
||||||
|
{
|
||||||
|
[collection1.id]: collection1,
|
||||||
|
[collection2.id]: collection2,
|
||||||
|
},
|
||||||
|
userId,
|
||||||
|
);
|
||||||
|
|
||||||
|
const updatedCollection1 = Object.assign(new CollectionData({} as any), collection1, {
|
||||||
|
name: makeEncString("UPDATED_ENC_NAME_" + collection1.id).encryptedString,
|
||||||
|
});
|
||||||
|
const newCollection3 = collectionDataFactory(org2);
|
||||||
|
|
||||||
|
await collectionService.upsert([updatedCollection1, newCollection3], userId);
|
||||||
|
|
||||||
|
const result = await firstValueFrom(collectionService.encryptedCollections$(of(userId)));
|
||||||
|
expect(result.length).toBe(3);
|
||||||
|
expect(result).toIncludeAllPartialMembers([
|
||||||
|
{
|
||||||
|
id: collection1.id,
|
||||||
|
name: makeEncString("UPDATED_ENC_NAME_" + collection1.id),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: collection2.id,
|
||||||
|
name: makeEncString("ENC_NAME_" + collection2.id),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: newCollection3.id,
|
||||||
|
name: makeEncString("ENC_NAME_" + newCollection3.id),
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("upserts to a null state", async () => {
|
||||||
|
const org1 = Utils.newGuid() as OrganizationId;
|
||||||
|
const collection1 = collectionDataFactory(org1);
|
||||||
|
|
||||||
|
await stateProvider.setUserState(ENCRYPTED_COLLECTION_DATA_KEY, null, userId);
|
||||||
|
|
||||||
|
await collectionService.upsert(collection1, userId);
|
||||||
|
|
||||||
|
const result = await firstValueFrom(collectionService.encryptedCollections$(of(userId)));
|
||||||
|
expect(result.length).toBe(1);
|
||||||
|
expect(result).toIncludeAllPartialMembers([
|
||||||
|
{
|
||||||
|
id: collection1.id,
|
||||||
|
name: makeEncString("ENC_NAME_" + collection1.id),
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
const collectionDataFactory = (orgId: OrganizationId) => {
|
const collectionDataFactory = (orgId: OrganizationId) => {
|
||||||
|
@ -73,11 +73,11 @@ export class DefaultCollectionvNextService implements CollectionvNextService {
|
|||||||
return userId$.pipe(switchMap((userId) => this.decryptedCollectionState(userId).state$));
|
return userId$.pipe(switchMap((userId) => this.decryptedCollectionState(userId).state$));
|
||||||
}
|
}
|
||||||
|
|
||||||
async upsert(toUpdate: CollectionData | CollectionData[]): Promise<void> {
|
async upsert(toUpdate: CollectionData | CollectionData[], userId: UserId): Promise<void> {
|
||||||
if (toUpdate == null) {
|
if (toUpdate == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await this.activeUserEncryptedCollectionDataState.update((collections) => {
|
await this.encryptedCollectionState(userId).update((collections) => {
|
||||||
if (collections == null) {
|
if (collections == null) {
|
||||||
collections = {};
|
collections = {};
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user