refactor: import/export use cases (#2397)

This commit is contained in:
Mo
2023-08-10 08:08:17 -05:00
committed by GitHub
parent 5bb749b601
commit 1e965caf18
43 changed files with 1085 additions and 673 deletions

View File

@@ -4,7 +4,7 @@ import * as Collaboration from '../lib/Collaboration.js'
chai.use(chaiAsPromised)
const expect = chai.expect
describe.skip('vault importing', function () {
describe('vault importing', function () {
this.timeout(Factory.TwentySecondTimeout)
let context
@@ -15,45 +15,181 @@ describe.skip('vault importing', function () {
context = await Factory.createVaultsContextWithRealCrypto()
await context.launch()
await context.register()
})
afterEach(async function () {
await context.deinit()
localStorage.clear()
sinon.restore()
context = undefined
})
it('should import vaulted items with synced root key', async () => {
console.error('TODO: implement')
describe('exports', () => {
describe('no account and no passcode', () => {
it('should throw if attempting to create encrypted backup', async () => {
const vault = await context.vaults.createUserInputtedPasswordVault({
name: 'test vault',
userInputtedPassword: 'test password',
storagePreference: KeySystemRootKeyStorageMode.Ephemeral,
})
const note = await context.createSyncedNote('foo', 'bar')
await Collaboration.moveItemToVault(context, vault, note)
await context.application.vaultLocks.lockNonPersistentVault(vault)
await Factory.expectThrowsAsync(
() => context.application.createEncryptedBackupFile.execute(),
'Attempting root key encryption with no root key',
)
})
it('decrypted backups should export unlocked password vaulted items as decrypted and locked as encrypted', async () => {
const lockedVault = await context.vaults.createUserInputtedPasswordVault({
name: 'test vault',
userInputtedPassword: 'test password',
storagePreference: KeySystemRootKeyStorageMode.Ephemeral,
})
const lockedVaultNote = await context.createSyncedNote('foo', 'bar')
await Collaboration.moveItemToVault(context, lockedVault, lockedVaultNote)
await context.application.vaultLocks.lockNonPersistentVault(lockedVault)
const unlockedVault = await context.vaults.createUserInputtedPasswordVault({
name: 'test vault',
userInputtedPassword: 'test password',
storagePreference: KeySystemRootKeyStorageMode.Ephemeral,
})
const unlockedVaultNote = await context.createSyncedNote('har', 'zar')
await Collaboration.moveItemToVault(context, unlockedVault, unlockedVaultNote)
const backupData = (await context.application.createDecryptedBackupFile.execute()).getValue()
const backupLockedVaultNote = backupData.items.find((item) => item.uuid === lockedVaultNote.uuid)
expect(isEncryptedPayload(backupLockedVaultNote)).to.be.true
const backupUnlockedVaultNote = backupData.items.find((item) => item.uuid === unlockedVaultNote.uuid)
expect(isEncryptedPayload(backupUnlockedVaultNote)).to.be.false
})
})
})
it('should import vaulted items with non-present root key', async () => {
const vault = await context.vaults.createUserInputtedPasswordVault({
name: 'test vault',
userInputtedPassword: 'test password',
storagePreference: KeySystemRootKeyStorageMode.Ephemeral,
describe('imports', () => {
describe('password vaults', () => {
it('should import password vaulted items with non-present root key as-is without decrypting', async () => {
await context.register()
const vault = await context.vaults.createUserInputtedPasswordVault({
name: 'test vault',
userInputtedPassword: 'test password',
storagePreference: KeySystemRootKeyStorageMode.Ephemeral,
})
const note = await context.createSyncedNote('foo', 'bar')
await Collaboration.moveItemToVault(context, vault, note)
const backupData = (
await context.application.createEncryptedBackupFile.execute({ skipAuthorization: true })
).getValue()
const otherContext = await Factory.createVaultsContextWithRealCrypto()
otherContext.password = context.password
await otherContext.launch()
await otherContext.application.importData(backupData)
const expectedImportedItems = ['vault-items-key', 'note']
const invalidItems = otherContext.items.invalidItems
expect(invalidItems.length).to.equal(expectedImportedItems.length)
const encryptedItemsKey = invalidItems.find((item) => item.content_type === ContentType.TYPES.KeySystemItemsKey)
expect(encryptedItemsKey.key_system_identifier).to.equal(vault.systemIdentifier)
expect(encryptedItemsKey.errorDecrypting).to.be.true
const encryptedNote = invalidItems.find((item) => item.content_type === ContentType.TYPES.Note)
expect(encryptedNote.key_system_identifier).to.equal(vault.systemIdentifier)
expect(encryptedNote.errorDecrypting).to.be.true
expect(encryptedNote.uuid).to.equal(note.uuid)
await otherContext.deinit()
})
})
const note = await context.createSyncedNote('foo', 'bar')
await Collaboration.moveItemToVault(context, vault, note)
describe('randomized vaults', () => {
it('should import backup file for randomized vault created without account or passcode', async () => {
const vault = await context.vaults.createRandomizedVault({
name: 'test vault',
})
const backupData = await context.application.createEncryptedBackupFileForAutomatedDesktopBackups()
const note = await context.createSyncedNote('foo', 'bar')
await Collaboration.moveItemToVault(context, vault, note)
const otherContext = await Factory.createVaultsContextWithRealCrypto()
await otherContext.launch()
const backupData = (await context.application.createDecryptedBackupFile.execute()).getValue()
await otherContext.application.importData(backupData)
const otherContext = await Factory.createVaultsContextWithRealCrypto()
otherContext.password = context.password
await otherContext.launch()
const expectedImportedItems = ['vault-items-key', 'note']
const invalidItems = otherContext.items.invalidItems
expect(invalidItems.length).to.equal(expectedImportedItems.length)
const result = (await otherContext.application.importData(backupData)).getValue()
const encryptedItem = invalidItems[0]
expect(encryptedItem.key_system_identifier).to.equal(vault.systemIdentifier)
expect(encryptedItem.errorDecrypting).to.be.true
expect(encryptedItem.uuid).to.equal(note.uuid)
const invalidItems = otherContext.items.invalidItems
expect(invalidItems.length).to.equal(0)
await otherContext.deinit()
expect(result.affectedItems.length).to.equal(backupData.items.length)
const itemsKey = result.affectedItems.find((item) => item.content_type === ContentType.TYPES.KeySystemItemsKey)
expect(itemsKey.key_system_identifier).to.equal(vault.systemIdentifier)
const importedNote = result.affectedItems.find((item) => item.content_type === ContentType.TYPES.Note)
expect(importedNote.key_system_identifier).to.equal(vault.systemIdentifier)
expect(importedNote.uuid).to.equal(note.uuid)
const importedRootKey = result.affectedItems.find(
(item) => item.content_type === ContentType.TYPES.KeySystemRootKey,
)
expect(importedRootKey.systemIdentifier).to.equal(vault.systemIdentifier)
await otherContext.deinit()
})
it('should import synced-key vaulted items by decrypting', async () => {
await context.register()
const vault = await context.vaults.createRandomizedVault({
name: 'test vault',
})
const note = await context.createSyncedNote('foo', 'bar')
await Collaboration.moveItemToVault(context, vault, note)
const backupData = (
await context.application.createEncryptedBackupFile.execute({ skipAuthorization: true })
).getValue()
const otherContext = await Factory.createVaultsContextWithRealCrypto()
otherContext.password = context.password
await otherContext.launch()
const result = (await otherContext.application.importData(backupData)).getValue()
const invalidItems = otherContext.items.invalidItems
expect(invalidItems.length).to.equal(0)
expect(result.affectedItems.length).to.equal(backupData.items.length)
const itemsKey = result.affectedItems.find((item) => item.content_type === ContentType.TYPES.KeySystemItemsKey)
expect(itemsKey.key_system_identifier).to.equal(vault.systemIdentifier)
const importedNote = result.affectedItems.find((item) => item.content_type === ContentType.TYPES.Note)
expect(importedNote.key_system_identifier).to.equal(vault.systemIdentifier)
expect(importedNote.uuid).to.equal(note.uuid)
const importedRootKey = result.affectedItems.find(
(item) => item.content_type === ContentType.TYPES.KeySystemRootKey,
)
expect(importedRootKey.systemIdentifier).to.equal(vault.systemIdentifier)
await otherContext.deinit()
})
})
})
})