Skip to content

Commit 215484c

Browse files
committed
add tests
1 parent 851d3ba commit 215484c

File tree

2 files changed

+147
-7
lines changed

2 files changed

+147
-7
lines changed

browser/main/lib/dataApi/attachmentManagement.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ function copyAttachment (sourceFilePath, storageKey, noteKey, useRandomName = tr
181181

182182
if (isBase64) {
183183
const base64Data = sourceFilePath.data.replace(/^data:image\/\w+;base64,/, '')
184-
const dataBuffer = new Buffer(base64Data, 'base64')
184+
const dataBuffer = Buffer.from(base64Data, 'base64')
185185
outputFile.write(dataBuffer, () => {
186186
resolve(destinationName)
187187
})

tests/dataApi/attachmentManagement.test.js

Lines changed: 146 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ it('should test that copyAttachment should throw an error if sourcePath dosen\'t
3838
fs.existsSync = jest.fn()
3939
fs.existsSync.mockReturnValue(false)
4040

41-
systemUnderTest.copyAttachment('path', 'storageKey', 'noteKey').then(() => {}, error => {
41+
return systemUnderTest.copyAttachment('path', 'storageKey', 'noteKey').then(() => {}, error => {
4242
expect(error).toBe('source file does not exist')
4343
expect(fs.existsSync).toHaveBeenCalledWith('path')
4444
})
@@ -64,7 +64,7 @@ it('should test that copyAttachment works correctly assuming correct working of
6464
findStorage.findStorage.mockReturnValue(dummyStorage)
6565
uniqueSlug.mockReturnValue(dummyUniquePath)
6666

67-
systemUnderTest.copyAttachment(sourcePath, storageKey, noteKey).then(
67+
return systemUnderTest.copyAttachment(sourcePath, storageKey, noteKey).then(
6868
function (newFileName) {
6969
expect(findStorage.findStorage).toHaveBeenCalledWith(storageKey)
7070
expect(fs.createReadStream).toHaveBeenCalledWith(sourcePath)
@@ -83,7 +83,7 @@ it('should test that copyAttachment creates a new folder if the attachment folde
8383
const dummyReadStream = {}
8484

8585
dummyReadStream.pipe = jest.fn()
86-
dummyReadStream.on = jest.fn()
86+
dummyReadStream.on = jest.fn((event, callback) => { callback() })
8787
fs.createReadStream = jest.fn(() => dummyReadStream)
8888
fs.existsSync = jest.fn()
8989
fs.existsSync.mockReturnValueOnce(true)
@@ -95,7 +95,7 @@ it('should test that copyAttachment creates a new folder if the attachment folde
9595
findStorage.findStorage.mockReturnValue(dummyStorage)
9696
uniqueSlug.mockReturnValue('dummyPath')
9797

98-
systemUnderTest.copyAttachment('path', 'storageKey', 'noteKey').then(
98+
return systemUnderTest.copyAttachment('path', 'storageKey', 'noteKey').then(
9999
function () {
100100
expect(fs.existsSync).toHaveBeenCalledWith(attachmentFolderPath)
101101
expect(fs.mkdirSync).toHaveBeenCalledWith(attachmentFolderPath)
@@ -109,7 +109,7 @@ it('should test that copyAttachment don\'t uses a random file name if not intend
109109
const dummyReadStream = {}
110110

111111
dummyReadStream.pipe = jest.fn()
112-
dummyReadStream.on = jest.fn()
112+
dummyReadStream.on = jest.fn((event, callback) => { callback() })
113113
fs.createReadStream = jest.fn(() => dummyReadStream)
114114
fs.existsSync = jest.fn()
115115
fs.existsSync.mockReturnValueOnce(true)
@@ -120,12 +120,152 @@ it('should test that copyAttachment don\'t uses a random file name if not intend
120120
findStorage.findStorage.mockReturnValue(dummyStorage)
121121
uniqueSlug.mockReturnValue('dummyPath')
122122

123-
systemUnderTest.copyAttachment('path', 'storageKey', 'noteKey', false).then(
123+
return systemUnderTest.copyAttachment('path', 'storageKey', 'noteKey', false).then(
124124
function (newFileName) {
125125
expect(newFileName).toBe('path')
126126
})
127127
})
128128

129+
it('should test that copyAttachment with url (with extension, without query)', function () {
130+
const dummyStorage = {path: 'dummyStoragePath'}
131+
132+
const dummyReadStream = {
133+
pipe: jest.fn(),
134+
on: jest.fn((event, callback) => { callback() })
135+
}
136+
fs.createReadStream = jest.fn(() => dummyReadStream)
137+
138+
const dummyWriteStream = {
139+
write: jest.fn((data, callback) => { callback() })
140+
}
141+
fs.createWriteStream = jest.fn(() => dummyWriteStream)
142+
143+
fs.existsSync = jest.fn()
144+
fs.existsSync.mockReturnValueOnce(true)
145+
fs.existsSync.mockReturnValueOnce(false)
146+
fs.mkdirSync = jest.fn()
147+
148+
findStorage.findStorage = jest.fn()
149+
findStorage.findStorage.mockReturnValue(dummyStorage)
150+
uniqueSlug.mockReturnValue('dummyPath')
151+
152+
const sourcePath = {
153+
sourceFilePath: 'http://www.foo.bar/baz/qux.jpg',
154+
type: 'base64',
155+
data: 'data:image/jpeg;base64,Ym9vc3Rub3Rl'
156+
}
157+
158+
return systemUnderTest.copyAttachment(sourcePath, 'storageKey', 'noteKey').then(
159+
function (newFileName) {
160+
expect(newFileName).toBe('dummyPath.jpg')
161+
})
162+
})
163+
164+
it('should test that copyAttachment with url (with extension, with query)', function () {
165+
const dummyStorage = {path: 'dummyStoragePath'}
166+
167+
const dummyReadStream = {
168+
pipe: jest.fn(),
169+
on: jest.fn((event, callback) => { callback() })
170+
}
171+
fs.createReadStream = jest.fn(() => dummyReadStream)
172+
173+
const dummyWriteStream = {
174+
write: jest.fn((data, callback) => { callback() })
175+
}
176+
fs.createWriteStream = jest.fn(() => dummyWriteStream)
177+
178+
fs.existsSync = jest.fn()
179+
fs.existsSync.mockReturnValueOnce(true)
180+
fs.existsSync.mockReturnValueOnce(false)
181+
fs.mkdirSync = jest.fn()
182+
183+
findStorage.findStorage = jest.fn()
184+
findStorage.findStorage.mockReturnValue(dummyStorage)
185+
uniqueSlug.mockReturnValue('dummyPath')
186+
187+
const sourcePath = {
188+
sourceFilePath: 'http://www.foo.bar/baz/qux.jpg?h=1080',
189+
type: 'base64',
190+
data: 'data:image/jpeg;base64,Ym9vc3Rub3Rl'
191+
}
192+
193+
return systemUnderTest.copyAttachment(sourcePath, 'storageKey', 'noteKey').then(
194+
function (newFileName) {
195+
expect(newFileName).toBe('dummyPath.jpg')
196+
})
197+
})
198+
199+
it('should test that copyAttachment with url (without extension, without query)', function () {
200+
const dummyStorage = {path: 'dummyStoragePath'}
201+
202+
const dummyReadStream = {
203+
pipe: jest.fn(),
204+
on: jest.fn((event, callback) => { callback() })
205+
}
206+
fs.createReadStream = jest.fn(() => dummyReadStream)
207+
208+
const dummyWriteStream = {
209+
write: jest.fn((data, callback) => { callback() })
210+
}
211+
fs.createWriteStream = jest.fn(() => dummyWriteStream)
212+
213+
fs.existsSync = jest.fn()
214+
fs.existsSync.mockReturnValueOnce(true)
215+
fs.existsSync.mockReturnValueOnce(false)
216+
fs.mkdirSync = jest.fn()
217+
218+
findStorage.findStorage = jest.fn()
219+
findStorage.findStorage.mockReturnValue(dummyStorage)
220+
uniqueSlug.mockReturnValue('dummyPath')
221+
222+
const sourcePath = {
223+
sourceFilePath: 'http://www.foo.bar/baz/qux',
224+
type: 'base64',
225+
data: 'data:image/jpeg;base64,Ym9vc3Rub3Rl'
226+
}
227+
228+
return systemUnderTest.copyAttachment(sourcePath, 'storageKey', 'noteKey').then(
229+
function (newFileName) {
230+
expect(newFileName).toBe('dummyPath.png')
231+
})
232+
})
233+
234+
it('should test that copyAttachment with url (without extension, with query)', function () {
235+
const dummyStorage = {path: 'dummyStoragePath'}
236+
237+
const dummyReadStream = {
238+
pipe: jest.fn(),
239+
on: jest.fn((event, callback) => { callback() })
240+
}
241+
fs.createReadStream = jest.fn(() => dummyReadStream)
242+
243+
const dummyWriteStream = {
244+
write: jest.fn((data, callback) => { callback() })
245+
}
246+
fs.createWriteStream = jest.fn(() => dummyWriteStream)
247+
248+
fs.existsSync = jest.fn()
249+
fs.existsSync.mockReturnValueOnce(true)
250+
fs.existsSync.mockReturnValueOnce(false)
251+
fs.mkdirSync = jest.fn()
252+
253+
findStorage.findStorage = jest.fn()
254+
findStorage.findStorage.mockReturnValue(dummyStorage)
255+
uniqueSlug.mockReturnValue('dummyPath')
256+
257+
const sourcePath = {
258+
sourceFilePath: 'http://www.foo.bar/baz/qux?h=1080',
259+
type: 'base64',
260+
data: 'data:image/jpeg;base64,Ym9vc3Rub3Rl'
261+
}
262+
263+
return systemUnderTest.copyAttachment(sourcePath, 'storageKey', 'noteKey').then(
264+
function (newFileName) {
265+
expect(newFileName).toBe('dummyPath.png')
266+
})
267+
})
268+
129269
it('should replace the all ":storage" path with the actual storage path', function () {
130270
const storageFolder = systemUnderTest.DESTINATION_FOLDER
131271
const testInput =

0 commit comments

Comments
 (0)