Skip to content

Commit

Permalink
test: Pull methods tests out into their own files. Easier to manage b…
Browse files Browse the repository at this point in the history
…efore/after setup and teardown
  • Loading branch information
jezhiggins committed Jun 2, 2020
1 parent 1635997 commit 7a1be9a
Show file tree
Hide file tree
Showing 10 changed files with 254 additions and 127 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
66 changes: 66 additions & 0 deletions test/method-copyFileToRemotePath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* eslint-env mocha */

const chai = require('chai')
chai.use(require('dirty-chai'))
chai.use(require('chai-string'))
const expect = chai.expect

const path = require('path')
const fs = require('fs')

const { tearDownDirectories } = require('./test-helpers')

const LocalStorageService = require('../lib/components/services/localfilestorage').serviceClass

describe('copyFileToRemotePath', () => {
let localstorage

const rootPath = path.join(__dirname, 'fixture', 'methods', 'to-write')
const options = {
bootedServices: {
cloudstorage: {
registerProvider: () => { }
}
},
config: {
localstorage: {
rootPath: rootPath
}
}
}

before(() => {
tearDownDirectories(rootPath)
fs.mkdirSync(rootPath)

localstorage = new LocalStorageService()
localstorage.boot(options)
})

after(() => {
tearDownDirectories(rootPath)
})

describe('happy path', () => {
const fileName = "hello.txt"
const localPath = path.join(__dirname, "fixture", "local", fileName)
const remotePath = path.join('to-write')

it('make sure target folder exists and is empty', async () => {
await localstorage.ensureFolderPath(remotePath)
const contents = await localstorage.listFolderContentsFromPath(remotePath)
expect(contents).to.have.length(0)
})

it('copy file to remote location', async () => {
const remoteFile = await localstorage.copyFileToRemotePath(localPath, remotePath)
expect(remoteFile).to.equal(path.join('to-write', remoteFile))
})

it('target folder lists the file', async () => {
const contents = await localstorage.listFolderContentsFromPath(remotePath)
expect(contents).to.have.length(1)
expect(contents).to.deep.include({ Name: fileName })
})
})
})
86 changes: 86 additions & 0 deletions test/method-ensureFolderPath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* eslint-env mocha */

const chai = require('chai')
chai.use(require('dirty-chai'))
chai.use(require('chai-string'))
chai.use(require('chai-as-promised'))
const expect = chai.expect

const path = require('path')
const fs = require('fs')

const { tearDownDirectories } = require('./test-helpers')

const LocalStorageService = require('../lib/components/services/localfilestorage').serviceClass

describe('ensureFolderPath', () => {
let localstorage

const rootPath = path.join(__dirname, 'fixture', 'methods', 'ensure')
const options = {
bootedServices: {
cloudstorage: {
registerProvider: () => { }
}
},
config: {
localstorage: {
rootPath: rootPath
}
}
}

before(() => {
tearDownDirectories(rootPath)
fs.mkdirSync(rootPath)

localstorage = new LocalStorageService()
localstorage.boot(options)
}) // before

after(() => {
tearDownDirectories(rootPath)
})



describe('create good paths', () => {
it('absolute paths are rooted in root path', async () => {
await localstorage.ensureFolderPath('/absolute')

expect(fs.existsSync(path.join(rootPath, 'absolute'))).to.be.true()
})

it('relative paths are resolved relative to root path', async () => {
await localstorage.ensureFolderPath('relative')

expect(fs.existsSync(path.join(rootPath, 'relative'))).to.be.true()
})

it('can create nested directories', async () => {
const folderPath = 'one/two/three/deep'
await localstorage.ensureFolderPath(folderPath)

let checkPath = rootPath
for (const p of folderPath.split('/')) {
checkPath = path.join(checkPath, p)
expect(fs.existsSync(checkPath)).to.be.true()
}
})
}) // good paths

describe('reject naughty paths that try to escape rootPath', () => {
const badPaths = [
'.',
'..',
'../../../poop',
'start/out/ok/but/../../../../../../../oh'
]

for (const p of badPaths) {
it(p, () => {
return expect(localstorage.ensureFolderPath(p)).to.eventually.be.rejectedWith(Error)
})
} // for ...
})
})
79 changes: 79 additions & 0 deletions test/method-listFolderContentsFromPath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* eslint-env mocha */

const chai = require('chai')
chai.use(require('dirty-chai'))
chai.use(require('chai-string'))
const expect = chai.expect

const path = require('path')
const fs = require('fs')

const LocalStorageService = require('../lib/components/services/localfilestorage').serviceClass

describe('listFolderContentsFromPath', () => {
let localstorage

const rootPath = path.join(__dirname, 'fixture', 'methods', 'list')
const emptyDir = path.join(rootPath, 'to-read', 'empty')

const options = {
bootedServices: {
cloudstorage: {
registerProvider: () => { }
}
},
config: {
localstorage: {
rootPath: rootPath
}
}
}

before(() => {
if (!fs.existsSync(emptyDir)) fs.mkdirSync(emptyDir)

localstorage = new LocalStorageService()
localstorage.boot(options)
})

after(() => {
fs.rmdirSync(emptyDir)
})

describe('good path', () => {
it('folder with files', async () => {
const list = await localstorage.listFolderContentsFromPath('to-read/sub-directory')
expect(list).to.have.length(2)
})

it('empty folder', async () => {
const list = await localstorage.listFolderContentsFromPath('to-read/empty')
expect(list).to.have.length(0)
})

it('folder with files and sub-folders', async () => {
const list = await localstorage.listFolderContentsFromPath('to-read')
expect(list).to.have.length(5)

expect(list).to.deep.include({ Name: 'empty' })
expect(list).to.deep.include({ Name: 'file-one.txt' })
})
})

describe('return nothing for bad paths', () => {
const badPaths = [
'..',
'../..',
'../../../poop',
'start/out/ok/but/../../../../../../../oh',
'good/path/but/which/does/not/exist'
]

for (const p of badPaths) {
it(p, async () => {
const list = await localstorage.listFolderContentsFromPath(p)
expect(list).to.have.length(0)
})
}
})
})
127 changes: 0 additions & 127 deletions test/methods.js

This file was deleted.

23 changes: 23 additions & 0 deletions test/test-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const path = require('path')
const fs = require('fs')

function tearDownDirectories (dirPath, root) {
if (!fs.existsSync(dirPath)) return

const files = fs.readdirSync(dirPath, { withFileTypes: true })
.filter(f => f.isFile())
.map(f => path.join(dirPath, f.name))
.forEach(f => fs.unlinkSync(f))

const cleanUp = fs.readdirSync(dirPath, { withFileTypes: true })
.filter(d => d.isDirectory())
.map(d => path.join(dirPath, d.name))
cleanUp.forEach(dir => tearDownDirectories(dir, dirPath))
cleanUp.forEach(dir => fs.rmdirSync(dir))

if (!root) fs.rmdirSync(dirPath)
}

module.exports = {
tearDownDirectories
}

0 comments on commit 7a1be9a

Please sign in to comment.