1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 17:56:25 +00:00

add tests

This commit is contained in:
Baptiste Augrain
2019-02-01 16:27:47 +01:00
parent 851d3ba159
commit 215484c19a
2 changed files with 147 additions and 7 deletions

View File

@@ -181,7 +181,7 @@ function copyAttachment (sourceFilePath, storageKey, noteKey, useRandomName = tr
if (isBase64) { if (isBase64) {
const base64Data = sourceFilePath.data.replace(/^data:image\/\w+;base64,/, '') const base64Data = sourceFilePath.data.replace(/^data:image\/\w+;base64,/, '')
const dataBuffer = new Buffer(base64Data, 'base64') const dataBuffer = Buffer.from(base64Data, 'base64')
outputFile.write(dataBuffer, () => { outputFile.write(dataBuffer, () => {
resolve(destinationName) resolve(destinationName)
}) })

View File

@@ -38,7 +38,7 @@ it('should test that copyAttachment should throw an error if sourcePath dosen\'t
fs.existsSync = jest.fn() fs.existsSync = jest.fn()
fs.existsSync.mockReturnValue(false) fs.existsSync.mockReturnValue(false)
systemUnderTest.copyAttachment('path', 'storageKey', 'noteKey').then(() => {}, error => { return systemUnderTest.copyAttachment('path', 'storageKey', 'noteKey').then(() => {}, error => {
expect(error).toBe('source file does not exist') expect(error).toBe('source file does not exist')
expect(fs.existsSync).toHaveBeenCalledWith('path') expect(fs.existsSync).toHaveBeenCalledWith('path')
}) })
@@ -64,7 +64,7 @@ it('should test that copyAttachment works correctly assuming correct working of
findStorage.findStorage.mockReturnValue(dummyStorage) findStorage.findStorage.mockReturnValue(dummyStorage)
uniqueSlug.mockReturnValue(dummyUniquePath) uniqueSlug.mockReturnValue(dummyUniquePath)
systemUnderTest.copyAttachment(sourcePath, storageKey, noteKey).then( return systemUnderTest.copyAttachment(sourcePath, storageKey, noteKey).then(
function (newFileName) { function (newFileName) {
expect(findStorage.findStorage).toHaveBeenCalledWith(storageKey) expect(findStorage.findStorage).toHaveBeenCalledWith(storageKey)
expect(fs.createReadStream).toHaveBeenCalledWith(sourcePath) expect(fs.createReadStream).toHaveBeenCalledWith(sourcePath)
@@ -83,7 +83,7 @@ it('should test that copyAttachment creates a new folder if the attachment folde
const dummyReadStream = {} const dummyReadStream = {}
dummyReadStream.pipe = jest.fn() dummyReadStream.pipe = jest.fn()
dummyReadStream.on = jest.fn() dummyReadStream.on = jest.fn((event, callback) => { callback() })
fs.createReadStream = jest.fn(() => dummyReadStream) fs.createReadStream = jest.fn(() => dummyReadStream)
fs.existsSync = jest.fn() fs.existsSync = jest.fn()
fs.existsSync.mockReturnValueOnce(true) fs.existsSync.mockReturnValueOnce(true)
@@ -95,7 +95,7 @@ it('should test that copyAttachment creates a new folder if the attachment folde
findStorage.findStorage.mockReturnValue(dummyStorage) findStorage.findStorage.mockReturnValue(dummyStorage)
uniqueSlug.mockReturnValue('dummyPath') uniqueSlug.mockReturnValue('dummyPath')
systemUnderTest.copyAttachment('path', 'storageKey', 'noteKey').then( return systemUnderTest.copyAttachment('path', 'storageKey', 'noteKey').then(
function () { function () {
expect(fs.existsSync).toHaveBeenCalledWith(attachmentFolderPath) expect(fs.existsSync).toHaveBeenCalledWith(attachmentFolderPath)
expect(fs.mkdirSync).toHaveBeenCalledWith(attachmentFolderPath) expect(fs.mkdirSync).toHaveBeenCalledWith(attachmentFolderPath)
@@ -109,7 +109,7 @@ it('should test that copyAttachment don\'t uses a random file name if not intend
const dummyReadStream = {} const dummyReadStream = {}
dummyReadStream.pipe = jest.fn() dummyReadStream.pipe = jest.fn()
dummyReadStream.on = jest.fn() dummyReadStream.on = jest.fn((event, callback) => { callback() })
fs.createReadStream = jest.fn(() => dummyReadStream) fs.createReadStream = jest.fn(() => dummyReadStream)
fs.existsSync = jest.fn() fs.existsSync = jest.fn()
fs.existsSync.mockReturnValueOnce(true) fs.existsSync.mockReturnValueOnce(true)
@@ -120,12 +120,152 @@ it('should test that copyAttachment don\'t uses a random file name if not intend
findStorage.findStorage.mockReturnValue(dummyStorage) findStorage.findStorage.mockReturnValue(dummyStorage)
uniqueSlug.mockReturnValue('dummyPath') uniqueSlug.mockReturnValue('dummyPath')
systemUnderTest.copyAttachment('path', 'storageKey', 'noteKey', false).then( return systemUnderTest.copyAttachment('path', 'storageKey', 'noteKey', false).then(
function (newFileName) { function (newFileName) {
expect(newFileName).toBe('path') expect(newFileName).toBe('path')
}) })
}) })
it('should test that copyAttachment with url (with extension, without query)', function () {
const dummyStorage = {path: 'dummyStoragePath'}
const dummyReadStream = {
pipe: jest.fn(),
on: jest.fn((event, callback) => { callback() })
}
fs.createReadStream = jest.fn(() => dummyReadStream)
const dummyWriteStream = {
write: jest.fn((data, callback) => { callback() })
}
fs.createWriteStream = jest.fn(() => dummyWriteStream)
fs.existsSync = jest.fn()
fs.existsSync.mockReturnValueOnce(true)
fs.existsSync.mockReturnValueOnce(false)
fs.mkdirSync = jest.fn()
findStorage.findStorage = jest.fn()
findStorage.findStorage.mockReturnValue(dummyStorage)
uniqueSlug.mockReturnValue('dummyPath')
const sourcePath = {
sourceFilePath: 'http://www.foo.bar/baz/qux.jpg',
type: 'base64',
data: 'data:image/jpeg;base64,Ym9vc3Rub3Rl'
}
return systemUnderTest.copyAttachment(sourcePath, 'storageKey', 'noteKey').then(
function (newFileName) {
expect(newFileName).toBe('dummyPath.jpg')
})
})
it('should test that copyAttachment with url (with extension, with query)', function () {
const dummyStorage = {path: 'dummyStoragePath'}
const dummyReadStream = {
pipe: jest.fn(),
on: jest.fn((event, callback) => { callback() })
}
fs.createReadStream = jest.fn(() => dummyReadStream)
const dummyWriteStream = {
write: jest.fn((data, callback) => { callback() })
}
fs.createWriteStream = jest.fn(() => dummyWriteStream)
fs.existsSync = jest.fn()
fs.existsSync.mockReturnValueOnce(true)
fs.existsSync.mockReturnValueOnce(false)
fs.mkdirSync = jest.fn()
findStorage.findStorage = jest.fn()
findStorage.findStorage.mockReturnValue(dummyStorage)
uniqueSlug.mockReturnValue('dummyPath')
const sourcePath = {
sourceFilePath: 'http://www.foo.bar/baz/qux.jpg?h=1080',
type: 'base64',
data: 'data:image/jpeg;base64,Ym9vc3Rub3Rl'
}
return systemUnderTest.copyAttachment(sourcePath, 'storageKey', 'noteKey').then(
function (newFileName) {
expect(newFileName).toBe('dummyPath.jpg')
})
})
it('should test that copyAttachment with url (without extension, without query)', function () {
const dummyStorage = {path: 'dummyStoragePath'}
const dummyReadStream = {
pipe: jest.fn(),
on: jest.fn((event, callback) => { callback() })
}
fs.createReadStream = jest.fn(() => dummyReadStream)
const dummyWriteStream = {
write: jest.fn((data, callback) => { callback() })
}
fs.createWriteStream = jest.fn(() => dummyWriteStream)
fs.existsSync = jest.fn()
fs.existsSync.mockReturnValueOnce(true)
fs.existsSync.mockReturnValueOnce(false)
fs.mkdirSync = jest.fn()
findStorage.findStorage = jest.fn()
findStorage.findStorage.mockReturnValue(dummyStorage)
uniqueSlug.mockReturnValue('dummyPath')
const sourcePath = {
sourceFilePath: 'http://www.foo.bar/baz/qux',
type: 'base64',
data: 'data:image/jpeg;base64,Ym9vc3Rub3Rl'
}
return systemUnderTest.copyAttachment(sourcePath, 'storageKey', 'noteKey').then(
function (newFileName) {
expect(newFileName).toBe('dummyPath.png')
})
})
it('should test that copyAttachment with url (without extension, with query)', function () {
const dummyStorage = {path: 'dummyStoragePath'}
const dummyReadStream = {
pipe: jest.fn(),
on: jest.fn((event, callback) => { callback() })
}
fs.createReadStream = jest.fn(() => dummyReadStream)
const dummyWriteStream = {
write: jest.fn((data, callback) => { callback() })
}
fs.createWriteStream = jest.fn(() => dummyWriteStream)
fs.existsSync = jest.fn()
fs.existsSync.mockReturnValueOnce(true)
fs.existsSync.mockReturnValueOnce(false)
fs.mkdirSync = jest.fn()
findStorage.findStorage = jest.fn()
findStorage.findStorage.mockReturnValue(dummyStorage)
uniqueSlug.mockReturnValue('dummyPath')
const sourcePath = {
sourceFilePath: 'http://www.foo.bar/baz/qux?h=1080',
type: 'base64',
data: 'data:image/jpeg;base64,Ym9vc3Rub3Rl'
}
return systemUnderTest.copyAttachment(sourcePath, 'storageKey', 'noteKey').then(
function (newFileName) {
expect(newFileName).toBe('dummyPath.png')
})
})
it('should replace the all ":storage" path with the actual storage path', function () { it('should replace the all ":storage" path with the actual storage path', function () {
const storageFolder = systemUnderTest.DESTINATION_FOLDER const storageFolder = systemUnderTest.DESTINATION_FOLDER
const testInput = const testInput =