diff --git a/staff/daniel-hernandez/project/api/handlers/index.js b/staff/daniel-hernandez/project/api/handlers/index.js index 108bd0a13..4476af89d 100644 --- a/staff/daniel-hernandez/project/api/handlers/index.js +++ b/staff/daniel-hernandez/project/api/handlers/index.js @@ -2,7 +2,7 @@ import loginHandler from './auth/login.js'; import registerHandler from './auth/register.js'; import checkEmailHandler from './auth/checkEmail.js'; import logHandler from './log.js'; -import streamTrackHandler from './streamTrack.js'; +import streamHandler from './stream.js'; import queryHandler from './query.js'; export default { @@ -10,6 +10,6 @@ export default { registerHandler, checkEmailHandler, logHandler, - streamTrackHandler, + streamHandler, queryHandler }; diff --git a/staff/daniel-hernandez/project/api/handlers/streamTrack.js b/staff/daniel-hernandez/project/api/handlers/stream.js similarity index 80% rename from staff/daniel-hernandez/project/api/handlers/streamTrack.js rename to staff/daniel-hernandez/project/api/handlers/stream.js index 2cc6dcf95..803cb41b6 100644 --- a/staff/daniel-hernandez/project/api/handlers/streamTrack.js +++ b/staff/daniel-hernandez/project/api/handlers/stream.js @@ -1,12 +1,12 @@ import services from '../services/index.js'; -const streamTrack = async (req, res, next) => { +const stream = async (req, res, next) => { const { id: userId } = req.user; const { trackId } = req.params; const { range } = req.body; try { - const { stream, contentType, contentLength, contentRange } = await services.streamTrack(userId, trackId, range); + const { stream, contentType, contentLength, contentRange } = await services.stream(userId, trackId, range); res.setHeader('Content-Type', contentType); res.setHeader('Content-Length', contentLength); @@ -20,4 +20,4 @@ const streamTrack = async (req, res, next) => { } }; -export default streamTrack; +export default stream; diff --git a/staff/daniel-hernandez/project/api/routes/tracks.js b/staff/daniel-hernandez/project/api/routes/tracks.js index 68b47e928..035627877 100644 --- a/staff/daniel-hernandez/project/api/routes/tracks.js +++ b/staff/daniel-hernandez/project/api/routes/tracks.js @@ -2,6 +2,6 @@ import express from 'express'; import handlers from '../handlers/index.js'; const router = express.Router(); -router.route('/:trackId').post(handlers.streamTrackHandler); +router.route('/:trackId').post(handlers.streamHandler); export default router; diff --git a/staff/daniel-hernandez/project/api/services/index.js b/staff/daniel-hernandez/project/api/services/index.js index 2423a3322..7c0d6bf66 100644 --- a/staff/daniel-hernandez/project/api/services/index.js +++ b/staff/daniel-hernandez/project/api/services/index.js @@ -2,7 +2,7 @@ import login from './auth/login.js'; import register from './auth/register.js'; import checkEmail from './auth/checkEmail.js'; import log from './log.js'; -import streamTrack from './streamTrack.js'; +import stream from './stream.js'; import query from './query.js'; export default { @@ -10,6 +10,6 @@ export default { register, checkEmail, log, - streamTrack, + stream, query }; diff --git a/staff/daniel-hernandez/project/api/services/streamTrack.js b/staff/daniel-hernandez/project/api/services/stream.js similarity index 97% rename from staff/daniel-hernandez/project/api/services/streamTrack.js rename to staff/daniel-hernandez/project/api/services/stream.js index ffe9a1c64..c3a9942c3 100644 --- a/staff/daniel-hernandez/project/api/services/streamTrack.js +++ b/staff/daniel-hernandez/project/api/services/stream.js @@ -9,7 +9,7 @@ import log from './log.js'; const { TRACK_DIR } = process.env; -const streamTrack = (userId, trackId, range) => { +const stream = (userId, trackId, range) => { validate.inputs(userId, trackId); validate.objectId(userId); validate.objectId(trackId); @@ -94,4 +94,4 @@ const streamTrack = (userId, trackId, range) => { })(); }; -export default streamTrack; +export default stream; diff --git a/staff/daniel-hernandez/project/api/tests/automated/services/streamTrack.spec.js b/staff/daniel-hernandez/project/api/tests/automated/services/stream.spec.js similarity index 76% rename from staff/daniel-hernandez/project/api/tests/automated/services/streamTrack.spec.js rename to staff/daniel-hernandez/project/api/tests/automated/services/stream.spec.js index b0fd7c3fe..067d67b39 100644 --- a/staff/daniel-hernandez/project/api/tests/automated/services/streamTrack.spec.js +++ b/staff/daniel-hernandez/project/api/tests/automated/services/stream.spec.js @@ -7,7 +7,7 @@ import { mkdir, rm, writeFile } from 'fs/promises'; import path from 'path'; import { Track, User, Log } from '../../../data/index.js'; import { CredentialError, InvalidArgumentError, NotFoundError, SystemError } from 'com/errors.js'; -import streamTrack from '../../../services/streamTrack.js'; +import stream from '../../../services/stream.js'; import constants from 'com/constants.js'; // WARN: Change TRACK_DIR to test with the test track directory to not work with "production" data @@ -15,7 +15,7 @@ const { MONGO_TEST_URI, TRACK_DIR } = process.env; chai.use(chaiAsPromised); const expect = chai.expect; -describe('streamTrack', () => { +describe('stream', () => { const testTrackId = new mongoose.Types.ObjectId().toString(); const testTrackPath = path.join(TRACK_DIR, testTrackId); @@ -39,7 +39,7 @@ describe('streamTrack', () => { await writeFile(testTrackPath, 'a'.repeat(1000), 'utf8'); - const streamInfo = await expect(streamTrack(user.id, track.id)).to.be.fulfilled.and.to.eventually.be.a('object'); + const streamInfo = await expect(stream(user.id, track.id)).to.be.fulfilled.and.to.eventually.be.a('object'); expect(streamInfo).to.have.property('contentRange').that.equals('bytes 0-999/1000'); expect(streamInfo).to.have.property('contentLength').that.equals(1000); expect(streamInfo).to.have.property('contentType').that.equals('audio/mpeg'); @@ -58,7 +58,7 @@ describe('streamTrack', () => { await writeFile(testTrackPath, 'a'.repeat(1500), 'utf8'); - const streamInfo = await expect(streamTrack(user.id, track.id, 'bytes=0-500')).to.be.fulfilled.and.to.eventually.be.a('object'); + const streamInfo = await expect(stream(user.id, track.id, 'bytes=0-500')).to.be.fulfilled.and.to.eventually.be.a('object'); expect(streamInfo).to.have.property('contentRange').that.equals('bytes 0-500/1500'); expect(streamInfo).to.have.property('contentLength').that.equals(501); expect(streamInfo).to.have.property('contentType').that.equals('audio/mpeg'); @@ -77,7 +77,7 @@ describe('streamTrack', () => { await writeFile(testTrackPath, 'a'.repeat(750), 'utf8'); - const streamInfo = await expect(streamTrack(user.id, track.id, 'bytes=200-')).to.be.fulfilled.and.to.eventually.be.a('object'); + const streamInfo = await expect(stream(user.id, track.id, 'bytes=200-')).to.be.fulfilled.and.to.eventually.be.a('object'); expect(streamInfo).to.have.property('contentRange').that.equals('bytes 200-749/750'); expect(streamInfo).to.have.property('contentLength').that.equals(550); expect(streamInfo).to.have.property('contentType').that.equals('audio/mpeg'); @@ -96,7 +96,7 @@ describe('streamTrack', () => { await writeFile(testTrackPath, 'a'.repeat(921), 'utf8'); - const streamInfo = await expect(streamTrack(user.id, track.id, 'bytes=-211')).to.be.fulfilled.and.to.eventually.be.a('object'); + const streamInfo = await expect(stream(user.id, track.id, 'bytes=-211')).to.be.fulfilled.and.to.eventually.be.a('object'); expect(streamInfo).to.have.property('contentRange').that.equals('bytes 710-920/921'); expect(streamInfo).to.have.property('contentLength').that.equals(211); expect(streamInfo).to.have.property('contentType').that.equals('audio/mpeg'); @@ -113,14 +113,14 @@ describe('streamTrack', () => { const user = await User.create({ username: 'eva02', email: 'asuka@soryu.com', passwordHash: hash }); const track = await Track.create({ _id: testTrackId, addedBy: user.id, name: 'test_track_2024', duration: 60 }); - await expect(streamTrack('66b2cebc5621e4111875102c', track.id)).to.be.rejectedWith(CredentialError, "User doesn't exist"); + await expect(stream('66b2cebc5621e4111875102c', track.id)).to.be.rejectedWith(CredentialError, "User doesn't exist"); }); it('fails when the track is not found', async () => { const hash = await bcrypt.hash('Neon-Genesis02', 8); const user = await User.create({ username: 'eva02', email: 'asuka@soryu.com', passwordHash: hash }); - await expect(streamTrack(user.id, '66b2cebc5621e4111875102c')).to.be.rejectedWith(NotFoundError, 'Track not found'); + await expect(stream(user.id, '66b2cebc5621e4111875102c')).to.be.rejectedWith(NotFoundError, 'Track not found'); }); it('fails when the track file is not found', async () => { @@ -128,7 +128,7 @@ describe('streamTrack', () => { const user = await User.create({ username: 'eva02', email: 'asuka@soryu.com', passwordHash: hash }); const track = await Track.create({ _id: testTrackId, addedBy: user.id, name: 'test_track_2024', duration: 60 }); - await expect(streamTrack(user.id, track.id)).to.be.rejectedWith(NotFoundError, 'Track file not found'); + await expect(stream(user.id, track.id)).to.be.rejectedWith(NotFoundError, 'Track file not found'); }); it('fails when the range exceeds the file size', async () => { @@ -138,7 +138,7 @@ describe('streamTrack', () => { await writeFile(testTrackPath, 'a'.repeat(900), 'utf8'); - await expect(streamTrack(user.id, track.id, 'bytes=0-1000')).to.be.rejectedWith(InvalidArgumentError, 'Range exceeds file size'); + await expect(stream(user.id, track.id, 'bytes=0-1000')).to.be.rejectedWith(InvalidArgumentError, 'Range exceeds file size'); }); it('fails with SystemError on database error during user search', async () => { @@ -150,7 +150,7 @@ describe('streamTrack', () => { throw new Error('Database connection error'); }; - await expect(streamTrack(user.id, '66b2cebc5621e4111875102c')).to.be.rejectedWith(SystemError, 'Streaming the track failed: Database connection error'); + await expect(stream(user.id, '66b2cebc5621e4111875102c')).to.be.rejectedWith(SystemError, 'Streaming the track failed: Database connection error'); User.findById = findById; }); @@ -165,49 +165,49 @@ describe('streamTrack', () => { throw new Error('Database connection error'); }; - await expect(streamTrack(user.id, track.id)).to.be.rejectedWith(SystemError, 'Streaming the track failed: Database connection error'); + await expect(stream(user.id, track.id)).to.be.rejectedWith(SystemError, 'Streaming the track failed: Database connection error'); Track.findById = findById; }); it('fails when all fields are empty', () => { - expect(() => streamTrack('', '')).to.throw(InvalidArgumentError, 'All inputs are required'); + expect(() => stream('', '')).to.throw(InvalidArgumentError, 'All inputs are required'); }); it('fails when not provided with a user id', () => { - expect(() => streamTrack('', '66b2cebc5621e4111875102c')).to.throw(InvalidArgumentError, ''); + expect(() => stream('', '66b2cebc5621e4111875102c')).to.throw(InvalidArgumentError, ''); }); it('fails when not provided with a track id', () => { - expect(() => streamTrack('66b2cebc5621e4111875102c', '')).to.throw(InvalidArgumentError, ''); + expect(() => stream('66b2cebc5621e4111875102c', '')).to.throw(InvalidArgumentError, ''); }); it('fails when the user id is invalid', () => { - expect(() => streamTrack('66b2cebc5621e4111875102X', '66b2cebc5621e4111875102c')).to.throw(InvalidArgumentError, 'Invalid ObjectId'); + expect(() => stream('66b2cebc5621e4111875102X', '66b2cebc5621e4111875102c')).to.throw(InvalidArgumentError, 'Invalid ObjectId'); }); it('fails when the track id is invalid', () => { - expect(() => streamTrack('66b2cebc5621e4111875102c', '66b2cebc5621e4111875102X')).to.throw(InvalidArgumentError, 'Invalid ObjectId'); + expect(() => stream('66b2cebc5621e4111875102c', '66b2cebc5621e4111875102X')).to.throw(InvalidArgumentError, 'Invalid ObjectId'); }); it('fails when the range is provided but incorrect (number)', () => { - expect(() => streamTrack('66b2cebc5621e4111875102c', '66b2cebc5621e4111875102c', 200)).to.throw(InvalidArgumentError, 'Invalid range format'); + expect(() => stream('66b2cebc5621e4111875102c', '66b2cebc5621e4111875102c', 200)).to.throw(InvalidArgumentError, 'Invalid range format'); }); it('fails when the range is provided but incorrect (Range start value)', () => { - expect(() => streamTrack('66b2cebc5621e4111875102c', '66b2cebc5621e4111875102c', 'bytes=undefined-200')).to.throw(InvalidArgumentError, 'Invalid range start value'); + expect(() => stream('66b2cebc5621e4111875102c', '66b2cebc5621e4111875102c', 'bytes=undefined-200')).to.throw(InvalidArgumentError, 'Invalid range start value'); }); it('fails when the range is provided but incorrect (Range end value)', () => { - expect(() => streamTrack('66b2cebc5621e4111875102c', '66b2cebc5621e4111875102c', 'bytes=0-undefined')).to.throw(InvalidArgumentError, 'Invalid range end value'); + expect(() => stream('66b2cebc5621e4111875102c', '66b2cebc5621e4111875102c', 'bytes=0-undefined')).to.throw(InvalidArgumentError, 'Invalid range end value'); }); it('fails when the range is provided but incorrect !(Range >= 0)', () => { - expect(() => streamTrack('66b2cebc5621e4111875102c', '66b2cebc5621e4111875102c', 'bytes=-1-100')).to.throw(InvalidArgumentError, 'Invalid range format'); + expect(() => stream('66b2cebc5621e4111875102c', '66b2cebc5621e4111875102c', 'bytes=-1-100')).to.throw(InvalidArgumentError, 'Invalid range format'); }); it('fails when the range is provided but incorrect (Range start value > Range end value)', () => { - expect(() => streamTrack('66b2cebc5621e4111875102c', '66b2cebc5621e4111875102c', 'bytes=200-100')).to.throw(InvalidArgumentError, 'Invalid range. End value must be >= start value'); + expect(() => stream('66b2cebc5621e4111875102c', '66b2cebc5621e4111875102c', 'bytes=200-100')).to.throw(InvalidArgumentError, 'Invalid range. End value must be >= start value'); }); after(async () => {