test(fs/abstract): use getSyncedHandler
This commit is contained in:
parent
545a65521a
commit
720e363577
@ -1,8 +1,8 @@
|
|||||||
/* eslint-env jest */
|
/* eslint-env jest */
|
||||||
|
|
||||||
import { DEFAULT_ENCRYPTION_ALGORITHM, _getEncryptor } from './_encryptor'
|
import { DEFAULT_ENCRYPTION_ALGORITHM, _getEncryptor } from './_encryptor'
|
||||||
import { getHandler } from '.'
|
import { Disposable, pFromCallback, TimeoutError } from 'promise-toolbox'
|
||||||
import { pFromCallback, TimeoutError } from 'promise-toolbox'
|
import { getSyncedHandler } from '.'
|
||||||
import AbstractHandler from './abstract'
|
import AbstractHandler from './abstract'
|
||||||
import fs from 'fs-extra'
|
import fs from 'fs-extra'
|
||||||
import rimraf from 'rimraf'
|
import rimraf from 'rimraf'
|
||||||
@ -22,6 +22,8 @@ class TestHandler extends AbstractHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const noop = Function.prototype
|
||||||
|
|
||||||
jest.useFakeTimers()
|
jest.useFakeTimers()
|
||||||
|
|
||||||
describe('closeFile()', () => {
|
describe('closeFile()', () => {
|
||||||
@ -109,27 +111,25 @@ describe('rmdir()', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('encryption', () => {
|
describe('encryption', () => {
|
||||||
let handler, dir
|
let dir
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
dir = await pFromCallback(cb => tmp.dir(cb))
|
dir = await pFromCallback(cb => tmp.dir(cb))
|
||||||
})
|
})
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
await handler?.forget()
|
|
||||||
handler = undefined
|
|
||||||
|
|
||||||
await pFromCallback(cb => rimraf(dir, cb))
|
await pFromCallback(cb => rimraf(dir, cb))
|
||||||
})
|
})
|
||||||
|
|
||||||
it('sync should NOT create metadata if missing (not encrypted)', async () => {
|
it('sync should NOT create metadata if missing (not encrypted)', async () => {
|
||||||
handler = getHandler({ url: `file://${dir}` })
|
await Disposable.use(getSyncedHandler({ url: `file://${dir}` }), noop)
|
||||||
await handler.sync()
|
|
||||||
|
|
||||||
expect(await fs.readdir(dir)).toEqual([])
|
expect(await fs.readdir(dir)).toEqual([])
|
||||||
})
|
})
|
||||||
|
|
||||||
it('sync should create metadata if missing (encrypted)', async () => {
|
it('sync should create metadata if missing (encrypted)', async () => {
|
||||||
handler = getHandler({ url: `file://${dir}?encryptionKey="73c1838d7d8a6088ca2317fb5f29cd00"` })
|
await Disposable.use(
|
||||||
await handler.sync()
|
getSyncedHandler({ url: `file://${dir}?encryptionKey="73c1838d7d8a6088ca2317fb5f29cd00"` }),
|
||||||
|
noop
|
||||||
|
)
|
||||||
|
|
||||||
expect(await fs.readdir(dir)).toEqual(['encryption.json', 'metadata.json'])
|
expect(await fs.readdir(dir)).toEqual(['encryption.json', 'metadata.json'])
|
||||||
|
|
||||||
@ -142,9 +142,8 @@ describe('encryption', () => {
|
|||||||
it('sync should not modify existing metadata', async () => {
|
it('sync should not modify existing metadata', async () => {
|
||||||
await fs.writeFile(`${dir}/encryption.json`, `{"algorithm": "none"}`)
|
await fs.writeFile(`${dir}/encryption.json`, `{"algorithm": "none"}`)
|
||||||
await fs.writeFile(`${dir}/metadata.json`, `{"random": "NOTSORANDOM"}`)
|
await fs.writeFile(`${dir}/metadata.json`, `{"random": "NOTSORANDOM"}`)
|
||||||
handler = getHandler({ url: `file://${dir}` })
|
|
||||||
|
|
||||||
await handler.sync()
|
await Disposable.use(await getSyncedHandler({ url: `file://${dir}` }), noop)
|
||||||
|
|
||||||
const encryption = JSON.parse(await fs.readFile(`${dir}/encryption.json`, 'utf-8'))
|
const encryption = JSON.parse(await fs.readFile(`${dir}/encryption.json`, 'utf-8'))
|
||||||
expect(encryption.algorithm).toEqual('none')
|
expect(encryption.algorithm).toEqual('none')
|
||||||
@ -153,35 +152,37 @@ describe('encryption', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should modify metadata if empty', async () => {
|
it('should modify metadata if empty', async () => {
|
||||||
handler = getHandler({ url: `file://${dir}` })
|
await Disposable.use(getSyncedHandler({ url: `file://${dir}` }), noop)
|
||||||
await handler.sync()
|
|
||||||
await handler.forget()
|
|
||||||
// nothing created without encryption
|
// nothing created without encryption
|
||||||
handler = getHandler({ url: `file://${dir}?encryptionKey="73c1838d7d8a6088ca2317fb5f29cd00"` })
|
|
||||||
await handler.sync()
|
await Disposable.use(
|
||||||
|
getSyncedHandler({ url: `file://${dir}?encryptionKey="73c1838d7d8a6088ca2317fb5f29cd00"` }),
|
||||||
|
noop
|
||||||
|
)
|
||||||
let encryption = JSON.parse(await fs.readFile(`${dir}/encryption.json`, 'utf-8'))
|
let encryption = JSON.parse(await fs.readFile(`${dir}/encryption.json`, 'utf-8'))
|
||||||
expect(encryption.algorithm).toEqual(DEFAULT_ENCRYPTION_ALGORITHM)
|
expect(encryption.algorithm).toEqual(DEFAULT_ENCRYPTION_ALGORITHM)
|
||||||
await handler.forget()
|
|
||||||
handler = getHandler({ url: `file://${dir}` })
|
await Disposable.use(getSyncedHandler({ url: `file://${dir}` }), noop)
|
||||||
await handler.sync()
|
|
||||||
encryption = JSON.parse(await fs.readFile(`${dir}/encryption.json`, 'utf-8'))
|
encryption = JSON.parse(await fs.readFile(`${dir}/encryption.json`, 'utf-8'))
|
||||||
expect(encryption.algorithm).toEqual('none')
|
expect(encryption.algorithm).toEqual('none')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('sync should work with encrypted', async () => {
|
it(
|
||||||
|
'sync should work with encrypted',
|
||||||
|
Disposable.wrap(async function* () {
|
||||||
const encryptor = _getEncryptor(DEFAULT_ENCRYPTION_ALGORITHM, '73c1838d7d8a6088ca2317fb5f29cd91')
|
const encryptor = _getEncryptor(DEFAULT_ENCRYPTION_ALGORITHM, '73c1838d7d8a6088ca2317fb5f29cd91')
|
||||||
|
|
||||||
await fs.writeFile(`${dir}/encryption.json`, `{"algorithm": "${DEFAULT_ENCRYPTION_ALGORITHM}"}`)
|
await fs.writeFile(`${dir}/encryption.json`, `{"algorithm": "${DEFAULT_ENCRYPTION_ALGORITHM}"}`)
|
||||||
await fs.writeFile(`${dir}/metadata.json`, encryptor.encryptData(`{"random": "NOTSORANDOM"}`))
|
await fs.writeFile(`${dir}/metadata.json`, encryptor.encryptData(`{"random": "NOTSORANDOM"}`))
|
||||||
|
|
||||||
handler = getHandler({ url: `file://${dir}?encryptionKey="73c1838d7d8a6088ca2317fb5f29cd91"` })
|
const handler = yield getSyncedHandler({ url: `file://${dir}?encryptionKey="73c1838d7d8a6088ca2317fb5f29cd91"` })
|
||||||
await handler.sync()
|
|
||||||
|
|
||||||
const encryption = JSON.parse(await fs.readFile(`${dir}/encryption.json`, 'utf-8'))
|
const encryption = JSON.parse(await fs.readFile(`${dir}/encryption.json`, 'utf-8'))
|
||||||
expect(encryption.algorithm).toEqual(DEFAULT_ENCRYPTION_ALGORITHM)
|
expect(encryption.algorithm).toEqual(DEFAULT_ENCRYPTION_ALGORITHM)
|
||||||
const metadata = JSON.parse(await handler.readFile(`./metadata.json`))
|
const metadata = JSON.parse(await handler.readFile(`./metadata.json`))
|
||||||
expect(metadata.random).toEqual('NOTSORANDOM')
|
expect(metadata.random).toEqual('NOTSORANDOM')
|
||||||
})
|
})
|
||||||
|
)
|
||||||
|
|
||||||
it('sync should fail when changing key on non empty remote ', async () => {
|
it('sync should fail when changing key on non empty remote ', async () => {
|
||||||
const encryptor = _getEncryptor(DEFAULT_ENCRYPTION_ALGORITHM, '73c1838d7d8a6088ca2317fb5f29cd91')
|
const encryptor = _getEncryptor(DEFAULT_ENCRYPTION_ALGORITHM, '73c1838d7d8a6088ca2317fb5f29cd91')
|
||||||
@ -190,13 +191,16 @@ describe('encryption', () => {
|
|||||||
await fs.writeFile(`${dir}/metadata.json`, encryptor.encryptData(`{"random": "NOTSORANDOM"}`))
|
await fs.writeFile(`${dir}/metadata.json`, encryptor.encryptData(`{"random": "NOTSORANDOM"}`))
|
||||||
|
|
||||||
// different key but empty remote => ok
|
// different key but empty remote => ok
|
||||||
handler = getHandler({ url: `file://${dir}?encryptionKey="73c1838d7d8a6088ca2317fb5f29cd00"` })
|
await Disposable.use(
|
||||||
await expect(handler.sync()).resolves.not.toThrowError()
|
getSyncedHandler({ url: `file://${dir}?encryptionKey="73c1838d7d8a6088ca2317fb5f29cd00"` }),
|
||||||
|
noop
|
||||||
|
)
|
||||||
|
|
||||||
// rmote is now non empty : can't modify key anymore
|
// remote is now non empty : can't modify key anymore
|
||||||
await fs.writeFile(`${dir}/nonempty.json`, 'content')
|
await fs.writeFile(`${dir}/nonempty.json`, 'content')
|
||||||
handler = getHandler({ url: `file://${dir}?encryptionKey="73c1838d7d8a6088ca2317fb5f29cd10"` })
|
await expect(
|
||||||
await expect(handler.sync()).rejects.toThrowError()
|
Disposable.use(getSyncedHandler({ url: `file://${dir}?encryptionKey="73c1838d7d8a6088ca2317fb5f29cd10"` }), noop)
|
||||||
|
).rejects.toThrowError()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('sync should fail when changing algorithm', async () => {
|
it('sync should fail when changing algorithm', async () => {
|
||||||
@ -209,7 +213,8 @@ describe('encryption', () => {
|
|||||||
// remote is now non empty : can't modify key anymore
|
// remote is now non empty : can't modify key anymore
|
||||||
await fs.writeFile(`${dir}/nonempty.json`, 'content')
|
await fs.writeFile(`${dir}/nonempty.json`, 'content')
|
||||||
|
|
||||||
handler = getHandler({ url: `file://${dir}?encryptionKey="73c1838d7d8a6088ca2317fb5f29cd91"` })
|
await expect(
|
||||||
await expect(handler.sync()).rejects.toThrowError()
|
Disposable.use(getSyncedHandler({ url: `file://${dir}?encryptionKey="73c1838d7d8a6088ca2317fb5f29cd91"` }), noop)
|
||||||
|
).rejects.toThrowError()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user