Merge branch 'master' into v3

This commit is contained in:
advplyr committed 2026-11-19 17:39:40 -06:00
commit ab05d7fb5f
7 files changed
+460 -32

No files matched your search

@@ -0,0 +1,139 @@
const { expect } = require('chai')
const { Sequelize } = require('sequelize')
const sinon = require('sinon')
const Database = require('../../../server/Database')
const AuthorController = require('../../../server/controllers/AuthorController')
const AuthorFinder = require('../../../server/finders/AuthorFinder')
const Logger = require('../../../server/Logger')
const unsafeDescription = '<p>Hello</p><script>alert(1)</script><img src="x" onerror="alert(document.domain)"><a href="https://example.com">link</a>'
const safeDescription = '<p>Hello</p><a href="https://example.com">link</a>'
describe('AuthorController', () => {
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')
})
afterEach(async () => {
sinon.restore()
// Clear all tables
await Database.sequelize.sync({ force: true })
})
/**
* @param {string} [description]
*/
async function createAuthor(description) {
const library = await Database.libraryModel.create({ name: 'Test Library', mediaType: 'book' })
return Database.authorModel.create({
name: 'Test Author',
libraryId: library.id,
description: description || null
})
}
describe('update', () => {
it('should store and return a sanitized description', async () => {
const author = await createAuthor()
const fakeReq = {
body: {
description: unsafeDescription
},
author
}
const fakeRes = {
status: sinon.stub().returnsThis(),
send: sinon.spy(),
json: sinon.spy()
}
await AuthorController.update(fakeReq, fakeRes)
expect(fakeRes.json.calledOnce).to.be.true
const response = fakeRes.json.firstCall.args[0]
expect(response.updated).to.be.true
expect(response.author.description).to.equal(safeDescription)
expect(response.author.description).to.not.include('script')
expect(response.author.description).to.not.include('onerror')
const stored = await Database.authorModel.findByPk(author.id)
expect(stored.description).to.equal(safeDescription)
})
it('should store null when sanitizing leaves an empty description', async () => {
const author = await createAuthor('<p>Existing</p>')
const fakeReq = {
body: {
description: '<script>alert(1)</script><img src="x" onerror="alert(1)">'
},
author
}
const fakeRes = {
json: sinon.spy()
}
await AuthorController.update(fakeReq, fakeRes)
const response = fakeRes.json.firstCall.args[0]
expect(response.author.description).to.equal(null)
const stored = await Database.authorModel.findByPk(author.id)
expect(stored.description).to.equal(null)
})
})
describe('toOldJSON', () => {
it('should sanitize a stored description without rewriting the row', async () => {
const author = await createAuthor(unsafeDescription)
expect(author.toOldJSON().description).to.equal(safeDescription)
expect(author.description).to.equal(unsafeDescription)
const stored = await Database.authorModel.findByPk(author.id)
expect(stored.description).to.equal(unsafeDescription)
expect(stored.toOldJSON().description).to.equal(safeDescription)
})
})
describe('match', () => {
it('should store the sanitized Audnexus description', async () => {
const author = await createAuthor()
sinon.stub(AuthorFinder, 'findAuthorByASIN').resolves({
asin: 'B00TEST000',
name: 'Test Author',
description: '<p>Bio</p><script>alert(1)</script>',
image: null
})
const fakeReq = {
body: {
asin: 'B00TEST000'
},
author
}
const fakeRes = {
status: sinon.stub().returnsThis(),
send: sinon.spy(),
json: sinon.spy()
}
await AuthorController.match(fakeReq, fakeRes)
expect(fakeRes.json.calledOnce).to.be.true
const response = fakeRes.json.firstCall.args[0]
expect(response.updated).to.be.true
expect(response.author.description).to.equal('<p>Bio</p>')
const stored = await Database.authorModel.findByPk(author.id)
expect(stored.description).to.equal('<p>Bio</p>')
})
})
})
@@ -0,0 +1,113 @@
const { expect } = require('chai')
const { Sequelize } = require('sequelize')
const sinon = require('sinon')
const Database = require('../../../server/Database')
const Logger = require('../../../server/Logger')
const CollectionController = require('../../../server/controllers/CollectionController')
describe('CollectionController', () => {
/** @type {import('../../../server/models/Collection')} */
let collection
/** @type {import('../../../server/models/LibraryItem')} */
let libraryItem
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')
const library = await Database.libraryModel.create({ name: 'Test Library', mediaType: 'book' })
const libraryFolder = await Database.libraryFolderModel.create({ path: '/test-lib', libraryId: library.id })
const book = await Database.bookModel.create({ title: 'Test Book', audioFiles: [], tags: [], narrators: [], genres: [], chapters: [] })
libraryItem = await Database.libraryItemModel.create({
path: '/test-lib/book',
isFile: false,
libraryFiles: [],
mediaId: book.id,
mediaType: 'book',
libraryId: library.id,
libraryFolderId: libraryFolder.id
})
collection = await Database.collectionModel.create({ name: 'Test Collection', libraryId: library.id })
await Database.collectionBookModel.create({ collectionId: collection.id, bookId: book.id, order: 1 })
})
afterEach(async () => {
sinon.restore()
await Database.sequelize.sync({ force: true })
})
it('does not throw when the collectionBook link already exists', async () => {
sinon.stub(collection, 'getCollectionBooks').resolves([])
const res = {
statusCode: null,
body: null,
status(code) {
this.statusCode = code
return this
},
send(body) {
this.body = body
return this
},
json(body) {
this.body = body
return this
}
}
await CollectionController.addBook({ body: { id: libraryItem.id }, collection, user: { id: 'user-id' } }, res)
expect(res.statusCode).to.equal(400)
expect(res.body).to.equal('Book already in collection')
const links = await Database.collectionBookModel.findAll({ where: { collectionId: collection.id, bookId: libraryItem.mediaId } })
expect(links).to.have.lengthOf(1)
})
it('does not throw when a batch includes a book already in the collection', async () => {
const book = await Database.bookModel.create({ title: 'Second Book', audioFiles: [], tags: [], narrators: [], genres: [], chapters: [] })
const secondItem = await Database.libraryItemModel.create({
path: '/test-lib/book-2',
isFile: false,
libraryFiles: [],
mediaId: book.id,
mediaType: 'book',
libraryId: libraryItem.libraryId,
libraryFolderId: libraryItem.libraryFolderId
})
sinon.stub(collection, 'getCollectionBooks').resolves([])
sinon.stub(collection, 'getOldJsonExpanded').resolves(null)
const res = {
statusCode: null,
body: null,
status(code) {
this.statusCode = code
return this
},
send(body) {
this.body = body
return this
},
json(body) {
this.body = body
return this
}
}
await CollectionController.addBatch({ body: { books: [libraryItem.id, secondItem.id] }, collection, user: { id: 'user-id' } }, res)
const existingLinks = await Database.collectionBookModel.findAll({ where: { collectionId: collection.id, bookId: libraryItem.mediaId } })
expect(existingLinks).to.have.lengthOf(1)
const newLinks = await Database.collectionBookModel.findAll({ where: { collectionId: collection.id, bookId: secondItem.mediaId } })
expect(newLinks).to.have.lengthOf(1)
})
})
+120
View File
@@ -0,0 +1,120 @@
const { expect } = require('chai')
const { Sequelize } = require('sequelize')
const sinon = require('sinon')
const Database = require('../../../server/Database')
const Logger = require('../../../server/Logger')
const Scanner = require('../../../server/scanner/Scanner')
describe('Scanner.quickMatchBookBuildUpdatePayload', () => {
/** @type {import('../../../server/models/Library')} */
let library
/** @type {import('../../../server/models/LibraryItem')} */
let libraryItem
/** @type {import('../../../server/models/Book')} */
let book
const apiRouterCtx = {
checkRemoveAuthorsWithNoBooks: sinon.stub().resolves(),
checkRemoveEmptySeries: sinon.stub().resolves()
}
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: 'Test Library', mediaType: 'book' })
const libraryFolder = await Database.libraryFolderModel.create({ path: '/test', libraryId: library.id })
book = await Database.bookModel.create({ title: 'Test Book', audioFiles: [], tags: [], narrators: [], genres: [], chapters: [] })
libraryItem = await Database.libraryItemModel.create({
libraryFiles: [],
mediaId: book.id,
mediaType: 'book',
libraryId: library.id,
libraryFolderId: libraryFolder.id
})
libraryItem.media = book
// Stale in-memory associations: DB may already have links from a concurrent quick match
book.authors = []
book.series = []
})
afterEach(async () => {
sinon.restore()
await Database.sequelize.sync({ force: true })
})
it('does not throw when bookSeries link already exists from a concurrent quick match', async () => {
const series = await Database.seriesModel.create({ name: 'Crescent City', libraryId: library.id })
await Database.bookSeriesModel.create({ bookId: book.id, seriesId: series.id, sequence: '3' })
const result = await Scanner.quickMatchBookBuildUpdatePayload(
apiRouterCtx,
libraryItem,
{
series: [{ series: 'Crescent City', sequence: '3' }]
},
{}
)
expect(result.hasSeriesUpdates).to.be.true
expect(libraryItem.media.series).to.have.lengthOf(1)
expect(libraryItem.media.series[0].id).to.equal(series.id)
expect(libraryItem.media.series[0].bookSeries).to.exist
expect(libraryItem.media.series[0].bookSeries.sequence).to.equal('3')
const links = await Database.bookSeriesModel.findAll({ where: { bookId: book.id, seriesId: series.id } })
expect(links).to.have.lengthOf(1)
})
it('reuses an existing series when create hits the name unique constraint', async () => {
const series = await Database.seriesModel.create({ name: 'Crescent City', libraryId: library.id })
sinon.stub(Database.seriesModel, 'getByNameAndLibrary').resolves(null)
const result = await Scanner.quickMatchBookBuildUpdatePayload(
apiRouterCtx,
libraryItem,
{
series: [{ series: 'Crescent City', sequence: '3' }]
},
{}
)
expect(result.hasSeriesUpdates).to.be.true
expect(libraryItem.media.series).to.have.lengthOf(1)
expect(libraryItem.media.series[0].id).to.equal(series.id)
const seriesRows = await Database.seriesModel.findAll({ where: { libraryId: library.id, name: 'Crescent City' } })
expect(seriesRows).to.have.lengthOf(1)
const links = await Database.bookSeriesModel.findAll({ where: { bookId: book.id, seriesId: series.id } })
expect(links).to.have.lengthOf(1)
})
it('does not throw when bookAuthor link already exists from a concurrent quick match', async () => {
const author = await Database.authorModel.create({ name: 'Sarah Maas', libraryId: library.id })
await Database.bookAuthorModel.create({ bookId: book.id, authorId: author.id })
const result = await Scanner.quickMatchBookBuildUpdatePayload(
apiRouterCtx,
libraryItem,
{
author: ['Sarah Maas']
},
{}
)
expect(result.hasAuthorUpdates).to.be.true
expect(libraryItem.media.authors).to.have.lengthOf(1)
expect(libraryItem.media.authors[0].id).to.equal(author.id)
const links = await Database.bookAuthorModel.findAll({ where: { bookId: book.id, authorId: author.id } })
expect(links).to.have.lengthOf(1)
})
})