mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-12-04 22:16:28 +01:00
516 lines
17 KiB
JavaScript
516 lines
17 KiB
JavaScript
const { expect } = require('chai')
|
|
const { Sequelize } = require('sequelize')
|
|
const sinon = require('sinon')
|
|
|
|
const Database = require('../../../server/Database')
|
|
const PodcastController = require('../../../server/controllers/PodcastController')
|
|
const Logger = require('../../../server/Logger')
|
|
const zipHelpers = require('../../../server/utils/zipHelpers')
|
|
|
|
const { buildEpisodeUpdatePayload } = PodcastController
|
|
|
|
describe('PodcastController.buildEpisodeUpdatePayload', () => {
|
|
beforeEach(() => {
|
|
sinon.stub(Logger, 'debug')
|
|
})
|
|
|
|
afterEach(() => {
|
|
sinon.restore()
|
|
})
|
|
|
|
it('returns an empty object for null, undefined, or non-object input', () => {
|
|
expect(buildEpisodeUpdatePayload(null)).to.deep.equal({})
|
|
expect(buildEpisodeUpdatePayload(undefined)).to.deep.equal({})
|
|
expect(buildEpisodeUpdatePayload('invalid')).to.deep.equal({})
|
|
})
|
|
|
|
it('copies supported string fields and ignores unknown or non-string fields', () => {
|
|
const result = buildEpisodeUpdatePayload({
|
|
title: 'Episode 1',
|
|
pubDate: 'Mon, 01 Jan 2024 00:00:00 GMT',
|
|
episode: '1',
|
|
season: '2',
|
|
episodeType: 'full',
|
|
// ignored
|
|
id: 'override-id',
|
|
podcastId: 'override-podcast',
|
|
audioFile: { path: '/etc/passwd' },
|
|
seasonNumber: 2,
|
|
title2: 'ignored',
|
|
episode2: 123
|
|
})
|
|
|
|
expect(result).to.deep.equal({
|
|
title: 'Episode 1',
|
|
pubDate: 'Mon, 01 Jan 2024 00:00:00 GMT',
|
|
episode: '1',
|
|
season: '2',
|
|
episodeType: 'full'
|
|
})
|
|
})
|
|
|
|
it('ignores non-string values for supported string fields', () => {
|
|
expect(buildEpisodeUpdatePayload({ title: 123, season: null, description: {} })).to.deep.equal({})
|
|
})
|
|
|
|
it('sanitizes HTML in description and subtitle while keeping allowed tags and links', () => {
|
|
const result = buildEpisodeUpdatePayload({
|
|
description: '<p>Hello</p><script>alert(1)</script><img src="x" onerror="alert(1)"><a href="https://example.com">ok</a><a href="javascript:alert(1)">bad</a>',
|
|
subtitle: '<b>Sub</b><script>xss()</script>'
|
|
})
|
|
|
|
expect(result.description).to.equal('<p>Hello</p><a href="https://example.com">ok</a><a>bad</a>')
|
|
expect(result.subtitle).to.equal('<b>Sub</b>')
|
|
})
|
|
|
|
it('clears enclosure columns when enclosure is null', () => {
|
|
expect(buildEpisodeUpdatePayload({ enclosure: null })).to.deep.equal({
|
|
enclosureURL: null,
|
|
enclosureSize: null,
|
|
enclosureType: null
|
|
})
|
|
})
|
|
|
|
it('maps valid enclosure fields and normalizes enclosureSize', () => {
|
|
expect(
|
|
buildEpisodeUpdatePayload({
|
|
enclosure: { url: 'https://example.com/ep.mp3', type: 'audio/mpeg', length: '123456' }
|
|
})
|
|
).to.deep.equal({
|
|
enclosureURL: 'https://example.com/ep.mp3',
|
|
enclosureType: 'audio/mpeg',
|
|
enclosureSize: 123456
|
|
})
|
|
|
|
expect(
|
|
buildEpisodeUpdatePayload({
|
|
enclosure: { url: 'https://example.com/ep.mp3', length: 987.9 }
|
|
})
|
|
).to.deep.equal({
|
|
enclosureURL: 'https://example.com/ep.mp3',
|
|
enclosureType: null,
|
|
enclosureSize: 987
|
|
})
|
|
|
|
expect(
|
|
buildEpisodeUpdatePayload({
|
|
enclosure: { url: 'https://example.com/ep.mp3', length: -5 }
|
|
})
|
|
).to.deep.equal({
|
|
enclosureURL: 'https://example.com/ep.mp3',
|
|
enclosureType: null,
|
|
enclosureSize: null
|
|
})
|
|
|
|
expect(
|
|
buildEpisodeUpdatePayload({
|
|
enclosure: { url: 'https://example.com/ep.mp3', length: { bad: true } }
|
|
})
|
|
).to.deep.equal({
|
|
enclosureURL: 'https://example.com/ep.mp3',
|
|
enclosureType: null,
|
|
enclosureSize: null
|
|
})
|
|
|
|
expect(buildEpisodeUpdatePayload({ enclosure: { type: 'audio/mpeg' } })).to.deep.equal({})
|
|
})
|
|
|
|
it('accepts chapters starting at 0 and rejects invalid chapter arrays', () => {
|
|
const validChapters = [
|
|
{ id: 0, start: 0, end: 60, title: 'Intro' },
|
|
{ id: 1, start: 60, end: 180, title: 'Main' }
|
|
]
|
|
expect(buildEpisodeUpdatePayload({ chapters: validChapters })).to.deep.equal({ chapters: validChapters })
|
|
|
|
expect(buildEpisodeUpdatePayload({ chapters: [null] })).to.deep.equal({})
|
|
expect(buildEpisodeUpdatePayload({ chapters: [{ start: 0, title: '' }] })).to.deep.equal({})
|
|
expect(buildEpisodeUpdatePayload({ chapters: [{ start: '0', title: 'Intro' }] })).to.deep.equal({})
|
|
expect(buildEpisodeUpdatePayload({ chapters: [{ start: Number.NaN, title: 'Intro' }] })).to.deep.equal({})
|
|
})
|
|
|
|
it('accepts finite publishedAt timestamps and rejects NaN, Infinity, or non-numbers', () => {
|
|
expect(buildEpisodeUpdatePayload({ publishedAt: 1700000000000 })).to.deep.equal({ publishedAt: 1700000000000 })
|
|
expect(buildEpisodeUpdatePayload({ publishedAt: 0 })).to.deep.equal({ publishedAt: 0 })
|
|
expect(buildEpisodeUpdatePayload({ publishedAt: Number.NaN })).to.deep.equal({})
|
|
expect(buildEpisodeUpdatePayload({ publishedAt: Number.POSITIVE_INFINITY })).to.deep.equal({})
|
|
expect(buildEpisodeUpdatePayload({ publishedAt: '1700000000000' })).to.deep.equal({})
|
|
})
|
|
})
|
|
|
|
describe('PodcastController batch episode endpoints', () => {
|
|
let library
|
|
let libraryFolder
|
|
let podcastItem1
|
|
let podcastItem2
|
|
let episode1
|
|
let episode2
|
|
let episode3
|
|
let user
|
|
|
|
function createFakeRes() {
|
|
return {
|
|
status: sinon.stub().returnsThis(),
|
|
send: sinon.spy(),
|
|
sendStatus: sinon.spy(),
|
|
json: sinon.spy()
|
|
}
|
|
}
|
|
|
|
function createAudioFile(filename, filePath, ino = '1') {
|
|
return {
|
|
ino,
|
|
duration: 120,
|
|
metadata: {
|
|
filename,
|
|
path: filePath,
|
|
size: 1024
|
|
}
|
|
}
|
|
}
|
|
|
|
beforeEach(async () => {
|
|
global.ServerSettings = {}
|
|
Database.sequelize = new Sequelize({ dialect: 'sqlite', storage: ':memory:', logging: false })
|
|
Database.sequelize.uppercaseFirst = (str) => (str ? `${str[0].toUpperCase()}${str.substr(1)}` : '')
|
|
await Database.buildModels()
|
|
|
|
sinon.stub(Logger, 'info')
|
|
sinon.stub(Logger, 'debug')
|
|
sinon.stub(Logger, 'warn')
|
|
sinon.stub(Logger, 'error')
|
|
|
|
library = await Database.libraryModel.create({ name: 'Podcasts', mediaType: 'podcast' })
|
|
libraryFolder = await Database.libraryFolderModel.create({ path: '/podcasts', libraryId: library.id })
|
|
|
|
const podcast1 = await Database.podcastModel.create({
|
|
title: 'Podcast One',
|
|
explicit: false,
|
|
tags: ['tech'],
|
|
genres: []
|
|
})
|
|
podcastItem1 = await Database.libraryItemModel.create({
|
|
path: '/podcasts/podcast-one',
|
|
libraryFiles: [{ ino: 'huge-blob-sentinel' }],
|
|
mediaId: podcast1.id,
|
|
mediaType: 'podcast',
|
|
libraryId: library.id,
|
|
libraryFolderId: libraryFolder.id
|
|
})
|
|
|
|
const podcast2 = await Database.podcastModel.create({
|
|
title: '',
|
|
explicit: true,
|
|
tags: ['restricted'],
|
|
genres: []
|
|
})
|
|
podcastItem2 = await Database.libraryItemModel.create({
|
|
path: '/podcasts/podcast-two',
|
|
libraryFiles: [],
|
|
mediaId: podcast2.id,
|
|
mediaType: 'podcast',
|
|
libraryId: library.id,
|
|
libraryFolderId: libraryFolder.id
|
|
})
|
|
|
|
episode1 = await Database.podcastEpisodeModel.create({
|
|
title: 'Ep 1',
|
|
podcastId: podcast1.id,
|
|
audioFile: createAudioFile('ep1.mp3', '/podcasts/podcast-one/ep1.mp3', '101'),
|
|
chapters: [],
|
|
extraData: {}
|
|
})
|
|
episode2 = await Database.podcastEpisodeModel.create({
|
|
title: 'Ep 2',
|
|
podcastId: podcast1.id,
|
|
audioFile: createAudioFile('ep2.mp3', '/podcasts/podcast-one/ep2.mp3', '102'),
|
|
chapters: [],
|
|
extraData: {}
|
|
})
|
|
episode3 = await Database.podcastEpisodeModel.create({
|
|
title: 'Ep 3',
|
|
podcastId: podcast2.id,
|
|
audioFile: createAudioFile('ep1.mp3', '/podcasts/podcast-two/ep1.mp3', '103'),
|
|
chapters: [],
|
|
extraData: {}
|
|
})
|
|
|
|
user = await Database.userModel.create({
|
|
username: 'tester',
|
|
type: 'user',
|
|
isActive: true,
|
|
permissions: {
|
|
download: true,
|
|
update: true,
|
|
delete: false,
|
|
upload: false,
|
|
accessAllLibraries: true,
|
|
accessAllTags: true,
|
|
accessExplicitContent: true
|
|
}
|
|
})
|
|
})
|
|
|
|
afterEach(async () => {
|
|
sinon.restore()
|
|
await Database.sequelize.sync({ force: true })
|
|
})
|
|
|
|
describe('batchGetEpisodes', () => {
|
|
it('returns 400 when episodes payload is missing, empty, contains any malformed entry, or has duplicate pairs', async () => {
|
|
const findSpy = sinon.spy(Database.libraryItemModel, 'findAll')
|
|
|
|
for (const body of [
|
|
{},
|
|
{ episodes: [] },
|
|
{ episodes: [{ libraryItemId: podcastItem1.id }, { episodeId: episode1.id }] },
|
|
{ episodes: [{ libraryItemId: 123, episodeId: episode1.id }] },
|
|
{ episodes: [{ libraryItemId: podcastItem1.id, episodeId: episode1.id }, { libraryItemId: podcastItem1.id }] },
|
|
{
|
|
episodes: [
|
|
{ libraryItemId: podcastItem1.id, episodeId: episode1.id },
|
|
{ libraryItemId: podcastItem1.id, episodeId: episode1.id }
|
|
]
|
|
}
|
|
]) {
|
|
const res = createFakeRes()
|
|
await PodcastController.batchGetEpisodes({ body, user }, res)
|
|
expect(res.status.calledOnceWith(400)).to.be.true
|
|
expect(res.send.calledOnceWith('Invalid payload')).to.be.true
|
|
}
|
|
|
|
expect(findSpy.called).to.be.false
|
|
})
|
|
|
|
it('preserves request order, omits missing/non-podcast entries, and returns expanded episodes', async () => {
|
|
const book = await Database.bookModel.create({ title: 'A Book', audioFiles: [], tags: [], narrators: [], genres: [], chapters: [] })
|
|
const bookItem = await Database.libraryItemModel.create({
|
|
libraryFiles: [],
|
|
mediaId: book.id,
|
|
mediaType: 'book',
|
|
libraryId: library.id,
|
|
libraryFolderId: libraryFolder.id
|
|
})
|
|
|
|
const res = createFakeRes()
|
|
await PodcastController.batchGetEpisodes(
|
|
{
|
|
user,
|
|
body: {
|
|
episodes: [
|
|
{ libraryItemId: podcastItem2.id, episodeId: episode3.id },
|
|
{ libraryItemId: podcastItem1.id, episodeId: episode2.id },
|
|
{ libraryItemId: podcastItem1.id, episodeId: 'missing-episode' },
|
|
{ libraryItemId: bookItem.id, episodeId: episode1.id },
|
|
{ libraryItemId: podcastItem1.id, episodeId: episode1.id }
|
|
]
|
|
}
|
|
},
|
|
res
|
|
)
|
|
|
|
expect(res.json.calledOnce).to.be.true
|
|
const { episodes } = res.json.firstCall.args[0]
|
|
expect(episodes.map((e) => `${e.libraryItemId}:${e.id}`)).to.deep.equal([`${podcastItem2.id}:${episode3.id}`, `${podcastItem1.id}:${episode2.id}`, `${podcastItem1.id}:${episode1.id}`])
|
|
expect(episodes[1].audioTrack.contentUrl).to.equal(`/api/items/${podcastItem1.id}/file/102`)
|
|
expect(episodes[1].duration).to.equal(120)
|
|
})
|
|
|
|
it('omits cross-podcast mismatched (libraryItemId, episodeId) pairs', async () => {
|
|
const res = createFakeRes()
|
|
await PodcastController.batchGetEpisodes(
|
|
{
|
|
user,
|
|
body: {
|
|
episodes: [
|
|
{ libraryItemId: podcastItem1.id, episodeId: episode3.id },
|
|
{ libraryItemId: podcastItem2.id, episodeId: episode1.id }
|
|
]
|
|
}
|
|
},
|
|
res
|
|
)
|
|
|
|
expect(res.json.calledOnce).to.be.true
|
|
expect(res.json.firstCall.args[0]).to.deep.equal({ episodes: [] })
|
|
})
|
|
|
|
it('returns 403 when user lacks access to a referenced podcast', async () => {
|
|
user.permissions = {
|
|
...user.permissions,
|
|
accessExplicitContent: false
|
|
}
|
|
|
|
const res = createFakeRes()
|
|
await PodcastController.batchGetEpisodes(
|
|
{
|
|
user,
|
|
body: {
|
|
episodes: [
|
|
{ libraryItemId: podcastItem1.id, episodeId: episode1.id },
|
|
{ libraryItemId: podcastItem2.id, episodeId: episode3.id }
|
|
]
|
|
}
|
|
},
|
|
res
|
|
)
|
|
|
|
expect(res.sendStatus.calledOnceWith(403)).to.be.true
|
|
expect(res.json.called).to.be.false
|
|
})
|
|
|
|
it('excludes orphaned podcast library items with missing media without crashing access checks', async () => {
|
|
const orphanedItem = await Database.libraryItemModel.create({
|
|
libraryFiles: [],
|
|
mediaId: '00000000-0000-4000-8000-000000000999',
|
|
mediaType: 'podcast',
|
|
libraryId: library.id,
|
|
libraryFolderId: libraryFolder.id
|
|
})
|
|
|
|
const res = createFakeRes()
|
|
await PodcastController.batchGetEpisodes(
|
|
{
|
|
user,
|
|
body: {
|
|
episodes: [
|
|
{ libraryItemId: orphanedItem.id, episodeId: episode1.id },
|
|
{ libraryItemId: podcastItem1.id, episodeId: episode1.id }
|
|
]
|
|
}
|
|
},
|
|
res
|
|
)
|
|
|
|
expect(res.json.calledOnce).to.be.true
|
|
expect(res.json.firstCall.args[0].episodes).to.have.lengthOf(1)
|
|
expect(res.json.firstCall.args[0].episodes[0].id).to.equal(episode1.id)
|
|
})
|
|
|
|
it('projects only required libraryItem and podcast attributes and uses required inner join', async () => {
|
|
const findSpy = sinon.spy(Database.libraryItemModel, 'findAll')
|
|
|
|
const res = createFakeRes()
|
|
await PodcastController.batchGetEpisodes(
|
|
{
|
|
user,
|
|
body: {
|
|
episodes: [{ libraryItemId: podcastItem1.id, episodeId: episode1.id }]
|
|
}
|
|
},
|
|
res
|
|
)
|
|
|
|
expect(findSpy.calledOnce).to.be.true
|
|
const queryOptions = findSpy.firstCall.args[0]
|
|
expect(queryOptions.attributes).to.deep.equal(['id', 'libraryId', 'mediaId', 'mediaType', 'path'])
|
|
expect(queryOptions.attributes).to.not.include('libraryFiles')
|
|
expect(queryOptions.include[0].required).to.be.true
|
|
expect(queryOptions.include[0].attributes).to.deep.equal(['id', 'explicit', 'tags'])
|
|
})
|
|
|
|
it('returns 500 when a database query throws unexpectedly', async () => {
|
|
sinon.stub(Database.libraryItemModel, 'findAll').rejects(new Error('SQLITE_BUSY'))
|
|
|
|
const res = createFakeRes()
|
|
await PodcastController.batchGetEpisodes(
|
|
{
|
|
user,
|
|
body: {
|
|
episodes: [{ libraryItemId: podcastItem1.id, episodeId: episode1.id }]
|
|
}
|
|
},
|
|
res
|
|
)
|
|
|
|
expect(res.status.calledOnceWith(500)).to.be.true
|
|
expect(res.send.calledOnceWith('Failed to get episodes')).to.be.true
|
|
expect(Logger.error.calledOnce).to.be.true
|
|
})
|
|
})
|
|
|
|
describe('batchDownloadEpisodes', () => {
|
|
it('returns 400 for mismatched partial entries without querying DB', async () => {
|
|
const findSpy = sinon.spy(Database.libraryItemModel, 'findAll')
|
|
const res = createFakeRes()
|
|
|
|
await PodcastController.batchDownloadEpisodes(
|
|
{
|
|
user,
|
|
body: {
|
|
episodes: [{ libraryItemId: podcastItem1.id }, { episodeId: episode1.id }]
|
|
}
|
|
},
|
|
res
|
|
)
|
|
|
|
expect(res.status.calledOnceWith(400)).to.be.true
|
|
expect(findSpy.called).to.be.false
|
|
})
|
|
|
|
it('returns 400 for duplicate pairs without querying DB', async () => {
|
|
const findSpy = sinon.spy(Database.libraryItemModel, 'findAll')
|
|
const res = createFakeRes()
|
|
|
|
await PodcastController.batchDownloadEpisodes(
|
|
{
|
|
user,
|
|
body: {
|
|
episodes: [
|
|
{ libraryItemId: podcastItem1.id, episodeId: episode1.id },
|
|
{ libraryItemId: podcastItem1.id, episodeId: episode1.id }
|
|
]
|
|
}
|
|
},
|
|
res
|
|
)
|
|
|
|
expect(res.status.calledOnceWith(400)).to.be.true
|
|
expect(findSpy.called).to.be.false
|
|
})
|
|
|
|
it('groups multi-podcast episodes into podcast folders', async () => {
|
|
const zipStub = sinon.stub(zipHelpers, 'zipDirectoriesPipe').resolves()
|
|
const res = createFakeRes()
|
|
|
|
await PodcastController.batchDownloadEpisodes(
|
|
{
|
|
user,
|
|
body: {
|
|
episodes: [
|
|
{ libraryItemId: podcastItem1.id, episodeId: episode1.id },
|
|
{ libraryItemId: podcastItem2.id, episodeId: episode3.id }
|
|
]
|
|
}
|
|
},
|
|
res
|
|
)
|
|
|
|
expect(zipStub.calledOnce).to.be.true
|
|
const [fileEntries] = zipStub.firstCall.args
|
|
expect(fileEntries).to.deep.equal([
|
|
{ path: '/podcasts/podcast-one/ep1.mp3', isFile: true, name: 'podcast-one/ep1.mp3' },
|
|
{ path: '/podcasts/podcast-two/ep1.mp3', isFile: true, name: 'podcast-two/ep1.mp3' }
|
|
])
|
|
})
|
|
|
|
it('returns 500 when episode resolution fails unexpectedly', async () => {
|
|
sinon.stub(Database.libraryItemModel, 'findAll').rejects(new Error('SQLITE_BUSY'))
|
|
const res = createFakeRes()
|
|
|
|
await PodcastController.batchDownloadEpisodes(
|
|
{
|
|
user,
|
|
body: {
|
|
episodes: [{ libraryItemId: podcastItem1.id, episodeId: episode1.id }]
|
|
}
|
|
},
|
|
res
|
|
)
|
|
|
|
expect(res.status.calledOnceWith(500)).to.be.true
|
|
expect(res.send.calledOnceWith('Download failed')).to.be.true
|
|
expect(Logger.error.calledOnce).to.be.true
|
|
})
|
|
})
|
|
})
|