Skip to content

Commit

Permalink
unit tests for creator-node/src/routes/track.js (#286)
Browse files Browse the repository at this point in the history
* addition to test/track.js

* adding happy path test for creating a downloadable track

* removing unnecessary test

* adding /.nyc_output to .gitignore & adding comments to package.json for new scripts
  • Loading branch information
vicky-g committed Feb 10, 2020
1 parent bac2325 commit 42203e6
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 2 deletions.
4 changes: 3 additions & 1 deletion creator-node/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ npm-debug.log
local-test/
eth-contract-config.json
local-storage/
test_file_storage/
test_file_storage/
coverage/
.nyc_output/
7 changes: 7 additions & 0 deletions creator-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"scripts": {
"start": "nodemon src/index.js | ./node_modules/.bin/bunyan || exit 0",
"test": "./scripts/run-tests.sh",
"coverage": "nyc npm run test",
"report": "nyc report --reporter=html",
"lint": "./node_modules/.bin/standard",
"lint-fix": "./node_modules/.bin/standard --fix",
"psql": "psql -U postgres -h localhost -p 4432 -d audius_creator_node"
Expand Down Expand Up @@ -49,6 +51,7 @@
"devDependencies": {
"mocha": "^5.2.0",
"nodemon": "^1.18.6",
"nyc": "^15.0.0",
"sequelize-cli": "^5.3.0",
"sinon": "^7.0.0",
"standard": "^12.0.1",
Expand All @@ -57,6 +60,10 @@
"//": {
"dependenciesComments": {
"lodash": "Vuln in < 4.17.13, fixed by https://github.com/lodash/lodash/pull/4336"
},
"scriptsComments": {
"coverage": "Runs nyc on tests/ dir and outputs results in ./nyc_output. Can be used for vscode extensions.",
"report": "Generates static html files representing code coverage per test file and outputs them into /coverage."
}
},
"standard": {
Expand Down
3 changes: 2 additions & 1 deletion creator-node/src/routes/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ module.exports = function (app) {
}))

/**
* Serve IPFS data hosted by creator node.
* Serve IPFS data hosted by creator node and create download route using query string pattern
* `...?filename=<file_name.mp3>`.
* @dev This route does not handle responses by design, so we can pipe the response to client.
*/
app.get('/ipfs/:CID', async (req, res) => {
Expand Down
86 changes: 86 additions & 0 deletions creator-node/test/tracks.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,4 +301,90 @@ describe('test Tracks', function () {
.send({ blockchainTrackId: 1, blockNumber: 10, metadataFileUUID: resp2.body.metadataFileUUID })
.expect(200)
})

// depends on "upload file to IPFS"
it('fails to create downloadable track with no track_id and no source_id present', async function () {
const file = fs.readFileSync(testAudioFilePath)

ipfsMock.addFromFs.exactly(34)
ipfsMock.pin.add.exactly(34)
libsMock.ethContracts.ServiceProviderFactoryClient.getServiceProviderInfoFromAddress.exactly(2)
libsMock.User.getUsers.exactly(2)

const resp1 = await request(app)
.post('/track_content')
.attach('file', file, { filename: 'fname.mp3' })
.set('Content-Type', 'multipart/form-data')
.set('X-Session-ID', session)
.expect(200)

expect(resp1.body.track_segments[0].multihash).to.equal('testCIDLink')
expect(resp1.body.track_segments.length).to.equal(32)

// creates a downloadable Audius track with no track_id and no source_file
const metadata = {
test: 'field1',
owner_id: 1,
track_segments: [{ 'multihash': 'testCIDLink', 'duration': 1000 }],
download: {
is_downloadable: true,
requires_follow: false
}
}

await request(app)
.post('/tracks/metadata')
.set('X-Session-ID', session)
.send({ metadata })
.expect(400)
})

// depends on "upload file to IPFS" and "creates Audius user" tests
it('creates a downloadable track', async function () {
const file = fs.readFileSync(testAudioFilePath)

ipfsMock.addFromFs.exactly(34)
ipfsMock.pin.add.exactly(34)
libsMock.ethContracts.ServiceProviderFactoryClient.getServiceProviderInfoFromAddress.exactly(4)
libsMock.User.getUsers.exactly(4)

const resp1 = await request(app)
.post('/track_content')
.attach('file', file, { filename: 'fname.mp3' })
.set('Content-Type', 'multipart/form-data')
.set('X-Session-ID', session)
.expect(200)

expect(resp1.body.track_segments[0].multihash).to.equal('testCIDLink')
expect(resp1.body.track_segments.length).to.equal(32)
expect(resp1.body.source_file).to.contain('.mp3')

// needs debugging as to why this 'cid' key is needed for test to work
const metadata = {
test: 'field1',
track_segments: [{ 'multihash': 'testCIDLink', 'duration': 1000 }],
owner_id: 1,
download: {
'is_downloadable': true,
'requires_follow': false,
'cid': 'testCIDLink'
}
}

const resp2 = await request(app)
.post('/tracks/metadata')
.set('X-Session-ID', session)
.send({ metadata, sourceFile: resp1.body.source_file })
.expect(200)

if (resp2.body.metadataMultihash !== 'testCIDLink') {
throw new Error('invalid return data')
}

await request(app)
.post('/tracks')
.set('X-Session-ID', session)
.send({ blockchainTrackId: 1, blockNumber: 10, metadataFileUUID: resp2.body.metadataFileUUID })
.expect(200)
})
})

0 comments on commit 42203e6

Please sign in to comment.