This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
203 additions
and
54 deletions.
There are no files selected for viewing
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
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
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
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
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 @@ | ||
hello world |
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,62 @@ | ||
import * as Promise from 'bluebird'; | ||
import { expect } from 'chai'; | ||
import * as chai from 'chai'; | ||
import * as chaiAsPromised from 'chai-as-promised'; | ||
import { readFileSync } from 'fs'; | ||
import * as mongo from 'mongodb'; | ||
import { MongoClient } from 'mongodb'; | ||
import * as path from 'path'; | ||
import { FileMetadata } from '../../src/file-api/FileMetadata'; | ||
import { GridFsStorage } from '../../src/impl/GridFsStorage'; | ||
|
||
const connectAsync = Promise.promisify<mongo.Db, string>(MongoClient.connect); | ||
|
||
chai.use(chaiAsPromised); | ||
|
||
function readStream(stream: NodeJS.ReadableStream): Promise<string> { | ||
let data = ''; | ||
stream.setEncoding('utf8'); | ||
stream.on('data', chunk => data += chunk); | ||
return new Promise(resolve => stream.on('end', () => resolve(data))); | ||
} | ||
|
||
describe('GridFsStorage', function() { | ||
const mongoUrl = 'mongodb://127.0.0.1:27017/testdb'; | ||
const dbFactory = () => connectAsync(mongoUrl); | ||
let db: mongo.Db; | ||
const metadata: FileMetadata = { | ||
id: 'test-file' | ||
}; | ||
const fileLocation = path.resolve(__dirname, '../fixtures/test.txt'); | ||
const fileContents = readFileSync(fileLocation, { | ||
encoding: 'utf8' | ||
}); | ||
|
||
before(function() { | ||
return dbFactory().then(database => db = database); | ||
}); | ||
after(function() { | ||
db.close(); | ||
}); | ||
|
||
describe('constructor', function() { | ||
it('should accept a mongodb connection', function() { | ||
const storage = new GridFsStorage(db); | ||
return expect(storage.gridFileSystem).to.exist && | ||
expect(storage.getFileSystem()).to.eventually.be.fulfilled; | ||
}); | ||
it('should accept a mongodb url', function() { | ||
const storage = new GridFsStorage(mongoUrl); | ||
}); | ||
}); | ||
|
||
it('should write and read back a file from storage', function() { | ||
const storage = new GridFsStorage(db); | ||
const write = storage.writeFile(metadata, fileLocation); | ||
const read = write | ||
.then(() => storage.readFile(metadata.id)) | ||
.then(readStream); | ||
return expect(write).to.eventually.be.fulfilled && | ||
expect(read).to.eventually.equal(fileContents); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
--compilers ts:ts-node/register | ||
--require source-map-support/register | ||
test/**.ts | ||
test/**/*.ts |
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,78 @@ | ||
import * as Promise from 'bluebird'; | ||
import * as chai from 'chai'; | ||
import * as chaiAsPromised from 'chai-as-promised'; | ||
import * as del from 'del'; | ||
import * as fs from 'fs'; | ||
import { memoryStorage } from 'multer'; | ||
import { Stream } from 'stream'; | ||
import { FileMetadata } from '../../src/file-api/FileMetadata'; | ||
import * as fileService from '../../src/services/FileService'; | ||
|
||
import strToStream = require('string-to-stream'); | ||
|
||
chai.use(chaiAsPromised); | ||
|
||
const existsAsync = (path) => | ||
// can't use promisify because this doesn't return an error | ||
new Promise(resolve => fs.exists(path, exists => resolve(exists))); | ||
const readFileAsync = Promise.promisify<string, string, string>(fs.readFile); | ||
|
||
const { expect } = chai; | ||
|
||
describe('FileService', function() { | ||
describe('createTemporaryStorageFolder', function() { | ||
const deleteDir = () => del(fileService.FILE_STORAGE_DIRECTORY, { force: true }); | ||
beforeEach(() => deleteDir()); | ||
|
||
it('should create a default directory', function() { | ||
const recreate = deleteDir() | ||
.then(() => fileService.createTemporaryStorageFolder()) | ||
.then(() => existsAsync(fileService.FILE_STORAGE_DIRECTORY)); | ||
return expect(recreate).to.eventually.be.true; | ||
}); | ||
it('should be idempotent', function() { | ||
const createTwice = fileService.createTemporaryStorageFolder() | ||
.then(() => fileService.createTemporaryStorageFolder()) | ||
.then(() => existsAsync(fileService.FILE_STORAGE_DIRECTORY)); | ||
return expect(createTwice).to.eventually.be.true; | ||
}); | ||
|
||
after(() => deleteDir()); | ||
}); | ||
describe('writeStreamToFile', function() { | ||
const metadata: FileMetadata = { | ||
id: 'some-uuid' | ||
}; | ||
const testContent = 'test'; | ||
const contentStream = strToStream(testContent); | ||
const filePath = fileService.buildFilePath(metadata.id); | ||
|
||
it('should write data to the file identified by id', function() { | ||
const writeAndRead = fileService.createTemporaryStorageFolder() | ||
.then(() => fileService.writeStreamToFile(metadata, contentStream)) | ||
.then(() => readFileAsync(filePath, 'utf-8')); | ||
return expect(writeAndRead).to.eventually.equal(testContent); | ||
}); | ||
|
||
after(function() { | ||
// reset written file | ||
return del(filePath, { force: true }); | ||
}); | ||
}); | ||
describe('buildFilePath', function() { | ||
it('should return a relative path to the root directory', function() { | ||
const value = fileService.buildFilePath('fake-file-id'); | ||
expect(value).to.contain(fileService.FILE_STORAGE_DIRECTORY); | ||
}); | ||
}); | ||
describe('multerMiddleware', function() { | ||
it('should return a middleware', function() { | ||
const middleware = fileService.multerMiddleware().any(); | ||
expect(middleware).to.be.a('function'); | ||
}); | ||
it('should accept other storage implementations', function() { | ||
const middleware = fileService.multerMiddleware(memoryStorage()).any(); | ||
expect(middleware).to.be.a('function'); | ||
}); | ||
}); | ||
}); |