-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Pull methods tests out into their own files. Easier to manage b…
…efore/after setup and teardown
- Loading branch information
1 parent
1635997
commit 7a1be9a
Showing
10 changed files
with
254 additions
and
127 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ... | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |