Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: vaults-2 #2368

Merged
merged 4 commits into from Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -3,7 +3,7 @@ import { V004Algorithm } from '../../../../Algorithm'
import {
KeySystemRootKeyInterface,
KeySystemRootKeyParamsInterface,
KeySystemRootKeyPasswordType,
KeySystemPasswordType,
} from '@standardnotes/models'
import { ProtocolVersion } from '@standardnotes/common'
import { DeriveKeySystemRootKeyUseCase } from './DeriveKeySystemRootKey'
Expand All @@ -20,7 +20,7 @@ export class CreateRandomKeySystemRootKey {

const keyParams: KeySystemRootKeyParamsInterface = {
systemIdentifier: dto.systemIdentifier,
passwordType: KeySystemRootKeyPasswordType.Randomized,
passwordType: KeySystemPasswordType.Randomized,
creationTimestamp: new Date().getTime(),
seed,
version,
Expand Down
Expand Up @@ -4,7 +4,7 @@ import {
KeySystemIdentifier,
KeySystemRootKeyInterface,
KeySystemRootKeyParamsInterface,
KeySystemRootKeyPasswordType,
KeySystemPasswordType,
} from '@standardnotes/models'
import { ProtocolVersion } from '@standardnotes/common'
import { DeriveKeySystemRootKeyUseCase } from './DeriveKeySystemRootKey'
Expand All @@ -19,7 +19,7 @@ export class CreateUserInputKeySystemRootKey {

const keyParams: KeySystemRootKeyParamsInterface = {
systemIdentifier: dto.systemIdentifier,
passwordType: KeySystemRootKeyPasswordType.UserInputted,
passwordType: KeySystemPasswordType.UserInputted,
creationTimestamp: new Date().getTime(),
seed,
version,
Expand Down
@@ -1,4 +1,4 @@
export enum KeySystemRootKeyPasswordType {
export enum KeySystemPasswordType {
UserInputted = 'user_inputted',
Randomized = 'randomized',
}
@@ -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.
Expand All @@ -11,6 +11,6 @@ export interface KeySystemRootKeyParamsInterface {
systemIdentifier: KeySystemIdentifier
seed: string
version: ProtocolVersion
passwordType: KeySystemRootKeyPasswordType
passwordType: KeySystemPasswordType
creationTimestamp: number
}
127 changes: 127 additions & 0 deletions packages/models/src/Domain/Runtime/Collection/Collection.spec.ts
@@ -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)
})
})
10 changes: 5 additions & 5 deletions packages/models/src/Domain/Runtime/Collection/Collection.ts
Expand Up @@ -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 => {
Expand All @@ -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()
Expand Down
Expand Up @@ -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'
Expand Down Expand Up @@ -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
}

Expand Down
@@ -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'
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/models/src/Domain/index.ts
Expand Up @@ -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'
Expand Down
Expand Up @@ -2,24 +2,24 @@ import { ItemsKeyMutator } from '@standardnotes/encryption'
import { MutatorClientInterface } from '../../../Mutator/MutatorClientInterface'
import { ItemManagerInterface } from '../../../Item/ItemManagerInterface'
import { CreateNewDefaultItemsKey } from './CreateNewDefaultItemsKey'
import { RemoveItemsLocally } from '../../../UseCase/RemoveItemsLocally'
import { DiscardItemsLocally } from '../../../UseCase/DiscardItemsLocally'
import { FindDefaultItemsKey } from './FindDefaultItemsKey'

export class CreateNewItemsKeyWithRollback {
constructor(
private mutator: MutatorClientInterface,
private items: ItemManagerInterface,
private createDefaultItemsKey: CreateNewDefaultItemsKey,
private removeItemsLocally: RemoveItemsLocally,
private findDefaultItemsKey: FindDefaultItemsKey,
private _createDefaultItemsKey: CreateNewDefaultItemsKey,
private _discardItemsLocally: DiscardItemsLocally,
private _findDefaultItemsKey: FindDefaultItemsKey,
) {}

async execute(): Promise<() => Promise<void>> {
const currentDefaultItemsKey = this.findDefaultItemsKey.execute(this.items.getDisplayableItemsKeys()).getValue()
const newDefaultItemsKey = await this.createDefaultItemsKey.execute()
const currentDefaultItemsKey = this._findDefaultItemsKey.execute(this.items.getDisplayableItemsKeys()).getValue()
const newDefaultItemsKey = await this._createDefaultItemsKey.execute()

const rollback = async () => {
await this.removeItemsLocally.execute([newDefaultItemsKey])
await this._discardItemsLocally.execute([newDefaultItemsKey])

if (currentDefaultItemsKey) {
await this.mutator.changeItem<ItemsKeyMutator>(currentDefaultItemsKey, (mutator) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/services/src/Domain/Item/ItemManagerInterface.ts
Expand Up @@ -80,7 +80,7 @@ export interface ItemManagerInterface extends AbstractService {
): T[]
subItemsMatchingPredicates<T extends DecryptedItemInterface>(items: T[], predicates: PredicateInterface<T>[]): T[]
removeAllItemsFromMemory(): Promise<void>
removeItemsLocally(items: AnyItemInterface[]): void
removeItemsFromMemory(items: AnyItemInterface[]): void
getDirtyItems(): (DecryptedItemInterface | DeletedItemInterface)[]
getTagLongTitle(tag: SNTag): string
getSortedTagsForItem(item: DecryptedItemInterface<ItemContent>): SNTag[]
Expand Down