From 961059dd5d5610a995918135a56fb0d2d5f7703f Mon Sep 17 00:00:00 2001 From: vicky Date: Mon, 16 May 2022 23:00:42 +0000 Subject: [PATCH 1/7] wip --- creator-node/src/blacklistManager.js | 6 +- .../contentBlacklistComponentService.js | 36 +- .../contentBlacklistController.js | 44 +- creator-node/src/utils.js | 4 + creator-node/test/contentBlacklist.test.js | 504 ++++++++++++------ creator-node/test/lib/dataSeeds.js | 90 +++- creator-node/test/lib/utils.js | 33 +- 7 files changed, 525 insertions(+), 192 deletions(-) diff --git a/creator-node/src/blacklistManager.js b/creator-node/src/blacklistManager.js index 306b2e53c88..57911bf65c5 100644 --- a/creator-node/src/blacklistManager.js +++ b/creator-node/src/blacklistManager.js @@ -662,8 +662,10 @@ class BlacklistManager { } // Retrieves all track ids in redis - static async getAllTrackIds() { - return redis.smembers(REDIS_SET_BLACKLIST_TRACKID_KEY) + static async getAllTrackIds({ offset = 0, limit = 100 }) { + const allTrackIds = await redis.smembers(REDIS_SET_BLACKLIST_TRACKID_KEY) + + return allTrackIds.slice(offset, limit) } static async getAllInvalidTrackIds() { diff --git a/creator-node/src/components/contentBlacklist/contentBlacklistComponentService.js b/creator-node/src/components/contentBlacklist/contentBlacklistComponentService.js index 3b237821917..7f8fd356464 100644 --- a/creator-node/src/components/contentBlacklist/contentBlacklistComponentService.js +++ b/creator-node/src/components/contentBlacklist/contentBlacklistComponentService.js @@ -3,28 +3,24 @@ const models = require('../../models') const types = models.ContentBlacklist.Types -const getAllContentBlacklist = async () => { - // Segments stored in the ContentBlacklist may not be associated with a track - const segmentsFromCBL = await models.ContentBlacklist.findAll({ - attributes: ['value'], - where: { - type: types.cid - }, - raw: true - }) - const individuallyBlacklistedSegments = segmentsFromCBL.map( - (entry) => entry.value - ) - const allSegments = await BlacklistManager.getAllCIDs() - const blacklistedContent = { - trackIds: await BlacklistManager.getAllTrackIds(), - userIds: await BlacklistManager.getAllUserIds(), - individualSegments: individuallyBlacklistedSegments, - numberOfSegments: allSegments.length, - allSegments +const getAllContentBlacklist = async ({ type, limit, offset }) => { + let values + switch (type) { + case types.user: { + values = await BlacklistManager.getAllUserIds() + break + } + case types.track: { + values = await BlacklistManager.getAllTrackIds({ offset, limit }) + break + } + case types.cid: { + values = await BlacklistManager.getAllCIDs() + break + } } - return blacklistedContent + return { count: values.length, values } } const addToContentBlacklist = async ({ type, values }) => { diff --git a/creator-node/src/components/contentBlacklist/contentBlacklistController.js b/creator-node/src/components/contentBlacklist/contentBlacklistController.js index abaeb2b42b4..27e61ac0305 100644 --- a/creator-node/src/components/contentBlacklist/contentBlacklistController.js +++ b/creator-node/src/components/contentBlacklist/contentBlacklistController.js @@ -25,7 +25,25 @@ const TYPES_SET = new Set([types.cid, types.user, types.track]) // Controllers const contentBlacklistGetAllController = async (req) => { - const blacklistedContent = await getAllContentBlacklist() + const type = parseContentType(req.query.type) + + if (!type) { + return errorResponseBadRequest( + 'Content type not specified or is not proper' + ) + } + + const { limit, offset } = parsePagination({ + limit: req.query.limit, + offset: req.query.offset + }) + + const blacklistedContent = await getAllContentBlacklist({ + type, + limit, + offset + }) + return successResponse(blacklistedContent) } @@ -263,6 +281,30 @@ const filterNonexistantIds = async (libs, type, ids) => { return ids } +const parseContentType = async (inputType) => { + if (!inputType) return null + + const type = inputType.toUpperCase() + if (!TYPES_SET.has(type)) return null + + return type +} + +const parsePagination = async ({ limit: inputLimit, offset: inputOffset }) => { + let limit = parseInt(inputLimit) + let offset = parseInt(inputOffset) + + if (limit < 0) { + limit = 100 + } + + if (offset < 0) { + offset = 0 + } + + return { offset, limit } +} + // Routes router.get('/blacklist', handleResponse(contentBlacklistGetAllController)) diff --git a/creator-node/src/utils.js b/creator-node/src/utils.js index 30a1b961607..ee1b9679f8f 100644 --- a/creator-node/src/utils.js +++ b/creator-node/src/utils.js @@ -29,6 +29,10 @@ class Utils { return new Promise((resolve) => setTimeout(resolve, ms)) } + /** + * Generates a random number from [0, max) + * @param {number} max the max random number. exclusive + */ static getRandomInt(max) { return Math.floor(Math.random() * max) } diff --git a/creator-node/test/contentBlacklist.test.js b/creator-node/test/contentBlacklist.test.js index 8d40dee5711..e8fe599c629 100644 --- a/creator-node/test/contentBlacklist.test.js +++ b/creator-node/test/contentBlacklist.test.js @@ -3,6 +3,7 @@ const request = require('supertest') const sinon = require('sinon') const path = require('path') +const Utils = require('../src/utils') const BlacklistManager = require('../src/blacklistManager') const models = require('../src/models') const redis = require('../src/redis') @@ -10,12 +11,19 @@ const { generateTimestampAndSignature } = require('../src/apiSigning') const { getApp } = require('./lib/app') const { getLibsMock } = require('./lib/libsMock') -const { createStarterCNodeUser, getCNodeUser, destroyUsers } = require('./lib/dataSeeds') +const { + createStarterCNodeUser, + getCNodeUser, + destroyUsers +} = require('./lib/dataSeeds') +const { generateRandomCID } = require('./lib/utils') const { uploadTrack } = require('./lib/helpers') // Dummy keys from circle config.yml const DELEGATE_OWNER_WALLET = '0x1eC723075E67a1a2B6969dC5CfF0C6793cb36D25' -const DELEGATE_PRIVATE_KEY = '0xdb527e4d4a2412a443c17e1666764d3bba43e89e61129a35f9abc337ec170a5d' +const DELEGATE_PRIVATE_KEY = + '0xdb527e4d4a2412a443c17e1666764d3bba43e89e61129a35f9abc337ec170a5d' +const MAX_ID = 1000 // throwaway wallet pair to set as notifier + sign for the request const trustedNotifierConfig = { @@ -69,10 +77,13 @@ describe('test ContentBlacklist', function () { it('should return the proper userIds, trackIds, and segments', async () => { ids = [43021] - const addUserData = generateTimestampAndSignature({ - type: BlacklistManager.getTypes().user, - values: ids - }, DELEGATE_PRIVATE_KEY) + const addUserData = generateTimestampAndSignature( + { + type: BlacklistManager.getTypes().user, + values: ids + }, + DELEGATE_PRIVATE_KEY + ) await request(app) .post('/blacklist/add') @@ -84,10 +95,13 @@ describe('test ContentBlacklist', function () { }) .expect(200) - const addTrackData = generateTimestampAndSignature({ - type: BlacklistManager.getTypes().track, - values: ids - }, DELEGATE_PRIVATE_KEY) + const addTrackData = generateTimestampAndSignature( + { + type: BlacklistManager.getTypes().track, + values: ids + }, + DELEGATE_PRIVATE_KEY + ) await request(app) .post('/blacklist/add') @@ -100,10 +114,13 @@ describe('test ContentBlacklist', function () { .expect(200) const cids = [generateRandomCID()] - const addCIDData = generateTimestampAndSignature({ - type: BlacklistManager.getTypes().cid, - values: cids - }, DELEGATE_PRIVATE_KEY) + const addCIDData = generateTimestampAndSignature( + { + type: BlacklistManager.getTypes().cid, + values: cids + }, + DELEGATE_PRIVATE_KEY + ) await request(app) .post('/blacklist/add') @@ -118,7 +135,7 @@ describe('test ContentBlacklist', function () { await request(app) .get('/blacklist') .expect(200) - .expect(resp => { + .expect((resp) => { assert.deepStrictEqual(resp.body.data.trackIds.length, 1) assert.deepStrictEqual(resp.body.data.trackIds[0], '43021') assert.deepStrictEqual(resp.body.data.userIds.length, 1) @@ -129,13 +146,17 @@ describe('test ContentBlacklist', function () { }) it('should return the proper userIds, trackIds, and segments when sent by trusted notifier', async () => { - mockServiceRegistry.trustedNotifierManager.trustedNotifierData.wallet = trustedNotifierConfig.wallet + mockServiceRegistry.trustedNotifierManager.trustedNotifierData.wallet = + trustedNotifierConfig.wallet ids = [43021] - const addUserData = generateTimestampAndSignature({ - type: BlacklistManager.getTypes().user, - values: ids - }, trustedNotifierConfig.privateKey) + const addUserData = generateTimestampAndSignature( + { + type: BlacklistManager.getTypes().user, + values: ids + }, + trustedNotifierConfig.privateKey + ) await request(app) .post('/blacklist/add') @@ -147,10 +168,13 @@ describe('test ContentBlacklist', function () { }) .expect(200) - const addTrackData = generateTimestampAndSignature({ - type: BlacklistManager.getTypes().track, - values: ids - }, trustedNotifierConfig.privateKey) + const addTrackData = generateTimestampAndSignature( + { + type: BlacklistManager.getTypes().track, + values: ids + }, + trustedNotifierConfig.privateKey + ) await request(app) .post('/blacklist/add') @@ -163,10 +187,13 @@ describe('test ContentBlacklist', function () { .expect(200) const cids = [generateRandomCID()] - const addCIDData = generateTimestampAndSignature({ - type: BlacklistManager.getTypes().cid, - values: cids - }, trustedNotifierConfig.privateKey) + const addCIDData = generateTimestampAndSignature( + { + type: BlacklistManager.getTypes().cid, + values: cids + }, + trustedNotifierConfig.privateKey + ) await request(app) .post('/blacklist/add') @@ -181,7 +208,7 @@ describe('test ContentBlacklist', function () { await request(app) .get('/blacklist') .expect(200) - .expect(resp => { + .expect((resp) => { assert.deepStrictEqual(resp.body.data.trackIds.length, 1) assert.deepStrictEqual(resp.body.data.trackIds[0], '43021') assert.deepStrictEqual(resp.body.data.userIds.length, 1) @@ -192,9 +219,12 @@ describe('test ContentBlacklist', function () { }) it('should add user type and id to db and redis', async () => { - ids = [generateRandomNaturalNumber()] + ids = [Utils.getRandomInt(MAX_ID)] const type = BlacklistManager.getTypes().user - const { signature, timestamp } = generateTimestampAndSignature({ type, values: ids }, DELEGATE_PRIVATE_KEY) + const { signature, timestamp } = generateTimestampAndSignature( + { type, values: ids }, + DELEGATE_PRIVATE_KEY + ) await request(app) .post('/blacklist/add') @@ -203,20 +233,26 @@ describe('test ContentBlacklist', function () { const user = await models.ContentBlacklist.findOne({ where: { - value: { [models.Sequelize.Op.in]: ids.map(id => id.toString()) }, + value: { [models.Sequelize.Op.in]: ids.map((id) => id.toString()) }, type } }) assert.deepStrictEqual(user.value, ids[0].toString()) assert.deepStrictEqual(user.type, type) - assert.deepStrictEqual(await BlacklistManager.userIdIsInBlacklist(user.value), 1) + assert.deepStrictEqual( + await BlacklistManager.userIdIsInBlacklist(user.value), + 1 + ) }) it('should add track type and id to db and redis', async () => { - ids = [generateRandomNaturalNumber()] + ids = [Utils.getRandomInt(MAX_ID)] const type = BlacklistManager.getTypes().track - const { signature, timestamp } = generateTimestampAndSignature({ type, values: ids }, DELEGATE_PRIVATE_KEY) + const { signature, timestamp } = generateTimestampAndSignature( + { type, values: ids }, + DELEGATE_PRIVATE_KEY + ) await request(app) .post('/blacklist/add') @@ -226,19 +262,26 @@ describe('test ContentBlacklist', function () { const track = await models.ContentBlacklist.findOne({ where: { value: { - [models.Sequelize.Op.in]: ids.map(id => id.toString()) }, + [models.Sequelize.Op.in]: ids.map((id) => id.toString()) + }, type } }) assert.deepStrictEqual(track.value, ids[0].toString()) assert.deepStrictEqual(track.type, type) - assert.deepStrictEqual(await BlacklistManager.trackIdIsInBlacklist(track.value), 1) + assert.deepStrictEqual( + await BlacklistManager.trackIdIsInBlacklist(track.value), + 1 + ) }) it('should remove user type and id from db and redis', async () => { - ids = [generateRandomNaturalNumber()] + ids = [Utils.getRandomInt(MAX_ID)] const type = BlacklistManager.getTypes().user - const { signature, timestamp } = generateTimestampAndSignature({ type, values: ids }, DELEGATE_PRIVATE_KEY) + const { signature, timestamp } = generateTimestampAndSignature( + { type, values: ids }, + DELEGATE_PRIVATE_KEY + ) await request(app) .post('/blacklist/add') @@ -253,18 +296,25 @@ describe('test ContentBlacklist', function () { const user = await models.ContentBlacklist.findOne({ where: { value: { - [models.Sequelize.Op.in]: ids.map(id => id.toString()) }, + [models.Sequelize.Op.in]: ids.map((id) => id.toString()) + }, type } }) assert.deepStrictEqual(user, null) - assert.deepStrictEqual(await BlacklistManager.userIdIsInBlacklist(ids[0]), 0) + assert.deepStrictEqual( + await BlacklistManager.userIdIsInBlacklist(ids[0]), + 0 + ) }) it('should remove track type and id from db and redis', async () => { - ids = [generateRandomNaturalNumber()] + ids = [Utils.getRandomInt(MAX_ID)] const type = BlacklistManager.getTypes().track - const { signature, timestamp } = generateTimestampAndSignature({ type, values: ids }, DELEGATE_PRIVATE_KEY) + const { signature, timestamp } = generateTimestampAndSignature( + { type, values: ids }, + DELEGATE_PRIVATE_KEY + ) await request(app) .post('/blacklist/add') @@ -279,57 +329,81 @@ describe('test ContentBlacklist', function () { const track = await models.ContentBlacklist.findOne({ where: { value: { - [models.Sequelize.Op.in]: ids.map(id => id.toString()) }, + [models.Sequelize.Op.in]: ids.map((id) => id.toString()) + }, type } }) assert.deepStrictEqual(track, null) - assert.deepStrictEqual(await BlacklistManager.trackIdIsInBlacklist(ids[0]), 0) + assert.deepStrictEqual( + await BlacklistManager.trackIdIsInBlacklist(ids[0]), + 0 + ) }) it('should return success when removing a user that does not exist', async () => { - ids = [generateRandomNaturalNumber()] + ids = [Utils.getRandomInt(MAX_ID)] const type = BlacklistManager.getTypes().user - const { signature, timestamp } = generateTimestampAndSignature({ type, values: ids }, DELEGATE_PRIVATE_KEY) + const { signature, timestamp } = generateTimestampAndSignature( + { type, values: ids }, + DELEGATE_PRIVATE_KEY + ) await request(app) .post('/blacklist/remove') .query({ type, 'values[]': ids, timestamp, signature }) .expect(200) - const user = await models.ContentBlacklist.findOne({ where: { - value: { - [models.Sequelize.Op.in]: ids.map(id => id.toString()) }, - type - } }) + const user = await models.ContentBlacklist.findOne({ + where: { + value: { + [models.Sequelize.Op.in]: ids.map((id) => id.toString()) + }, + type + } + }) assert.deepStrictEqual(user, null) - assert.deepStrictEqual(await BlacklistManager.userIdIsInBlacklist(ids[0]), 0) + assert.deepStrictEqual( + await BlacklistManager.userIdIsInBlacklist(ids[0]), + 0 + ) }) it('should return success when removing a track that does not exist', async () => { - ids = [generateRandomNaturalNumber()] + ids = [Utils.getRandomInt(MAX_ID)] const type = BlacklistManager.getTypes().track - const { signature, timestamp } = generateTimestampAndSignature({ type, values: ids }, DELEGATE_PRIVATE_KEY) + const { signature, timestamp } = generateTimestampAndSignature( + { type, values: ids }, + DELEGATE_PRIVATE_KEY + ) await request(app) .post('/blacklist/remove') .query({ type, 'values[]': ids, timestamp, signature }) .expect(200) - const track = await models.ContentBlacklist.findOne({ where: { - value: { - [models.Sequelize.Op.in]: ids.map(id => id.toString()) - }, - type - } }) + const track = await models.ContentBlacklist.findOne({ + where: { + value: { + [models.Sequelize.Op.in]: ids.map((id) => id.toString()) + }, + type + } + }) assert.deepStrictEqual(track, null) - assert.deepStrictEqual(await BlacklistManager.trackIdIsInBlacklist(ids[0]), 0) + assert.deepStrictEqual( + await BlacklistManager.trackIdIsInBlacklist(ids[0]), + 0 + ) }) it('should ignore duplicate add for track', async () => { - ids = [generateRandomNaturalNumber()] + ids = [Utils.getRandomInt(MAX_ID)] const type = BlacklistManager.getTypes().track - const { signature, timestamp } = generateTimestampAndSignature({ type, values: ids }, DELEGATE_PRIVATE_KEY) + const { signature, timestamp } = generateTimestampAndSignature( + { type, values: ids }, + DELEGATE_PRIVATE_KEY + ) await request(app) .post('/blacklist/add') @@ -341,22 +415,31 @@ describe('test ContentBlacklist', function () { .query({ type, 'values[]': ids, timestamp, signature }) .expect(200) - const tracks = await models.ContentBlacklist.findAll({ where: { - value: { - [models.Sequelize.Op.in]: ids.map(id => id.toString()) }, - type - } }) + const tracks = await models.ContentBlacklist.findAll({ + where: { + value: { + [models.Sequelize.Op.in]: ids.map((id) => id.toString()) + }, + type + } + }) assert.deepStrictEqual(tracks.length, 1) const track = tracks[0] assert.deepStrictEqual(track.value, ids[0].toString()) assert.deepStrictEqual(track.type, type) - assert.deepStrictEqual(await BlacklistManager.trackIdIsInBlacklist(track.value), 1) + assert.deepStrictEqual( + await BlacklistManager.trackIdIsInBlacklist(track.value), + 1 + ) }) it('should ignore duplicate add for user', async () => { - ids = [generateRandomNaturalNumber()] + ids = [Utils.getRandomInt(MAX_ID)] const type = BlacklistManager.getTypes().user - const { signature, timestamp } = generateTimestampAndSignature({ type, values: ids }, DELEGATE_PRIVATE_KEY) + const { signature, timestamp } = generateTimestampAndSignature( + { type, values: ids }, + DELEGATE_PRIVATE_KEY + ) await request(app) .post('/blacklist/add') @@ -368,23 +451,32 @@ describe('test ContentBlacklist', function () { .query({ type, 'values[]': ids, timestamp, signature }) .expect(200) - const users = await models.ContentBlacklist.findAll({ where: { - value: { - [models.Sequelize.Op.in]: ids.map(id => id.toString()) }, - type - } }) + const users = await models.ContentBlacklist.findAll({ + where: { + value: { + [models.Sequelize.Op.in]: ids.map((id) => id.toString()) + }, + type + } + }) assert.deepStrictEqual(users.length, 1) const user = users[0] assert.deepStrictEqual(user.value, ids[0].toString()) assert.deepStrictEqual(user.type, type) - assert.deepStrictEqual(await BlacklistManager.userIdIsInBlacklist(user.value), 1) + assert.deepStrictEqual( + await BlacklistManager.userIdIsInBlacklist(user.value), + 1 + ) }) it('should only blacklist partial user ids list if only some ids are found', async () => { - ids = [generateRandomNaturalNumber(), generateRandomNaturalNumber()] + ids = [Utils.getRandomInt(MAX_ID), Utils.getRandomInt(MAX_ID)] libsMock.User.getUsers.returns([{ user_id: ids[0] }]) // only user @ index 0 is found const type = BlacklistManager.getTypes().user - const { signature, timestamp } = generateTimestampAndSignature({ type, values: ids }, DELEGATE_PRIVATE_KEY) + const { signature, timestamp } = generateTimestampAndSignature( + { type, values: ids }, + DELEGATE_PRIVATE_KEY + ) await request(app) .post('/blacklist/add') @@ -394,7 +486,7 @@ describe('test ContentBlacklist', function () { // Ensure only one user was added to the blacklist const users = await models.ContentBlacklist.findAll({ where: { - value: { [models.Sequelize.Op.in]: ids.map(id => id.toString()) }, + value: { [models.Sequelize.Op.in]: ids.map((id) => id.toString()) }, type } }) @@ -403,14 +495,20 @@ describe('test ContentBlacklist', function () { const user = users[0] assert.deepStrictEqual(user.value, ids[0].toString()) assert.deepStrictEqual(user.type, type) - assert.deepStrictEqual(await BlacklistManager.userIdIsInBlacklist(user.value), 1) + assert.deepStrictEqual( + await BlacklistManager.userIdIsInBlacklist(user.value), + 1 + ) }) it('should only blacklist partial track ids list if only some ids are found', async () => { - ids = [generateRandomNaturalNumber(), generateRandomNaturalNumber()] + ids = [Utils.getRandomInt(MAX_ID), Utils.getRandomInt(MAX_ID)] libsMock.Track.getTracks.returns([{ track_id: ids[0] }]) // only user @ index 0 is found const type = BlacklistManager.getTypes().track - const { signature, timestamp } = generateTimestampAndSignature({ type, values: ids }, DELEGATE_PRIVATE_KEY) + const { signature, timestamp } = generateTimestampAndSignature( + { type, values: ids }, + DELEGATE_PRIVATE_KEY + ) await request(app) .post('/blacklist/add') @@ -420,7 +518,7 @@ describe('test ContentBlacklist', function () { // Ensure only one track was added to the blacklist const tracks = await models.ContentBlacklist.findAll({ where: { - value: { [models.Sequelize.Op.in]: ids.map(id => id.toString()) }, + value: { [models.Sequelize.Op.in]: ids.map((id) => id.toString()) }, type } }) @@ -429,13 +527,19 @@ describe('test ContentBlacklist', function () { const track = tracks[0] assert.deepStrictEqual(track.value, ids[0].toString()) assert.deepStrictEqual(track.type, type) - assert.deepStrictEqual(await BlacklistManager.trackIdIsInBlacklist(track.value), 1) + assert.deepStrictEqual( + await BlacklistManager.trackIdIsInBlacklist(track.value), + 1 + ) }) it('should add cids to db and redis', async () => { const cids = [generateRandomCID()] const type = BlacklistManager.getTypes().cid - const { signature, timestamp } = generateTimestampAndSignature({ type, values: cids }, DELEGATE_PRIVATE_KEY) + const { signature, timestamp } = generateTimestampAndSignature( + { type, values: cids }, + DELEGATE_PRIVATE_KEY + ) await request(app) .post('/blacklist/add') @@ -457,7 +561,10 @@ describe('test ContentBlacklist', function () { it('should remove cids from db and redis', async () => { const cids = [generateRandomCID()] const type = BlacklistManager.getTypes().cid - const { signature, timestamp } = generateTimestampAndSignature({ type, values: cids }, DELEGATE_PRIVATE_KEY) + const { signature, timestamp } = generateTimestampAndSignature( + { type, values: cids }, + DELEGATE_PRIVATE_KEY + ) await request(app) .post('/blacklist/add') @@ -481,11 +588,15 @@ describe('test ContentBlacklist', function () { }) it("should throw an error if delegate private key does not match that of the creator node's", async () => { - ids = [generateRandomNaturalNumber()] + ids = [Utils.getRandomInt(MAX_ID)] const type = BlacklistManager.getTypes().user - const BAD_KEY = '0xBADKEY4d4a2412a443c17e1666764d3bba43e89e61129a35f9abc337ec170a5d' + const BAD_KEY = + '0xBADKEY4d4a2412a443c17e1666764d3bba43e89e61129a35f9abc337ec170a5d' - const { signature, timestamp } = generateTimestampAndSignature({ type, values: ids }, BAD_KEY) + const { signature, timestamp } = generateTimestampAndSignature( + { type, values: ids }, + BAD_KEY + ) await request(app) .post('/blacklist/add') @@ -494,7 +605,7 @@ describe('test ContentBlacklist', function () { }) it('should throw an error if query params does not contain all necessary keys', async () => { - ids = [generateRandomNaturalNumber()] + ids = [Utils.getRandomInt(MAX_ID)] const type = BlacklistManager.getTypes().track await request(app) @@ -511,7 +622,10 @@ describe('test ContentBlacklist', function () { it('should throw an error if query params id and type are not proper', async () => { const improperIds = 'halsey' const type = 'is fantastic' - const { signature, timestamp } = generateTimestampAndSignature({ type, values: improperIds }, DELEGATE_PRIVATE_KEY) + const { signature, timestamp } = generateTimestampAndSignature( + { type, values: improperIds }, + DELEGATE_PRIVATE_KEY + ) await request(app) .post('/blacklist/add') @@ -533,7 +647,10 @@ describe('test ContentBlacklist', function () { // Blacklist trackId const type = BlacklistManager.getTypes().user - const { signature, timestamp } = generateTimestampAndSignature({ type, values: ids }, DELEGATE_PRIVATE_KEY) + const { signature, timestamp } = generateTimestampAndSignature( + { type, values: ids }, + DELEGATE_PRIVATE_KEY + ) await request(app) .post('/blacklist/add') .query({ type, 'values[]': ids, signature, timestamp }) @@ -541,7 +658,7 @@ describe('test ContentBlacklist', function () { // Hit /ipfs/:CID route for all track CIDs and ensure error response is returned await Promise.all( - data.track.trackSegments.map(segment => + data.track.trackSegments.map((segment) => request(app) .get(`/ipfs/${segment.multihash}`) .query({ trackId }) @@ -560,7 +677,10 @@ describe('test ContentBlacklist', function () { // Blacklist trackId const type = BlacklistManager.getTypes().track - const { signature, timestamp } = generateTimestampAndSignature({ type, values: ids }, DELEGATE_PRIVATE_KEY) + const { signature, timestamp } = generateTimestampAndSignature( + { type, values: ids }, + DELEGATE_PRIVATE_KEY + ) await request(app) .post('/blacklist/add') .query({ type, 'values[]': ids, signature, timestamp }) @@ -568,10 +688,8 @@ describe('test ContentBlacklist', function () { // Hit /ipfs/:CID route for all track CIDs and ensure error response is returned because no trackId was passed await Promise.all( - data.track.trackSegments.map(segment => - request(app) - .get(`/ipfs/${segment.multihash}`) - .expect(403) + data.track.trackSegments.map((segment) => + request(app).get(`/ipfs/${segment.multihash}`).expect(403) ) ) }) @@ -584,7 +702,10 @@ describe('test ContentBlacklist', function () { // Blacklist trackId const type = BlacklistManager.getTypes().track - const { signature, timestamp } = generateTimestampAndSignature({ type, values: ids }, DELEGATE_PRIVATE_KEY) + const { signature, timestamp } = generateTimestampAndSignature( + { type, values: ids }, + DELEGATE_PRIVATE_KEY + ) await request(app) .post('/blacklist/add') .query({ type, 'values[]': ids, signature, timestamp }) @@ -592,7 +713,7 @@ describe('test ContentBlacklist', function () { // Hit /ipfs/:CID route for all track CIDs and ensure error response is returned await Promise.all( - data.track.trackSegments.map(segment => + data.track.trackSegments.map((segment) => request(app) .get(`/ipfs/${segment.multihash}`) .query({ trackId: data.track.blockchainId }) @@ -607,7 +728,7 @@ describe('test ContentBlacklist', function () { // After removing from blacklist, track should be streamable await Promise.all( - data.track.trackSegments.map(segment => + data.track.trackSegments.map((segment) => request(app) .get(`/ipfs/${segment.multihash}`) .query({ trackId }) @@ -624,7 +745,10 @@ describe('test ContentBlacklist', function () { // Blacklist trackId const type = BlacklistManager.getTypes().track - const { signature, timestamp } = generateTimestampAndSignature({ type, values: ids }, DELEGATE_PRIVATE_KEY) + const { signature, timestamp } = generateTimestampAndSignature( + { type, values: ids }, + DELEGATE_PRIVATE_KEY + ) await request(app) .post('/blacklist/add') .query({ type, 'values[]': ids, signature, timestamp }) @@ -632,7 +756,7 @@ describe('test ContentBlacklist', function () { // Hit /ipfs/:CID route for all track CIDs and ensure error response is returned await Promise.all( - data.track.trackSegments.map(segment => + data.track.trackSegments.map((segment) => request(app) .get(`/ipfs/${segment.multihash}`) .query({ trackId: 1234 }) @@ -645,12 +769,19 @@ describe('test ContentBlacklist', function () { it('should not throw an error when streaming a blacklisted CID of a non-blacklisted track at /ipfs/:CID?trackId=', async () => { // Create user and upload track const track1 = await createUserAndUploadTrack() - const track2 = await createUserAndUploadTrack({ inputUserId: 2, trackId: 2, pubKey: '0x3f8f51ed837b15af580eb96cee740c723d340e7f' }) + const track2 = await createUserAndUploadTrack({ + inputUserId: 2, + trackId: 2, + pubKey: '0x3f8f51ed837b15af580eb96cee740c723d340e7f' + }) ids = [track1.track.blockchainId] // Blacklist trackId const type = BlacklistManager.getTypes().track - const { signature, timestamp } = generateTimestampAndSignature({ type, values: ids }, DELEGATE_PRIVATE_KEY) + const { signature, timestamp } = generateTimestampAndSignature( + { type, values: ids }, + DELEGATE_PRIVATE_KEY + ) await request(app) .post('/blacklist/add') .query({ type, 'values[]': ids, signature, timestamp }) @@ -658,7 +789,7 @@ describe('test ContentBlacklist', function () { // Hit /ipfs/:CID route for all track CIDs and ensure no error response is returned await Promise.all( - track2.track.trackSegments.map(segment => + track2.track.trackSegments.map((segment) => request(app) .get(`/ipfs/${segment.multihash}`) .query({ trackId: track2.track.blockchainId }) @@ -670,85 +801,142 @@ describe('test ContentBlacklist', function () { it('should throw an error when adding a cid to the blacklist and streaming /ipfs/:CID', async () => { const cids = [generateRandomCID()] const type = BlacklistManager.getTypes().cid - const { signature, timestamp } = generateTimestampAndSignature({ type, values: cids }, DELEGATE_PRIVATE_KEY) + const { signature, timestamp } = generateTimestampAndSignature( + { type, values: cids }, + DELEGATE_PRIVATE_KEY + ) await request(app) .post('/blacklist/add') .query({ type, 'values[]': cids, timestamp, signature }) .expect(200) - await request(app) - .get(`/ipfs/${cids[0]}`) - .expect(403) + await request(app).get(`/ipfs/${cids[0]}`).expect(403) }) it('should throw an error if user id does not exist', async () => { libsMock.User.getUsers.returns([]) - ids = [generateRandomNaturalNumber()] + ids = [Utils.getRandomInt(MAX_ID)] const type = BlacklistManager.getTypes().user - const resp1 = generateTimestampAndSignature({ type, values: ids }, DELEGATE_PRIVATE_KEY) + const resp1 = generateTimestampAndSignature( + { type, values: ids }, + DELEGATE_PRIVATE_KEY + ) await request(app) .post('/blacklist/add') - .query({ type, 'values[]': ids, timestamp: resp1.timestamp, signature: resp1.signature }) + .query({ + type, + 'values[]': ids, + timestamp: resp1.timestamp, + signature: resp1.signature + }) .expect(400) // Ensure works with multiple ids - ids.push(generateRandomNaturalNumber()) - const resp2 = generateTimestampAndSignature({ type, values: ids }, DELEGATE_PRIVATE_KEY) + ids.push(Utils.getRandomInt(MAX_ID)) + const resp2 = generateTimestampAndSignature( + { type, values: ids }, + DELEGATE_PRIVATE_KEY + ) await request(app) .post('/blacklist/add') - .query({ type, 'values[]': ids, timestamp: resp2.timestamp, signature: resp2.signature }) + .query({ + type, + 'values[]': ids, + timestamp: resp2.timestamp, + signature: resp2.signature + }) .expect(400) }) it('should throw an error if track id does not exist', async () => { libsMock.Track.getTracks.returns([]) - ids = [generateRandomNaturalNumber()] + ids = [Utils.getRandomInt(MAX_ID)] const type = BlacklistManager.getTypes().track - const resp1 = generateTimestampAndSignature({ type, values: ids }, DELEGATE_PRIVATE_KEY) + const resp1 = generateTimestampAndSignature( + { type, values: ids }, + DELEGATE_PRIVATE_KEY + ) await request(app) .post('/blacklist/add') - .query({ type, 'values[]': ids, timestamp: resp1.timestamp, signature: resp1.signature }) + .query({ + type, + 'values[]': ids, + timestamp: resp1.timestamp, + signature: resp1.signature + }) .expect(400) // Ensure works with multiple ids - ids.push(generateRandomNaturalNumber()) - const resp2 = generateTimestampAndSignature({ type, values: ids }, DELEGATE_PRIVATE_KEY) + ids.push(Utils.getRandomInt(MAX_ID)) + const resp2 = generateTimestampAndSignature( + { type, values: ids }, + DELEGATE_PRIVATE_KEY + ) await request(app) .post('/blacklist/add') - .query({ type, 'values[]': ids, timestamp: resp2.timestamp, signature: resp2.signature }) + .query({ + type, + 'values[]': ids, + timestamp: resp2.timestamp, + signature: resp2.signature + }) .expect(400) }) it('should throw an error if disc prov is unable to lookup ids', async () => { libsMock.User.getUsers.returns([]) - ids = [generateRandomNaturalNumber()] + ids = [Utils.getRandomInt(MAX_ID)] const type = BlacklistManager.getTypes().user - const resp1 = generateTimestampAndSignature({ type, values: ids }, DELEGATE_PRIVATE_KEY) + const resp1 = generateTimestampAndSignature( + { type, values: ids }, + DELEGATE_PRIVATE_KEY + ) await request(app) .post('/blacklist/add') - .query({ type, 'values[]': ids, timestamp: resp1.timestamp, signature: resp1.signature }) + .query({ + type, + 'values[]': ids, + timestamp: resp1.timestamp, + signature: resp1.signature + }) .expect(400) // Ensure works with multiple ids - ids.push(generateRandomNaturalNumber()) - const resp2 = generateTimestampAndSignature({ type, values: ids }, DELEGATE_PRIVATE_KEY) + ids.push(Utils.getRandomInt(MAX_ID)) + const resp2 = generateTimestampAndSignature( + { type, values: ids }, + DELEGATE_PRIVATE_KEY + ) await request(app) .post('/blacklist/add') - .query({ type, 'values[]': ids, timestamp: resp2.timestamp, signature: resp2.signature }) + .query({ + type, + 'values[]': ids, + timestamp: resp2.timestamp, + signature: resp2.signature + }) .expect(400) }) it('should throw an error if query params cids does not match the Qm... pattern', async () => { - const cids = ['vicky was here', 'and here too', generateRandomNaturalNumber(), '###%^&'] + const cids = [ + 'vicky was here', + 'and here too', + Utils.getRandomInt(MAX_ID), + '###%^&' + ] const type = BlacklistManager.getTypes().cid - const { timestamp, signature } = generateTimestampAndSignature({ type: BlacklistManager.getTypes().cid, values: cids }, DELEGATE_PRIVATE_KEY) + const { timestamp, signature } = generateTimestampAndSignature( + { type: BlacklistManager.getTypes().cid, values: cids }, + DELEGATE_PRIVATE_KEY + ) await request(app) .post('/blacklist/add') @@ -769,7 +957,10 @@ describe('test ContentBlacklist', function () { // Blacklist trackId const type = BlacklistManager.getTypes().track - const { signature, timestamp } = generateTimestampAndSignature({ type, values: ids }, DELEGATE_PRIVATE_KEY) + const { signature, timestamp } = generateTimestampAndSignature( + { type, values: ids }, + DELEGATE_PRIVATE_KEY + ) await request(app) .post('/blacklist/add') .query({ type, 'values[]': ids, signature, timestamp }) @@ -784,13 +975,24 @@ describe('test ContentBlacklist', function () { }) /** Helper setup method to test ContentBlacklist. */ - async function createUserAndUploadTrack ({ inputUserId, trackId, pubKey } = { inputUserId: userId, trackId: generateRandomNaturalNumber(), pubKey: null }) { + async function createUserAndUploadTrack( + { inputUserId, trackId, pubKey } = { + inputUserId: userId, + trackId: Utils.getRandomInt(MAX_ID), + pubKey: null + } + ) { // Create user let cnodeUserUUID, sessionToken if (!pubKey) { - ({ cnodeUserUUID, sessionToken } = await createStarterCNodeUser(inputUserId)) + ;({ cnodeUserUUID, sessionToken } = await createStarterCNodeUser( + inputUserId + )) } else { - ({ cnodeUserUUID, sessionToken } = await createStarterCNodeUser(inputUserId, pubKey)) + ;({ cnodeUserUUID, sessionToken } = await createStarterCNodeUser( + inputUserId, + pubKey + )) } const cnodeUser = await getCNodeUser(cnodeUserUUID) @@ -800,7 +1002,11 @@ describe('test ContentBlacklist', function () { testField: 'testValue' } } - const { body: { data: { metadataFileUUID } } } = await request(app) + const { + body: { + data: { metadataFileUUID } + } + } = await request(app) .post('/audius_users/metadata') .set('X-Session-ID', sessionToken) .set('User-Id', inputUserId) @@ -839,13 +1045,15 @@ describe('test ContentBlacklist', function () { track_segments: trackSegments } const { - body: { data: { metadataFileUUID: trackMetadataFileUUID } } + body: { + data: { metadataFileUUID: trackMetadataFileUUID } + } } = await request(app) .post('/tracks/metadata') .set('X-Session-ID', sessionToken) .set('User-Id', inputUserId) .send({ metadata: trackMetadata, source_file: sourceFile }) - // associate track metadata with track + // associate track metadata with track await request(app) .post('/tracks') .set('X-Session-ID', sessionToken) @@ -862,20 +1070,6 @@ describe('test ContentBlacklist', function () { } }) -// Generates a random number from [0, max) -// https://stackoverflow.com/questions/29640432/generate-4-digit-random-number-using-substring/29640472 -// NOTE: There is a chance the same number will be returned....... :-) -const generateRandomNaturalNumber = (max = 1000) => Math.floor(Math.random() * max) - -// Generates a random CID with suffix of a random number of n digits -const generateRandomCID = (numRandomDigits = 5, maxRandomNumber = 1000) => { - // If n is out of bounds, default to 5 - if (numRandomDigits < 0 || numRandomDigits > 46) numRandomDigits = 5 - const randomNDigitNumber = (Array(numRandomDigits).join('0') + generateRandomNaturalNumber(maxRandomNumber)).slice(-numRandomDigits) - - // Return Qm..aaa... of length 46. Array(..) part needs + 1 to generate the remaining amount - return 'Qm' + randomNDigitNumber + Array(46 - 2 - numRandomDigits + 1).join('a') -} // Setup libs mock according to ContentBlacklist needs by using libsMock as the base const setupLibsMock = (libsMock) => { libsMock = getLibsMock() @@ -885,7 +1079,7 @@ const setupLibsMock = (libsMock) => { // getUsers() is used in creating user/uploading track flow. // setting ids to dummy value allows for above flows to work if (!ids) ids = [0] - const resp = ids.map(id => { + const resp = ids.map((id) => { return { creator_node_endpoint: 'http://localhost:5000', blocknumber: 10, @@ -899,7 +1093,7 @@ const setupLibsMock = (libsMock) => { libsMock.Track = { getTracks: sinon.mock() } libsMock.Track.getTracks.callsFake((limit, offset, ids) => { - return ids.map(id => { + return ids.map((id) => { return { track_id: id } diff --git a/creator-node/test/lib/dataSeeds.js b/creator-node/test/lib/dataSeeds.js index 24ff3239761..33ba310be4f 100644 --- a/creator-node/test/lib/dataSeeds.js +++ b/creator-node/test/lib/dataSeeds.js @@ -1,32 +1,42 @@ -const sessionManager = require('../../src/sessionManager') -const { CNodeUser, SessionToken } = require('../../src/models') const uuid = require('uuid/v4') +const Utils = require('../../src/utils') +const sessionManager = require('../../src/sessionManager') +const models = require('../../src/models') + +const { generateRandomCID } = require('./utils') + const testEthereumConstants = { pubKey: '0xadD36bad12002f1097Cdb7eE24085C28e960FC32', privKeyHex: 'acd6db99f7354043bf7a14a4fbb81b348e028717933eda978afd97b3e80cf1da' } const getCNodeUser = async (cnodeUserUUID) => { - const { dataValues } = await CNodeUser.findOne({ where: { cnodeUserUUID } }) + const { dataValues } = await models.CNodeUser.findOne({ + where: { cnodeUserUUID } + }) return dataValues } -const destroyUsers = async () => ( - CNodeUser.destroy({ +const destroyUsers = async () => + models.CNodeUser.destroy({ where: {}, truncate: true, cascade: true // cascades delete to all rows with foreign key on cnodeUser }) -) -async function createStarterCNodeUser (userId = null, pubKey = testEthereumConstants.pubKey.toLowerCase()) { +async function createStarterCNodeUser( + userId = null, + pubKey = testEthereumConstants.pubKey.toLowerCase() +) { return createStarterCNodeUserWithKey(pubKey, userId) } -async function createStarterCNodeUserWithKey (walletPublicKey, userId = null) { - const cnodeUser = await CNodeUser.create({ walletPublicKey, clock: 0 }) - const sessionToken = await sessionManager.createSession(cnodeUser.cnodeUserUUID) +async function createStarterCNodeUserWithKey(walletPublicKey, userId = null) { + const cnodeUser = await models.CNodeUser.create({ walletPublicKey, clock: 0 }) + const sessionToken = await sessionManager.createSession( + cnodeUser.cnodeUserUUID + ) const resp = { cnodeUserUUID: cnodeUser.cnodeUserUUID, sessionToken: sessionToken, @@ -36,13 +46,67 @@ async function createStarterCNodeUserWithKey (walletPublicKey, userId = null) { return resp } -async function createSession () { +async function createSession() { const dummyKey = uuid() const user = await createStarterCNodeUserWithKey(dummyKey, null) - const tokenObject = await SessionToken.findOne({ + const tokenObject = await models.SessionToken.findOne({ where: { token: user.sessionToken } }) return tokenObject } -module.exports = { createStarterCNodeUser, createStarterCNodeUserWithKey, testEthereumConstants, getCNodeUser, destroyUsers, createSession } +// Seeds ContentBlacklist with the input number of tracks, cids, and users +const seedContentBlacklist = async ({ + numTracks = 10, + numCids = 10, + numUsers = 5 +}) => { + console.log('tracks', numTracks, 'cids', numCids, 'users', numUsers) + + const MAX_ID = 100 + const contentBlacklistEntries = [] + + let i + for (i = 0; i < numTracks; i++) { + contentBlacklistEntries.push({ + value: Utils.getRandomInt(MAX_ID), + type: models.ContentBlacklist.Types.track + }) + } + + for (i = 0; i < numUsers; i++) { + contentBlacklistEntries.push({ + value: Utils.getRandomInt(MAX_ID), + type: models.ContentBlacklist.Types.user + }) + } + + for (i = 0; i < numCids; i++) { + contentBlacklistEntries.push({ + value: generateRandomCID(), + type: models.ContentBlacklist.Types.cid + }) + } + + await models.ContentBlacklist.bulkCreate({ + values + }) +} + +const clearContentBlacklist = async () => { + await models.ContentBlacklist.destory({ + where: {}, + truncate: true + }) +} + +module.exports = { + createStarterCNodeUser, + createStarterCNodeUserWithKey, + testEthereumConstants, + getCNodeUser, + destroyUsers, + createSession, + clearContentBlacklist, + seedContentBlacklist +} diff --git a/creator-node/test/lib/utils.js b/creator-node/test/lib/utils.js index 9e77b41798e..92a7926d74e 100644 --- a/creator-node/test/lib/utils.js +++ b/creator-node/test/lib/utils.js @@ -1,3 +1,5 @@ +const Utils = require('../../src/utils') + const stringifiedDateFields = (obj) => { const newObj = { ...obj } if (newObj.createdAt) newObj.createdAt = newObj.createdAt.toISOString() @@ -15,8 +17,37 @@ const functionThatThrowsWithMessage = (errorMessage) => { } } +/** + * Generates a fake CID (length: 46) with the letter 'a' and suffix of a random number of n digits + * Example: Qmaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa12345 + * + * Reference: + * https://stackoverflow.com/questions/29640432/generate-4-digit-random-number-using-substring/29640472 + * @param {number} numRandomDigits number of random digits suffix + * @param {number} maxRandomNumber the max random number + * @returns + */ +const generateRandomCID = (numRandomDigits = 5, maxRandomNumber = 10000) => { + // If n is out of bounds, default to 5 + if (numRandomDigits < 0 || numRandomDigits > 46) numRandomDigits = 5 + + const randomNDigitNumber = ( + Array(numRandomDigits).join('0') + Utils.getRandomInt(maxRandomNumber) + ).slice(-numRandomDigits) + + // Return Qm..aaa... of length 46. Array(..) part needs + 1 to generate the remaining amount + return ( + 'Qm' + + Array( + 46 /* total length of cid */ - 2 /* 'Qm' prefix */ - numRandomDigits + 1 + ).join('a') + + randomNDigitNumber + ) +} + module.exports = { wait, stringifiedDateFields, - functionThatThrowsWithMessage + functionThatThrowsWithMessage, + generateRandomCID } From c954156a7e597b4ab9912e3d8091c7b99177df78 Mon Sep 17 00:00:00 2001 From: vicky Date: Wed, 18 May 2022 18:17:30 +0000 Subject: [PATCH 2/7] route for tracks no pag --- creator-node/src/blacklistManager.js | 6 +- .../contentBlacklistComponentService.js | 43 ++++++++------ .../contentBlacklistController.js | 58 +++++-------------- 3 files changed, 44 insertions(+), 63 deletions(-) diff --git a/creator-node/src/blacklistManager.js b/creator-node/src/blacklistManager.js index 57911bf65c5..306b2e53c88 100644 --- a/creator-node/src/blacklistManager.js +++ b/creator-node/src/blacklistManager.js @@ -662,10 +662,8 @@ class BlacklistManager { } // Retrieves all track ids in redis - static async getAllTrackIds({ offset = 0, limit = 100 }) { - const allTrackIds = await redis.smembers(REDIS_SET_BLACKLIST_TRACKID_KEY) - - return allTrackIds.slice(offset, limit) + static async getAllTrackIds() { + return redis.smembers(REDIS_SET_BLACKLIST_TRACKID_KEY) } static async getAllInvalidTrackIds() { diff --git a/creator-node/src/components/contentBlacklist/contentBlacklistComponentService.js b/creator-node/src/components/contentBlacklist/contentBlacklistComponentService.js index 7f8fd356464..a49d287ad94 100644 --- a/creator-node/src/components/contentBlacklist/contentBlacklistComponentService.js +++ b/creator-node/src/components/contentBlacklist/contentBlacklistComponentService.js @@ -3,24 +3,32 @@ const models = require('../../models') const types = models.ContentBlacklist.Types -const getAllContentBlacklist = async ({ type, limit, offset }) => { - let values - switch (type) { - case types.user: { - values = await BlacklistManager.getAllUserIds() - break - } - case types.track: { - values = await BlacklistManager.getAllTrackIds({ offset, limit }) - break - } - case types.cid: { - values = await BlacklistManager.getAllCIDs() - break - } +const getTracks = async () => { + return BlacklistManager.getAllTrackIds() +} + +const getAllContentBlacklist = async () => { + // Segments stored in the ContentBlacklist may not be associated with a track + const segmentsFromCBL = await models.ContentBlacklist.findAll({ + attributes: ['value'], + where: { + type: types.cid + }, + raw: true + }) + const individuallyBlacklistedSegments = segmentsFromCBL.map( + (entry) => entry.value + ) + const allSegments = await BlacklistManager.getAllCIDs() + const blacklistedContent = { + trackIds: await BlacklistManager.getAllTrackIds(), + userIds: await BlacklistManager.getAllUserIds(), + individualSegments: individuallyBlacklistedSegments, + numberOfSegments: allSegments.length, + allSegments } - return { count: values.length, values } + return blacklistedContent } const addToContentBlacklist = async ({ type, values }) => { @@ -34,5 +42,6 @@ const removeFromContentBlacklist = async ({ type, values }) => { module.exports = { getAllContentBlacklist, addToContentBlacklist, - removeFromContentBlacklist + removeFromContentBlacklist, + getTracks } diff --git a/creator-node/src/components/contentBlacklist/contentBlacklistController.js b/creator-node/src/components/contentBlacklist/contentBlacklistController.js index 27e61ac0305..daa175f1365 100644 --- a/creator-node/src/components/contentBlacklist/contentBlacklistController.js +++ b/creator-node/src/components/contentBlacklist/contentBlacklistController.js @@ -3,7 +3,8 @@ const express = require('express') const { getAllContentBlacklist, addToContentBlacklist, - removeFromContentBlacklist + removeFromContentBlacklist, + getTracks } = require('./contentBlacklistComponentService') const { handleResponse, @@ -24,26 +25,23 @@ const TYPES_SET = new Set([types.cid, types.user, types.track]) // Controllers -const contentBlacklistGetAllController = async (req) => { - const type = parseContentType(req.query.type) - - if (!type) { - return errorResponseBadRequest( - 'Content type not specified or is not proper' +const getTracksController = async (req) => { + let trackIds + try { + trackIds = await getTracks() + } catch (e) { + req.logger.error( + `ContentBlackListController - Could not fetch tracks: ${e.message}` ) - } - const { limit, offset } = parsePagination({ - limit: req.query.limit, - offset: req.query.offset - }) + return errorResponseServerError(`Could not fetch tracks`) + } - const blacklistedContent = await getAllContentBlacklist({ - type, - limit, - offset - }) + return successResponse(trackIds) +} +const contentBlacklistGetAllController = async (req) => { + const blacklistedContent = await getAllContentBlacklist() return successResponse(blacklistedContent) } @@ -281,32 +279,8 @@ const filterNonexistantIds = async (libs, type, ids) => { return ids } -const parseContentType = async (inputType) => { - if (!inputType) return null - - const type = inputType.toUpperCase() - if (!TYPES_SET.has(type)) return null - - return type -} - -const parsePagination = async ({ limit: inputLimit, offset: inputOffset }) => { - let limit = parseInt(inputLimit) - let offset = parseInt(inputOffset) - - if (limit < 0) { - limit = 100 - } - - if (offset < 0) { - offset = 0 - } - - return { offset, limit } -} - // Routes - +router.get('/blacklist/tracks', handleResponse(getTracksController)) router.get('/blacklist', handleResponse(contentBlacklistGetAllController)) router.post('/blacklist/add', handleResponse(contentBlacklistAddController)) router.post( From f3a065470a2c110f3e0957e316ffe83dff14046d Mon Sep 17 00:00:00 2001 From: vicky Date: Wed, 18 May 2022 23:03:53 +0000 Subject: [PATCH 3/7] update with key --- .../components/contentBlacklist/contentBlacklistController.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/creator-node/src/components/contentBlacklist/contentBlacklistController.js b/creator-node/src/components/contentBlacklist/contentBlacklistController.js index daa175f1365..e8de89440e1 100644 --- a/creator-node/src/components/contentBlacklist/contentBlacklistController.js +++ b/creator-node/src/components/contentBlacklist/contentBlacklistController.js @@ -37,7 +37,7 @@ const getTracksController = async (req) => { return errorResponseServerError(`Could not fetch tracks`) } - return successResponse(trackIds) + return successResponse({ values: trackIds }) } const contentBlacklistGetAllController = async (req) => { From 8131395458c9d46690490bf2af8f0f1baa29f3ec Mon Sep 17 00:00:00 2001 From: vicky Date: Wed, 18 May 2022 23:26:46 +0000 Subject: [PATCH 4/7] tests --- creator-node/test/contentBlacklist.test.js | 43 ++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/creator-node/test/contentBlacklist.test.js b/creator-node/test/contentBlacklist.test.js index e8fe599c629..54218eeb35f 100644 --- a/creator-node/test/contentBlacklist.test.js +++ b/creator-node/test/contentBlacklist.test.js @@ -75,6 +75,49 @@ describe('test ContentBlacklist', function () { await redis.flushall() }) + it('should expose the proper tracks if added', async function () { + const expectedIds = [1, 2, 3, 4, 5, 6, 7] + const addTrackData = generateTimestampAndSignature( + { + type: BlacklistManager.getTypes().track, + values: expectedIds + }, + DELEGATE_PRIVATE_KEY + ) + + await request(app) + .post('/blacklist/add') + .query({ + type: BlacklistManager.getTypes().track, + 'values[]': expectedIds, + signature: addTrackData.signature, + timestamp: addTrackData.timestamp + }) + .expect(200) + + await request(app) + .get('/blacklist/tracks') + .expect(200) + .expect((resp) => { + const actualIds = resp.body.data.values + assert.deepStrictEqual(actualIds.length, expectedIds.length) + + actualIds.forEach((id, i) => { + assert.ok(id, expectedIds[i]) + }) + }) + }) + + it('should expose empty list if no tracks are added', async function () { + await request(app) + .get('/blacklist/tracks') + .expect(200) + .expect((resp) => { + const actualIds = resp.body.data.values + assert.deepStrictEqual(actualIds, []) + }) + }) + it('should return the proper userIds, trackIds, and segments', async () => { ids = [43021] const addUserData = generateTimestampAndSignature( From c3c5a8151a789ec44b814b4c15a16a02e2b76451 Mon Sep 17 00:00:00 2001 From: vicky Date: Wed, 18 May 2022 23:28:19 +0000 Subject: [PATCH 5/7] revert seeds --- creator-node/test/lib/dataSeeds.js | 90 +++++------------------------- 1 file changed, 13 insertions(+), 77 deletions(-) diff --git a/creator-node/test/lib/dataSeeds.js b/creator-node/test/lib/dataSeeds.js index 33ba310be4f..24ff3239761 100644 --- a/creator-node/test/lib/dataSeeds.js +++ b/creator-node/test/lib/dataSeeds.js @@ -1,10 +1,6 @@ -const uuid = require('uuid/v4') - -const Utils = require('../../src/utils') const sessionManager = require('../../src/sessionManager') -const models = require('../../src/models') - -const { generateRandomCID } = require('./utils') +const { CNodeUser, SessionToken } = require('../../src/models') +const uuid = require('uuid/v4') const testEthereumConstants = { pubKey: '0xadD36bad12002f1097Cdb7eE24085C28e960FC32', @@ -12,31 +8,25 @@ const testEthereumConstants = { } const getCNodeUser = async (cnodeUserUUID) => { - const { dataValues } = await models.CNodeUser.findOne({ - where: { cnodeUserUUID } - }) + const { dataValues } = await CNodeUser.findOne({ where: { cnodeUserUUID } }) return dataValues } -const destroyUsers = async () => - models.CNodeUser.destroy({ +const destroyUsers = async () => ( + CNodeUser.destroy({ where: {}, truncate: true, cascade: true // cascades delete to all rows with foreign key on cnodeUser }) +) -async function createStarterCNodeUser( - userId = null, - pubKey = testEthereumConstants.pubKey.toLowerCase() -) { +async function createStarterCNodeUser (userId = null, pubKey = testEthereumConstants.pubKey.toLowerCase()) { return createStarterCNodeUserWithKey(pubKey, userId) } -async function createStarterCNodeUserWithKey(walletPublicKey, userId = null) { - const cnodeUser = await models.CNodeUser.create({ walletPublicKey, clock: 0 }) - const sessionToken = await sessionManager.createSession( - cnodeUser.cnodeUserUUID - ) +async function createStarterCNodeUserWithKey (walletPublicKey, userId = null) { + const cnodeUser = await CNodeUser.create({ walletPublicKey, clock: 0 }) + const sessionToken = await sessionManager.createSession(cnodeUser.cnodeUserUUID) const resp = { cnodeUserUUID: cnodeUser.cnodeUserUUID, sessionToken: sessionToken, @@ -46,67 +36,13 @@ async function createStarterCNodeUserWithKey(walletPublicKey, userId = null) { return resp } -async function createSession() { +async function createSession () { const dummyKey = uuid() const user = await createStarterCNodeUserWithKey(dummyKey, null) - const tokenObject = await models.SessionToken.findOne({ + const tokenObject = await SessionToken.findOne({ where: { token: user.sessionToken } }) return tokenObject } -// Seeds ContentBlacklist with the input number of tracks, cids, and users -const seedContentBlacklist = async ({ - numTracks = 10, - numCids = 10, - numUsers = 5 -}) => { - console.log('tracks', numTracks, 'cids', numCids, 'users', numUsers) - - const MAX_ID = 100 - const contentBlacklistEntries = [] - - let i - for (i = 0; i < numTracks; i++) { - contentBlacklistEntries.push({ - value: Utils.getRandomInt(MAX_ID), - type: models.ContentBlacklist.Types.track - }) - } - - for (i = 0; i < numUsers; i++) { - contentBlacklistEntries.push({ - value: Utils.getRandomInt(MAX_ID), - type: models.ContentBlacklist.Types.user - }) - } - - for (i = 0; i < numCids; i++) { - contentBlacklistEntries.push({ - value: generateRandomCID(), - type: models.ContentBlacklist.Types.cid - }) - } - - await models.ContentBlacklist.bulkCreate({ - values - }) -} - -const clearContentBlacklist = async () => { - await models.ContentBlacklist.destory({ - where: {}, - truncate: true - }) -} - -module.exports = { - createStarterCNodeUser, - createStarterCNodeUserWithKey, - testEthereumConstants, - getCNodeUser, - destroyUsers, - createSession, - clearContentBlacklist, - seedContentBlacklist -} +module.exports = { createStarterCNodeUser, createStarterCNodeUserWithKey, testEthereumConstants, getCNodeUser, destroyUsers, createSession } From d8e1eba62aa024e87c6dc65d4a177bb7f3390979 Mon Sep 17 00:00:00 2001 From: vicky Date: Thu, 19 May 2022 16:10:11 +0000 Subject: [PATCH 6/7] refactoring tests + fn name --- .../contentBlacklist/contentBlacklistComponentService.js | 4 ++-- .../components/contentBlacklist/contentBlacklistController.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/creator-node/src/components/contentBlacklist/contentBlacklistComponentService.js b/creator-node/src/components/contentBlacklist/contentBlacklistComponentService.js index a49d287ad94..1df0110778d 100644 --- a/creator-node/src/components/contentBlacklist/contentBlacklistComponentService.js +++ b/creator-node/src/components/contentBlacklist/contentBlacklistComponentService.js @@ -3,7 +3,7 @@ const models = require('../../models') const types = models.ContentBlacklist.Types -const getTracks = async () => { +const getAllTrackIds = async () => { return BlacklistManager.getAllTrackIds() } @@ -43,5 +43,5 @@ module.exports = { getAllContentBlacklist, addToContentBlacklist, removeFromContentBlacklist, - getTracks + getAllTrackIds } diff --git a/creator-node/src/components/contentBlacklist/contentBlacklistController.js b/creator-node/src/components/contentBlacklist/contentBlacklistController.js index e8de89440e1..3d83fc3bb31 100644 --- a/creator-node/src/components/contentBlacklist/contentBlacklistController.js +++ b/creator-node/src/components/contentBlacklist/contentBlacklistController.js @@ -4,7 +4,7 @@ const { getAllContentBlacklist, addToContentBlacklist, removeFromContentBlacklist, - getTracks + getAllTrackIds } = require('./contentBlacklistComponentService') const { handleResponse, @@ -28,7 +28,7 @@ const TYPES_SET = new Set([types.cid, types.user, types.track]) const getTracksController = async (req) => { let trackIds try { - trackIds = await getTracks() + trackIds = await getAllTrackIds() } catch (e) { req.logger.error( `ContentBlackListController - Could not fetch tracks: ${e.message}` From a86a55ac411199cff94f210a3e4925c0cf430d96 Mon Sep 17 00:00:00 2001 From: vicky Date: Thu, 19 May 2022 16:15:36 +0000 Subject: [PATCH 7/7] forgot import --- creator-node/test/contentBlacklist.test.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/creator-node/test/contentBlacklist.test.js b/creator-node/test/contentBlacklist.test.js index 54218eeb35f..afa7c720cf5 100644 --- a/creator-node/test/contentBlacklist.test.js +++ b/creator-node/test/contentBlacklist.test.js @@ -2,6 +2,7 @@ const assert = require('assert') const request = require('supertest') const sinon = require('sinon') const path = require('path') +const _ = require('lodash') const Utils = require('../src/utils') const BlacklistManager = require('../src/blacklistManager') @@ -102,9 +103,7 @@ describe('test ContentBlacklist', function () { const actualIds = resp.body.data.values assert.deepStrictEqual(actualIds.length, expectedIds.length) - actualIds.forEach((id, i) => { - assert.ok(id, expectedIds[i]) - }) + _.isEqual(actualIds.sort(), expectedIds.sort()) }) })