Skip to content

Commit

Permalink
renamed streamTrack to stream and updated its references b00tc4mp#175
Browse files Browse the repository at this point in the history
  • Loading branch information
breadislife committed Sep 12, 2024
1 parent 27e9db2 commit 7a75b0b
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 32 deletions.
4 changes: 2 additions & 2 deletions staff/daniel-hernandez/project/api/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ 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 {
loginHandler,
registerHandler,
checkEmailHandler,
logHandler,
streamTrackHandler,
streamHandler,
queryHandler
};
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -20,4 +20,4 @@ const streamTrack = async (req, res, next) => {
}
};

export default streamTrack;
export default stream;
2 changes: 1 addition & 1 deletion staff/daniel-hernandez/project/api/routes/tracks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
4 changes: 2 additions & 2 deletions staff/daniel-hernandez/project/api/services/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ 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 {
login,
register,
checkEmail,
log,
streamTrack,
stream,
query
};
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -94,4 +94,4 @@ const streamTrack = (userId, trackId, range) => {
})();
};

export default streamTrack;
export default stream;
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ 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
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);

Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -113,22 +113,22 @@ describe('streamTrack', () => {
const user = await User.create({ username: 'eva02', email: '[email protected]', 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: '[email protected]', 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 () => {
const hash = await bcrypt.hash('Neon-Genesis02', 8);
const user = await User.create({ username: 'eva02', email: '[email protected]', 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 () => {
Expand All @@ -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 () => {
Expand All @@ -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;
});
Expand All @@ -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 () => {
Expand Down

0 comments on commit 7a75b0b

Please sign in to comment.