mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-12-05 06:26:26 +01:00
Add unit tests for podcast episode batch update, delete, download, and validation
This commit is contained in:
1 parent
b7c0e039c8
commit
177b945c60
1 file changed
+496
-36
@@ -5,9 +5,12 @@ const sinon = require('sinon')
|
||||
const Database = require('../../../server/Database')
|
||||
const PodcastController = require('../../../server/controllers/PodcastController')
|
||||
const Logger = require('../../../server/Logger')
|
||||
const SocketAuthority = require('../../../server/SocketAuthority')
|
||||
const fs = require('../../../server/libs/fsExtra')
|
||||
const libraryItemsPodcastFilters = require('../../../server/utils/queries/libraryItemsPodcastFilters')
|
||||
const zipHelpers = require('../../../server/utils/zipHelpers')
|
||||
|
||||
const { buildEpisodeUpdatePayload } = PodcastController
|
||||
const { buildEpisodeUpdatePayload, validateEpisodeBatchEntries } = PodcastController
|
||||
|
||||
describe('PodcastController.buildEpisodeUpdatePayload', () => {
|
||||
beforeEach(() => {
|
||||
@@ -137,6 +140,56 @@ describe('PodcastController.buildEpisodeUpdatePayload', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('PodcastController.validateEpisodeBatchEntries', () => {
|
||||
it('returns 400 when entries are missing, empty, malformed, or contain duplicate pairs', () => {
|
||||
for (const entries of [
|
||||
undefined,
|
||||
null,
|
||||
'invalid',
|
||||
[],
|
||||
[{ libraryItemId: 'li1' }, { episodeId: 'ep1' }],
|
||||
[{ libraryItemId: 123, episodeId: 'ep1' }],
|
||||
[{ libraryItemId: 'li1', episodeId: 'ep1' }, { libraryItemId: 'li1' }],
|
||||
[
|
||||
{ libraryItemId: 'li1', episodeId: 'ep1' },
|
||||
{ libraryItemId: 'li1', episodeId: 'ep1' }
|
||||
]
|
||||
]) {
|
||||
expect(validateEpisodeBatchEntries(entries)).to.deep.equal({ errorStatus: 400, errorMessage: 'Invalid payload' })
|
||||
}
|
||||
})
|
||||
|
||||
it('requires a non-null payload object on every entry when requirePayload is true', () => {
|
||||
for (const entries of [
|
||||
[{ libraryItemId: 'li1', episodeId: 'ep1' }],
|
||||
[{ libraryItemId: 'li1', episodeId: 'ep1', payload: null }],
|
||||
[{ libraryItemId: 'li1', episodeId: 'ep1', payload: 'bad' }]
|
||||
]) {
|
||||
expect(validateEpisodeBatchEntries(entries, { requirePayload: true })).to.deep.equal({
|
||||
errorStatus: 400,
|
||||
errorMessage: 'Invalid payload'
|
||||
})
|
||||
}
|
||||
|
||||
expect(
|
||||
validateEpisodeBatchEntries([{ libraryItemId: 'li1', episodeId: 'ep1', payload: { title: 'A' } }], { requirePayload: true })
|
||||
).to.deep.equal({ libraryItemIds: ['li1'], episodeIds: ['ep1'] })
|
||||
})
|
||||
|
||||
it('returns deduplicated libraryItemIds and episodeIds for valid entries', () => {
|
||||
expect(
|
||||
validateEpisodeBatchEntries([
|
||||
{ libraryItemId: 'li1', episodeId: 'ep1' },
|
||||
{ libraryItemId: 'li1', episodeId: 'ep2' },
|
||||
{ libraryItemId: 'li2', episodeId: 'ep3' }
|
||||
])
|
||||
).to.deep.equal({
|
||||
libraryItemIds: ['li1', 'li2'],
|
||||
episodeIds: ['ep1', 'ep2', 'ep3']
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('PodcastController batch episode endpoints', () => {
|
||||
let library
|
||||
let libraryFolder
|
||||
@@ -255,32 +308,28 @@ describe('PodcastController batch episode endpoints', () => {
|
||||
await Database.sequelize.sync({ force: true })
|
||||
})
|
||||
|
||||
it('returns 400 without querying DB when any batch episode endpoint receives an invalid payload', async () => {
|
||||
user.permissions = { ...user.permissions, delete: true }
|
||||
const findAllSpy = sinon.spy(Database.libraryItemModel, 'findAll')
|
||||
const findExpandedSpy = sinon.spy(Database.libraryItemModel, 'findAllExpandedWhere')
|
||||
|
||||
for (const invoke of [
|
||||
(res) => PodcastController.batchGetEpisodes({ user, body: { episodes: [] } }, res),
|
||||
(res) => PodcastController.batchUpdateEpisodes({ user, body: [{ libraryItemId: podcastItem1.id, episodeId: episode1.id }] }, res),
|
||||
(res) => PodcastController.batchDeleteEpisodes({ user, query: {}, body: { episodes: [] } }, res),
|
||||
(res) => PodcastController.batchDownloadEpisodes({ user, body: { episodes: [] } }, res)
|
||||
]) {
|
||||
const res = createFakeRes()
|
||||
await invoke(res)
|
||||
expect(res.status.calledOnceWith(400)).to.be.true
|
||||
expect(res.send.calledOnceWith('Invalid payload')).to.be.true
|
||||
}
|
||||
|
||||
expect(findAllSpy.called).to.be.false
|
||||
expect(findExpandedSpy.called).to.be.false
|
||||
})
|
||||
|
||||
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({
|
||||
@@ -428,27 +477,365 @@ describe('PodcastController batch episode endpoints', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('batchUpdateEpisodes', () => {
|
||||
it('returns 403 when user lacks canUpdate permission without querying DB', async () => {
|
||||
user.permissions = { ...user.permissions, update: false }
|
||||
const findSpy = sinon.spy(Database.libraryItemModel, 'findAllExpandedWhere')
|
||||
const res = createFakeRes()
|
||||
|
||||
await PodcastController.batchUpdateEpisodes(
|
||||
{
|
||||
user,
|
||||
body: [{ libraryItemId: podcastItem1.id, episodeId: episode1.id, payload: { title: 'Updated' } }]
|
||||
},
|
||||
res
|
||||
)
|
||||
|
||||
expect(res.sendStatus.calledOnceWith(403)).to.be.true
|
||||
expect(findSpy.called).to.be.false
|
||||
})
|
||||
|
||||
it('returns 403 when user lacks access to a referenced podcast', async () => {
|
||||
user.permissions = { ...user.permissions, accessExplicitContent: false }
|
||||
const emitStub = sinon.stub(SocketAuthority, 'libraryItemEmitter')
|
||||
const res = createFakeRes()
|
||||
|
||||
await PodcastController.batchUpdateEpisodes(
|
||||
{
|
||||
user,
|
||||
body: [
|
||||
{ libraryItemId: podcastItem1.id, episodeId: episode1.id, payload: { title: 'Should Not Save' } },
|
||||
{ libraryItemId: podcastItem2.id, episodeId: episode3.id, payload: { title: 'Restricted' } }
|
||||
]
|
||||
},
|
||||
res
|
||||
)
|
||||
|
||||
expect(res.sendStatus.calledOnceWith(403)).to.be.true
|
||||
expect(emitStub.called).to.be.false
|
||||
await episode1.reload()
|
||||
expect(episode1.title).to.equal('Ep 1')
|
||||
})
|
||||
|
||||
it('returns 404 when no requested episodes exist or only non-podcast items match', 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.batchUpdateEpisodes(
|
||||
{
|
||||
user,
|
||||
body: [
|
||||
{ libraryItemId: podcastItem1.id, episodeId: 'missing-ep', payload: { title: 'Nope' } },
|
||||
{ libraryItemId: bookItem.id, episodeId: episode1.id, payload: { title: 'Nope' } }
|
||||
]
|
||||
},
|
||||
res
|
||||
)
|
||||
|
||||
expect(res.status.calledOnceWith(404)).to.be.true
|
||||
expect(res.send.calledOnceWith('Episodes not found')).to.be.true
|
||||
})
|
||||
|
||||
it('updates episodes across podcasts in a transaction, omits missing entries, and emits once per modified podcast', async () => {
|
||||
const emitStub = sinon.stub(SocketAuthority, 'libraryItemEmitter')
|
||||
const res = createFakeRes()
|
||||
|
||||
await PodcastController.batchUpdateEpisodes(
|
||||
{
|
||||
user,
|
||||
body: [
|
||||
{ libraryItemId: podcastItem1.id, episodeId: episode1.id, payload: { title: 'Ep 1 Updated', season: '2' } },
|
||||
{ libraryItemId: podcastItem1.id, episodeId: episode2.id, payload: { title: 'Ep 2' } },
|
||||
{ libraryItemId: podcastItem1.id, episodeId: 'missing-ep', payload: { title: 'Ignored' } },
|
||||
{ libraryItemId: podcastItem2.id, episodeId: episode3.id, payload: { title: 'Ep 3 Updated', description: '<p>Clean</p><script>bad()</script>' } }
|
||||
]
|
||||
},
|
||||
res
|
||||
)
|
||||
|
||||
expect(res.json.calledOnceWith({ success: true, updates: 2 })).to.be.true
|
||||
|
||||
await episode1.reload()
|
||||
await episode2.reload()
|
||||
await episode3.reload()
|
||||
expect(episode1.title).to.equal('Ep 1 Updated')
|
||||
expect(episode1.season).to.equal('2')
|
||||
expect(episode2.title).to.equal('Ep 2')
|
||||
expect(episode3.title).to.equal('Ep 3 Updated')
|
||||
expect(episode3.description).to.equal('<p>Clean</p>')
|
||||
|
||||
expect(emitStub.callCount).to.equal(2)
|
||||
expect(emitStub.getCalls().map((c) => [c.args[0], c.args[1].id])).to.deep.equal([
|
||||
['item_updated', podcastItem1.id],
|
||||
['item_updated', podcastItem2.id]
|
||||
])
|
||||
})
|
||||
|
||||
it('returns updates: 0 and emits no socket events when payloads produce no changes', async () => {
|
||||
const emitStub = sinon.stub(SocketAuthority, 'libraryItemEmitter')
|
||||
const res = createFakeRes()
|
||||
|
||||
await PodcastController.batchUpdateEpisodes(
|
||||
{
|
||||
user,
|
||||
body: [{ libraryItemId: podcastItem1.id, episodeId: episode1.id, payload: { title: 'Ep 1', unknownField: 'ignored' } }]
|
||||
},
|
||||
res
|
||||
)
|
||||
|
||||
expect(res.json.calledOnceWith({ success: true, updates: 0 })).to.be.true
|
||||
expect(emitStub.called).to.be.false
|
||||
})
|
||||
|
||||
it('rolls back transaction and returns 500 when saving an episode fails mid-batch', async () => {
|
||||
const emitStub = sinon.stub(SocketAuthority, 'libraryItemEmitter')
|
||||
const originalSave = Database.podcastEpisodeModel.prototype.save
|
||||
let saveCalls = 0
|
||||
sinon.stub(Database.podcastEpisodeModel.prototype, 'save').callsFake(function (...args) {
|
||||
saveCalls++
|
||||
if (saveCalls === 2) return Promise.reject(new Error('SQLITE_BUSY'))
|
||||
return originalSave.apply(this, args)
|
||||
})
|
||||
|
||||
const res = createFakeRes()
|
||||
await PodcastController.batchUpdateEpisodes(
|
||||
{
|
||||
user,
|
||||
body: [
|
||||
{ libraryItemId: podcastItem1.id, episodeId: episode1.id, payload: { title: 'Rolled Back' } },
|
||||
{ libraryItemId: podcastItem1.id, episodeId: episode2.id, payload: { title: 'Fails Save' } }
|
||||
]
|
||||
},
|
||||
res
|
||||
)
|
||||
|
||||
expect(res.status.calledOnceWith(500)).to.be.true
|
||||
expect(res.send.calledOnceWith('Failed to update episodes')).to.be.true
|
||||
expect(emitStub.called).to.be.false
|
||||
|
||||
await episode1.reload()
|
||||
expect(episode1.title).to.equal('Ep 1')
|
||||
})
|
||||
})
|
||||
|
||||
describe('batchDeleteEpisodes', () => {
|
||||
it('returns 403 when user lacks canDelete permission without querying DB', async () => {
|
||||
const findSpy = sinon.spy(Database.libraryItemModel, 'findAllExpandedWhere')
|
||||
const res = createFakeRes()
|
||||
|
||||
await PodcastController.batchDeleteEpisodes(
|
||||
{
|
||||
user,
|
||||
query: {},
|
||||
body: { episodes: [{ libraryItemId: podcastItem1.id, episodeId: episode1.id }] }
|
||||
},
|
||||
res
|
||||
)
|
||||
|
||||
expect(res.sendStatus.calledOnceWith(403)).to.be.true
|
||||
expect(findSpy.called).to.be.false
|
||||
})
|
||||
|
||||
it('returns 403 when user lacks access to a referenced podcast', async () => {
|
||||
user.permissions = { ...user.permissions, delete: true, accessExplicitContent: false }
|
||||
const res = createFakeRes()
|
||||
|
||||
await PodcastController.batchDeleteEpisodes(
|
||||
{
|
||||
user,
|
||||
query: {},
|
||||
body: {
|
||||
episodes: [
|
||||
{ libraryItemId: podcastItem1.id, episodeId: episode1.id },
|
||||
{ libraryItemId: podcastItem2.id, episodeId: episode3.id }
|
||||
]
|
||||
}
|
||||
},
|
||||
res
|
||||
)
|
||||
|
||||
expect(res.sendStatus.calledOnceWith(403)).to.be.true
|
||||
expect(await Database.podcastEpisodeModel.findByPk(episode1.id)).to.not.be.null
|
||||
})
|
||||
|
||||
it('returns 404 when no requested episodes are found', async () => {
|
||||
user.permissions = { ...user.permissions, delete: true }
|
||||
const res = createFakeRes()
|
||||
|
||||
await PodcastController.batchDeleteEpisodes(
|
||||
{
|
||||
user,
|
||||
query: {},
|
||||
body: { episodes: [{ libraryItemId: podcastItem1.id, episodeId: 'missing-ep' }] }
|
||||
},
|
||||
res
|
||||
)
|
||||
|
||||
expect(res.status.calledOnceWith(404)).to.be.true
|
||||
expect(res.send.calledOnceWith('Episodes not found')).to.be.true
|
||||
})
|
||||
|
||||
it('soft-deletes episodes across podcasts, cleans up progress/playlists/libraryFiles/numEpisodes, and emits once per podcast', async () => {
|
||||
user.permissions = { ...user.permissions, delete: true }
|
||||
|
||||
podcastItem1.libraryFiles = [{ ino: '101' }, { ino: '102' }, { ino: 'keep-1' }]
|
||||
podcastItem1.changed('libraryFiles', true)
|
||||
await podcastItem1.save()
|
||||
|
||||
podcastItem2.libraryFiles = [{ ino: '103' }]
|
||||
podcastItem2.changed('libraryFiles', true)
|
||||
await podcastItem2.save()
|
||||
|
||||
await Database.mediaProgressModel.create({
|
||||
userId: user.id,
|
||||
mediaItemId: episode1.id,
|
||||
mediaItemType: 'podcastEpisode',
|
||||
duration: 120,
|
||||
currentTime: 30,
|
||||
isFinished: false
|
||||
})
|
||||
await Database.mediaProgressModel.create({
|
||||
userId: user.id,
|
||||
mediaItemId: episode2.id,
|
||||
mediaItemType: 'podcastEpisode',
|
||||
duration: 120,
|
||||
currentTime: 60,
|
||||
isFinished: false
|
||||
})
|
||||
|
||||
const playlistStub = sinon.stub(Database.playlistModel, 'removeMediaItemsFromPlaylists').resolves()
|
||||
const cacheStub = sinon.stub(libraryItemsPodcastFilters, 'clearCountCache')
|
||||
const fsRemoveStub = sinon.stub(fs, 'remove').resolves()
|
||||
const emitStub = sinon.stub(SocketAuthority, 'libraryItemEmitter')
|
||||
|
||||
const res = createFakeRes()
|
||||
await PodcastController.batchDeleteEpisodes(
|
||||
{
|
||||
user,
|
||||
query: {},
|
||||
body: {
|
||||
episodes: [
|
||||
{ libraryItemId: podcastItem1.id, episodeId: episode1.id },
|
||||
{ libraryItemId: podcastItem1.id, episodeId: 'missing-ep' },
|
||||
{ libraryItemId: podcastItem2.id, episodeId: episode3.id }
|
||||
]
|
||||
}
|
||||
},
|
||||
res
|
||||
)
|
||||
|
||||
expect(res.json.calledOnceWith({ success: true, removed: 2 })).to.be.true
|
||||
expect(fsRemoveStub.called).to.be.false
|
||||
expect(playlistStub.calledOnceWith([episode1.id, episode3.id])).to.be.true
|
||||
expect(cacheStub.calledOnceWith('podcastEpisode', 'afterDestroy')).to.be.true
|
||||
|
||||
expect(await Database.podcastEpisodeModel.findByPk(episode1.id)).to.be.null
|
||||
expect(await Database.podcastEpisodeModel.findByPk(episode3.id)).to.be.null
|
||||
expect(await Database.podcastEpisodeModel.findByPk(episode2.id)).to.not.be.null
|
||||
|
||||
expect(await Database.mediaProgressModel.count({ where: { mediaItemId: episode1.id } })).to.equal(0)
|
||||
expect(await Database.mediaProgressModel.count({ where: { mediaItemId: episode2.id } })).to.equal(1)
|
||||
|
||||
await podcastItem1.reload()
|
||||
await podcastItem2.reload()
|
||||
const podcast1 = await Database.podcastModel.findByPk(podcastItem1.mediaId)
|
||||
const podcast2 = await Database.podcastModel.findByPk(podcastItem2.mediaId)
|
||||
expect(podcastItem1.libraryFiles.map((f) => f.ino)).to.deep.equal(['102', 'keep-1'])
|
||||
expect(podcast1.numEpisodes).to.equal(1)
|
||||
expect(podcastItem2.libraryFiles).to.deep.equal([])
|
||||
expect(podcast2.numEpisodes).to.equal(0)
|
||||
|
||||
expect(emitStub.callCount).to.equal(2)
|
||||
expect(emitStub.getCalls().map((c) => [c.args[0], c.args[1].id])).to.deep.equal([
|
||||
['item_updated', podcastItem1.id],
|
||||
['item_updated', podcastItem2.id]
|
||||
])
|
||||
})
|
||||
|
||||
it('hard-deletes audio files when hard=1 and continues if one unlink fails', async () => {
|
||||
user.permissions = { ...user.permissions, delete: true }
|
||||
sinon.stub(Database.playlistModel, 'removeMediaItemsFromPlaylists').resolves()
|
||||
sinon.stub(libraryItemsPodcastFilters, 'clearCountCache')
|
||||
sinon.stub(SocketAuthority, 'libraryItemEmitter')
|
||||
|
||||
const fsRemoveStub = sinon.stub(fs, 'remove')
|
||||
fsRemoveStub.onFirstCall().rejects(new Error('ENOENT'))
|
||||
fsRemoveStub.onSecondCall().resolves()
|
||||
|
||||
const res = createFakeRes()
|
||||
await PodcastController.batchDeleteEpisodes(
|
||||
{
|
||||
user,
|
||||
query: { hard: '1' },
|
||||
body: {
|
||||
episodes: [
|
||||
{ libraryItemId: podcastItem1.id, episodeId: episode1.id },
|
||||
{ libraryItemId: podcastItem2.id, episodeId: episode3.id }
|
||||
]
|
||||
}
|
||||
},
|
||||
res
|
||||
)
|
||||
|
||||
expect(res.json.calledOnceWith({ success: true, removed: 2 })).to.be.true
|
||||
expect(fsRemoveStub.callCount).to.equal(2)
|
||||
expect(fsRemoveStub.firstCall.args[0]).to.equal('/podcasts/podcast-one/ep1.mp3')
|
||||
expect(fsRemoveStub.secondCall.args[0]).to.equal('/podcasts/podcast-two/ep1.mp3')
|
||||
expect(Logger.error.calledOnce).to.be.true
|
||||
expect(await Database.podcastEpisodeModel.findByPk(episode1.id)).to.be.null
|
||||
expect(await Database.podcastEpisodeModel.findByPk(episode3.id)).to.be.null
|
||||
})
|
||||
|
||||
it('returns 500 when deletion fails unexpectedly', async () => {
|
||||
user.permissions = { ...user.permissions, delete: true }
|
||||
sinon.stub(Database.playlistModel, 'removeMediaItemsFromPlaylists').resolves()
|
||||
const emitStub = sinon.stub(SocketAuthority, 'libraryItemEmitter')
|
||||
sinon.stub(Database.podcastEpisodeModel, 'destroy').rejects(new Error('SQLITE_BUSY'))
|
||||
|
||||
const res = createFakeRes()
|
||||
await PodcastController.batchDeleteEpisodes(
|
||||
{
|
||||
user,
|
||||
query: {},
|
||||
body: { episodes: [{ libraryItemId: podcastItem1.id, episodeId: episode1.id }] }
|
||||
},
|
||||
res
|
||||
)
|
||||
|
||||
expect(res.status.calledOnceWith(500)).to.be.true
|
||||
expect(res.send.calledOnceWith('Failed to delete episodes')).to.be.true
|
||||
expect(emitStub.called).to.be.false
|
||||
})
|
||||
})
|
||||
|
||||
describe('batchDownloadEpisodes', () => {
|
||||
it('returns 400 for mismatched partial entries without querying DB', async () => {
|
||||
it('returns 403 when user lacks canDownload permission without querying DB', async () => {
|
||||
user.permissions = { ...user.permissions, download: false }
|
||||
const findSpy = sinon.spy(Database.libraryItemModel, 'findAll')
|
||||
const res = createFakeRes()
|
||||
|
||||
await PodcastController.batchDownloadEpisodes(
|
||||
{
|
||||
user,
|
||||
body: {
|
||||
episodes: [{ libraryItemId: podcastItem1.id }, { episodeId: episode1.id }]
|
||||
}
|
||||
body: { episodes: [{ libraryItemId: podcastItem1.id, episodeId: episode1.id }] }
|
||||
},
|
||||
res
|
||||
)
|
||||
|
||||
expect(res.status.calledOnceWith(400)).to.be.true
|
||||
expect(res.sendStatus.calledOnceWith(403)).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')
|
||||
it('returns 403 when user lacks access to a referenced podcast', async () => {
|
||||
user.permissions = { ...user.permissions, accessExplicitContent: false }
|
||||
const zipStub = sinon.stub(zipHelpers, 'zipDirectoriesPipe').resolves()
|
||||
const res = createFakeRes()
|
||||
|
||||
await PodcastController.batchDownloadEpisodes(
|
||||
@@ -457,15 +844,15 @@ describe('PodcastController batch episode endpoints', () => {
|
||||
body: {
|
||||
episodes: [
|
||||
{ libraryItemId: podcastItem1.id, episodeId: episode1.id },
|
||||
{ libraryItemId: podcastItem1.id, episodeId: episode1.id }
|
||||
{ libraryItemId: podcastItem2.id, episodeId: episode3.id }
|
||||
]
|
||||
}
|
||||
},
|
||||
res
|
||||
)
|
||||
|
||||
expect(res.status.calledOnceWith(400)).to.be.true
|
||||
expect(findSpy.called).to.be.false
|
||||
expect(res.sendStatus.calledOnceWith(403)).to.be.true
|
||||
expect(zipStub.called).to.be.false
|
||||
})
|
||||
|
||||
it('groups multi-podcast episodes into podcast folders', async () => {
|
||||
@@ -493,6 +880,79 @@ describe('PodcastController batch episode endpoints', () => {
|
||||
])
|
||||
})
|
||||
|
||||
it('uses flat filenames for single-podcast downloads and disambiguates duplicate names', async () => {
|
||||
episode2.audioFile = createAudioFile('ep1.mp3', '/podcasts/podcast-one/sub/ep1.mp3', '102')
|
||||
episode2.changed('audioFile', true)
|
||||
await episode2.save()
|
||||
|
||||
const zipStub = sinon.stub(zipHelpers, 'zipDirectoriesPipe').resolves()
|
||||
const res = createFakeRes()
|
||||
|
||||
await PodcastController.batchDownloadEpisodes(
|
||||
{
|
||||
user,
|
||||
body: {
|
||||
episodes: [
|
||||
{ libraryItemId: podcastItem1.id, episodeId: episode1.id },
|
||||
{ libraryItemId: podcastItem1.id, episodeId: episode2.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: 'ep1.mp3' },
|
||||
{ path: '/podcasts/podcast-one/sub/ep1.mp3', isFile: true, name: 'ep1 (2).mp3' }
|
||||
])
|
||||
})
|
||||
|
||||
it('returns 404 when no downloadable episode files are found', async () => {
|
||||
episode1.audioFile = { ino: '101', duration: 120, metadata: {} }
|
||||
episode1.changed('audioFile', true)
|
||||
await episode1.save()
|
||||
|
||||
const zipStub = sinon.stub(zipHelpers, 'zipDirectoriesPipe').resolves()
|
||||
const res = createFakeRes()
|
||||
|
||||
await PodcastController.batchDownloadEpisodes(
|
||||
{
|
||||
user,
|
||||
body: {
|
||||
episodes: [
|
||||
{ libraryItemId: podcastItem1.id, episodeId: episode1.id },
|
||||
{ libraryItemId: podcastItem1.id, episodeId: 'missing-ep' }
|
||||
]
|
||||
}
|
||||
},
|
||||
res
|
||||
)
|
||||
|
||||
expect(res.status.calledOnceWith(404)).to.be.true
|
||||
expect(res.send.calledOnceWith('No downloadable episode files found')).to.be.true
|
||||
expect(zipStub.called).to.be.false
|
||||
})
|
||||
|
||||
it('delegates zip streaming errors to zipHelpers.handleDownloadError', async () => {
|
||||
const zipError = new Error('ENOENT')
|
||||
sinon.stub(zipHelpers, 'zipDirectoriesPipe').rejects(zipError)
|
||||
const handleErrorStub = sinon.stub(zipHelpers, 'handleDownloadError')
|
||||
const res = createFakeRes()
|
||||
|
||||
await PodcastController.batchDownloadEpisodes(
|
||||
{
|
||||
user,
|
||||
body: { episodes: [{ libraryItemId: podcastItem1.id, episodeId: episode1.id }] }
|
||||
},
|
||||
res
|
||||
)
|
||||
|
||||
expect(handleErrorStub.calledOnceWith(zipError, res)).to.be.true
|
||||
expect(Logger.error.calledOnce).to.be.true
|
||||
})
|
||||
|
||||
it('returns 500 when episode resolution fails unexpectedly', async () => {
|
||||
sinon.stub(Database.libraryItemModel, 'findAll').rejects(new Error('SQLITE_BUSY'))
|
||||
const res = createFakeRes()
|
||||
|
||||
Reference in new issue
Block a user