tests: vaults-2 (#2368)

This commit is contained in:
Mo
2023-07-26 04:55:58 -05:00
committed by GitHub
parent 6ad5d028af
commit 7222ca7fd0
47 changed files with 900 additions and 310 deletions

View File

@@ -1,4 +1,4 @@
export enum KeySystemRootKeyPasswordType {
export enum KeySystemPasswordType {
UserInputted = 'user_inputted',
Randomized = 'randomized',
}

View File

@@ -1,6 +1,6 @@
import { ProtocolVersion } from '@standardnotes/common'
import { KeySystemIdentifier } from '../../Syncable/KeySystemRootKey/KeySystemIdentifier'
import { KeySystemRootKeyPasswordType } from './KeySystemRootKeyPasswordType'
import { KeySystemPasswordType } from './KeySystemPasswordType'
/**
* Key params are public data that contain information about how a root key was created.
@@ -11,6 +11,6 @@ export interface KeySystemRootKeyParamsInterface {
systemIdentifier: KeySystemIdentifier
seed: string
version: ProtocolVersion
passwordType: KeySystemRootKeyPasswordType
passwordType: KeySystemPasswordType
creationTimestamp: number
}

View File

@@ -0,0 +1,127 @@
import {
Collection,
DecryptedCollectionElement,
DeletedCollectionElement,
EncryptedCollectionElement,
} from './Collection'
import { FullyFormedPayloadInterface } from '../../Abstract/Payload'
class TestCollection<P extends FullyFormedPayloadInterface = FullyFormedPayloadInterface> extends Collection<
P,
DecryptedCollectionElement,
EncryptedCollectionElement,
DeletedCollectionElement
> {}
describe('Collection', () => {
let collection: TestCollection
beforeEach(() => {
collection = new TestCollection()
})
it('should initialize correctly', () => {
expect(collection.map).toEqual({})
expect(collection.typedMap).toEqual({})
expect(collection.referenceMap).toBeDefined()
expect(collection.conflictMap).toBeDefined()
})
it('should set and get element correctly', () => {
const testElement = {
uuid: 'test-uuid',
content_type: 'test-type',
content: {},
references: [],
} as unknown as FullyFormedPayloadInterface
collection.set(testElement)
const element = collection.find('test-uuid')
expect(element).toBe(testElement)
})
it('should check existence of an element correctly', () => {
const testElement = {
uuid: 'test-uuid',
content_type: 'test-type',
content: {},
references: [],
} as unknown as FullyFormedPayloadInterface
collection.set(testElement)
const hasElement = collection.has('test-uuid')
expect(hasElement).toBe(true)
})
it('should return all elements', () => {
const testElement1 = {
uuid: 'test-uuid-1',
content_type: 'test-type',
content: {},
references: [],
} as unknown as FullyFormedPayloadInterface
const testElement2 = {
uuid: 'test-uuid-2',
content_type: 'test-type',
content: {},
references: [],
} as unknown as FullyFormedPayloadInterface
collection.set(testElement1)
collection.set(testElement2)
const allElements = collection.all()
expect(allElements).toEqual([testElement1, testElement2])
})
it('should add uuid to invalidsIndex if element is error decrypting', () => {
const testElement = {
uuid: 'test-uuid',
content_type: 'test-type',
content: 'encrypted content',
errorDecrypting: true,
} as unknown as FullyFormedPayloadInterface
collection.set(testElement)
expect(collection.invalidsIndex.has(testElement.uuid)).toBe(true)
})
it('should add uuid to invalidsIndex if element is encrypted', () => {
const testElement = {
uuid: 'test-uuid',
content_type: 'test-type',
content: 'encrypted content',
} as unknown as FullyFormedPayloadInterface
collection.set(testElement)
expect(collection.invalidsIndex.has(testElement.uuid)).toBe(true)
})
it('should remove uuid from invalidsIndex if element is not encrypted', () => {
const testElement1 = {
uuid: 'test-uuid-1',
content_type: 'test-type',
content: 'encrypted content',
errorDecrypting: true,
} as unknown as FullyFormedPayloadInterface
const testElement2 = {
uuid: 'test-uuid-1',
content_type: 'test-type',
content: {},
references: [],
} as unknown as FullyFormedPayloadInterface
collection.set(testElement1)
expect(collection.invalidsIndex.has(testElement1.uuid)).toBe(true)
collection.set(testElement2)
expect(collection.invalidsIndex.has(testElement2.uuid)).toBe(false)
})
})

View File

@@ -59,7 +59,7 @@ export abstract class Collection<
}
isErrorDecryptingElement = (e: Decrypted | Encrypted | Deleted): e is Encrypted => {
return this.isEncryptedElement(e) && e.errorDecrypting === true
return this.isEncryptedElement(e)
}
isDeletedElement = (e: Decrypted | Encrypted | Deleted): e is Deleted => {
@@ -78,10 +78,10 @@ export abstract class Collection<
conflictMapCopy?: UuidMap,
) {
if (copy) {
this.map = mapCopy!
this.typedMap = typedMapCopy!
this.referenceMap = referenceMapCopy!
this.conflictMap = conflictMapCopy!
this.map = mapCopy as Record<string, Element>
this.typedMap = typedMapCopy as Record<string, Element[]>
this.referenceMap = referenceMapCopy as UuidMap
this.conflictMap = conflictMapCopy as UuidMap
} else {
this.referenceMap = new UuidMap()
this.conflictMap = new UuidMap()

View File

@@ -2,7 +2,7 @@ import { ConflictStrategy, DecryptedItem } from '../../Abstract/Item'
import { DecryptedPayloadInterface } from '../../Abstract/Payload'
import { HistoryEntryInterface } from '../../Runtime/History'
import { KeySystemRootKeyParamsInterface } from '../../Local/KeyParams/KeySystemRootKeyParamsInterface'
import { KeySystemRootKeyPasswordType } from '../../Local/KeyParams/KeySystemRootKeyPasswordType'
import { KeySystemPasswordType } from '../../Local/KeyParams/KeySystemPasswordType'
import { SharedVaultListingInterface, VaultListingInterface } from './VaultListingInterface'
import { VaultListingContent } from './VaultListingContent'
import { KeySystemRootKeyStorageMode } from '../KeySystemRootKey/KeySystemRootKeyStorageMode'
@@ -44,7 +44,7 @@ export class VaultListing extends DecryptedItem<VaultListingContent> implements
return incomingKeyTimestamp > baseKeyTimestamp ? ConflictStrategy.KeepApply : ConflictStrategy.KeepBase
}
get keyPasswordType(): KeySystemRootKeyPasswordType {
get keyPasswordType(): KeySystemPasswordType {
return this.rootKeyParams.passwordType
}

View File

@@ -1,6 +1,6 @@
import { KeySystemIdentifier } from '../KeySystemRootKey/KeySystemIdentifier'
import { KeySystemRootKeyParamsInterface } from '../../Local/KeyParams/KeySystemRootKeyParamsInterface'
import { KeySystemRootKeyPasswordType } from '../../Local/KeyParams/KeySystemRootKeyPasswordType'
import { KeySystemPasswordType } from '../../Local/KeyParams/KeySystemPasswordType'
import { KeySystemRootKeyStorageMode } from '../KeySystemRootKey/KeySystemRootKeyStorageMode'
import { VaultListingSharingInfo } from './VaultListingSharingInfo'
import { VaultListingContent } from './VaultListingContent'
@@ -17,7 +17,7 @@ export interface VaultListingInterface extends DecryptedItemInterface<VaultListi
sharing?: VaultListingSharingInfo
get keyPasswordType(): KeySystemRootKeyPasswordType
get keyPasswordType(): KeySystemPasswordType
isSharedVaultListing(): this is SharedVaultListingInterface
get key_system_identifier(): undefined

View File

@@ -37,7 +37,7 @@ export * from './Device/Platform'
export * from './Local/KeyParams/RootKeyParamsInterface'
export * from './Local/KeyParams/KeySystemRootKeyParamsInterface'
export * from './Local/KeyParams/KeySystemRootKeyPasswordType'
export * from './Local/KeyParams/KeySystemPasswordType'
export * from './Local/RootKey/KeychainTypes'
export * from './Local/RootKey/RootKeyContent'
export * from './Local/RootKey/RootKeyInterface'