Files

444 lines
13 KiB
JavaScript
Raw Permalink Normal View History

2020-05-15 19:49:25 -07:00
/*
Unit tests for the IPFS Class.
*/
2021-01-02 15:41:12 -08:00
const assert = require('chai').assert
const sinon = require('sinon')
const BCHJS = require('../../src/bch-js')
2020-05-11 11:31:07 -07:00
let bchjs
2021-01-02 15:41:12 -08:00
const mockData = require('./fixtures/ipfs-mock')
2021-01-02 15:41:12 -08:00
describe('#IPFS', () => {
let sandbox
2020-05-11 11:31:07 -07:00
beforeEach(() => {
sandbox = sinon.createSandbox()
bchjs = new BCHJS()
})
afterEach(() => sandbox.restore())
2021-01-02 15:41:12 -08:00
describe('#initUppy', () => {
it('should initialize uppy', () => {
bchjs.IPFS.initUppy()
})
})
2021-01-02 15:41:12 -08:00
describe('#createFileModelServer', () => {
it('should throw an error if file does not exist', async () => {
try {
2021-01-02 15:41:12 -08:00
const path = '/non-existant-file'
await bchjs.IPFS.createFileModelServer(path)
2021-01-02 15:41:12 -08:00
assert.equal(true, false, 'Unexpected result')
} catch (err) {
2021-01-02 15:41:12 -08:00
// console.log(`err.message: ${err.message}`)
assert.include(err.message, 'Could not find this file')
}
})
2021-01-02 15:41:12 -08:00
it('should create a new file model', async () => {
2021-01-02 16:39:44 -08:00
const path = `${__dirname.toString()}/ipfs.js`
2020-05-15 19:49:25 -07:00
2020-05-16 09:17:44 -07:00
sandbox
2021-01-02 15:41:12 -08:00
.stub(bchjs.IPFS.axios, 'post')
.resolves({ data: mockData.mockNewFileModel })
2020-05-16 09:17:44 -07:00
const result = await bchjs.IPFS.createFileModelServer(path)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
2020-05-16 09:17:44 -07:00
2021-01-02 15:41:12 -08:00
assert.property(result, 'success')
assert.equal(result.success, true)
2021-01-02 15:41:12 -08:00
assert.property(result, 'hostingCostBCH')
assert.property(result, 'hostingCostUSD')
assert.property(result, 'file')
assert.property(result.file, 'payloadLink')
assert.property(result.file, 'hasBeenPaid')
assert.property(result.file, '_id')
assert.property(result.file, 'schemaVersion')
assert.property(result.file, 'size')
assert.property(result.file, 'fileName')
assert.property(result.file, 'fileExtension')
assert.property(result.file, 'createdTimestamp')
assert.property(result.file, 'hostingCost')
assert.property(result.file, 'walletIndex')
assert.property(result.file, 'bchAddr')
2020-05-16 09:17:44 -07:00
})
})
2021-01-02 15:41:12 -08:00
describe('#uploadFileServer', () => {
it('should throw an error if file does not exist', async () => {
try {
2021-01-02 15:41:12 -08:00
const path = '/non-existant-file'
await bchjs.IPFS.uploadFileServer(path)
2021-01-02 15:41:12 -08:00
assert.equal(true, false, 'Unexpected result')
} catch (err) {
2021-01-02 15:41:12 -08:00
// console.log(`err.message: ${err.message}`)
assert.include(err.message, 'Could not find this file')
}
})
2021-01-02 15:41:12 -08:00
it('should throw an error if modelId is not included', async () => {
try {
2021-01-02 16:39:44 -08:00
const path = `${__dirname.toString()}/ipfs.js`
await bchjs.IPFS.uploadFileServer(path)
2021-01-02 15:41:12 -08:00
assert.equal(true, false, 'Unexpected result')
} catch (err) {
2021-01-02 15:41:12 -08:00
// console.log(`err.message: ${err.message}`)
assert.include(err.message, 'Must include a file model ID')
}
})
2021-01-02 15:41:12 -08:00
it('Should throw error if the file was not uploaded', async () => {
try {
const mock = {
successful: [],
failed: [
{
2021-01-02 15:41:12 -08:00
id: 'file id'
}
]
}
2021-01-02 15:41:12 -08:00
sandbox.stub(bchjs.IPFS.uppy, 'upload').resolves(mock)
2021-01-02 16:39:44 -08:00
const path = `${__dirname.toString()}/ipfs.js`
2021-01-02 15:41:12 -08:00
await bchjs.IPFS.uploadFileServer(path, '5ec562319bfacc745e8d8a52')
2021-01-02 15:41:12 -08:00
assert.equal(true, false, 'Unexpected result')
} catch (err) {
2021-01-02 15:41:12 -08:00
// console.log(err)
assert.include(err.message, 'The file could not be uploaded')
}
})
2021-01-02 15:41:12 -08:00
it('should return file object if the file is uploaded', async () => {
try {
2021-01-02 15:41:12 -08:00
sandbox.stub(bchjs.IPFS.uppy, 'upload').resolves(mockData.uploadData)
2021-01-02 16:39:44 -08:00
const path = `${__dirname.toString()}/ipfs.js`
const result = await bchjs.IPFS.uploadFileServer(
path,
2021-01-02 15:41:12 -08:00
'5ec562319bfacc745e8d8a52'
)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
2021-01-02 15:41:12 -08:00
assert.property(result, 'schemaVersion')
assert.property(result, 'size')
assert.property(result, 'fileId')
assert.property(result, 'fileName')
assert.property(result, 'fileExtension')
} catch (err) {
2021-01-02 15:41:12 -08:00
// console.log(err)
assert.equal(true, false, 'Unexpected result')
}
})
})
2021-01-02 15:41:12 -08:00
describe('#getStatus', () => {
it('should throw an error if modelId is not included', async () => {
try {
await bchjs.IPFS.getStatus()
2021-01-02 15:41:12 -08:00
assert.equal(true, false, 'Unexpected result')
} catch (err) {
2021-01-02 15:41:12 -08:00
// console.log(`err.message: ${err.message}`)
assert.include(err.message, 'Must include a file model ID')
}
})
2021-01-02 15:41:12 -08:00
it('should get data on an unpaid file', async () => {
const modelId = '5ec7392c2acfe57aa62e945a'
sandbox
2021-01-02 15:41:12 -08:00
.stub(bchjs.IPFS.axios, 'get')
.resolves({ data: mockData.unpaidFileData })
const result = await bchjs.IPFS.getStatus(modelId)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
2021-01-02 15:41:12 -08:00
assert.property(result, 'hasBeenPaid')
assert.property(result, 'satCost')
assert.property(result, 'bchAddr')
assert.property(result, 'ipfsHash')
assert.property(result, 'fileId')
assert.property(result, 'fileName')
})
2021-01-02 15:41:12 -08:00
it('should get data on an unpaid file', async () => {
const modelId = '5ec7392c2acfe57aa62e945a'
sandbox
2021-01-02 15:41:12 -08:00
.stub(bchjs.IPFS.axios, 'get')
.resolves({ data: mockData.paidFileData })
const result = await bchjs.IPFS.getStatus(modelId)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
2021-01-02 15:41:12 -08:00
assert.property(result, 'hasBeenPaid')
assert.property(result, 'satCost')
assert.property(result, 'bchAddr')
assert.property(result, 'ipfsHash')
assert.property(result, 'fileId')
assert.property(result, 'fileName')
})
})
2021-01-02 15:41:12 -08:00
describe('#createFileModelWeb', () => {
it('should throw an error if file is undefined', async () => {
try {
let file
await bchjs.IPFS.createFileModelWeb(file)
2021-01-02 15:41:12 -08:00
assert.equal(true, false, 'Unexpected result')
} catch (err) {
2021-01-02 15:41:12 -08:00
// console.log(`err.message: ${err.message}`)
assert.include(err.message, 'File is required')
}
})
2021-01-02 15:41:12 -08:00
it('should throw an error if file is empty', async () => {
try {
const file = {}
await bchjs.IPFS.createFileModelWeb(file)
2021-01-02 15:41:12 -08:00
assert.equal(true, false, 'Unexpected result')
} catch (err) {
2021-01-02 15:41:12 -08:00
// console.log(`err.message: ${err.message}`)
assert.include(
err.message,
2021-01-02 15:41:12 -08:00
'File should have the property \'name\' of string type'
)
}
})
it("should throw an error if 'name' property is not included", async () => {
try {
const file = {
size: 5000
}
await bchjs.IPFS.createFileModelWeb(file)
2021-01-02 15:41:12 -08:00
assert.equal(true, false, 'Unexpected result')
} catch (err) {
2021-01-02 15:41:12 -08:00
// console.log(`err.message: ${err.message}`)
assert.include(
err.message,
2021-01-02 15:41:12 -08:00
'File should have the property \'name\' of string type'
)
}
})
it("should throw an error if 'size' property is not included", async () => {
try {
const file = {
2021-01-02 15:41:12 -08:00
name: 'ipfs.js'
}
await bchjs.IPFS.createFileModelWeb(file)
2021-01-02 15:41:12 -08:00
assert.equal(true, false, 'Unexpected result')
} catch (err) {
2021-01-02 15:41:12 -08:00
// console.log(`err.message: ${err.message}`)
assert.include(
err.message,
2021-01-02 15:41:12 -08:00
'File should have the property \'size\' of number type'
)
}
})
2021-01-02 15:41:12 -08:00
it('should create a new file model', async () => {
const file = {
2021-01-02 15:41:12 -08:00
name: 'ipfs.js',
size: 5000,
2021-01-02 15:41:12 -08:00
type: 'text/plain'
}
sandbox
2021-01-02 15:41:12 -08:00
.stub(bchjs.IPFS.axios, 'post')
.resolves({ data: mockData.mockNewFileModel })
const result = await bchjs.IPFS.createFileModelWeb(file)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
2021-01-02 15:41:12 -08:00
assert.property(result, 'success')
assert.equal(result.success, true)
2021-01-02 15:41:12 -08:00
assert.property(result, 'hostingCostBCH')
assert.property(result, 'hostingCostUSD')
assert.property(result, 'file')
assert.property(result.file, 'payloadLink')
assert.property(result.file, 'hasBeenPaid')
assert.property(result.file, '_id')
assert.property(result.file, 'schemaVersion')
assert.property(result.file, 'size')
assert.property(result.file, 'fileName')
assert.property(result.file, 'fileExtension')
assert.property(result.file, 'createdTimestamp')
assert.property(result.file, 'hostingCost')
assert.property(result.file, 'walletIndex')
assert.property(result.file, 'bchAddr')
})
})
2021-01-02 15:41:12 -08:00
describe('#uploadFileWeb', () => {
it('should throw an error if file is undefined', async () => {
try {
let file
await bchjs.IPFS.uploadFileWeb(file)
2021-01-02 15:41:12 -08:00
assert.equal(true, false, 'Unexpected result')
} catch (err) {
2021-01-02 15:41:12 -08:00
// console.log(`err.message: ${err.message}`)
assert.include(err.message, 'File is required')
}
})
2021-01-02 15:41:12 -08:00
it('should throw an error if file is empty', async () => {
try {
const file = {}
await bchjs.IPFS.uploadFileWeb(file)
2021-01-02 15:41:12 -08:00
assert.equal(true, false, 'Unexpected result')
} catch (err) {
2021-01-02 15:41:12 -08:00
// console.log(`err.message: ${err.message}`)
assert.include(
err.message,
2021-01-02 15:41:12 -08:00
'File should have the property \'name\' of string type'
)
}
})
it("should throw an error if 'name' property is not included", async () => {
try {
const file = {
size: 5000,
2021-01-02 15:41:12 -08:00
type: 'text/plain'
}
await bchjs.IPFS.uploadFileWeb(file)
2021-01-02 15:41:12 -08:00
assert.equal(true, false, 'Unexpected result')
} catch (err) {
2021-01-02 15:41:12 -08:00
// console.log(`err.message: ${err.message}`)
assert.include(
err.message,
2021-01-02 15:41:12 -08:00
'File should have the property \'name\' of string type'
)
}
})
it("should throw an error if 'size' property is not included", async () => {
try {
const file = {
2021-01-02 15:41:12 -08:00
name: 'ipfs.js',
type: 'text/plain'
}
await bchjs.IPFS.uploadFileWeb(file)
2021-01-02 15:41:12 -08:00
assert.equal(true, false, 'Unexpected result')
} catch (err) {
2021-01-02 15:41:12 -08:00
// console.log(`err.message: ${err.message}`)
assert.include(
err.message,
2021-01-02 15:41:12 -08:00
'File should have the property \'size\' of number type'
)
}
})
it("should throw an error if 'type' property is not included", async () => {
try {
const file = {
2021-01-02 15:41:12 -08:00
name: 'ipfs.js',
size: 5000
}
await bchjs.IPFS.uploadFileWeb(file)
2021-01-02 15:41:12 -08:00
assert.equal(true, false, 'Unexpected result')
} catch (err) {
2021-01-02 15:41:12 -08:00
// console.log(`err.message: ${err.message}`)
assert.include(
err.message,
2021-01-02 15:41:12 -08:00
'File should have the property \'type\' of string type'
)
}
})
2021-01-02 15:41:12 -08:00
it('should throw an error if modelId is not included', async () => {
try {
const file = {
2021-01-02 15:41:12 -08:00
name: 'ipfs.js',
size: 5000,
2021-01-02 15:41:12 -08:00
type: 'text/plain'
}
await bchjs.IPFS.uploadFileWeb(file)
2021-01-02 15:41:12 -08:00
assert.equal(true, false, 'Unexpected result')
} catch (err) {
2021-01-02 15:41:12 -08:00
// console.log(`err.message: ${err.message}`)
assert.include(err.message, 'Must include a file model ID')
}
})
2021-01-02 15:41:12 -08:00
it('Should throw error if the file was not uploaded', async () => {
try {
const mock = {
successful: [],
failed: [
{
2021-01-02 15:41:12 -08:00
id: 'file id'
}
]
}
2021-01-02 15:41:12 -08:00
sandbox.stub(bchjs.IPFS.uppy, 'upload').resolves(mock)
const file = {
2021-01-02 15:41:12 -08:00
name: 'ipfs.js',
size: 5000,
2021-01-02 15:41:12 -08:00
type: 'text/plain'
}
2021-01-02 15:41:12 -08:00
await bchjs.IPFS.uploadFileWeb(file, '5ec562319bfacc745e8d8a52')
2021-01-02 15:41:12 -08:00
assert.equal(true, false, 'Unexpected result')
} catch (err) {
2021-01-02 15:41:12 -08:00
// console.log(err)
assert.include(err.message, 'The file could not be uploaded')
}
})
2021-01-02 15:41:12 -08:00
it('should return file object if the file is uploaded', async () => {
try {
2021-01-02 15:41:12 -08:00
sandbox.stub(bchjs.IPFS.uppy, 'upload').resolves(mockData.uploadData)
const file = {
2021-01-02 15:41:12 -08:00
name: 'ipfs.js',
size: 5000,
2021-01-02 15:41:12 -08:00
type: 'text/plain'
}
const result = await bchjs.IPFS.uploadFileWeb(
file,
2021-01-02 15:41:12 -08:00
'5ec562319bfacc745e8d8a52'
)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
2021-01-02 15:41:12 -08:00
assert.property(result, 'schemaVersion')
assert.property(result, 'size')
assert.property(result, 'fileId')
assert.property(result, 'fileName')
assert.property(result, 'fileExtension')
} catch (err) {
2021-01-02 15:41:12 -08:00
// console.log(err)
assert.equal(true, false, 'Unexpected result')
}
})
})
})